119
119
local function build_select_iterator (space_name , user_conditions , opts )
120
120
dev_checks (' string' , ' ?table' , {
121
121
after = ' ?table' ,
122
- limit = ' ?number' ,
122
+ first = ' ?number' ,
123
123
timeout = ' ?number' ,
124
124
batch_size = ' ?number' ,
125
125
})
@@ -132,10 +132,6 @@ local function build_select_iterator(space_name, user_conditions, opts)
132
132
133
133
local batch_size = opts .batch_size or DEFAULT_BATCH_SIZE
134
134
135
- if opts .limit ~= nil and opts .limit < 0 then
136
- return nil , SelectError :new (" limit should be >= 0" )
137
- end
138
-
139
135
-- check conditions
140
136
local conditions , err = select_conditions .parse (user_conditions )
141
137
if err ~= nil then
@@ -153,25 +149,26 @@ local function build_select_iterator(space_name, user_conditions, opts)
153
149
end
154
150
local space_format = space :format ()
155
151
152
+ -- set after tuple
153
+ local after_tuple = utils .flatten (opts .after , space_format )
154
+
156
155
-- plan select
157
156
local plan , err = select_plan .new (space , conditions , {
158
- limit = opts .limit ,
157
+ first = opts .first ,
158
+ after_tuple = after_tuple ,
159
159
})
160
160
161
161
if err ~= nil then
162
162
return nil , SelectError :new (" Failed to plan select: %s" , err )
163
163
end
164
164
165
- -- set limit and replicasets to select from
165
+ -- set replicasets to select from
166
166
local replicasets_to_select = replicasets
167
167
168
168
if plan .sharding_key ~= nil then
169
169
replicasets_to_select = get_replicasets_by_sharding_key (plan .sharding_key )
170
170
end
171
171
172
- -- set after tuple
173
- local after_tuple = utils .flatten (opts .after , space_format )
174
-
175
172
-- generate tuples comparator
176
173
local scan_index = space .index [plan .index_id ]
177
174
local primary_index = space .index [0 ]
@@ -191,7 +188,6 @@ local function build_select_iterator(space_name, user_conditions, opts)
191
188
comparator = tuples_comparator ,
192
189
193
190
plan = plan ,
194
- after_tuple = after_tuple ,
195
191
196
192
batch_size = batch_size ,
197
193
replicasets = replicasets_to_select ,
@@ -205,16 +201,20 @@ end
205
201
function select_module .pairs (space_name , user_conditions , opts )
206
202
checks (' string' , ' ?table' , {
207
203
after = ' ?table' ,
208
- limit = ' ?number' ,
204
+ first = ' ?number' ,
209
205
timeout = ' ?number' ,
210
206
batch_size = ' ?number' ,
211
207
})
212
208
213
209
opts = opts or {}
214
210
211
+ if opts .first ~= nil and opts .first < 0 then
212
+ error (string.format (" Negative first isn't allowed for pairs" ))
213
+ end
214
+
215
215
local iter , err = build_select_iterator (space_name , user_conditions , {
216
216
after = opts .after ,
217
- limit = opts .limit ,
217
+ first = opts .first ,
218
218
timeout = opts .timeout ,
219
219
batch_size = opts .batch_size ,
220
220
})
@@ -247,16 +247,22 @@ end
247
247
function select_module .call (space_name , user_conditions , opts )
248
248
checks (' string' , ' ?table' , {
249
249
after = ' ?table' ,
250
- limit = ' ?number' ,
250
+ first = ' ?number' ,
251
251
timeout = ' ?number' ,
252
252
batch_size = ' ?number' ,
253
253
})
254
254
255
255
opts = opts or {}
256
256
257
+ if opts .first ~= nil and opts .first < 0 then
258
+ if opts .after == nil then
259
+ return nil , SelectError :new (" Negative first should be specified only with after option" )
260
+ end
261
+ end
262
+
257
263
local iter , err = build_select_iterator (space_name , user_conditions , {
258
264
after = opts .after ,
259
- limit = opts .limit ,
265
+ first = opts .first ,
260
266
timeout = opts .timeout ,
261
267
batch_size = opts .batch_size ,
262
268
})
@@ -268,16 +274,20 @@ function select_module.call(space_name, user_conditions, opts)
268
274
local tuples = {}
269
275
270
276
while iter :has_next () do
271
- local obj , err = iter :get ()
277
+ local tuple , err = iter :get ()
272
278
if err ~= nil then
273
279
return nil , SelectError :new (" Failed to get next object: %s" , err )
274
280
end
275
281
276
- if obj == nil then
282
+ if tuple == nil then
277
283
break
278
284
end
279
285
280
- table.insert (tuples , obj )
286
+ table.insert (tuples , tuple )
287
+ end
288
+
289
+ if opts .first ~= nil and opts .first < 0 then
290
+ utils .reverse_inplace (tuples )
281
291
end
282
292
283
293
return {
0 commit comments