119119local function build_select_iterator (space_name , user_conditions , opts )
120120 dev_checks (' string' , ' ?table' , {
121121 after = ' ?table' ,
122- limit = ' ?number' ,
122+ first = ' ?number' ,
123123 timeout = ' ?number' ,
124124 batch_size = ' ?number' ,
125125 })
@@ -132,10 +132,6 @@ local function build_select_iterator(space_name, user_conditions, opts)
132132
133133 local batch_size = opts .batch_size or DEFAULT_BATCH_SIZE
134134
135- if opts .limit ~= nil and opts .limit < 0 then
136- return nil , SelectError :new (" limit should be >= 0" )
137- end
138-
139135 -- check conditions
140136 local conditions , err = select_conditions .parse (user_conditions )
141137 if err ~= nil then
@@ -153,25 +149,26 @@ local function build_select_iterator(space_name, user_conditions, opts)
153149 end
154150 local space_format = space :format ()
155151
152+ -- set after tuple
153+ local after_tuple = utils .flatten (opts .after , space_format )
154+
156155 -- plan select
157156 local plan , err = select_plan .new (space , conditions , {
158- limit = opts .limit ,
157+ first = opts .first ,
158+ after_tuple = after_tuple ,
159159 })
160160
161161 if err ~= nil then
162162 return nil , SelectError :new (" Failed to plan select: %s" , err )
163163 end
164164
165- -- set limit and replicasets to select from
165+ -- set replicasets to select from
166166 local replicasets_to_select = replicasets
167167
168168 if plan .sharding_key ~= nil then
169169 replicasets_to_select = get_replicasets_by_sharding_key (plan .sharding_key )
170170 end
171171
172- -- set after tuple
173- local after_tuple = utils .flatten (opts .after , space_format )
174-
175172 -- generate tuples comparator
176173 local scan_index = space .index [plan .index_id ]
177174 local primary_index = space .index [0 ]
@@ -191,7 +188,6 @@ local function build_select_iterator(space_name, user_conditions, opts)
191188 comparator = tuples_comparator ,
192189
193190 plan = plan ,
194- after_tuple = after_tuple ,
195191
196192 batch_size = batch_size ,
197193 replicasets = replicasets_to_select ,
@@ -205,16 +201,20 @@ end
205201function select_module .pairs (space_name , user_conditions , opts )
206202 checks (' string' , ' ?table' , {
207203 after = ' ?table' ,
208- limit = ' ?number' ,
204+ first = ' ?number' ,
209205 timeout = ' ?number' ,
210206 batch_size = ' ?number' ,
211207 })
212208
213209 opts = opts or {}
214210
211+ if opts .first ~= nil and opts .first < 0 then
212+ error (string.format (" Negative first isn't allowed for pairs" ))
213+ end
214+
215215 local iter , err = build_select_iterator (space_name , user_conditions , {
216216 after = opts .after ,
217- limit = opts .limit ,
217+ first = opts .first ,
218218 timeout = opts .timeout ,
219219 batch_size = opts .batch_size ,
220220 })
@@ -247,16 +247,22 @@ end
247247function select_module .call (space_name , user_conditions , opts )
248248 checks (' string' , ' ?table' , {
249249 after = ' ?table' ,
250- limit = ' ?number' ,
250+ first = ' ?number' ,
251251 timeout = ' ?number' ,
252252 batch_size = ' ?number' ,
253253 })
254254
255255 opts = opts or {}
256256
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+
257263 local iter , err = build_select_iterator (space_name , user_conditions , {
258264 after = opts .after ,
259- limit = opts .limit ,
265+ first = opts .first ,
260266 timeout = opts .timeout ,
261267 batch_size = opts .batch_size ,
262268 })
@@ -268,16 +274,20 @@ function select_module.call(space_name, user_conditions, opts)
268274 local tuples = {}
269275
270276 while iter :has_next () do
271- local obj , err = iter :get ()
277+ local tuple , err = iter :get ()
272278 if err ~= nil then
273279 return nil , SelectError :new (" Failed to get next object: %s" , err )
274280 end
275281
276- if obj == nil then
282+ if tuple == nil then
277283 break
278284 end
279285
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 )
281291 end
282292
283293 return {
0 commit comments