Skip to content

Commit

Permalink
chore(db) small cleanups on off strategy dao
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle committed May 17, 2022
1 parent 337122e commit e80acd0
Showing 1 changed file with 41 additions and 35 deletions.
76 changes: 41 additions & 35 deletions kong/db/strategies/off/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,28 @@ local lmdb = require("resty.lmdb")
local marshaller = require("kong.db.declarative.marshaller")
local yield = require("kong.tools.utils").yield


local kong = kong
local fmt = string.format
local type = type
local next = next
local sort = table.sort
local pairs = pairs
local match = string.match
local assert = assert
local tostring = tostring
local tonumber = tonumber
local encode_base64 = ngx.encode_base64
local decode_base64 = ngx.decode_base64
local null = ngx.null
local unmarshall = marshaller.unmarshall
local lmdb_get = lmdb.get
local null = ngx.null
local unmarshall = marshaller.unmarshall
local lmdb_get = lmdb.get
local get_workspace_id = workspaces.get_workspace_id


local PROCESS_AUTO_FIELDS_OPTS = {
no_defaults = true,
show_ws_id = true,
}


local off = {}
Expand All @@ -26,8 +35,8 @@ local _mt = {}
_mt.__index = _mt


local function ws(self, options)
if not self.schema.workspaceable then
local function ws(schema, options)
if not schema.workspaceable then
return ""
end

Expand All @@ -39,7 +48,8 @@ local function ws(self, options)
return options.workspace
end
end
return workspaces.get_workspace_id() or kong.default_workspace

return get_workspace_id()
end


Expand Down Expand Up @@ -99,7 +109,7 @@ local function get_entity_ids_tagged(key, tag_names, tags_cond)
len = len + 1
arr[len] = entity_id
end
table.sort(arr) -- consistency when paginating results
sort(arr) -- consistency when paginating results

return arr
end
Expand Down Expand Up @@ -146,7 +156,8 @@ local function page_for_key(self, key, size, offset, options)
yield()

local ret = {}
local schema_name = self.schema.name
local schema = self.schema
local schema_name = schema.name

local item
for i = offset, offset + size - 1 do
Expand All @@ -164,7 +175,7 @@ local function page_for_key(self, key, size, offset, options)
-- For example "admin|services|<a service uuid>"
-- This loop transforms each individual string into tables.
if schema_name == "tags" then
local tag_name, entity_name, uuid = string.match(item, "^([^|]+)|([^|]+)|(.+)$")
local tag_name, entity_name, uuid = match(item, "^([^|]+)|([^|]+)|(.+)$")
if not tag_name then
return nil, "Could not parse tag from cache: " .. tostring(item)
end
Expand All @@ -184,12 +195,7 @@ local function page_for_key(self, key, size, offset, options)
return nil, "stale data detected while paginating"
end

item = self.schema:process_auto_fields(item, "select", true, {
no_defaults = true,
show_ws_id = true,
})

ret[i - offset + 1] = item
ret[i - offset + 1] = schema:process_auto_fields(item, "select", true, PROCESS_AUTO_FIELDS_OPTS)
end

if offset then
Expand All @@ -200,33 +206,32 @@ local function page_for_key(self, key, size, offset, options)
end


local function select_by_key(self, key)
local function select_by_key(schema, key)
local entity, err = unmarshall(lmdb_get(key))
if not entity then
return nil, err
end

entity = self.schema:process_auto_fields(entity, "select", true, {
no_defaults = true,
show_ws_id = true,
})
entity = schema:process_auto_fields(entity, "select", true, PROCESS_AUTO_FIELDS_OPTS)

return entity
end


local function page(self, size, offset, options)
local ws_id = ws(self, options)
local key = self.schema.name .. "|" .. ws_id .. "|@list"
local schema = self.schema
local ws_id = ws(schema, options)
local key = schema.name .. "|" .. ws_id .. "|@list"
return page_for_key(self, key, size, offset, options)
end


local function select(self, pk, options)
local ws_id = ws(self, options)
local id = declarative_config.pk_string(self.schema, pk)
local key = self.schema.name .. ":" .. id .. ":::::" .. ws_id
return select_by_key(self, key)
local schema = self.schema
local ws_id = ws(schema, options)
local id = declarative_config.pk_string(schema, pk)
local key = schema.name .. ":" .. id .. ":::::" .. ws_id
return select_by_key(schema, key)
end


Expand All @@ -236,28 +241,30 @@ local function select_by_field(self, field, value, options)
_, value = next(value)
end

local ws_id = ws(self, options)
local schema = self.schema
local ws_id = ws(schema, options)

local key
if field ~= "cache_key" then
local unique_across_ws = self.schema.fields[field].unique_across_ws
local unique_across_ws = schema.fields[field].unique_across_ws
if unique_across_ws then
ws_id = ""
end

-- only accept global query by field if field is unique across workspaces
assert(not options or options.workspace ~= null or unique_across_ws)

key = self.schema.name .. "|" .. ws_id .. "|" .. field .. ":" .. value
key = schema.name .. "|" .. ws_id .. "|" .. field .. ":" .. value

else
-- if select_by_cache_key, use the provided cache_key as key directly
key = value
end

return select_by_key(self, key)
return select_by_key(schema, key)
end


do
local unsupported = function(operation)
return function(self)
Expand All @@ -268,7 +275,6 @@ do
end

local unsupported_by = function(operation)

return function(self, field_name)
local err = fmt("cannot %s '%s' entities by '%s' when not using a database",
operation, self.schema.name, '%s')
Expand All @@ -291,6 +297,7 @@ do
_mt.page_for_key = page_for_key
end


function off.new(connector, schema, errors)
local self = {
connector = connector, -- instance of kong.db.strategies.off.connector
Expand All @@ -305,14 +312,13 @@ function off.new(connector, schema, errors)
kong.default_workspace = "00000000-0000-0000-0000-000000000000"
end

local name = self.schema.name
local name = schema.name
for fname, fdata in schema:each_field() do
if fdata.type == "foreign" then
local entity = fdata.reference
local method = "page_for_" .. fname
self[method] = function(_, foreign_key, size, offset, options)
local ws_id = ws(self, options)

local ws_id = ws(schema, options)
local key = name .. "|" .. ws_id .. "|" .. entity .. "|" .. foreign_key.id .. "|@list"
return page_for_key(self, key, size, offset, options)
end
Expand Down

0 comments on commit e80acd0

Please sign in to comment.