Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
StarlightIbuki committed Nov 22, 2024
1 parent 901cc12 commit a2510e8
Showing 1 changed file with 96 additions and 96 deletions.
192 changes: 96 additions & 96 deletions kong/db/strategies/off/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,41 @@ local function page_for_prefix(self, prefix, size, offset, options, follow, sche
end


-- Define the filter logic
local function matches_condition(item_tags, tags, tags_cond)
local matches = {}
for _, tag in ipairs(tags) do
matches[tag] = false
end

-- Mark matches
for _, item_tag in ipairs(item_tags) do
if matches[item_tag] ~= nil then
matches[item_tag] = true
end
end

-- Evaluate the condition
if tags_cond == "and" then
for _, matched in pairs(matches) do
if not matched then
return false
end
end
return true
end

if tags_cond == "or" then
for _, matched in pairs(matches) do
if matched then
return true
end
end
return false
end
end


-- AI generated function:
--
-- This function filters a list of items based on a set of tags and a condition
Expand All @@ -170,40 +205,6 @@ end
local function filter_tag_items(items, tags, tags_cond)
assert(tags_cond == "and" or tags_cond == "or")

-- Define the filter logic
local function matches_condition(item_tags, tags, tags_cond)
local matches = {}
for _, tag in ipairs(tags) do
matches[tag] = false
end

-- Mark matches
for _, item_tag in ipairs(item_tags) do
if matches[item_tag] ~= nil then
matches[item_tag] = true
end
end

-- Evaluate the condition
if tags_cond == "and" then
for _, matched in pairs(matches) do
if not matched then
return false
end
end
return true
end

if tags_cond == "or" then
for _, matched in pairs(matches) do
if matched then
return true
end
end
return false
end
end

-- Filter the items
local filtered_items = {}
for _, item in ipairs(items) do
Expand All @@ -219,89 +220,88 @@ end
-- Tags are a global concept across workspaces, there we don't need to handle
-- ws_id here.
local function page_for_tags(self, size, offset, options)
-- /:entitiy?tags=:tags
-- search all key-values: <entity_name>|*|*|<pk_string> => actual item key
if self.schema.name ~= "tags" then
local prefix = item_key_prefix(self.schema.name, "*") -- "<entity_name>|*|*|"
local items, err, offset = page_for_prefix(self, prefix, size, offset,
options, true)
if not items then
return nil, err
end

items = filter_tag_items(items, options.tags, options.tags_cond)

return items, err, offset
end

-- /tags
-- /tags/:tags
if self.schema.name == "tags" then
local matched_tags = nil

local matched_tags = nil

if options.tags then
matched_tags = {}
for _, tag in ipairs(options.tags) do
matched_tags[tag] = true
end
if options.tags then
matched_tags = {}
for _, tag in ipairs(options.tags) do
matched_tags[tag] = true
end
end

-- Each page operation retrieves the entities of only one DAO type.
local schema_name, offset_token, dao
-- Each page operation retrieves the entities of only one DAO type.
local schema_name, offset_token, dao

if offset then
schema_name, offset_token = offset:match("^([^|]+)|(.+)")
if not schema_name then
return nil, self.errors:invalid_offset(offset, "bad offset string")
end
if offset then
schema_name, offset_token = offset:match("^([^|]+)|(.+)")
if not schema_name then
return nil, self.errors:invalid_offset(offset, "bad offset string")
end

if offset_token == "nil" then
offset_token = nil
if offset_token == "nil" then
offset_token = nil

else
offset_token = decode_base64(offset_token)
if not offset_token then
return nil, self.errors:invalid_offset(offset_token, "bad base64 encoding")
end
else
offset_token = decode_base64(offset_token)
if not offset_token then
return nil, self.errors:invalid_offset(offset_token, "bad base64 encoding")
end
end
end

if offset_token then
-- There are still some entities left from the last page operation that
-- haven't been retrieved, so we need to use the previous dao
dao = kong.db.daos[schema_name]
else
schema_name, dao = next(kong.db.daos, schema_name)
end
if offset_token then
-- There are still some entities left from the last page operation that
-- haven't been retrieved, so we need to use the previous dao
dao = kong.db.daos[schema_name]
else
schema_name, dao = next(kong.db.daos, schema_name)
end

local prefix = item_key_prefix(schema_name, "*")
local prefix = item_key_prefix(schema_name, "*")

local rows, err
local rows, err

rows, err, offset_token = page_for_prefix(self, prefix, size, offset_token,
options, true, dao.schema)
if not rows then
return nil, err
end
rows, err, offset_token = page_for_prefix(self, prefix, size, offset_token,
options, true, dao.schema)
if not rows then
return nil, err
end

local items = {}
for _, item in ipairs(rows) do
for _, tag in ipairs(item.tags or {}) do
-- TODO: Could item.id be used as entity_id precisely?
if not matched_tags or matched_tags[tag] then
local item = { tag = tag, entity_name = schema_name, entity_id = item.id }
table.insert(items, item)
end
local items = {}
for _, item in ipairs(rows) do
for _, tag in ipairs(item.tags or {}) do
-- TODO: Could item.id be used as entity_id precisely?
if not matched_tags or matched_tags[tag] then
local item = { tag = tag, entity_name = schema_name, entity_id = item.id }
table.insert(items, item)
end
end

if not offset_token and not next(kong.db.daos, schema_name) then -- end
offset = nil
else
offset = schema_name .. "|" .. (offset_token or "nil")
end

return items, nil, offset
end

-- /:entitiy?tags=:tags
-- search all key-values: <entity_name>|*|*|<pk_string> => actual item key
local prefix = item_key_prefix(self.schema.name, "*") -- "<entity_name>|*|*|"
local items, err, offset = page_for_prefix(self, prefix, size, offset,
options, true)
if not items then
return nil, err
if not offset_token and not next(kong.db.daos, schema_name) then -- end
offset = nil
else
offset = schema_name .. "|" .. (offset_token or "nil")
end

items = filter_tag_items(items, options.tags, options.tags_cond)

return items, err, offset
return items, nil, offset
end


Expand Down

0 comments on commit a2510e8

Please sign in to comment.