Search code examples
tarantool

Tarantool Querying Questions


I have the following data structure format:

unix/:/var/run/tarantool/tarantool.sock> s:format()
---
- [{'name': 'id', 'type': 'unsigned'}, {'name': 'version', 'type': 'array'}, {'name': 'data',
    'type': 'array'}]
...

And I have the following data already inside it:

unix/:/var/run/tarantool/tarantool.sock> s:select{}
---
- - [0, [[21, 'action123'], [12, 'actionXYZ'], [11, 'actionABC']], [['actionXYZ',
        'SOME_JAVASCRIPT_CONTENT']]]
  - [1, [[33, 'action123'], [12, 'baseXYZ'], [11, 'baseABC']], [['bas123', 'SOME_CSS_CONTENT']]]
...

I have read through the references and documentation and I'm a bit lost on completing the following:

  1. What's the "WHERE" equivalent? ie. Select to find entries that have a version of 12

    Not seeing applicable examples in https://www.tarantool.io/en/doc/2.2/reference/reference_lua/box_space/#lua-function.space_object.select

  2. List items with the field names (so I know what block I'm looking at). In a way, sort of like having "column headers" in your results in SQL.

    I have named tuples in my format() - how can I see these names when I'm querying data?

    {'name': 'id', 'type': 'unsigned'}, {'name': 'version', 'type': 'array'}, {'name': 'data', 'type': 'array'}]

  3. Pretty print! (preferably yaml)

    I tried using https://www.tarantool.io/en/doc/2.2/reference/reference_lua/yaml/ to wrap around my select statements, but nothing was working.


Solution

    1. You need to use indexes for imperative effective queries, look here:

    https://www.tarantool.io/en/doc/2.2/reference/reference_lua/box_space/#lua-function.space_object.create_index

    https://www.tarantool.io/en/doc/2.2/reference/reference_lua/box_index/

    1. use tuple:tomap():

    https://www.tarantool.io/en/doc/2.2/reference/reference_lua/box_tuple/#lua-function.tuple_object.tomap

    1. It depends on where do you want it pretty. You may have to tune yaml settings, or simply chain tomap calls:
    tarantool> box.space.TEST:pairs():map(function(x) return x:tomap({names_only=true}) end):totable()
    ---
    - - COLUMN1: 1
        COLUMN2: a
      - COLUMN1: 13
        COLUMN2: a
      - COLUMN1: 1000
        COLUMN2: a