@@ -5,6 +5,7 @@ local call = require('crud.common.call')
5
5
local const = require (' crud.common.const' )
6
6
local dev_checks = require (' crud.common.dev_checks' )
7
7
local cache = require (' crud.common.sharding.sharding_metadata_cache' )
8
+ local sharding_func = require (' crud.common.sharding.sharding_func' )
8
9
local sharding_key = require (' crud.common.sharding.sharding_key' )
9
10
10
11
local FetchShardingMetadataError = errors .new_class (' FetchShardingMetadataError' , {capture_stack = false })
@@ -38,25 +39,58 @@ local function locked(f)
38
39
end
39
40
end
40
41
41
- -- Return a map with metadata or nil when space box.space._ddl_sharding_key is
42
- -- not available on storage.
42
+ local function extract_sharding_func_def (tuple )
43
+ if not tuple then
44
+ return nil
45
+ end
46
+
47
+ local SPACE_SHARDING_FUNC_NAME_FIELDNO = 2
48
+ local SPACE_SHARDING_FUNC_BODY_FIELDNO = 3
49
+
50
+ if tuple [SPACE_SHARDING_FUNC_BODY_FIELDNO ] ~= nil then
51
+ return {body = tuple [SPACE_SHARDING_FUNC_BODY_FIELDNO ]}
52
+ end
53
+
54
+ if tuple [SPACE_SHARDING_FUNC_NAME_FIELDNO ] ~= nil then
55
+ return tuple [SPACE_SHARDING_FUNC_NAME_FIELDNO ]
56
+ end
57
+
58
+ return nil
59
+ end
60
+
61
+ -- Return a map with metadata or nil when spaces box.space._ddl_sharding_key and
62
+ -- box.space._ddl_sharding_func are not available on storage.
43
63
function sharding_metadata_module .fetch_on_storage ()
44
64
local sharding_key_space = box .space ._ddl_sharding_key
45
- if sharding_key_space == nil then
65
+ local sharding_func_space = box .space ._ddl_sharding_func
66
+
67
+ if sharding_key_space == nil and sharding_func_space == nil then
46
68
return nil
47
69
end
48
70
49
71
local SPACE_NAME_FIELDNO = 1
50
72
local SPACE_SHARDING_KEY_FIELDNO = 2
51
73
local metadata_map = {}
52
- for _ , tuple in sharding_key_space :pairs () do
53
- local space_name = tuple [SPACE_NAME_FIELDNO ]
54
- local sharding_key_def = tuple [SPACE_SHARDING_KEY_FIELDNO ]
55
- local space_format = box .space [space_name ]:format ()
56
- metadata_map [space_name ] = {
57
- sharding_key_def = sharding_key_def ,
58
- space_format = space_format ,
59
- }
74
+
75
+ if sharding_key_space ~= nil then
76
+ for _ , tuple in sharding_key_space :pairs () do
77
+ local space_name = tuple [SPACE_NAME_FIELDNO ]
78
+ local sharding_key_def = tuple [SPACE_SHARDING_KEY_FIELDNO ]
79
+ local space_format = box .space [space_name ]:format ()
80
+ metadata_map [space_name ] = {
81
+ sharding_key_def = sharding_key_def ,
82
+ space_format = space_format ,
83
+ }
84
+ end
85
+ end
86
+
87
+ if sharding_func_space ~= nil then
88
+ for _ , tuple in sharding_func_space :pairs () do
89
+ local space_name = tuple [SPACE_NAME_FIELDNO ]
90
+ local sharding_func_def = extract_sharding_func_def (tuple )
91
+ metadata_map [space_name ] = metadata_map [space_name ] or {}
92
+ metadata_map [space_name ].sharding_func_def = sharding_func_def
93
+ end
60
94
end
61
95
62
96
return metadata_map
@@ -83,24 +117,21 @@ local _fetch_on_router = locked(function(timeout, space_name, metadata_map_name)
83
117
end
84
118
if metadata_map == nil then
85
119
cache [cache .SHARDING_KEY_MAP_NAME ] = {}
120
+ cache [cache .SHARDING_FUNC_MAP_NAME ] = {}
86
121
return
87
122
end
88
123
89
124
local err = sharding_key .construct_as_index_obj_cache (metadata_map , space_name )
90
125
if err ~= nil then
91
126
return err
92
127
end
128
+
129
+ local err = sharding_func .construct_as_callable_obj_cache (metadata_map , space_name )
130
+ if err ~= nil then
131
+ return err
132
+ end
93
133
end )
94
134
95
- -- Get sharding index for a certain space.
96
- --
97
- -- Return:
98
- -- - sharding key as index object, when sharding key definition found on
99
- -- storage.
100
- -- - nil, when sharding key definition was not found on storage. Pay attention
101
- -- that nil without error is a successfull return value.
102
- -- - nil and error, when something goes wrong on fetching attempt.
103
- --
104
135
local function fetch_on_router (space_name , metadata_map_name , timeout )
105
136
if cache [metadata_map_name ] ~= nil then
106
137
return cache [metadata_map_name ][space_name ]
@@ -120,17 +151,48 @@ local function fetch_on_router(space_name, metadata_map_name, timeout)
120
151
" Fetching sharding key for space '%s' is failed" , space_name )
121
152
end
122
153
154
+ -- Get sharding index for a certain space.
155
+ --
156
+ -- Return:
157
+ -- - sharding key as index object, when sharding key definition found on
158
+ -- storage.
159
+ -- - nil, when sharding key definition was not found on storage. Pay attention
160
+ -- that nil without error is a successfull return value.
161
+ -- - nil and error, when something goes wrong on fetching attempt.
162
+ --
123
163
function sharding_metadata_module .fetch_sharding_key_on_router (space_name , timeout )
124
164
dev_checks (' string' , ' ?number' )
125
165
126
166
return fetch_on_router (space_name , cache .SHARDING_KEY_MAP_NAME , timeout )
127
167
end
128
168
169
+ -- Get sharding func for a certain space.
170
+ --
171
+ -- Return:
172
+ -- - sharding func as callable object, when sharding func definition found on
173
+ -- storage.
174
+ -- - nil, when sharding func definition was not found on storage. Pay attention
175
+ -- that nil without error is a successfull return value.
176
+ -- - nil and error, when something goes wrong on fetching attempt.
177
+ --
178
+ function sharding_metadata_module .fetch_sharding_func_on_router (space_name , timeout )
179
+ dev_checks (' string' , ' ?number' )
180
+
181
+ return fetch_on_router (space_name , cache .SHARDING_FUNC_MAP_NAME , timeout )
182
+ end
183
+
129
184
function sharding_metadata_module .update_sharding_key_cache (space_name )
130
185
cache .drop_caches ()
186
+
131
187
return sharding_metadata_module .fetch_sharding_key_on_router (space_name )
132
188
end
133
189
190
+ function sharding_metadata_module .update_sharding_func_cache (space_name )
191
+ cache .drop_caches ()
192
+
193
+ return sharding_metadata_module .fetch_sharding_func_on_router (space_name )
194
+ end
195
+
134
196
function sharding_metadata_module .init ()
135
197
_G ._crud .fetch_on_storage = sharding_metadata_module .fetch_on_storage
136
198
end
0 commit comments