Skip to content

Commit 8046b6e

Browse files
committed
Fix gh-1457 Go get started: fix bugs + add cookbook recipe for select-all (#1540)
(cherry picked from commit d9a1e72)
1 parent 7305bef commit 8046b6e

File tree

7 files changed

+500
-249
lines changed

7 files changed

+500
-249
lines changed

doc/book/app_server/cookbook.rst

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ socket_udp_echo.lua
642642

643643
Tarantool does not currently have a `udp_server` function,
644644
therefore socket_udp_echo.lua is more complicated than
645-
socket_tcp_echo.lua.
645+
socket_tcp_echo.lua.
646646
It can be implemented with sockets and fibers.
647647

648648
.. code-block:: lua
@@ -824,7 +824,18 @@ An "HTML" file for this server, including Lua, could look like this
824824
</body>
825825
</html>
826826
827+
.. _cookbook-select-all-go:
827828
829+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
830+
select_all.go
831+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
832+
833+
In Go, there is no one-liner to select all tuples from a Tarantool space.
834+
Yet you can use a script like this one. Call it on the instance you want to
835+
connect to.
836+
837+
.. literalinclude:: cookbook/main.go
838+
:language: go
828839
829840
.. _rock: http://rocks.tarantool.org/
830841
.. _http: https://github.com/tarantool/http/

doc/book/app_server/cookbook/main.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/tarantool/go-tarantool"
8+
)
9+
10+
/*
11+
box.cfg{listen = 3301}
12+
box.schema.user.passwd('pass')
13+
14+
s = box.schema.space.create('tester')
15+
s:format({
16+
{name = 'id', type = 'unsigned'},
17+
{name = 'band_name', type = 'string'},
18+
{name = 'year', type = 'unsigned'}
19+
})
20+
s:create_index('primary', { type = 'hash', parts = {'id'} })
21+
s:create_index('scanner', { type = 'tree', parts = {'id', 'band_name'} })
22+
23+
s:insert{1, 'Roxette', 1986}
24+
s:insert{2, 'Scorpions', 2015}
25+
s:insert{3, 'Ace of Base', 1993}
26+
*/
27+
28+
func main() {
29+
conn, err := tarantool.Connect("127.0.0.1:3301", tarantool.Opts{
30+
User: "admin",
31+
Pass: "pass",
32+
})
33+
34+
if err != nil {
35+
log.Fatalf("Connection refused")
36+
}
37+
defer conn.Close()
38+
39+
spaceName := "tester"
40+
indexName := "scanner"
41+
idFn := conn.Schema.Spaces[spaceName].Fields["id"].Id
42+
bandNameFn := conn.Schema.Spaces[spaceName].Fields["band_name"].Id
43+
44+
var tuplesPerRequest uint32 = 2
45+
cursor := []interface{}{}
46+
47+
for {
48+
resp, err := conn.Select(spaceName, indexName, 0, tuplesPerRequest, tarantool.IterGt, cursor)
49+
if err != nil {
50+
log.Fatalf("Failed to select: %s", err)
51+
}
52+
53+
if resp.Code != tarantool.OkCode {
54+
log.Fatalf("Select failed: %s", resp.Error)
55+
}
56+
57+
if len(resp.Data) == 0 {
58+
break
59+
}
60+
61+
fmt.Println("Iteration")
62+
63+
tuples := resp.Tuples()
64+
for _, tuple := range tuples {
65+
fmt.Printf("\t%v\n", tuple)
66+
}
67+
68+
lastTuple := tuples[len(tuples)-1]
69+
cursor = []interface{}{lastTuple[idFn], lastTuple[bandNameFn]}
70+
}
71+
}

doc/getting_started/getting_started_go.rst

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,25 +71,21 @@ To get connected to the Tarantool server, write a simple Go program:
7171
)
7272
7373
func main() {
74-
conn, err := tarantool.Connect("127.0.0.1:3301", tarantool.Opts{})
74+
75+
conn, err := tarantool.Connect("127.0.0.1:3301", tarantool.Opts{
76+
User: "admin",
77+
Pass: "pass",
78+
})
7579
7680
if err != nil {
77-
fmt.Println("Connection refused")
81+
log.Fatalf("Connection refused")
7882
}
7983
8084
defer conn.Close()
8185
8286
// Your logic for interacting with the database
8387
}
8488
85-
You can also specify the user name and password, if needed:
86-
87-
.. code-block:: go
88-
89-
opts := tarantool.Opts{User: "username", Pass: "password"}
90-
conn, err := tarantool.Connect("127.0.0.1:3301", opts)
91-
...
92-
9389
The default user is ``guest``.
9490

9591
.. _getting_started-go-manipulate:
@@ -144,11 +140,9 @@ Next, select tuples by a secondary key.
144140
145141
resp, err = conn.Select("tester", "secondary", 0, 1, tarantool.IterEq, []interface{}{"ABBA"})
146142
147-
Finally, select all the tuples in a space:
148-
149-
.. code-block:: go
150-
151-
resp, err = conn.Select("tester", "primary", 0, tarantool.KeyLimit, tarantool.IterAll, []interface{}{})
143+
Finally, it would be nice to select all the tuples in a space. But there is no
144+
one-liner for this in Go; you would need a script like
145+
:ref:`this one <cookbook-select-all-go>`.
152146

153147
For more examples, see https://github.com/tarantool/go-tarantool#usage
154148

locale/book/app_server/cookbook.pot

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,3 +684,83 @@ msgid "<html>\n"
684684
"</html>"
685685
msgstr ""
686686

687+
msgid "select_all.go"
688+
msgstr ""
689+
690+
msgid "In Go, there is no one-liner to select all tuples from a Tarantool space. Yet you can use a script like this one. Call it on the instance you want to connect to."
691+
msgstr ""
692+
693+
msgid "package main\n"
694+
"\n"
695+
"import (\n"
696+
" \"fmt\"\n"
697+
" \"log\"\n"
698+
"\n"
699+
" \"github.com/tarantool/go-tarantool\"\n"
700+
")\n"
701+
"\n"
702+
"/*\n"
703+
"box.cfg{listen = 3301}\n"
704+
"box.schema.user.passwd('pass')\n"
705+
"\n"
706+
"s = box.schema.space.create('tester')\n"
707+
"s:format({\n"
708+
" {name = 'id', type = 'unsigned'},\n"
709+
" {name = 'band_name', type = 'string'},\n"
710+
" {name = 'year', type = 'unsigned'}\n"
711+
"})\n"
712+
"s:create_index('primary', { type = 'hash', parts = {'id'} })\n"
713+
"s:create_index('scanner', { type = 'tree', parts = {'id', 'band_name'} })\n"
714+
"\n"
715+
"s:insert{1, 'Roxette', 1986}\n"
716+
"s:insert{2, 'Scorpions', 2015}\n"
717+
"s:insert{3, 'Ace of Base', 1993}\n"
718+
"*/\n"
719+
"\n"
720+
"func main() {\n"
721+
" conn, err := tarantool.Connect(\"127.0.0.1:3301\", tarantool.Opts{\n"
722+
" User: \"admin\",\n"
723+
" Pass: \"pass\",\n"
724+
" })\n"
725+
"\n"
726+
" if err != nil {\n"
727+
" log.Fatalf(\"Connection refused\")\n"
728+
" }\n"
729+
" defer conn.Close()\n"
730+
"\n"
731+
" spaceName := \"tester\"\n"
732+
" indexName := \"scanner\"\n"
733+
" idFn := conn.Schema.Spaces[spaceName].Fields[\"id\"].Id\n"
734+
" bandNameFn := conn.Schema.Spaces[spaceName].Fields[\"band_name\"].Id\n"
735+
"\n"
736+
" var tuplesPerRequest uint32 = 2\n"
737+
" cursor := []interface{}{}\n"
738+
"\n"
739+
" for {\n"
740+
" resp, err := conn.Select(spaceName, indexName, 0, tuplesPerRequest, tarantool.IterGt, cursor)\n"
741+
" if err != nil {\n"
742+
" log.Fatalf(\"Failed to select: %s\", err)\n"
743+
" }\n"
744+
"\n"
745+
" if resp.Code != tarantool.OkCode {\n"
746+
" log.Fatalf(\"Select failed: %s\", resp.Error)\n"
747+
" }\n"
748+
"\n"
749+
" if len(resp.Data) == 0 {\n"
750+
" break\n"
751+
" }\n"
752+
"\n"
753+
" fmt.Println(\"Iteration\")\n"
754+
"\n"
755+
" tuples := resp.Tuples()\n"
756+
" for _, tuple := range tuples {\n"
757+
" fmt.Printf(\"\\t%v\\n\", tuple)\n"
758+
" }\n"
759+
"\n"
760+
" lastTuple := tuples[len(tuples)-1]\n"
761+
" cursor = []interface{}{lastTuple[idFn], lastTuple[bandNameFn]}\n"
762+
" }\n"
763+
"}\n"
764+
""
765+
msgstr ""
766+

locale/getting_started/getting_started_connectors.pot

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,14 @@ msgid "package main\n"
321321
")\n"
322322
"\n"
323323
"func main() {\n"
324-
" conn, err := tarantool.Connect(\"127.0.0.1:3301\", tarantool.Opts{})\n"
324+
"\n"
325+
" conn, err := tarantool.Connect(\"127.0.0.1:3301\", tarantool.Opts{\n"
326+
" User: \"admin\",\n"
327+
" Pass: \"pass\",\n"
328+
" })\n"
325329
"\n"
326330
" if err != nil {\n"
327-
" fmt.Println(\"Connection refused\")\n"
331+
" log.Fatalf(\"Connection refused\")\n"
328332
" }\n"
329333
"\n"
330334
" defer conn.Close()\n"
@@ -333,11 +337,6 @@ msgid "package main\n"
333337
"}"
334338
msgstr ""
335339

336-
msgid "opts := tarantool.Opts{User: \"username\", Pass: \"password\"}\n"
337-
"conn, err := tarantool.Connect(\"127.0.0.1:3301\", opts)\n"
338-
"..."
339-
msgstr ""
340-
341340
msgid "To insert a tuple into a space, use ``Insert``:"
342341
msgstr ""
343342

@@ -369,10 +368,7 @@ msgstr ""
369368
msgid "resp, err = conn.Select(\"tester\", \"secondary\", 0, 1, tarantool.IterEq, []interface{}{\"ABBA\"})"
370369
msgstr ""
371370

372-
msgid "Finally, select all the tuples in a space:"
373-
msgstr ""
374-
375-
msgid "resp, err = conn.Select(\"tester\", \"primary\", 0, tarantool.KeyLimit, tarantool.IterAll, []interface{}{})"
371+
msgid "Finally, it would be nice to select all the tuples in a space. But there is no one-liner for this in Go; you would need a script like :ref:`this one <cookbook-select-all-go>`."
376372
msgstr ""
377373

378374
msgid "For more examples, see https://github.com/tarantool/go-tarantool#usage"

0 commit comments

Comments
 (0)