@@ -10,6 +10,7 @@ local select_conditions = require('crud.select.conditions')
1010local select_plan = require (' crud.select.plan' )
1111local select_executor = require (' crud.select.executor' )
1212local select_comparators = require (' crud.select.comparators' )
13+ local select_filters = require (' crud.select.filters' )
1314
1415local Iterator = require (' crud.select.iterator' )
1516
@@ -22,29 +23,42 @@ local select_module = {}
2223
2324local SELECT_FUNC_NAME = ' __select'
2425
25- local function call_select_on_storage (space_name , conditions , opts )
26- checks (' string' , ' ?table' , {
27- limit = ' ?number' ,
26+ local DEFAULT_BATCH_SIZE = 100
27+
28+ local function call_select_on_storage (space_name , index_id , conditions , opts )
29+ checks (' string' , ' number' , ' ?table' , {
30+ scan_value = ' table' ,
2831 after_tuple = ' ?table' ,
32+ iter = ' number' ,
33+ limit = ' number' ,
34+ scan_condition_num = ' ?number' ,
2935 })
3036
3137 local space = box .space [space_name ]
3238 if space == nil then
3339 return nil , SelectError :new (" Space %s doesn't exists" , space_name )
3440 end
3541
36- -- plan select
37- local plan , err = select_plan .new (space , conditions , {
38- limit = opts .limit ,
39- after_tuple = opts .after_tuple ,
40- })
42+ local index = space .index [index_id ]
43+ if index == nil then
44+ return nil , SelectError :new (" Index with ID %s doesn't exists" , index_id )
45+ end
4146
47+ local filter_func , err = select_filters .gen_func (space , conditions , {
48+ iter = opts .iter ,
49+ scan_condition_num = opts .scan_condition_num ,
50+ })
4251 if err ~= nil then
43- return nil , SelectError :new (" Failed to plan select : %s" , err )
52+ return nil , SelectError :new (" Failed to generate tuples filter : %s" , err )
4453 end
4554
4655 -- execute select
47- local tuples , err = select_executor .execute (plan )
56+ local tuples , err = select_executor .execute (space , index , filter_func , {
57+ scan_value = opts .scan_value ,
58+ after_tuple = opts .after_tuple ,
59+ iter = opts .iter ,
60+ limit = opts .limit ,
61+ })
4862 if err ~= nil then
4963 return nil , SelectError :new (" Failed to execute select: %s" , err )
5064 end
@@ -58,21 +72,26 @@ function select_module.init()
5872 })
5973end
6074
61- local function select_iteration (space_name , conditions , opts )
75+ local function select_iteration (space_name , plan , opts )
6276 checks (' string' , ' ?table' , {
6377 after_tuple = ' ?table' ,
6478 replicasets = ' table' ,
6579 timeout = ' ?number' ,
66- batch_size = ' ? number' ,
80+ limit = ' number' ,
6781 })
6882
6983 -- call select on storages
7084 local storage_select_opts = {
85+ scan_value = plan .scan_value ,
7186 after_tuple = opts .after_tuple ,
72- limit = opts .batch_size ,
87+ iter = plan .iter ,
88+ limit = opts .limit ,
89+ scan_condition_num = plan .scan_condition_num ,
7390 }
7491
75- local storage_select_args = {space_name , conditions , storage_select_opts }
92+ local storage_select_args = {
93+ space_name , plan .index_id , plan .conditions , storage_select_opts ,
94+ }
7695
7796 local results , err = call .ro (SELECT_FUNC_NAME , storage_select_args , {
7897 replicasets = opts .replicasets ,
@@ -86,14 +105,8 @@ local function select_iteration(space_name, conditions, opts)
86105 return results
87106end
88107
89- local function get_replicasets_to_select_from (plan , all_replicasets )
90- if not plan .is_scan_by_full_sharding_key_eq then
91- return all_replicasets
92- end
93-
94- plan .scanner .limit = 1
95-
96- local bucket_id = vshard .router .bucket_id_strcrc32 (plan .scanner .value )
108+ local function get_replicasets_by_sharding_key (sharding_key )
109+ local bucket_id = vshard .router .bucket_id_strcrc32 (sharding_key )
97110 local replicaset , err = vshard .router .route (bucket_id )
98111 if replicaset == nil then
99112 return nil , GetReplicasetsError :new (" Failed to get replicaset for bucket_id %s: %s" , bucket_id , err .err )
@@ -118,6 +131,8 @@ local function build_select_iterator(space_name, user_conditions, opts)
118131 return nil , SelectError :new (" batch_size should be > 0" )
119132 end
120133
134+ local batch_size = opts .batch_size or DEFAULT_BATCH_SIZE
135+
121136 if opts .limit ~= nil and opts .limit < 0 then
122137 return nil , SelectError :new (" limit should be >= 0" )
123138 end
@@ -137,32 +152,32 @@ local function build_select_iterator(space_name, user_conditions, opts)
137152 if space == nil then
138153 return nil , SelectError :new (" Space %s doesn't exists" , space_name )
139154 end
140-
141155 local space_format = space :format ()
142156
143- local after_tuple = utils .flatten (opts .after , space_format )
144-
145157 -- plan select
146158 local plan , err = select_plan .new (space , conditions , {
147159 limit = opts .limit ,
148- after_tuple = after_tuple
149160 })
150161
151162 if err ~= nil then
152163 return nil , SelectError :new (" Failed to plan select: %s" , err )
153164 end
154165
155- -- get replicasets to select from
156- local replicasets , err = get_replicasets_to_select_from (plan , replicasets )
157- if err ~= nil then
158- return nil , SelectError :new (" Failed to get replicasets to select from: %s" , err )
166+ -- set limit and replicasets to select from
167+ local replicasets_to_select = replicasets
168+
169+ if plan .sharding_key ~= nil then
170+ replicasets_to_select = get_replicasets_by_sharding_key (plan .sharding_key )
159171 end
160172
161- local scan_index = space . index [ plan . scanner . index_id ]
162- local primary_index = space . index [ 0 ]
173+ -- set after tuple
174+ local after_tuple = utils . flatten ( opts . after , space_format )
163175
176+ -- generate tuples comparator
177+ local scan_index = space .index [plan .index_id ]
178+ local primary_index = space .index [0 ]
164179 local cmp_key_parts = utils .merge_primary_key_parts (scan_index .parts , primary_index .parts )
165- local cmp_operator = select_comparators .get_cmp_operator (plan .scanner . iter )
180+ local cmp_operator = select_comparators .get_cmp_operator (plan .iter )
166181 local tuples_comparator , err = select_comparators .gen_tuples_comparator (
167182 cmp_operator , cmp_key_parts
168183 )
@@ -176,12 +191,11 @@ local function build_select_iterator(space_name, user_conditions, opts)
176191 iteration_func = select_iteration ,
177192 comparator = tuples_comparator ,
178193
179- conditions = conditions ,
194+ plan = plan ,
180195 after_tuple = after_tuple ,
181- limit = plan .scanner .limit ,
182196
183- batch_size = opts . batch_size ,
184- replicasets = replicasets ,
197+ batch_size = batch_size ,
198+ replicasets = replicasets_to_select ,
185199
186200 timeout = opts .timeout ,
187201 })
0 commit comments