@@ -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 })
@@ -57,25 +58,39 @@ local function extract_sharding_func_def(tuple)
57
58
return nil
58
59
end
59
60
60
- -- Return a map with metadata or nil when space box.space._ddl_sharding_key is
61
- -- not available on storage.
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.
62
63
function sharding_metadata_module .fetch_on_storage ()
63
64
local sharding_key_space = box .space ._ddl_sharding_key
64
- 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
65
68
return nil
66
69
end
67
70
68
71
local SPACE_NAME_FIELDNO = 1
69
72
local SPACE_SHARDING_KEY_FIELDNO = 2
70
73
local metadata_map = {}
71
- for _ , tuple in sharding_key_space :pairs () do
72
- local space_name = tuple [SPACE_NAME_FIELDNO ]
73
- local sharding_key_def = tuple [SPACE_SHARDING_KEY_FIELDNO ]
74
- local space_format = box .space [space_name ]:format ()
75
- metadata_map [space_name ] = {
76
- sharding_key_def = sharding_key_def ,
77
- space_format = space_format ,
78
- }
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
79
94
end
80
95
81
96
return metadata_map
@@ -102,24 +117,21 @@ local _fetch_on_router = locked(function(timeout, space_name, metadata_map_name)
102
117
end
103
118
if metadata_map == nil then
104
119
cache [cache .SHARDING_KEY_MAP_NAME ] = {}
120
+ cache [cache .SHARDING_FUNC_MAP_NAME ] = {}
105
121
return
106
122
end
107
123
108
124
local err = sharding_key .construct_as_index_obj_cache (metadata_map , space_name )
109
125
if err ~= nil then
110
126
return err
111
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
112
133
end )
113
134
114
- -- Get sharding index for a certain space.
115
- --
116
- -- Return:
117
- -- - sharding key as index object, when sharding key definition found on
118
- -- storage.
119
- -- - nil, when sharding key definition was not found on storage. Pay attention
120
- -- that nil without error is a successfull return value.
121
- -- - nil and error, when something goes wrong on fetching attempt.
122
- --
123
135
local function fetch_on_router (space_name , metadata_map_name , timeout )
124
136
if cache [metadata_map_name ] ~= nil then
125
137
return cache [metadata_map_name ][space_name ]
@@ -139,17 +151,58 @@ local function fetch_on_router(space_name, metadata_map_name, timeout)
139
151
" Fetching sharding key for space '%s' is failed" , space_name )
140
152
end
141
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
+ --
142
163
function sharding_metadata_module .fetch_sharding_key_on_router (space_name , timeout )
143
164
dev_checks (' string' , ' ?number' )
144
165
145
166
return fetch_on_router (space_name , cache .SHARDING_KEY_MAP_NAME , timeout )
146
167
end
147
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
+
148
184
function sharding_metadata_module .update_sharding_key_cache (space_name )
149
185
cache .drop_caches ()
186
+
187
+ local _ , err = sharding_metadata_module .fetch_sharding_func_on_router (space_name )
188
+ if err ~= nil then
189
+ return nil , err
190
+ end
191
+
150
192
return sharding_metadata_module .fetch_sharding_key_on_router (space_name )
151
193
end
152
194
195
+ function sharding_metadata_module .update_sharding_func_cache (space_name )
196
+ cache .drop_caches ()
197
+
198
+ local _ , err = sharding_metadata_module .fetch_sharding_key_on_router (space_name )
199
+ if err ~= nil then
200
+ return nil , err
201
+ end
202
+
203
+ return sharding_metadata_module .fetch_sharding_func_on_router (space_name )
204
+ end
205
+
153
206
function sharding_metadata_module .init ()
154
207
_G ._crud .fetch_on_storage = sharding_metadata_module .fetch_on_storage
155
208
end
0 commit comments