Skip to content

Fix gh-1457 fix bug + add cookbook recipe for select-all #1540

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 4 commits into from
Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion doc/book/app_server/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ socket_udp_echo.lua

Tarantool does not currently have a `udp_server` function,
therefore socket_udp_echo.lua is more complicated than
socket_tcp_echo.lua.
socket_tcp_echo.lua.
It can be implemented with sockets and fibers.

.. code-block:: lua
Expand Down Expand Up @@ -774,7 +774,18 @@ An "HTML" file for this server, including Lua, could look like this
</body>
</html>

.. _cookbook-select-all-go:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
select_all.go
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In Go, there is no one-liner to select all tuples from a Tarantool space.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be better to explain that selecting all tuples is not a good idea not only in Go.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To discuss with @artur-barsegyan.

Yet you can use a script like this one. Call it on the instance you want to
connect to.

.. literalinclude:: cookbook/main.go
:language: go

.. _rock: http://rocks.tarantool.org/
.. _http: https://github.com/tarantool/http/
Expand Down
71 changes: 71 additions & 0 deletions doc/book/app_server/cookbook/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"fmt"
"log"

"github.com/tarantool/go-tarantool"
)

/*
box.cfg{listen = 3301}
box.schema.user.passwd('pass')

s = box.schema.space.create('tester')
s:format({
{name = 'id', type = 'unsigned'},
{name = 'band_name', type = 'string'},
{name = 'year', type = 'unsigned'}
})
s:create_index('primary', { type = 'hash', parts = {'id'} })
s:create_index('scanner', { type = 'tree', parts = {'id', 'band_name'} })

s:insert{1, 'Roxette', 1986}
s:insert{2, 'Scorpions', 2015}
s:insert{3, 'Ace of Base', 1993}
*/

func main() {
conn, err := tarantool.Connect("127.0.0.1:3301", tarantool.Opts{
User: "admin",
Pass: "pass",
})

if err != nil {
log.Fatalf("Connection refused")
}
defer conn.Close()

spaceName := "tester"
indexName := "scanner"
idFn := conn.Schema.Spaces[spaceName].Fields["id"].Id
bandNameFn := conn.Schema.Spaces[spaceName].Fields["band_name"].Id

var tuplesPerRequest uint32 = 2
cursor := []interface{}{}

for {
resp, err := conn.Select(spaceName, indexName, 0, tuplesPerRequest, tarantool.IterGt, cursor)
if err != nil {
log.Fatalf("Failed to select: %s", err)
}

if resp.Code != tarantool.OkCode {
log.Fatalf("Select failed: %s", resp.Error)
}

if len(resp.Data) == 0 {
break
}

fmt.Println("Iteration")

tuples := resp.Tuples()
for _, tuple := range tuples {
fmt.Printf("\t%v\n", tuple)
}

lastTuple := tuples[len(tuples)-1]
cursor = []interface{}{lastTuple[idFn], lastTuple[bandNameFn]}
}
}
24 changes: 9 additions & 15 deletions doc/getting_started/getting_started_go.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,21 @@ To get connected to the Tarantool server, write a simple Go program:
)

func main() {
conn, err := tarantool.Connect("127.0.0.1:3301", tarantool.Opts{})

conn, err := tarantool.Connect("127.0.0.1:3301", tarantool.Opts{
User: "admin",
Pass: "pass",
})

if err != nil {
fmt.Println("Connection refused")
log.Fatalf("Connection refused")
}

defer conn.Close()

// Your logic for interacting with the database
}

You can also specify the user name and password, if needed:

.. code-block:: go

opts := tarantool.Opts{User: "username", Pass: "password"}
conn, err := tarantool.Connect("127.0.0.1:3301", opts)
...

The default user is ``guest``.

.. _getting_started-go-manipulate:
Expand Down Expand Up @@ -144,11 +140,9 @@ Next, select tuples by a secondary key.

resp, err = conn.Select("tester", "secondary", 0, 1, tarantool.IterEq, []interface{}{"ABBA"})

Finally, select all the tuples in a space:

.. code-block:: go

resp, err = conn.Select("tester", "primary", 0, tarantool.KeyLimit, tarantool.IterAll, []interface{}{})
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>`.

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

Expand Down
80 changes: 80 additions & 0 deletions locale/book/app_server/cookbook.pot
Original file line number Diff line number Diff line change
Expand Up @@ -640,3 +640,83 @@ msgid "<html>\n"
"</html>"
msgstr ""

msgid "select_all.go"
msgstr ""

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."
msgstr ""

msgid "package main\n"
"\n"
"import (\n"
" \"fmt\"\n"
" \"log\"\n"
"\n"
" \"github.com/tarantool/go-tarantool\"\n"
")\n"
"\n"
"/*\n"
"box.cfg{listen = 3301}\n"
"box.schema.user.passwd('pass')\n"
"\n"
"s = box.schema.space.create('tester')\n"
"s:format({\n"
" {name = 'id', type = 'unsigned'},\n"
" {name = 'band_name', type = 'string'},\n"
" {name = 'year', type = 'unsigned'}\n"
"})\n"
"s:create_index('primary', { type = 'hash', parts = {'id'} })\n"
"s:create_index('scanner', { type = 'tree', parts = {'id', 'band_name'} })\n"
"\n"
"s:insert{1, 'Roxette', 1986}\n"
"s:insert{2, 'Scorpions', 2015}\n"
"s:insert{3, 'Ace of Base', 1993}\n"
"*/\n"
"\n"
"func main() {\n"
" conn, err := tarantool.Connect(\"127.0.0.1:3301\", tarantool.Opts{\n"
" User: \"admin\",\n"
" Pass: \"pass\",\n"
" })\n"
"\n"
" if err != nil {\n"
" log.Fatalf(\"Connection refused\")\n"
" }\n"
" defer conn.Close()\n"
"\n"
" spaceName := \"tester\"\n"
" indexName := \"scanner\"\n"
" idFn := conn.Schema.Spaces[spaceName].Fields[\"id\"].Id\n"
" bandNameFn := conn.Schema.Spaces[spaceName].Fields[\"band_name\"].Id\n"
"\n"
" var tuplesPerRequest uint32 = 2\n"
" cursor := []interface{}{}\n"
"\n"
" for {\n"
" resp, err := conn.Select(spaceName, indexName, 0, tuplesPerRequest, tarantool.IterGt, cursor)\n"
" if err != nil {\n"
" log.Fatalf(\"Failed to select: %s\", err)\n"
" }\n"
"\n"
" if resp.Code != tarantool.OkCode {\n"
" log.Fatalf(\"Select failed: %s\", resp.Error)\n"
" }\n"
"\n"
" if len(resp.Data) == 0 {\n"
" break\n"
" }\n"
"\n"
" fmt.Println(\"Iteration\")\n"
"\n"
" tuples := resp.Tuples()\n"
" for _, tuple := range tuples {\n"
" fmt.Printf(\"\\t%v\\n\", tuple)\n"
" }\n"
"\n"
" lastTuple := tuples[len(tuples)-1]\n"
" cursor = []interface{}{lastTuple[idFn], lastTuple[bandNameFn]}\n"
" }\n"
"}\n"
""
msgstr ""

18 changes: 7 additions & 11 deletions locale/getting_started/getting_started_connectors.pot
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,14 @@ msgid "package main\n"
")\n"
"\n"
"func main() {\n"
" conn, err := tarantool.Connect(\"127.0.0.1:3301\", tarantool.Opts{})\n"
"\n"
" conn, err := tarantool.Connect(\"127.0.0.1:3301\", tarantool.Opts{\n"
" User: \"admin\",\n"
" Pass: \"pass\",\n"
" })\n"
"\n"
" if err != nil {\n"
" fmt.Println(\"Connection refused\")\n"
" log.Fatalf(\"Connection refused\")\n"
" }\n"
"\n"
" defer conn.Close()\n"
Expand All @@ -333,11 +337,6 @@ msgid "package main\n"
"}"
msgstr ""

msgid "opts := tarantool.Opts{User: \"username\", Pass: \"password\"}\n"
"conn, err := tarantool.Connect(\"127.0.0.1:3301\", opts)\n"
"..."
msgstr ""

msgid "To insert a tuple into a space, use ``Insert``:"
msgstr ""

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

msgid "Finally, select all the tuples in a space:"
msgstr ""

msgid "resp, err = conn.Select(\"tester\", \"primary\", 0, tarantool.KeyLimit, tarantool.IterAll, []interface{}{})"
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>`."
msgstr ""

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