Skip to content

doc: add examples for GetTyped() and custom keys #225

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 2 commits into from
Nov 5, 2022
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
17 changes: 9 additions & 8 deletions client_tools.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package tarantool

// IntKey is utility type for passing integer key to Select*, Update* and Delete*.
// It serializes to array with single integer element.
// IntKey is utility type for passing integer key to Select*, Update*,
// Delete* and GetTyped. It serializes to array with single integer element.
type IntKey struct {
I int
}
Expand All @@ -12,8 +12,9 @@ func (k IntKey) EncodeMsgpack(enc *encoder) error {
return nil
}

// UintKey is utility type for passing unsigned integer key to Select*, Update* and Delete*.
// It serializes to array with single integer element.
// UintKey is utility type for passing unsigned integer key to Select*,
// Update*, Delete* and GetTyped. It serializes to array with single unsigned
// integer element.
type UintKey struct {
I uint
}
Expand All @@ -24,8 +25,8 @@ func (k UintKey) EncodeMsgpack(enc *encoder) error {
return nil
}

// UintKey is utility type for passing string key to Select*, Update* and Delete*.
// It serializes to array with single string element.
// StringKey is utility type for passing string key to Select*, Update*,
// Delete* and GetTyped. It serializes to array with single string element.
type StringKey struct {
S string
}
Expand All @@ -36,8 +37,8 @@ func (k StringKey) EncodeMsgpack(enc *encoder) error {
return nil
}

// IntIntKey is utility type for passing two integer keys to Select*, Update* and Delete*.
// It serializes to array with two integer elements.
// IntIntKey is utility type for passing two integer keys to Select*, Update*,
// Delete* and GetTyped. It serializes to array with two integer elements.
type IntIntKey struct {
I1, I2 int
}
Expand Down
82 changes: 60 additions & 22 deletions config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@ box.cfg{
}

box.once("init", function()
local s = box.schema.space.create('test', {
id = 517,
if_not_exists = true,
})
s:create_index('primary', {type = 'tree', parts = {1, 'uint'}, if_not_exists = true})

local sp = box.schema.space.create('SQL_TEST', {
id = 519,
if_not_exists = true,
format = {
{name = "NAME0", type = "unsigned"},
{name = "NAME1", type = "string"},
{name = "NAME2", type = "string"},
}
})
sp:create_index('primary', {type = 'tree', parts = {1, 'uint'}, if_not_exists = true})
sp:insert{1, "test", "test"}

local st = box.schema.space.create('schematest', {
id = 516,
temporary = true,
Expand Down Expand Up @@ -53,8 +35,54 @@ box.once("init", function()
})
st:truncate()

local s2 = box.schema.space.create('test_perf', {
local s = box.schema.space.create('test', {
id = 517,
if_not_exists = true,
})
s:create_index('primary', {
type = 'tree',
parts = {1, 'uint'},
if_not_exists = true
})

local s = box.schema.space.create('teststring', {
id = 518,
if_not_exists = true,
})
s:create_index('primary', {
type = 'tree',
parts = {1, 'string'},
if_not_exists = true
})

local s = box.schema.space.create('testintint', {
id = 519,
if_not_exists = true,
})
s:create_index('primary', {
type = 'tree',
parts = {1, 'int', 2, 'int'},
if_not_exists = true
})

local s = box.schema.space.create('SQL_TEST', {
id = 520,
if_not_exists = true,
format = {
{name = "NAME0", type = "unsigned"},
{name = "NAME1", type = "string"},
{name = "NAME2", type = "string"},
}
})
s:create_index('primary', {
type = 'tree',
parts = {1, 'uint'},
if_not_exists = true
})
s:insert{1, "test", "test"}

local s = box.schema.space.create('test_perf', {
id = 521,
temporary = true,
if_not_exists = true,
field_count = 3,
Expand All @@ -64,14 +92,24 @@ box.once("init", function()
{name = "arr1", type = "array"},
},
})
s2:create_index('primary', {type = 'tree', unique = true, parts = {1, 'unsigned'}, if_not_exists = true})
s2:create_index('secondary', {id = 5, type = 'tree', unique = false, parts = {2, 'string'}, if_not_exists = true})
s:create_index('primary', {
type = 'tree',
unique = true,
parts = {1, 'unsigned'},
if_not_exists = true
})
s:create_index('secondary', {
id = 5, type = 'tree',
unique = false,
parts = {2, 'string'},
if_not_exists = true
})
local arr_data = {}
for i = 1,100 do
arr_data[i] = i
end
for i = 1,1000 do
s2:insert{
s:insert{
i,
'test_name',
arr_data,
Expand Down
92 changes: 92 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,98 @@ func ExampleConnection_SelectAsync() {
// Future 2 Data [[18 val 18 bla]]
}

func ExampleConnection_GetTyped() {
conn := example_connect()
defer conn.Close()

const space = "test"
const index = "primary"
conn.Replace(space, []interface{}{uint(1111), "hello", "world"})

var t Tuple
err := conn.GetTyped(space, index, []interface{}{1111}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {{} 1111 hello world}
}

func ExampleIntKey() {
conn := example_connect()
defer conn.Close()

const space = "test"
const index = "primary"
conn.Replace(space, []interface{}{int(1111), "hello", "world"})

var t Tuple
err := conn.GetTyped(space, index, tarantool.IntKey{1111}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {{} 1111 hello world}
}

func ExampleUintKey() {
conn := example_connect()
defer conn.Close()

const space = "test"
const index = "primary"
conn.Replace(space, []interface{}{uint(1111), "hello", "world"})

var t Tuple
err := conn.GetTyped(space, index, tarantool.UintKey{1111}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {{} 1111 hello world}
}

func ExampleStringKey() {
conn := example_connect()
defer conn.Close()

const space = "teststring"
const index = "primary"
conn.Replace(space, []interface{}{"any", []byte{0x01, 0x02}})

t := struct {
Key string
Value []byte
}{}
err := conn.GetTyped(space, index, tarantool.StringKey{"any"}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {any [1 2]}
}

func ExampleIntIntKey() {
conn := example_connect()
defer conn.Close()

const space = "testintint"
const index = "primary"
conn.Replace(space, []interface{}{1, 2, "foo"})

t := struct {
Key1 int
Key2 int
Value string
}{}
err := conn.GetTyped(space, index, tarantool.IntIntKey{1, 2}, &t)
fmt.Println("Error", err)
fmt.Println("Data", t)
// Output:
// Error <nil>
// Data {1 2 foo}
}

func ExampleSelectRequest() {
conn := example_connect()
defer conn.Close()
Expand Down
16 changes: 5 additions & 11 deletions tarantool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ func BenchmarkClientSerialSQL(b *testing.B) {
conn := test_helpers.ConnectWithValidation(b, server, opts)
defer conn.Close()

spaceNo := 519
_, err := conn.Replace(spaceNo, []interface{}{uint(1111), "hello", "world"})
_, err := conn.Replace("SQL_TEST", []interface{}{uint(1111), "hello", "world"})
if err != nil {
b.Errorf("Failed to replace: %s", err)
}
Expand All @@ -227,8 +226,7 @@ func BenchmarkClientSerialSQLPrepared(b *testing.B) {
conn := test_helpers.ConnectWithValidation(b, server, opts)
defer conn.Close()

spaceNo := 519
_, err := conn.Replace(spaceNo, []interface{}{uint(1111), "hello", "world"})
_, err := conn.Replace("SQL_TEST", []interface{}{uint(1111), "hello", "world"})
if err != nil {
b.Errorf("Failed to replace: %s", err)
}
Expand Down Expand Up @@ -601,7 +599,6 @@ func BenchmarkClientParallelMassiveUntyped(b *testing.B) {
func BenchmarkClientReplaceParallel(b *testing.B) {
conn := test_helpers.ConnectWithValidation(b, server, opts)
defer conn.Close()
spaceNo = 520

rSpaceNo, _, err := conn.Schema.ResolveSpaceIndex("test_perf", "secondary")
if err != nil {
Expand Down Expand Up @@ -647,8 +644,7 @@ func BenchmarkClientParallelSQL(b *testing.B) {
conn := test_helpers.ConnectWithValidation(b, server, opts)
defer conn.Close()

spaceNo := 519
_, err := conn.Replace(spaceNo, []interface{}{uint(1111), "hello", "world"})
_, err := conn.Replace("SQL_TEST", []interface{}{uint(1111), "hello", "world"})
if err != nil {
b.Errorf("No connection available")
}
Expand All @@ -671,8 +667,7 @@ func BenchmarkClientParallelSQLPrepared(b *testing.B) {
conn := test_helpers.ConnectWithValidation(b, server, opts)
defer conn.Close()

spaceNo := 519
_, err := conn.Replace(spaceNo, []interface{}{uint(1111), "hello", "world"})
_, err := conn.Replace("SQL_TEST", []interface{}{uint(1111), "hello", "world"})
if err != nil {
b.Errorf("No connection available")
}
Expand Down Expand Up @@ -706,8 +701,7 @@ func BenchmarkSQLSerial(b *testing.B) {
conn := test_helpers.ConnectWithValidation(b, server, opts)
defer conn.Close()

spaceNo := 519
_, err := conn.Replace(spaceNo, []interface{}{uint(1111), "hello", "world"})
_, err := conn.Replace("SQL_TEST", []interface{}{uint(1111), "hello", "world"})
if err != nil {
b.Errorf("Failed to replace: %s", err)
}
Expand Down