Skip to content

Commit 16ccbe2

Browse files
Update the net.box tutorial (#4054)
1 parent eb2e255 commit 16ccbe2

File tree

13 files changed

+532
-323
lines changed

13 files changed

+532
-323
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Creating your first Tarantool database
2+
3+
A sample application created in the [Creating your first Tarantool database](https://www.tarantool.io/en/doc/latest/how-to/getting_started_db/) tutorial.
4+
5+
## Running
6+
7+
To start an instance, execute the following command in the [config](../../../config) directory:
8+
9+
```console
10+
$ tt start create_db
11+
```

doc/code_snippets/snippets/config/instances.enabled/create_db/config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ groups:
66
instance001:
77
iproto:
88
listen:
9-
- uri: '127.0.0.1:3301'
9+
- uri: '127.0.0.1:3301'

doc/code_snippets/snippets/config/instances.enabled/create_db/myapp.lua

-29
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Connectors
2+
3+
A sample application used to demonstrate how to connect to a database using connectors for different languages and execute requests for manipulating the data.
4+
5+
## Running
6+
7+
Start the application by executing the following command in the [connectors](../../../connectors) directory:
8+
9+
```console
10+
$ tt start sample_db
11+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
credentials:
2+
users:
3+
sampleuser:
4+
password: '123456'
5+
privileges:
6+
- permissions: [ read, write ]
7+
spaces: [ bands ]
8+
- permissions: [ execute ]
9+
functions: [ get_bands_older_than ]
10+
11+
groups:
12+
group001:
13+
replicasets:
14+
replicaset001:
15+
instances:
16+
instance001:
17+
iproto:
18+
listen:
19+
- uri: '127.0.0.1:3301'
20+
21+
app:
22+
file: 'myapp.lua'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
instance001:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- Create a space --
2+
box.schema.space.create('bands')
3+
4+
-- Specify field names and types --
5+
box.space.bands:format({
6+
{ name = 'id', type = 'unsigned' },
7+
{ name = 'band_name', type = 'string' },
8+
{ name = 'year', type = 'unsigned' }
9+
})
10+
11+
-- Create indexes --
12+
box.space.bands:create_index('primary', { parts = { 'id' } })
13+
box.space.bands:create_index('band', { parts = { 'band_name' } })
14+
box.space.bands:create_index('year_band', { parts = { { 'year' }, { 'band_name' } } })
15+
16+
-- Create a stored function --
17+
box.schema.func.create('get_bands_older_than', {
18+
body = [[
19+
function(year)
20+
return box.space.bands.index.year_band:select({ year }, { iterator = 'LT', limit = 10 })
21+
end
22+
]]
23+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# net.box
2+
3+
A sample application containing `net.box` requests from the [Getting started with net.box](https://www.tarantool.io/en/doc/latest/how-to/getting_started_net_box/) tutorial.
4+
5+
6+
## Running
7+
8+
Before running this sample, start an application that allows remote connections to a sample database: [sample_db](../instances.enabled/sample_db).
9+
10+
Then, start the interactive session by executing the following command in the [connectors](..) directory:
11+
12+
```console
13+
$ tt run -i net_box/myapp.lua
14+
```
15+
16+
In the console, you can use the `conn` object to execute requests for manipulating the data.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
net_box = require('net.box')
2+
--[[
3+
---
4+
...
5+
]]
6+
7+
conn = net_box.connect('sampleuser:[email protected]:3301')
8+
--[[
9+
---
10+
...
11+
]]
12+
13+
conn:ping()
14+
--[[
15+
---
16+
- true
17+
...
18+
]]
19+
20+
function net_box_data_operations()
21+
-- Start net.box session
22+
conn.space.bands:insert({ 1, 'Roxette', 1986 })
23+
--[[
24+
---
25+
- - [1, 'Roxette', 1986]
26+
...
27+
]]
28+
conn.space.bands:insert({ 2, 'Scorpions', 1965 })
29+
--[[
30+
---
31+
- [2, 'Scorpions', 1965]
32+
...
33+
]]
34+
conn.space.bands:insert({ 3, 'Ace of Base', 1987 })
35+
--[[
36+
---
37+
- [3, 'Ace of Base', 1987]
38+
...
39+
]]
40+
conn.space.bands:insert({ 4, 'The Beatles', 1960 })
41+
--[[
42+
---
43+
- [4, 'The Beatles', 1960]
44+
...
45+
]]
46+
47+
conn.space.bands:select({ 1 })
48+
--[[
49+
---
50+
- - [1, 'Roxette', 1986]
51+
...
52+
]]
53+
54+
conn.space.bands.index.band:select({ 'The Beatles' })
55+
--[[
56+
---
57+
- - [4, 'The Beatles', 1960]
58+
...
59+
]]
60+
61+
conn.space.bands:update({ 2 }, { { '=', 'band_name', 'Pink Floyd' } })
62+
--[[
63+
---
64+
- [2, 'Pink Floyd', 1965]
65+
...
66+
]]
67+
68+
conn.space.bands:upsert({ 5, 'The Rolling Stones', 1962 }, { { '=', 'band_name', 'The Doors' } })
69+
--[[
70+
---
71+
...
72+
]]
73+
74+
conn.space.bands:replace({ 1, 'Queen', 1970 })
75+
--[[
76+
---
77+
- [1, 'Queen', 1970]
78+
...
79+
]]
80+
81+
conn.space.bands:delete({ 5 })
82+
--[[
83+
---
84+
- [5, 'The Rolling Stones', 1962]
85+
...
86+
]]
87+
88+
conn:call('get_bands_older_than', { 1966 })
89+
-- ---
90+
-- - [[2, 'Pink Floyd', 1965], [4, 'The Beatles', 1960]]
91+
-- ...
92+
-- End net.box session
93+
end
94+
95+
function net_box_close_connection()
96+
conn:close()
97+
--[[
98+
---
99+
...
100+
]]
101+
-- Close net.box connection
102+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
modules:
2+
# Directory where the external modules are stored.
3+
directory: modules
4+
5+
env:
6+
# Restart instance on failure.
7+
restart_on_failure: false
8+
9+
# Directory that stores binary files.
10+
bin_dir: bin
11+
12+
# Directory that stores Tarantool header files.
13+
inc_dir: include
14+
15+
# Path to directory that stores all applications.
16+
# The directory can also contain symbolic links to applications.
17+
instances_enabled: instances.enabled
18+
19+
# Tarantoolctl artifacts layout compatibility: if set to true tt will not create application
20+
# sub-directories for control socket, pid files, log files, etc.. Data files (wal, vinyl,
21+
# snap) and multi-instance applications are not affected by this option.
22+
tarantoolctl_layout: false
23+
24+
app:
25+
# Directory that stores various instance runtime
26+
# artifacts like console socket, PID file, etc.
27+
run_dir: var/run
28+
29+
# Directory that stores log files.
30+
log_dir: var/log
31+
32+
# Directory where write-ahead log (.xlog) files are stored.
33+
wal_dir: var/lib
34+
35+
# Directory where memtx stores snapshot (.snap) files.
36+
memtx_dir: var/lib
37+
38+
# Directory where vinyl files or subdirectories will be stored.
39+
vinyl_dir: var/lib
40+
41+
# Path to file with credentials for downloading Tarantool Enterprise Edition.
42+
# credential_path: /path/to/file
43+
ee:
44+
credential_path:
45+
46+
templates:
47+
# The path to templates search directory.
48+
- path: templates
49+
50+
repo:
51+
# Directory where local rocks files could be found.
52+
rocks:
53+
# Directory that stores installation files.
54+
distfiles: distfiles

0 commit comments

Comments
 (0)