Skip to content

Commit

Permalink
set global key in lmdb
Browse files Browse the repository at this point in the history
  • Loading branch information
chronolaw committed Oct 22, 2024
1 parent 42e6740 commit 427835a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
34 changes: 24 additions & 10 deletions kong/db/declarative/import.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ local DECLARATIVE_EMPTY_CONFIG_HASH = constants.DECLARATIVE_EMPTY_CONFIG_HASH
local DECLARATIVE_DEFAULT_WORKSPACE_KEY = constants.DECLARATIVE_DEFAULT_WORKSPACE_KEY


local GLOBAL_WORKSPACE_TAG = "*"


-- Generates the appropriate workspace ID for current operating context
-- depends on schema settings
--
Expand All @@ -56,7 +59,7 @@ local function workspace_id(schema, options)
-- options.workspace == null must be handled by caller by querying
-- all available workspaces one by one
if options.workspace == null then
return kong.default_workspace
return GLOBAL_WORKSPACE_TAG
end

-- options.workspace is a UUID
Expand Down Expand Up @@ -292,13 +295,20 @@ local function _set_entity_for_txn(t, entity_name, item, options, is_delete)
-- store serialized entity into lmdb
t:set(itm_key, itm_value)

-- for global query
local global_key = item_key(entity_name, GLOBAL_WORKSPACE_TAG, pk)
t:set(global_key, idx_value)

-- select_by_cache_key
if schema.cache_key then
local cache_key = dao:cache_key(item)
local key = unique_field_key(entity_name, ws_id, "cache_key", cache_key)

-- store item_key or nil into lmdb
t:set(key, idx_value)
for _, wid in ipairs {ws_id, GLOBAL_WORKSPACE_TAG} do
local key = unique_field_key(entity_name, wid, "cache_key", cache_key)

-- store item_key or nil into lmdb
t:set(key, idx_value)
end
end

for fname, fdata in schema:each_field() do
Expand Down Expand Up @@ -328,10 +338,12 @@ local function _set_entity_for_txn(t, entity_name, item, options, is_delete)
ws_id = kong.default_workspace
end

local key = unique_field_key(entity_name, ws_id, fname, value_str or value)
for _, wid in ipairs {ws_id, GLOBAL_WORKSPACE_TAG} do
local key = unique_field_key(entity_name, wid, fname, value_str or value)

-- store item_key or nil into lmdb
t:set(key, idx_value)
-- store item_key or nil into lmdb
t:set(key, idx_value)
end
end

if is_foreign then
Expand All @@ -340,10 +352,12 @@ local function _set_entity_for_txn(t, entity_name, item, options, is_delete)

value_str = pk_string(kong.db[fdata_reference].schema, value)

local key = foreign_field_key(entity_name, ws_id, fname, value_str, pk)
for _, wid in ipairs {ws_id, GLOBAL_WORKSPACE_TAG} do
local key = foreign_field_key(entity_name, wid, fname, value_str, pk)

-- store item_key or nil into lmdb
t:set(key, idx_value)
-- store item_key or nil into lmdb
t:set(key, idx_value)
end
end

::continue::
Expand Down
9 changes: 7 additions & 2 deletions kong/db/strategies/off/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ _mt.__index = _mt
local UNINIT_WORKSPACE_ID = "00000000-0000-0000-0000-000000000000"


local function need_follow(ws_id)
return ws_id == "*"
end


local function get_default_workspace()
if kong.default_workspace == UNINIT_WORKSPACE_ID then
local res = kong.db.workspaces:select_by_name("default")
Expand Down Expand Up @@ -173,7 +178,7 @@ local function page(self, size, offset, options)
offset = token
end

return page_for_prefix(self, prefix, size, offset, options, false)
return page_for_prefix(self, prefix, size, offset, options, need_follow(ws_id))
end


Expand All @@ -183,7 +188,7 @@ local function select(self, pk, options)
local ws_id = workspace_id(schema, options)
local pk = pk_string(schema, pk)
local key = item_key(schema.name, ws_id, pk)
return select_by_key(schema, key, false)
return select_by_key(schema, key, need_follow(ws_id))
end


Expand Down

0 comments on commit 427835a

Please sign in to comment.