Skip to content

Commit 04f8ec1

Browse files
ligurioTotktonada
authored andcommitted
Add support of method bucket_id()
Patch adds a method bucket_id() that calculates a bucket id using sharding function specified in DDL schema or in _ddl_sharding_func space and passed sharding key. Closes #76
1 parent 05ad19a commit 04f8ec1

File tree

7 files changed

+400
-1
lines changed

7 files changed

+400
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1010

1111
- Added integration with service coveralls.io.
1212
- Allow specifying `sharding_func` for space.
13+
- Add a method `bucket_id` that calculate bucket id using sharding function
14+
specified in DDL schema.
1315

1416
## [1.5.0] - 2021-08-09
1517

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ bucket identifier (number)
8484

8585
Return values: table with space's schemas (see "Schema example")
8686

87+
### Get bucket id
88+
`ddl.bucket_id(space_name, sharding_key)`
89+
- Calculate bucket id for a specified space and sharding key.
90+
Method uses sharding function specified in DDL schema.
91+
92+
Return values: bucket_id if no error, otherwise return `nil, err`
93+
8794
## Input data format
8895

8996
```lua

ddl.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,29 @@ local function get_schema()
163163
}
164164
end
165165

166+
local function bucket_id(space_name, sharding_key)
167+
if type(space_name) ~= 'string' then
168+
return nil, string.format(
169+
"Invalid space name (string expected, got %s)", type(space_name)
170+
)
171+
end
172+
if sharding_key == nil then
173+
return nil, string.format(
174+
"Sharding key specified for space (%s) is nil", space_name)
175+
end
176+
177+
local bucket_id, err = ddl_get.internal.bucket_id(
178+
space_name, sharding_key)
179+
if err ~= nil then
180+
return nil, err
181+
end
182+
183+
return bucket_id
184+
end
185+
166186
return {
167187
check_schema = check_schema,
168188
set_schema = set_schema,
169189
get_schema = get_schema,
190+
bucket_id = bucket_id,
170191
}

ddl/check.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,5 +1030,8 @@ return {
10301030
check_index_part = check_index_part,
10311031
check_index_parts = check_index_parts,
10321032
check_index = check_index,
1033-
check_field = check_field
1033+
check_field = check_field,
1034+
internal = {
1035+
is_callable = is_callable,
1036+
}
10341037
}

ddl/get.lua

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
local utils = require('ddl.utils')
2+
local ddl_check = require('ddl.check')
3+
14
local function _get_index_field_path(space, index_part)
25
local space_field = space:format()[index_part.fieldno]
36

@@ -111,6 +114,56 @@ local function get_space_schema(space_name)
111114
return space_ddl
112115
end
113116

117+
local function prepare_sharding_func_for_call(space_name, sharding_func_def)
118+
if type(sharding_func_def) == 'string' then
119+
local sharding_func = utils.get_G_function(sharding_func_def)
120+
if sharding_func ~= nil and
121+
ddl_check.internal.is_callable(sharding_func) == true then
122+
return sharding_func
123+
end
124+
end
125+
126+
if type(sharding_func_def) == 'table' then
127+
local sharding_func, err = loadstring('return ' .. sharding_func_def.body)
128+
if sharding_func == nil then
129+
return nil, string.format(
130+
"Body is incorrect in sharding_func for space (%s): %s", space_name, err)
131+
end
132+
return sharding_func()
133+
end
134+
135+
return nil, string.format(
136+
"Wrong sharding function specified in DDL schema of space (%s)", space_name
137+
)
138+
end
139+
140+
local function bucket_id(space_name, sharding_key)
141+
local sharding_func_def = get_sharding_func(space_name)
142+
if sharding_func_def == nil then
143+
return nil, string.format(
144+
"No sharding function specified in DDL schema of space (%s)", space_name
145+
)
146+
end
147+
local sharding_func, err =
148+
prepare_sharding_func_for_call(space_name, sharding_func_def)
149+
if err ~= nil then
150+
return nil, err
151+
end
152+
153+
local ok, id = pcall(sharding_func, sharding_key)
154+
if not ok then
155+
return nil, string.format(
156+
"Failed to execute sharding function for space name (%s): %s",
157+
space_name, id
158+
)
159+
end
160+
161+
return id
162+
end
163+
114164
return {
115165
get_space_schema = get_space_schema,
166+
internal = {
167+
bucket_id = bucket_id,
168+
}
116169
}

0 commit comments

Comments
 (0)