Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 1e2e374

Browse files
committed
Pass a user query context to accessor functions
Requested by a customer, no issue.
1 parent c220f3f commit 1e2e374

File tree

6 files changed

+446
-78
lines changed

6 files changed

+446
-78
lines changed

graphql/accessor_general.lua

Lines changed: 73 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,12 @@ local function process_tuple(self, state, tuple, opts)
320320
local variables = qcontext.variables
321321

322322
-- convert tuple -> object
323-
local obj = opts.unflatten_tuple(self, collection_name, tuple,
324-
{ use_tomap = opts.use_tomap }, opts.default_unflatten_tuple)
323+
local avro_opts = {
324+
use_tomap = opts.use_tomap,
325+
user_context = qcontext.user_context,
326+
}
327+
local obj = opts.unflatten_tuple(self, collection_name, tuple, avro_opts,
328+
opts.default_unflatten_tuple)
325329

326330
-- skip all items before pivot (the item pointed by offset)
327331
if not state.pivot_found and pivot_filter then
@@ -383,15 +387,18 @@ end
383387
---
384388
--- @tparam table selected objects to perform the operation
385389
---
390+
--- @tparam table avro_opts options for the unflatten_tuple() accessor function
391+
---
386392
--- @tparam string operation 'update_tuple' or 'delete_tuple'
387393
---
388394
--- @param[opt] ... parameters to pass to the function
389395
---
390396
--- @treturn table `new_objects` list of returned objects (in the order of the
391397
--- `selected` parameter)
392398
local function perform_primary_key_operation(self, collection_name, schema_name,
393-
selected, operation, ...)
399+
selected, avro_opts, operation, ...)
394400
check(operation, 'operation', 'string')
401+
check(avro_opts, 'avro_opts', 'table')
395402

396403
local _, primary_index_meta = db_schema_helpers.get_primary_index_meta(
397404
self, collection_name)
@@ -407,8 +414,7 @@ local function perform_primary_key_operation(self, collection_name, schema_name,
407414
-- delete_tuple to save one get operation
408415
local new_tuple = self.funcs[operation](self, collection_name, key, ...)
409416
local new_object = self.funcs.unflatten_tuple(self, collection_name,
410-
new_tuple, {use_tomap = self.collection_use_tomap[collection_name]},
411-
self.default_unflatten_tuple[schema_name])
417+
new_tuple, avro_opts, self.default_unflatten_tuple[schema_name])
412418
table.insert(new_objects, new_object)
413419
end
414420

@@ -449,21 +455,28 @@ local function prepare_select_internal(self, collection_name, from, filter,
449455
-- XXX: save type of args.offset at parsing and check here
450456
-- check(args.offset, 'args.offset', ...)
451457
check(args.pcre, 'args.pcre', 'table', 'nil')
458+
check(extra, 'extra', 'table')
452459
check(extra.exp_tuple_count, 'extra.exp_tuple_count', 'number', 'nil')
453460

461+
local qcontext = extra.qcontext
462+
check(qcontext, 'qcontext', 'table')
463+
local get_index_opts = {user_context = qcontext.user_context}
464+
454465
local collection = self.collections[collection_name]
455466
assert(collection ~= nil,
456467
('cannot find the collection "%s"'):format(
457468
collection_name))
458-
assert(self.funcs.is_collection_exists(self, collection_name),
459-
('cannot find collection "%s"'):format(collection_name))
469+
assert(self.funcs.is_collection_exists(self, collection_name,
470+
get_index_opts), ('cannot find collection "%s"'):format(
471+
collection_name))
460472

461473
-- search for suitable index
462474
-- note: we redefine filter here
463475
local full_match, index_name, filter, index_value, pivot =
464476
self.index_finder:find(collection_name, from, filter, args)
465477
local index = index_name ~= nil and
466-
self.funcs.get_index(self, collection_name, index_name) or nil
478+
self.funcs.get_index(self, collection_name, index_name,
479+
get_index_opts) or nil
467480
if index_name ~= nil and index == nil then
468481
error(('cannot find actual index "%s" in the collection "%s", ' ..
469482
'but the index metainformation contains it'):format(index_name,
@@ -486,7 +499,6 @@ local function prepare_select_internal(self, collection_name, from, filter,
486499
collection_name))
487500

488501
-- read-write variables for process_tuple
489-
local qcontext = extra.qcontext
490502
local select_state = {
491503
count = 0,
492504
objs = {},
@@ -541,7 +553,8 @@ local function prepare_select_internal(self, collection_name, from, filter,
541553
if index == nil then
542554
assert(pivot == nil,
543555
'offset for top-level objects must use a primary index')
544-
index = self.funcs.get_primary_index(self, collection_name)
556+
index = self.funcs.get_primary_index(self, collection_name,
557+
get_index_opts)
545558
index_value = nil
546559
is_full_scan = true
547560
else
@@ -605,6 +618,7 @@ local function invoke_select_internal(self, prepared_select)
605618
local collection_name = prepared_select.collection_name
606619
local args = prepared_select.args
607620
local extra = prepared_select.extra
621+
local qcontext = extra.qcontext
608622

609623
local index = request_opts.index
610624
local index_name = request_opts.index_name
@@ -618,8 +632,9 @@ local function invoke_select_internal(self, prepared_select)
618632
-- lookup for needed data in the cache if it is supported
619633
local iterable
620634
if self:cache_is_supported() then
635+
local cache_opts = {user_context = qcontext.user_context}
621636
iterable = self.funcs.cache_lookup(self, collection_name, index_name,
622-
index_value, iterator_opts)
637+
index_value, iterator_opts, cache_opts)
623638
end
624639
if iterable == nil then
625640
iterable = index
@@ -698,23 +713,30 @@ local function insert_internal(self, collection_name, from, filter, args, extra)
698713
-- allow only top level collection
699714
check(from.collection_name, 'from.collection_name', 'nil')
700715

701-
-- convert object -> tuple (set default values from a schema)
702716
local schema_name = db_schema_helpers.get_schema_name(self, collection_name)
717+
local qcontext = extra.qcontext
718+
local avro_opts = {
719+
use_tomap = self.collection_use_tomap[collection_name],
720+
service_fields_defaults = self.service_fields_defaults[schema_name],
721+
user_context = qcontext.user_context,
722+
}
723+
local dml_opts = {
724+
user_context = qcontext.user_context,
725+
}
726+
727+
-- convert object -> tuple (set default values from a schema)
703728
local default_flatten_object = self.default_flatten_object[schema_name]
704729
assert(default_flatten_object ~= nil,
705730
('cannot find default_flatten_object ' ..
706731
'for collection "%s"'):format(collection_name))
707-
local tuple = self.funcs.flatten_object(self, collection_name, object, {
708-
service_fields_defaults =
709-
self.service_fields_defaults[schema_name],
710-
}, default_flatten_object)
732+
local tuple = self.funcs.flatten_object(self, collection_name, object,
733+
avro_opts, default_flatten_object)
711734

712735
-- insert tuple & tuple -> object (with default values set before)
713-
local new_tuple = self.funcs.insert_tuple(self, collection_name, tuple)
714-
local use_tomap = self.collection_use_tomap[collection_name]
736+
local new_tuple = self.funcs.insert_tuple(self, collection_name, tuple,
737+
dml_opts)
715738
local new_object = self.funcs.unflatten_tuple(self, collection_name,
716-
new_tuple, {use_tomap = use_tomap},
717-
self.default_unflatten_tuple[schema_name])
739+
new_tuple, avro_opts, self.default_unflatten_tuple[schema_name])
718740

719741
return {new_object}
720742
end
@@ -742,19 +764,27 @@ local function update_internal(self, collection_name, extra, selected)
742764
'arguments'
743765
assert(next(extra.extra_args, next(extra.extra_args)) == nil, err_msg)
744766

745-
-- convert xobject -> update statements
746767
local schema_name = db_schema_helpers.get_schema_name(self, collection_name)
768+
local qcontext = extra.qcontext
769+
local avro_opts = {
770+
use_tomap = self.collection_use_tomap[collection_name],
771+
service_fields_defaults = self.service_fields_defaults[schema_name],
772+
user_context = qcontext.user_context,
773+
}
774+
local dml_opts = {
775+
user_context = qcontext.user_context,
776+
}
777+
778+
-- convert xobject -> update statements
747779
local default_xflatten = self.default_xflatten[schema_name]
748780
assert(default_xflatten ~= nil,
749781
('cannot find default_xflatten ' ..
750782
'for collection "%s"'):format(collection_name))
751-
local statements = self.funcs.xflatten(self, collection_name, xobject, {
752-
service_fields_defaults =
753-
self.service_fields_defaults[schema_name],
754-
}, default_xflatten)
783+
local statements = self.funcs.xflatten(self, collection_name, xobject,
784+
avro_opts, default_xflatten)
755785

756786
return perform_primary_key_operation(self, collection_name, schema_name,
757-
selected, 'update_tuple', statements)
787+
selected, avro_opts, 'update_tuple', statements, dml_opts)
758788
end
759789

760790
--- Delete an object.
@@ -778,10 +808,18 @@ local function delete_internal(self, collection_name, extra, selected)
778808
'arguments'
779809
assert(next(extra.extra_args, next(extra.extra_args)) == nil, err_msg)
780810

811+
local qcontext = extra.qcontext
781812
local schema_name = db_schema_helpers.get_schema_name(self, collection_name)
782813

814+
local avro_opts = {
815+
use_tomap = self.collection_use_tomap[collection_name],
816+
user_context = qcontext.user_context,
817+
}
818+
local dml_opts = {
819+
user_context = qcontext.user_context,
820+
}
783821
return perform_primary_key_operation(self, collection_name, schema_name,
784-
selected, 'delete_tuple')
822+
selected, avro_opts, 'delete_tuple', dml_opts)
785823
end
786824

787825
--- Set of asserts to check the `funcs` argument of the @{accessor_general.new}
@@ -1122,7 +1160,8 @@ function accessor_general.new(opts, funcs)
11221160
return nil
11231161
end
11241162

1125-
local res = self.funcs.cache_fetch(self, batches)
1163+
local cache_opts = {user_context = qcontext.user_context}
1164+
local res = self.funcs.cache_fetch(self, batches, cache_opts)
11261165
if res == nil then
11271166
return nil
11281167
end
@@ -1151,17 +1190,19 @@ function accessor_general.new(opts, funcs)
11511190
return fetch_id
11521191
end,
11531192
-- Unused for now.
1154-
-- cache_delete = function(self, fetch_id)
1193+
-- cache_delete = function(self, fetch_id, qcontext)
11551194
-- if not self:cache_is_supported() then
11561195
-- return
11571196
-- end
1158-
-- self.funcs.cache_delete(self, fetch_id)
1197+
-- local cache_opts = {user_context = qcontext.user_context}
1198+
-- self.funcs.cache_delete(self, fetch_id, cache_opts)
11591199
-- end,
1160-
cache_truncate = function(self)
1200+
cache_truncate = function(self, qcontext)
11611201
if not self:cache_is_supported() then
11621202
return
11631203
end
1164-
self.funcs.cache_truncate(self)
1204+
local cache_opts = {user_context = qcontext.user_context}
1205+
self.funcs.cache_truncate(self, cache_opts)
11651206
end,
11661207
}
11671208
})

0 commit comments

Comments
 (0)