@@ -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,39 @@ 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
+ -- Return a map with metadata or nil when spaces box.space._ddl_sharding_key and
43
+ -- box.space._ddl_sharding_func are not available on storage.
43
44
function sharding_metadata_module .fetch_on_storage ()
44
45
local sharding_key_space = box .space ._ddl_sharding_key
45
- if sharding_key_space == nil then
46
+ local sharding_func_space = box .space ._ddl_sharding_func
47
+
48
+ if sharding_key_space == nil and sharding_func_space == nil then
46
49
return nil
47
50
end
48
51
49
52
local SPACE_NAME_FIELDNO = 1
50
53
local SPACE_SHARDING_KEY_FIELDNO = 2
51
54
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
- }
55
+
56
+ if sharding_key_space ~= nil then
57
+ for _ , tuple in sharding_key_space :pairs () do
58
+ local space_name = tuple [SPACE_NAME_FIELDNO ]
59
+ local sharding_key_def = tuple [SPACE_SHARDING_KEY_FIELDNO ]
60
+ local space_format = box .space [space_name ]:format ()
61
+ metadata_map [space_name ] = {
62
+ sharding_key_def = sharding_key_def ,
63
+ space_format = space_format ,
64
+ }
65
+ end
66
+ end
67
+
68
+ if sharding_func_space ~= nil then
69
+ for _ , tuple in sharding_func_space :pairs () do
70
+ local space_name = tuple [SPACE_NAME_FIELDNO ]
71
+ local sharding_func_def = sharding_func .extract_function_def (tuple )
72
+ metadata_map [space_name ] = metadata_map [space_name ] or {}
73
+ metadata_map [space_name ].sharding_func_def = sharding_func_def
74
+ end
60
75
end
61
76
62
77
return metadata_map
@@ -83,24 +98,21 @@ local _fetch_on_router = locked(function(timeout, space_name, metadata_map_name)
83
98
end
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
- -- 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
116
local function fetch_on_router (space_name , metadata_map_name , timeout )
105
117
if cache [metadata_map_name ] ~= nil then
106
118
return cache [metadata_map_name ][space_name ]
@@ -120,17 +132,46 @@ local function fetch_on_router(space_name, metadata_map_name, timeout)
120
132
" Fetching sharding key for space '%s' is failed" , space_name )
121
133
end
122
134
135
+ -- Get sharding index for a certain space.
136
+ --
137
+ -- Return:
138
+ -- - sharding key as index object, when sharding key definition found on
139
+ -- storage.
140
+ -- - nil, when sharding key definition was not found on storage. Pay attention
141
+ -- that nil without error is a successfull return value.
142
+ -- - nil and error, when something goes wrong on fetching attempt.
143
+ --
123
144
function sharding_metadata_module .fetch_sharding_key_on_router (space_name , timeout )
124
145
dev_checks (' string' , ' ?number' )
125
146
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