@@ -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 })
@@ -16,21 +17,35 @@ local sharding_metadata_module = {}
16
17
-- Return a map or nil when metadata is not available.
17
18
function sharding_metadata_module .fetch_on_storage ()
18
19
local sharding_key_space = box .space ._ddl_sharding_key
19
- if sharding_key_space == nil then
20
+ local sharding_func_space = box .space ._ddl_sharding_func
21
+
22
+ if sharding_key_space == nil and sharding_func_space == nil then
20
23
return nil
21
24
end
22
25
23
26
local SPACE_NAME_FIELDNO = 1
24
27
local SPACE_SHARDING_KEY_FIELDNO = 2
25
28
local metadata_map = {}
26
- for _ , tuple in sharding_key_space :pairs () do
27
- local space_name = tuple [SPACE_NAME_FIELDNO ]
28
- local sharding_key_def = tuple [SPACE_SHARDING_KEY_FIELDNO ]
29
- local space_format = box .space [space_name ]:format ()
30
- metadata_map [space_name ] = {
31
- sharding_key_def = sharding_key_def ,
32
- space_format = space_format ,
33
- }
29
+
30
+ if sharding_key_space ~= nil then
31
+ for _ , tuple in sharding_key_space :pairs () do
32
+ local space_name = tuple [SPACE_NAME_FIELDNO ]
33
+ local sharding_key_def = tuple [SPACE_SHARDING_KEY_FIELDNO ]
34
+ local space_format = box .space [space_name ]:format ()
35
+ metadata_map [space_name ] = {
36
+ sharding_key_def = sharding_key_def ,
37
+ space_format = space_format ,
38
+ }
39
+ end
40
+ end
41
+
42
+ if sharding_func_space ~= nil then
43
+ for _ , tuple in sharding_func_space :pairs () do
44
+ local space_name = tuple [SPACE_NAME_FIELDNO ]
45
+ local sharding_func_def = sharding_func .extract_function_def (tuple )
46
+ metadata_map [space_name ] = metadata_map [space_name ] or {}
47
+ metadata_map [space_name ].sharding_func_def = sharding_func_def
48
+ end
34
49
end
35
50
36
51
return metadata_map
@@ -83,13 +98,19 @@ local _fetch_on_router = locked(function(timeout, space_name, metadata_map_name)
83
98
84
99
if metadata_map == nil then
85
100
cache [cache .SHARDING_KEY_MAP_NAME ] = {}
101
+ cache [cache .SHARDING_FUNC_MAP_NAME ] = {}
86
102
return
87
103
end
88
104
89
105
local err = sharding_key .construct_as_index_obj_cache (metadata_map , space_name )
90
106
if err ~= nil then
91
107
return err
92
108
end
109
+
110
+ local err = sharding_func .construct_as_callable_obj_cache (metadata_map , space_name )
111
+ if err ~= nil then
112
+ return err
113
+ end
93
114
end )
94
115
95
116
local function fetch_on_router (space_name , metadata_map_name , timeout )
@@ -126,11 +147,31 @@ function sharding_metadata_module.fetch_sharding_key_on_router(space_name, timeo
126
147
return fetch_on_router (space_name , cache .SHARDING_KEY_MAP_NAME , timeout )
127
148
end
128
149
150
+ -- Get sharding func for a certain space.
151
+ --
152
+ -- Return:
153
+ -- - sharding func as callable object, when sharding func definition found on
154
+ -- storage.
155
+ -- - nil, when sharding func definition was not found on storage. Pay attention
156
+ -- that nil without error is a successfull return value.
157
+ -- - nil and error, when something goes wrong on fetching attempt.
158
+ --
159
+ function sharding_metadata_module .fetch_sharding_func_on_router (space_name , timeout )
160
+ dev_checks (' string' , ' ?number' )
161
+
162
+ return fetch_on_router (space_name , cache .SHARDING_FUNC_MAP_NAME , timeout )
163
+ end
164
+
129
165
function sharding_metadata_module .update_sharding_key_cache (space_name )
130
166
cache .drop_caches ()
131
167
return sharding_metadata_module .fetch_sharding_key_on_router (space_name )
132
168
end
133
169
170
+ function sharding_metadata_module .update_sharding_func_cache (space_name )
171
+ cache .drop_caches ()
172
+ return sharding_metadata_module .fetch_sharding_func_on_router (space_name )
173
+ end
174
+
134
175
function sharding_metadata_module .init ()
135
176
_G ._crud .fetch_on_storage = sharding_metadata_module .fetch_on_storage
136
177
end
0 commit comments