Skip to content

Commit 8802f44

Browse files
committed
chore(db) small cleanups on off strategy dao
1 parent eee3867 commit 8802f44

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

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() or kong.default_workspace
4353
end
4454

4555

@@ -97,7 +107,7 @@ local function get_entity_ids_tagged(key, tag_names, tags_cond)
97107
len = len + 1
98108
arr[len] = entity_id
99109
end
100-
table.sort(arr) -- consistency when paginating results
110+
sort(arr) -- consistency when paginating results
101111

102112
return arr
103113
end
@@ -142,7 +152,8 @@ local function page_for_key(self, key, size, offset, options)
142152
end
143153

144154
local ret = {}
145-
local schema_name = self.schema.name
155+
local schema = self.schema
156+
local schema_name = schema.name
146157

147158
local item
148159
for i = offset, offset + size - 1 do
@@ -158,7 +169,7 @@ local function page_for_key(self, key, size, offset, options)
158169
-- For example "admin|services|<a service uuid>"
159170
-- This loop transforms each individual string into tables.
160171
if schema_name == "tags" then
161-
local tag_name, entity_name, uuid = string.match(item, "^([^|]+)|([^|]+)|(.+)$")
172+
local tag_name, entity_name, uuid = match(item, "^([^|]+)|([^|]+)|(.+)$")
162173
if not tag_name then
163174
return nil, "Could not parse tag from cache: " .. tostring(item)
164175
end
@@ -178,12 +189,7 @@ local function page_for_key(self, key, size, offset, options)
178189
return nil, "stale data detected while paginating"
179190
end
180191

181-
item = self.schema:process_auto_fields(item, "select", true, {
182-
no_defaults = true,
183-
show_ws_id = true,
184-
})
185-
186-
ret[i - offset + 1] = item
192+
ret[i - offset + 1] = schema:process_auto_fields(item, "select", true, PROCESS_AUTO_FIELDS_OPTS)
187193
end
188194

189195
if offset then
@@ -194,33 +200,32 @@ local function page_for_key(self, key, size, offset, options)
194200
end
195201

196202

197-
local function select_by_key(self, key)
203+
local function select_by_key(schema, key)
198204
local entity, err = unmarshall(lmdb_get(key))
199205
if not entity then
200206
return nil, err
201207
end
202208

203-
entity = self.schema:process_auto_fields(entity, "select", true, {
204-
no_defaults = true,
205-
show_ws_id = true,
206-
})
209+
entity = schema:process_auto_fields(entity, "select", true, PROCESS_AUTO_FIELDS_OPTS)
207210

208211
return entity
209212
end
210213

211214

212215
local function page(self, size, offset, options)
213-
local ws_id = ws(self, options)
214-
local key = self.schema.name .. "|" .. ws_id .. "|@list"
216+
local schema = self.schema
217+
local ws_id = ws(schema, options)
218+
local key = schema.name .. "|" .. ws_id .. "|@list"
215219
return page_for_key(self, key, size, offset, options)
216220
end
217221

218222

219223
local function select(self, pk, options)
220-
local ws_id = ws(self, options)
221-
local id = declarative_config.pk_string(self.schema, pk)
222-
local key = self.schema.name .. ":" .. id .. ":::::" .. ws_id
223-
return select_by_key(self, key)
224+
local schema = self.schema
225+
local ws_id = ws(schema, options)
226+
local id = declarative_config.pk_string(schema, pk)
227+
local key = schema.name .. ":" .. id .. ":::::" .. ws_id
228+
return select_by_key(schema, key)
224229
end
225230

226231

@@ -230,28 +235,30 @@ local function select_by_field(self, field, value, options)
230235
_, value = next(value)
231236
end
232237

233-
local ws_id = ws(self, options)
238+
local schema = self.schema
239+
local ws_id = ws(schema, options)
234240

235241
local key
236242
if field ~= "cache_key" then
237-
local unique_across_ws = self.schema.fields[field].unique_across_ws
243+
local unique_across_ws = schema.fields[field].unique_across_ws
238244
if unique_across_ws then
239245
ws_id = ""
240246
end
241247

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

245-
key = self.schema.name .. "|" .. ws_id .. "|" .. field .. ":" .. value
251+
key = schema.name .. "|" .. ws_id .. "|" .. field .. ":" .. value
246252

247253
else
248254
-- if select_by_cache_key, use the provided cache_key as key directly
249255
key = value
250256
end
251257

252-
return select_by_key(self, key)
258+
return select_by_key(schema, key)
253259
end
254260

261+
255262
do
256263
local unsupported = function(operation)
257264
return function(self)
@@ -262,7 +269,6 @@ do
262269
end
263270

264271
local unsupported_by = function(operation)
265-
266272
return function(self, field_name)
267273
local err = fmt("cannot %s '%s' entities by '%s' when not using a database",
268274
operation, self.schema.name, '%s')
@@ -285,6 +291,7 @@ do
285291
_mt.page_for_key = page_for_key
286292
end
287293

294+
288295
function off.new(connector, schema, errors)
289296
local self = {
290297
connector = connector, -- instance of kong.db.strategies.off.connector
@@ -299,14 +306,13 @@ function off.new(connector, schema, errors)
299306
kong.default_workspace = "00000000-0000-0000-0000-000000000000"
300307
end
301308

302-
local name = self.schema.name
309+
local name = schema.name
303310
for fname, fdata in schema:each_field() do
304311
if fdata.type == "foreign" then
305312
local entity = fdata.reference
306313
local method = "page_for_" .. fname
307314
self[method] = function(_, foreign_key, size, offset, options)
308-
local ws_id = ws(self, options)
309-
315+
local ws_id = ws(schema, options)
310316
local key = name .. "|" .. ws_id .. "|" .. entity .. "|" .. foreign_key.id .. "|@list"
311317
return page_for_key(self, key, size, offset, options)
312318
end

0 commit comments

Comments
 (0)