@@ -4,19 +4,28 @@ local lmdb = require("resty.lmdb")
4
4
local marshaller = require (" kong.db.declarative.marshaller" )
5
5
local yield = require (" kong.tools.utils" ).yield
6
6
7
-
8
7
local kong = kong
9
8
local fmt = string.format
10
9
local type = type
11
10
local next = next
11
+ local sort = table.sort
12
12
local pairs = pairs
13
+ local match = string.match
14
+ local assert = assert
13
15
local tostring = tostring
14
16
local tonumber = tonumber
15
17
local encode_base64 = ngx .encode_base64
16
18
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
+ }
20
29
21
30
22
31
local off = {}
@@ -26,8 +35,8 @@ local _mt = {}
26
35
_mt .__index = _mt
27
36
28
37
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
31
40
return " "
32
41
end
33
42
@@ -39,7 +48,8 @@ local function ws(self, options)
39
48
return options .workspace
40
49
end
41
50
end
42
- return workspaces .get_workspace_id () or kong .default_workspace
51
+
52
+ return get_workspace_id ()
43
53
end
44
54
45
55
@@ -99,7 +109,7 @@ local function get_entity_ids_tagged(key, tag_names, tags_cond)
99
109
len = len + 1
100
110
arr [len ] = entity_id
101
111
end
102
- table. sort (arr ) -- consistency when paginating results
112
+ sort (arr ) -- consistency when paginating results
103
113
104
114
return arr
105
115
end
@@ -146,7 +156,8 @@ local function page_for_key(self, key, size, offset, options)
146
156
yield ()
147
157
148
158
local ret = {}
149
- local schema_name = self .schema .name
159
+ local schema = self .schema
160
+ local schema_name = schema .name
150
161
151
162
local item
152
163
for i = offset , offset + size - 1 do
@@ -164,7 +175,7 @@ local function page_for_key(self, key, size, offset, options)
164
175
-- For example "admin|services|<a service uuid>"
165
176
-- This loop transforms each individual string into tables.
166
177
if schema_name == " tags" then
167
- local tag_name , entity_name , uuid = string. match (item , " ^([^|]+)|([^|]+)|(.+)$" )
178
+ local tag_name , entity_name , uuid = match (item , " ^([^|]+)|([^|]+)|(.+)$" )
168
179
if not tag_name then
169
180
return nil , " Could not parse tag from cache: " .. tostring (item )
170
181
end
@@ -184,12 +195,7 @@ local function page_for_key(self, key, size, offset, options)
184
195
return nil , " stale data detected while paginating"
185
196
end
186
197
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 )
193
199
end
194
200
195
201
if offset then
@@ -200,33 +206,32 @@ local function page_for_key(self, key, size, offset, options)
200
206
end
201
207
202
208
203
- local function select_by_key (self , key )
209
+ local function select_by_key (schema , key )
204
210
local entity , err = unmarshall (lmdb_get (key ))
205
211
if not entity then
206
212
return nil , err
207
213
end
208
214
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 )
213
216
214
217
return entity
215
218
end
216
219
217
220
218
221
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"
221
225
return page_for_key (self , key , size , offset , options )
222
226
end
223
227
224
228
225
229
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 )
230
235
end
231
236
232
237
@@ -236,28 +241,30 @@ local function select_by_field(self, field, value, options)
236
241
_ , value = next (value )
237
242
end
238
243
239
- local ws_id = ws (self , options )
244
+ local schema = self .schema
245
+ local ws_id = ws (schema , options )
240
246
241
247
local key
242
248
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
244
250
if unique_across_ws then
245
251
ws_id = " "
246
252
end
247
253
248
254
-- only accept global query by field if field is unique across workspaces
249
255
assert (not options or options .workspace ~= null or unique_across_ws )
250
256
251
- key = self . schema .name .. " |" .. ws_id .. " |" .. field .. " :" .. value
257
+ key = schema .name .. " |" .. ws_id .. " |" .. field .. " :" .. value
252
258
253
259
else
254
260
-- if select_by_cache_key, use the provided cache_key as key directly
255
261
key = value
256
262
end
257
263
258
- return select_by_key (self , key )
264
+ return select_by_key (schema , key )
259
265
end
260
266
267
+
261
268
do
262
269
local unsupported = function (operation )
263
270
return function (self )
268
275
end
269
276
270
277
local unsupported_by = function (operation )
271
-
272
278
return function (self , field_name )
273
279
local err = fmt (" cannot %s '%s' entities by '%s' when not using a database" ,
274
280
operation , self .schema .name , ' %s' )
291
297
_mt .page_for_key = page_for_key
292
298
end
293
299
300
+
294
301
function off .new (connector , schema , errors )
295
302
local self = {
296
303
connector = connector , -- instance of kong.db.strategies.off.connector
@@ -305,14 +312,13 @@ function off.new(connector, schema, errors)
305
312
kong .default_workspace = " 00000000-0000-0000-0000-000000000000"
306
313
end
307
314
308
- local name = self . schema .name
315
+ local name = schema .name
309
316
for fname , fdata in schema :each_field () do
310
317
if fdata .type == " foreign" then
311
318
local entity = fdata .reference
312
319
local method = " page_for_" .. fname
313
320
self [method ] = function (_ , foreign_key , size , offset , options )
314
- local ws_id = ws (self , options )
315
-
321
+ local ws_id = ws (schema , options )
316
322
local key = name .. " |" .. ws_id .. " |" .. entity .. " |" .. foreign_key .id .. " |@list"
317
323
return page_for_key (self , key , size , offset , options )
318
324
end
0 commit comments