-
Notifications
You must be signed in to change notification settings - Fork 15
Added new examples of select and pairs queries. #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8c48d03
ADD: some examples of select && pairs
mRrvz f0a51c3
a little fix
mRrvz 84c3c48
Fix some PR issues
mRrvz 3393759
Get select examples more informative
mRrvz 55e1ff7
Add some pairs examples
mRrvz 5785fdf
Fix PR issues
mRrvz 99f6aff
Add more pairs examples
mRrvz 811f937
Fix PR issues
mRrvz d22f32f
PR fixes
mRrvz 61e3b98
PR issues fix
mRrvz b692ef0
newline fixes
mRrvz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
# Pairs examples | ||
|
||
With ``crud.pairs``, you can iterate across a distributed space. | ||
The arguments are the same as [``crud.select``](https://github.com/tarantool/crud/docs/select.md), except of the ``use_tomap`` parameter. | ||
Below are examples that may help you. | ||
|
||
## Getting space | ||
|
||
Let's check ``developers`` space contents to make other examples more clear. Just select first 4 values without conditions. | ||
|
||
**Example:** | ||
|
||
```lua | ||
tuples = {} | ||
for _, tuple in crud.pairs('developers', nil, { first = 4 }) do | ||
table.insert(tuples, tuple) | ||
end | ||
|
||
tuples | ||
--- | ||
- - - 1 -- id | ||
- 7331 -- bucket_id | ||
- Alexey -- name | ||
- Adams -- surname | ||
- 20 -- age | ||
- - 2 | ||
- 899 | ||
- Sergey | ||
- Allred | ||
- 21 | ||
- - 3 | ||
- 9661 | ||
- Pavel | ||
- Adams | ||
- 27 | ||
- - 4 | ||
- 501 | ||
- Mikhail | ||
- Liston | ||
- 51 | ||
... | ||
``` | ||
|
||
## ``use_tomap`` parameter | ||
|
||
With ``use_tomap`` flag, you can choose to iterate over objects or over tuples. | ||
If ``use_tomap = true``, you will iterate over objects. This parameter is false by default. | ||
|
||
**Example:** | ||
|
||
```lua | ||
objects = {} | ||
for _, obj in crud.pairs('developers', nil, { use_tomap = true, first = 3 }) do | ||
table.insert(tuples, tuple) | ||
end | ||
|
||
objects | ||
--- | ||
- - id: 1 | ||
bucket_id: 7331 | ||
name: Alexey | ||
surname: Adams | ||
age: 20 | ||
- id: 2 | ||
bucket_id: 899 | ||
name: Sergey | ||
surname: Allred | ||
age: 21 | ||
- id: 3 | ||
bucket_id: 9661 | ||
name: Pavel | ||
surname: Adams | ||
age: 27 | ||
... | ||
``` | ||
|
||
## Pagination | ||
|
||
``crud.pairs``, like [``crud.select``](https://github.com/tarantool/crud/doc/select#pagination), supports pagination. | ||
To use it, combine the ``first`` and ``after`` parameters. | ||
|
||
**Example:** | ||
|
||
```lua | ||
tuples = {} | ||
for _, tuple in crud.pairs('developers', nil, { first = 2 }) do | ||
table.insert(tuples, tuple) -- Got first two tuples | ||
end | ||
|
||
tuples | ||
--- | ||
- - - 1 | ||
- 7331 | ||
- Alexey | ||
- Adams | ||
- 20 | ||
- - 2 | ||
- 899 | ||
- Sergey | ||
- Allred | ||
- 21 | ||
... | ||
new_tuples = {} | ||
for _, tuple in crud.pairs('developers', nil, { after = tuples[2], first = 2 }) do | ||
table.insert(new_tuples, tuple) -- Got next two tuples | ||
end | ||
|
||
new_tuples | ||
--- | ||
- - - 3 | ||
- 9661 | ||
- Pavel | ||
- Adams | ||
- 27 | ||
- - 4 | ||
- 501 | ||
- Mikhail | ||
- Liston | ||
- 51 | ||
... | ||
``` | ||
mRrvz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Note that ``crud.pairs``, unlike ``crud.select``, **doesn't support reverse pagination.** | ||
|
||
## Lua Fun | ||
|
||
[``Pairs``](https://github.com/tarantool/crud#pairs) is [Lua Fun](https://github.com/luafun/luafun) compatible. Some examples of working with basic functional functions below. | ||
|
||
**Filter example:** | ||
|
||
```lua | ||
objects = {} | ||
for _, obj in crud.pairs('developers', {{'>=', 'age', 20}}, { use_tomap = true }):filter(function(x) return x.age % 5 == 0 end) do | ||
table.insert(objects, obj) | ||
end | ||
|
||
objects | ||
--- | ||
- - id: 1 | ||
bucket_id: 7331 | ||
name: Alexey | ||
surname: Adams | ||
age: 20 | ||
... | ||
``` | ||
|
||
**Reduce (foldl) example:** | ||
|
||
```lua | ||
age_sum = crud.pairs('developers', nil, { use_tomap = true }):reduce(function(acc, x) return acc + x.age end, 0) | ||
age_sum | ||
--- | ||
- 166 | ||
.... | ||
``` | ||
|
||
**Map example:** | ||
|
||
```lua | ||
objects = {} | ||
for _, obj in crud.pairs('developers', nil, { use_tomap = true }):map(function(x) return {id = obj.id, name = obj.name, age = obj.age * 2}) do | ||
table.insert(objects, obj) | ||
end | ||
|
||
objects | ||
--- | ||
- - id: 1 | ||
name: Alexey | ||
age: 40 | ||
- id: 2 | ||
name: Sergey | ||
age: 42 | ||
- id: 3 | ||
name: Pavel | ||
age: 54 | ||
- id: 4 | ||
name: Mikhail | ||
age: 102 | ||
- id: 5 | ||
name: Dmitry | ||
age: 32 | ||
- id: 6 | ||
name: Alexey | ||
age: 62 | ||
... | ||
``` | ||
|
||
**Take example**: | ||
|
||
```lua | ||
tuples = {} | ||
for _, tuple in crud.pairs('developers', {{'>=', 'age', 25}}):take(2) do | ||
table.insert(tuples, tuple) | ||
end | ||
|
||
tuples | ||
--- | ||
- - - 3 | ||
- 9661 | ||
- Pavel | ||
- Adams | ||
- 27 | ||
- - 4 | ||
- 501 | ||
- Mikhail | ||
- Liston | ||
- 51 | ||
... | ||
``` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.