Skip to content

Commit e80acd0

Browse files
committed
chore(db) small cleanups on off strategy dao
1 parent 337122e commit e80acd0

File tree

1 file changed

+41
-35
lines changed

1 file changed

+41
-35
lines changed

kong/db/strategies/off/init.lua

+41-35
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,28 @@ local lmdb = require("resty.lmdb")
44
local marshaller = require("kong.db.declarative.marshaller")
55
local yield = require("kong.tools.utils").yield
66

7-
87
local kong = kong
98
local fmt = string.format
109
local type = type
1110
local next = next
11+
local sort = table.sort
1212
local pairs = pairs
13+
local match = string.match
14+
local assert = assert
1315
local tostring = tostring
1416
local tonumber = tonumber
1517
local encode_base64 = ngx.encode_base64
1618
local decode_base64 = ngx.decode_base64
17-
local null = ngx.null
18-
local unmarshall = marshaller.unmarshall
19-
local lmdb_get = lmdb.get
19+
local null = ngx.null
20+
local unmarshall = marshaller.unmarshall
21+
local lmdb_get = lmdb.get
22+
local get_workspace_id = workspaces.get_workspace_id
23+
24+
25+
local PROCESS_AUTO_FIELDS_OPTS = {
26+
no_defaults = true,
27+
show_ws_id = true,
28+
}
2029

2130

2231
local off = {}
@@ -26,8 +35,8 @@ local _mt = {}
2635
_mt.__index = _mt
2736

2837

29-
local function ws(self, options)
30-
if not self.schema.workspaceable then
38+
local function ws(schema, options)
39+
if not schema.workspaceable then
3140
return ""
3241
end
3342

@@ -39,7 +48,8 @@ local function ws(self, options)
3948
return options.workspace
4049
end
4150
end
42-
return workspaces.get_workspace_id() or kong.default_workspace
51+
52+
return get_workspace_id()
4353
end
4454

4555

@@ -99,7 +109,7 @@ local function get_entity_ids_tagged(key, tag_names, tags_cond)
99109
len = len + 1
100110
arr[len] = entity_id
101111
end
102-
table.sort(arr) -- consistency when paginating results
112+
sort(arr) -- consistency when paginating results
103113

104114
return arr
105115
end
@@ -146,7 +156,8 @@ local function page_for_key(self, key, size, offset, options)
146156
yield()
147157

148158
local ret = {}
149-
local schema_name = self.schema.name
159+
local schema = self.schema
160+
local schema_name = schema.name
150161

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

187-
item = self.schema:process_auto_fields(item, "select", true, {
188-
no_defaults = true,
189-
show_ws_id = true,
190-
})
191-
192-
ret[i - offset + 1] = item
198+
ret[i - offset + 1] = schema:process_auto_fields(item, "select", true, PROCESS_AUTO_FIELDS_OPTS)
193199
end
194200

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

202208

203-
local function select_by_key(self, key)
209+
local function select_by_key(schema, key)
204210
local entity, err = unmarshall(lmdb_get(key))
205211
if not entity then
206212
return nil, err
207213
end
208214

209-
entity = self.schema:process_auto_fields(entity, "select", true, {
210-
no_defaults = true,
211-
show_ws_id = true,
212-
})
215+
entity = schema:process_auto_fields(entity, "select", true, PROCESS_AUTO_FIELDS_OPTS)
213216

214217
return entity
215218
end
216219

217220

218221
local function page(self, size, offset, options)
219-
local ws_id = ws(self, options)
220-
local key = self.schema.name .. "|" .. ws_id .. "|@list"
222+
local schema = self.schema
223+
local ws_id = ws(schema, options)
224+
local key = schema.name .. "|" .. ws_id .. "|@list"
221225
return page_for_key(self, key, size, offset, options)
222226
end
223227

224228

225229
local function select(self, pk, options)
226-
local ws_id = ws(self, options)
227-
local id = declarative_config.pk_string(self.schema, pk)
228-
local key = self.schema.name .. ":" .. id .. ":::::" .. ws_id
229-
return select_by_key(self, key)
230+
local schema = self.schema
231+
local ws_id = ws(schema, options)
232+
local id = declarative_config.pk_string(schema, pk)
233+
local key = schema.name .. ":" .. id .. ":::::" .. ws_id
234+
return select_by_key(schema, key)
230235
end
231236

232237

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

239-
local ws_id = ws(self, options)
244+
local schema = self.schema
245+
local ws_id = ws(schema, options)
240246

241247
local key
242248
if field ~= "cache_key" then
243-
local unique_across_ws = self.schema.fields[field].unique_across_ws
249+
local unique_across_ws = schema.fields[field].unique_across_ws
244250
if unique_across_ws then
245251
ws_id = ""
246252
end
247253

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

251-
key = self.schema.name .. "|" .. ws_id .. "|" .. field .. ":" .. value
257+
key = schema.name .. "|" .. ws_id .. "|" .. field .. ":" .. value
252258

253259
else
254260
-- if select_by_cache_key, use the provided cache_key as key directly
255261
key = value
256262
end
257263

258-
return select_by_key(self, key)
264+
return select_by_key(schema, key)
259265
end
260266

267+
261268
do
262269
local unsupported = function(operation)
263270
return function(self)
@@ -268,7 +275,6 @@ do
268275
end
269276

270277
local unsupported_by = function(operation)
271-
272278
return function(self, field_name)
273279
local err = fmt("cannot %s '%s' entities by '%s' when not using a database",
274280
operation, self.schema.name, '%s')
@@ -291,6 +297,7 @@ do
291297
_mt.page_for_key = page_for_key
292298
end
293299

300+
294301
function off.new(connector, schema, errors)
295302
local self = {
296303
connector = connector, -- instance of kong.db.strategies.off.connector
@@ -305,14 +312,13 @@ function off.new(connector, schema, errors)
305312
kong.default_workspace = "00000000-0000-0000-0000-000000000000"
306313
end
307314

308-
local name = self.schema.name
315+
local name = schema.name
309316
for fname, fdata in schema:each_field() do
310317
if fdata.type == "foreign" then
311318
local entity = fdata.reference
312319
local method = "page_for_" .. fname
313320
self[method] = function(_, foreign_key, size, offset, options)
314-
local ws_id = ws(self, options)
315-
321+
local ws_id = ws(schema, options)
316322
local key = name .. "|" .. ws_id .. "|" .. entity .. "|" .. foreign_key.id .. "|@list"
317323
return page_for_key(self, key, size, offset, options)
318324
end

0 commit comments

Comments
 (0)