Skip to content

Commit ab0af8a

Browse files
committed
introduce "use_tomap" option for pairs
This patch introduces "use_tomap" (`false` by default) option to convert raw selected tuples to objects. Usage: ```lua -- returns raw tuples crud.pairs('customers', {{'=', 'id', 1}}, {use_tomap = false}) -- returns objects crud.pairs('customers', {{'=', 'id', 1}}, {use_tomap = true}) ``` Follow-up #6
1 parent fe2d02f commit ab0af8a

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2323
* Insert/replace/upsert methods now accept tuples.
2424
To process unflattened objects *_object methods are introduced.
2525

26+
* `pairs` accepts `use_tomap` flag to return tuples or objects
27+
2628
### Changed
2729

2830
* `checks` is disabled for internal functions by default

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,17 @@ crud.select('customers', {{'<=', 'age', 35}})
304304
You can iterate across a distributed space using the `crud.pairs` function.
305305
Its arguments are the same as [`crud.select`](#select) arguments,
306306
but negative `first` values aren't allowed.
307+
User could pass use_tomap flag (false by default) to iterate over flat tuples or objects.
307308

308309
**Example:**
309310

310311
```lua
311-
for _, obj in crud.pairs('customers', {{'<=', 'age', 35}}) do
312-
-- do smth with the object
312+
for _, tuple in crud.pairs('customers', {{'<=', 'age', 35}}, {use_tomap = false}) do
313+
print(json.encode(tuple)) -- {5, 1172, 'Jack', 35}
314+
end
315+
316+
for _, object in crud.pairs('customers', {{'<=', 'age', 35}}, {use_tomap = true}) do
317+
print(json.encode(object)) -- {id = 5, name = 'Jack', bucket_id = 1172, age = 35}
313318
end
314319
```
315320

crud/select.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ function select_module.pairs(space_name, user_conditions, opts)
204204
first = '?number',
205205
timeout = '?number',
206206
batch_size = '?number',
207+
use_tomap = '?boolean',
207208
})
208209

209210
opts = opts or {}
@@ -233,12 +234,15 @@ function select_module.pairs(space_name, user_conditions, opts)
233234
error(string.format("Failed to get next object: %s", err))
234235
end
235236

236-
local obj, err = utils.unflatten(tuple, iter.space_format)
237-
if err ~= nil then
238-
error(string.format("Failed to unflatten next object: %s", err))
237+
local result = tuple
238+
if opts.use_tomap == true then
239+
result, err = utils.unflatten(tuple, iter.space_format)
240+
if err ~= nil then
241+
error(string.format("Failed to unflatten next object: %s", err))
242+
end
239243
end
240244

241-
return iter, obj
245+
return iter, result
242246
end
243247

244248
return gen, nil, iter

test/integration/pairs_test.lua

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,15 @@ add('test_pairs_no_conditions', function(g)
127127

128128
table.sort(customers, function(obj1, obj2) return obj1.id < obj2.id end)
129129

130-
-- no after
131-
local objects, err = g.cluster.main_server.net_box:eval([[
130+
local raw_rows = {
131+
{1, 477, 'Elizabeth', 'Jackson', 12, 'New York'},
132+
{2, 401, 'Mary', 'Brown', 46, 'Los Angeles'},
133+
{3, 2804, 'David', 'Smith', 33, 'Los Angeles'},
134+
{4, 1161, 'William', 'White', 81, 'Chicago'},
135+
}
136+
137+
-- without conditions and options
138+
local objects = g.cluster.main_server.net_box:eval([[
132139
local crud = require('crud')
133140
134141
local objects = {}
@@ -138,8 +145,33 @@ add('test_pairs_no_conditions', function(g)
138145
139146
return objects
140147
]])
148+
t.assert_equals(objects, raw_rows)
149+
150+
-- with use_tomap=false (the raw tuples returned)
151+
local objects = g.cluster.main_server.net_box:eval([[
152+
local crud = require('crud')
153+
154+
local objects = {}
155+
for _, object in crud.pairs('customers', nil, {use_tomap = false}) do
156+
table.insert(objects, object)
157+
end
158+
159+
return objects
160+
]])
161+
t.assert_equals(objects, raw_rows)
162+
163+
-- no after
164+
local objects = g.cluster.main_server.net_box:eval([[
165+
local crud = require('crud')
166+
167+
local objects = {}
168+
for _, object in crud.pairs('customers', nil, {use_tomap = true}) do
169+
table.insert(objects, object)
170+
end
171+
172+
return objects
173+
]])
141174

142-
t.assert_equals(err, nil)
143175
t.assert_equals(objects, customers)
144176

145177
-- after obj 2
@@ -150,7 +182,7 @@ add('test_pairs_no_conditions', function(g)
150182
local after = ...
151183
152184
local objects = {}
153-
for _, object in crud.pairs('customers', nil, {after = after}) do
185+
for _, object in crud.pairs('customers', nil, {after = after, use_tomap = true}) do
154186
table.insert(objects, object)
155187
end
156188
@@ -168,7 +200,7 @@ add('test_pairs_no_conditions', function(g)
168200
local after = ...
169201
170202
local objects = {}
171-
for _, object in crud.pairs('customers', nil, {after = after}) do
203+
for _, object in crud.pairs('customers', nil, {after = after, use_tomap = true}) do
172204
table.insert(objects, object)
173205
end
174206
@@ -209,7 +241,7 @@ add('test_ge_condition_with_index', function(g)
209241
local conditions = ...
210242
211243
local objects = {}
212-
for _, object in crud.pairs('customers', conditions) do
244+
for _, object in crud.pairs('customers', conditions, {use_tomap = true}) do
213245
table.insert(objects, object)
214246
end
215247
@@ -227,7 +259,7 @@ add('test_ge_condition_with_index', function(g)
227259
local conditions, after = ...
228260
229261
local objects = {}
230-
for _, object in crud.pairs('customers', conditions, {after = after}) do
262+
for _, object in crud.pairs('customers', conditions, {after = after, use_tomap = true}) do
231263
table.insert(objects, object)
232264
end
233265
@@ -268,7 +300,7 @@ add('test_le_condition_with_index', function(g)
268300
local conditions = ...
269301
270302
local objects = {}
271-
for _, object in crud.pairs('customers', conditions) do
303+
for _, object in crud.pairs('customers', conditions, {use_tomap = true}) do
272304
table.insert(objects, object)
273305
end
274306
@@ -286,7 +318,7 @@ add('test_le_condition_with_index', function(g)
286318
local conditions, after = ...
287319
288320
local objects = {}
289-
for _, object in crud.pairs('customers', conditions, {after = after}) do
321+
for _, object in crud.pairs('customers', conditions, {after = after, use_tomap = true}) do
290322
table.insert(objects, object)
291323
end
292324

0 commit comments

Comments
 (0)