|
| 1 | +#!/usr/bin/env tarantool |
| 2 | + |
| 3 | +-- How to run: |
| 4 | +-- |
| 5 | +-- $ ./doc/playground.lua |
| 6 | +-- |
| 7 | +-- Or |
| 8 | +-- |
| 9 | +-- $ KEEP_DATA=1 ./doc/playground.lua |
| 10 | +-- |
| 11 | +-- What to do next: |
| 12 | +-- |
| 13 | +-- Choose an example from README.md, doc/select.md or doc/pairs.md |
| 14 | +-- and run. For example: |
| 15 | +-- |
| 16 | +-- tarantool> crud.select('customers', {{'<=', 'age', 35}}, {first = 10}) |
| 17 | +-- tarantool> crud.select('developers', nil, {first = 6}) |
| 18 | + |
| 19 | +local fio = require('fio') |
| 20 | +local console = require('console') |
| 21 | +local vshard = require('vshard') |
| 22 | +local crud = require('crud') |
| 23 | + |
| 24 | +-- Trick to don't leave *.snap, *.xlog files. See |
| 25 | +-- test/tuple_keydef.test.lua in the tuple-keydef module. |
| 26 | +if os.getenv('KEEP_DATA') ~= nil then |
| 27 | + box.cfg() |
| 28 | +else |
| 29 | + local tempdir = fio.tempdir() |
| 30 | + box.cfg({ |
| 31 | + memtx_dir = tempdir, |
| 32 | + wal_mode = 'none', |
| 33 | + }) |
| 34 | + fio.rmtree(tempdir) |
| 35 | +end |
| 36 | + |
| 37 | +-- Setup vshard. |
| 38 | +_G.vshard = vshard |
| 39 | +box.once('guest', function() |
| 40 | + box.schema.user.grant('guest', 'super') |
| 41 | +end) |
| 42 | +local uri = 'guest@localhost:3301' |
| 43 | +local cfg = { |
| 44 | + bucket_count = 3000, |
| 45 | + sharding = { |
| 46 | + [box.info().cluster.uuid] = { |
| 47 | + replicas = { |
| 48 | + [box.info().uuid] = { |
| 49 | + uri = uri, |
| 50 | + name = 'storage', |
| 51 | + master = true, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | +} |
| 57 | +vshard.storage.cfg(cfg, box.info().uuid) |
| 58 | +vshard.router.cfg(cfg) |
| 59 | +vshard.router.bootstrap() |
| 60 | + |
| 61 | +-- Create the 'customers' space. |
| 62 | +box.once('customers', function() |
| 63 | + box.schema.create_space('customers', { |
| 64 | + format = { |
| 65 | + {name = 'id', type = 'unsigned'}, |
| 66 | + {name = 'bucket_id', type = 'unsigned'}, |
| 67 | + {name = 'name', type = 'string'}, |
| 68 | + {name = 'age', type = 'number'}, |
| 69 | + } |
| 70 | + }) |
| 71 | + box.space.customers:create_index('primary_index', { |
| 72 | + parts = { |
| 73 | + {field = 1, type = 'unsigned'}, |
| 74 | + }, |
| 75 | + }) |
| 76 | + box.space.customers:create_index('bucket_id', { |
| 77 | + parts = { |
| 78 | + {field = 2, type = 'unsigned'}, |
| 79 | + }, |
| 80 | + unique = false, |
| 81 | + }) |
| 82 | + box.space.customers:create_index('age', { |
| 83 | + parts = { |
| 84 | + {field = 4, type = 'number'}, |
| 85 | + }, |
| 86 | + unique = false, |
| 87 | + }) |
| 88 | + |
| 89 | + -- Fill the space. |
| 90 | + box.space.customers:insert({1, 477, 'Elizabeth', 12}) |
| 91 | + box.space.customers:insert({2, 401, 'Mary', 46}) |
| 92 | + box.space.customers:insert({3, 2804, 'David', 33}) |
| 93 | + box.space.customers:insert({4, 1161, 'William', 81}) |
| 94 | + box.space.customers:insert({5, 1172, 'Jack', 35}) |
| 95 | + box.space.customers:insert({6, 1064, 'William', 25}) |
| 96 | + box.space.customers:insert({7, 693, 'Elizabeth', 18}) |
| 97 | +end) |
| 98 | + |
| 99 | +-- Create the developers space. |
| 100 | +box.once('developers', function() |
| 101 | + box.schema.create_space('developers', { |
| 102 | + format = { |
| 103 | + {name = 'id', type = 'unsigned'}, |
| 104 | + {name = 'bucket_id', type = 'unsigned'}, |
| 105 | + {name = 'name', type = 'string'}, |
| 106 | + {name = 'surname', type = 'string'}, |
| 107 | + {name = 'age', type = 'number'}, |
| 108 | + } |
| 109 | + }) |
| 110 | + box.space.developers:create_index('primary_index', { |
| 111 | + parts = { |
| 112 | + {field = 1, type = 'unsigned'}, |
| 113 | + }, |
| 114 | + }) |
| 115 | + box.space.developers:create_index('bucket_id', { |
| 116 | + parts = { |
| 117 | + {field = 2, type = 'unsigned'}, |
| 118 | + }, |
| 119 | + unique = false, |
| 120 | + }) |
| 121 | + box.space.developers:create_index('age_index', { |
| 122 | + parts = { |
| 123 | + {field = 5, type = 'number'}, |
| 124 | + }, |
| 125 | + unique = false, |
| 126 | + }) |
| 127 | + box.space.developers:create_index('full_name', { |
| 128 | + parts = { |
| 129 | + {field = 3, type = 'string'}, |
| 130 | + {field = 4, type = 'string'}, |
| 131 | + }, |
| 132 | + unique = false, |
| 133 | + }) |
| 134 | + |
| 135 | + -- Fill the space. |
| 136 | + box.space.developers:insert({1, 477, 'Alexey', 'Adams', 20}) |
| 137 | + box.space.developers:insert({2, 401, 'Sergey', 'Allred', 21}) |
| 138 | + box.space.developers:insert({3, 2804, 'Pavel', 'Adams', 27}) |
| 139 | + box.space.developers:insert({4, 1161, 'Mikhail', 'Liston', 51}) |
| 140 | + box.space.developers:insert({5, 1172, 'Dmitry', 'Jacobi', 16}) |
| 141 | + box.space.developers:insert({6, 1064, 'Alexey', 'Sidorov', 31}) |
| 142 | +end) |
| 143 | + |
| 144 | +-- Initialize crud. |
| 145 | +crud.init_storage() |
| 146 | +crud.init_router() |
| 147 | + |
| 148 | +-- Start a console. |
| 149 | +console.start() |
| 150 | +os.exit() |
0 commit comments