Categorygithub.com/viciious/go-tarantool
modulepackage
1.2.2
Repository: https://github.com/viciious/go-tarantool.git
Documentation: pkg.go.dev

# README

go-tarantool GoDoc Build Status

The go-tarantool package has everything necessary for interfacing with Tarantool 1.6+.

The advantage of integrating Go with Tarantool, which is an application server plus a DBMS, is that Go programmers can handle databases with responses that are faster than other packages according to public benchmarks.

Table of contents

Key features

  • Support for both encoding and decoding of Tarantool queries/commands, which leads us to the following advantages:
    • implementing services that mimic a real Tarantool DBMS is relatively easy; for example, you can code a service which would relay queries and commands to a real Tarantool instance; the server interface is documented here;
    • replication support: you can implement a service which would mimic a Tarantool replication slave and get on-the-fly data updates from the Tarantool master, an example is provided here.
  • The interface for sending and packing queries is different from other go-tarantool implementations, which you may find more aesthetically pleasant to work with: all queries are represented with different types that follow the same interface rather than with individual methods in the connector, e.g. conn.Exec(&Update{...}) vs conn.Update({}).

Installation

Pre-requisites:

  • Tarantool version 1.6 or 1.7,
  • a modern Linux, BSD or Mac OS operating system,
  • a current version of go, version 1.8 or later (use go version to check the version number).

If your go version is older than 1.8, or if go is not installed, download the latest tarball from golang.org and say:

sudo tar -C /usr/local -xzf go1.8.3.linux-amd64.tar.gz
sudo chmod -R a+rwx /usr/local/go

Make sure go and go-tarantool are on your path. For example:

export PATH=$PATH:/usr/local/go/bin
export GOPATH="/usr/local/go/go-tarantool"

The go-tarantool package is in the viciious/go-tarantool repository. To download and install, say:

go get github.com/viciious/go-tarantool

This should bring source and binary files into subdirectories of /usr/local/go, making it possible to access by adding github.com/viciious/go-tarantool in the import {...} section at the start of any Go program.

Hello World

Here is a very short example Go program which tries to connect to a Tarantool server.

package main

import (
    "context"
    "fmt"
    "github.com/viciious/go-tarantool"
)

func main() {
    opts := tarantool.Options{User: "guest"}
    conn, err := tarantool.Connect("127.0.0.1:3301", &opts)
    if err != nil {
        fmt.Printf("Connection refused: %s\n", err.Error())
	return
    }

    query := &tarantool.Insert{Space: "examples", Tuple: []interface{}{uint64(99999), "BB"}}
    resp := conn.Exec(context.Background(), query)

    if resp.Error != nil {
        fmt.Println("Insert failed", resp.Error)
    } else {
        fmt.Println(fmt.Sprintf("Insert succeeded: %#v", resp.Data))
    }

    conn.Close()
}

Cut and paste this example into a file named example.go.

Start a Tarantool server on localhost, and make sure it is listening on port 3301. Set up a space named examples exactly as described in the Tarantool manual's Connectors section.

Again, make sure PATH and GOPATH point to the right places. Then build and run example.go:

go build example.go
./example

You should see: messages saying "Insert failed" or "Insert succeeded".

If that is what you see, then you have successfully installed go-tarantool and successfully executed a program that connected to a Tarantool server and manipulated the contents of a Tarantool database.

Walking through the example

We can now have a closer look at the example.go program and make some observations about what it does.

Observation 1: the line "github.com/viciious/go-tarantool" in the import(...) section brings in all Tarantool-related functions and structures. It is common to bring in context and fmt as well.

Observation 2: the line beginning with "Opts :=" sets up the options for Connect(). In this example, there is only one thing in the structure, a user name. The structure can also contain:

  • ConnectTimeout (the number of milliseconds the connector will wait a new connection to be established before giving up),
  • QueryTimeout (the default maximum number of milliseconds to wait before giving up - can be overriden on per-query basis),
  • DefaultSpace (the name of default Tarantool space)
  • Password (user's password)
  • UUID (used for replication)
  • ReplicaSetUUID (used for replication)

Observation 3: the line containing "tarantool.Connect" is one way to begin a session. There are two parameters:

  • a string with host:port format (or "/path/to/tarantool.socket"), and
  • the option structure that was set up earlier.

There is an alternative way to connect, we will describe it later.

Observation 4: the err structure will be nil if there is no error, otherwise it will have a description which can be retrieved with err.Error().

Observation 5: the conn.exec request, like many requests, is preceded by "conn." which is the name of the object that was returned by Connect(). In this case, for Insert, there are two parameters:

  • a space name (it could just as easily have been a space number), and
  • a tuple.

All the requests described in the Tarantool manual can be expressed in a similar way within connect.Exec(), with the format "&name-of-request{arguments}". For example: &ping{}. For a long example:

    data, err := conn.Exec(context.Background(), &Update{
        Space: "tester",
        Index: "primary",
        Key:   1,
        Set: []Operator{
            &OpAdd{
                Field:    2,
                Argument: 17,
            },
            &OpAssign{
                Field:    1,
                Argument: "Hello World",
            },
        },
    })

API reference

Read the Tarantool manual to find descriptions of terms like "connect", "space", "index", and the requests for creating and manipulating database objects or Lua functions.

The source files for the requests library are:

  • connection.go for the Connect() function plus functions related to connecting, and
  • insert_test.go for an example of a data-manipulation function used in tests.

See comments in these files for syntax details:

The supported requests have parameters and results equivalent to requests in the Tarantool manual. Browsing through the other *.go programs in the package will show how the packagers have paid attention to some of the more advanced features of Tarantool, such as vclock and replication.

Alternative way to connect

Here we show a variation of example.go, where the connect is done a different way.


package main

import (
    "context"
    "fmt"
    "github.com/viciious/go-tarantool"
)

func main() {
    opts := tarantool.Options{User: "guest"}
    tnt := tarantool.New("127.0.0.1:3301", &opts)
    conn, err := tnt.Connect()
    if err != nil {
        fmt.Printf("Connection refused: %s\n", err.Error())
	return
    }

    query := &tarantool.Insert{Space: "examples", Tuple: []interface{}{uint64(99999), "BB"}}
    resp := conn.Exec(context.Background(), query)

    if resp.Error != nil {
        fmt.Println("Insert failed", resp.Error)
    } else {
        fmt.Println(fmt.Sprintf("Insert succeeded: %#v", resp.Data))
    }

    conn.Close()
}

In this variation, tarantool.New returns a Connector instance, which is a goroutine-safe singleton object that can transparently handle reconnects.

Help

To contact go-tarantool developers on any problems, create an issue at viciious/go-tarantool.

The developers of the Tarantool server will also be happy to provide advice or receive feedback.

# Packages

No description provided by the author
No description provided by the author

# Functions

Connect to tarantool instance with options.
Connect to tarantool instance with options using the provided context.
ConnectionClosedError returns ConnectionError with message about closed connection or error depending on the connection state.
New Connector instance.
NewAnonSlave returns new AnonSlave instance.
No description provided by the author
NewConnectionError returns ConnectionError, which contains wrapped with remoteAddr error.
NewContextError returns ContextError with message and remoteAddr in error text.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
NewQueryError returns QueryError with message and Code.
NewReplicaSet returns empty ReplicaSet.
NewSlave instance with tarantool master uri.
NewUnexpectedReplicaSetUUIDError returns UnexpectedReplicaSetUUIDError.
NewVectorClock returns VectorClock with clocks equal to the given lsn elements sequentially.
No description provided by the author
No description provided by the author
No description provided by the author

# Constants

No description provided by the author
Tarantool >= 1.7.2.
No description provided by the author
No description provided by the author
No description provided by the author
%s access denied for user '%s'.
"Field %s contains %s on conflict action, but %s in index parts".
"Operation is not permitted if timer is already running".
Operation is not permitted when there is an active transaction.
"Failed to lock WAL directory %s and hot_standby mode is off".
"Can't modify sequence '%s': %s".
Can't modify space '%s': %s.
Argument type in operation '%c' on field %u does not match field type: expected a %s.
"Too many authentication attempts".
"Authentication required".
"Backup is already in progress".
"Some replica set members were not specified in box.cfg.replication"oAll.
"Replica %s chose a different bootstrap leader %s".
"Trying to bootstrap a local read-only instance as master".
"Couldn't find an instance to register this replica on.".
"Can't check who replica %s chose its bootstrap leader"r.
"Failed to initialize collation: %s.".
Attempt to modify a tuple field which is part of index '%s' in space '%s'.
"WAL has a rollback in progress".
Incorrect value for option '%s': %s.
"Snapshot is already in progress".
"Check constraint failed '%s': %s".
"%s are prohibited in a ck constraint definition".
Can't reset cluster id: it is already assigned.
Cluster id of the replica %s doesn't match cluster id of the master %s.
"Cluster name mismatch: expected %s, got %s".
"Can not commit transaction in a nested statement".
"Foreign key constraint '%s' failed: %s".
"Compression error: %s".
"Connection to self".
"%s constraint '%s' already exists in space '%s'".
"Failed to create check constraint '%s': %s".
"Failed to create constraint '%s' in space '%s': %s".
"Failed to create foreign key constraint '%s': %s".
"Failed to create foreign key '%s' in space '%s': %s".
Failed to create function '%s': %s.
Failed to create role '%s': %s.
"Failed to create sequence '%s': %s".
Failed to create space '%s': %s.
Failed to create user '%s': %s.
User not found or supplied credentials are invalid.
A multi-statement transaction can not use multiple storage engines.
"Decompression error: %s".
"Type of the default value does not match tuple field %s type: expected %s, got %s".
"Can't drop collation %s : %s".
"Failed to drop foreign key constraint '%s': %s".
Can't drop function %u: %s.
Can't drop primary key in space '%s' while secondary keys exist.
"Can't drop sequence '%s': %s".
Can't drop space '%s': %s.
Failed to drop user '%s': %s.
"Elections were turned off".
Invalid key part count in an exact match (expected %u, got %u).
Can not create a new fiber: recursion limit reached.
"Check constraint '%s' failed for field '%s'".
"Foreign key constraint '%s' failed for field '%s': %s".
Tuple field %u type does not match one required by operation: expected %s.
Ambiguous field type in index '%s', key part %u.
"Can not commit transaction: deferred foreign keys violations are not resolved".
"Foreign key '%s' integrity check failed: %s".
"Key format doesn't match one defined in functional index '%s' of space '%s': %s".
"Failed to build a key for functional index '%s' of space '%s': %s".
"Wrong functional index definition: %s".
"Function '%s' returned value of invalid type: expected %s got %s".
%s access denied for user '%s' to function '%s'.
Function '%s' already exists.
Unsupported language '%s' specified for function '%s'.
A limit on the total number of functions has been reached: %u.
"Wrong number of arguments is passed to %s(): expected %s, got %d".
Incorrect grant arguments: %s.
Setting password for guest user has no effect.
"Hex literal %s%s length %d exceeds the supported limit (%d)".
Invalid identifier '%s' (expected letters, digits or an underscore).
"Illegal mix of collations".
Illegal parameters, %s.
"Inconsistent types: expected %s got %s".
"%s are prohibited in an index definition".
Index '%s' already exists.
"Index '%s' already exists in space '%s'".
Tuple field count %u is less than required by a defined index (expected %u).
"Indexed field count limit reached: %d indexed fields".
Unsupported index type supplied for index '%s' in space '%s'.
Error injection '%s'.
"Duplicate replica name %s, already occupied by %s".
"Instance name mismatch: expected %s, got %s".
"Interfering elections started".
"Instance with replica id %u was promoted first".
"Integer literal %s%s exceeds the supported range [-9223372036854775808, 18446744073709551615]".
"Invalid '%s' data: %s".
"Invalid '%s' request: %s".
"Invalid INDEX file %s: %s".
Invalid MsgPack - %s.
Invalid LSN order for server %u: previous LSN = %llu, new lsn = %llu.
"Invalid RUN file: %s".
Invalid UUID: %s.
"Invalid VYLOG file: %s".
Failed to read xlog: %lld.
Invalid xlog name: expected %lld got %lld.
Invalid xlog order: %lld and %lld.
"Invalid xlog type: expected %s, got %s".
"Iterator position is invalid".
Unknown iterator type '%s'.
Invalid key part count (expected [0..%u], got %u).
"Key part is too long: %u of %u bytes".
Supplied key type of part %u does not match index part type: expected %s.
Can't drop the primary key in a system space, space '%s'.
Failed to dynamically load function '%s': %s.
"Instance bootstrap hasn't finished yet".
"Failed to dynamically load module '%.*s': %s".
"The local instance id %u is read-only"ly.
Local server is not active.
Failed to allocate %u bytes in %s for %s.
"Failed to allocate %u bytes for tuple: tuple is too large.
Missing mandatory field '%s' in request.
Can't find snapshot.
"Snapshot has no system spaces".
Can't create or modify index '%s' in space '%s': %s.
More than one tuple found by get().
"Field %s is used as multikey in one index and as single key in another".
"Nil UUID is reserved and can't be used in replication".
Operation is not permitted when there is no active transaction.
Connection is not established.
"Not enough peers connected to start elections: %d out of minimal required %d".
Can't modify data on a replication slave.
"Collation '%s' does not exist".
"Constraint '%s' does not exist in space '%s'".
Space engine '%s' does not exist.
"Unknown event %s".
Field %d was not found in the tuple.
"Field '%s' was not found in the tuple".
"Field '%s' was not found in space '%s' format".
Function '%s' does not exist.
"Replication group '%s' does not exist".
No index #%u is defined in space '%s'.
"No index '%s' is defined in space '%s'".
"Module '%s' does not exist".
Procedure '%.*s' is not defined.
Role '%s' is not found.
"Sequence '%s' does not exist".
"Session %llu does not exist".
"Session setting %s doesn't exist".
Space '%s' does not exist.
Trigger is not found.
User '%s' is not found.
"The instance is not a leader.
"No active transaction".
"Primary index of space '%s' can not contain nullable parts".
"Password must differ from last %d passwords".
"The term is outdated: old - %llu, new - %llu".
No description provided by the author
"%s index does not support selects via a partial key (expected %u parts, got %u).
"Password expired".
User '%s' already has %s access on %s '%s'.
User '%s' does not have %s access on %s '%s'.
???.
%s.
msgpack.encode: can not encode Lua type '%s'.
%s.
"Couldn't wait for quorum %d: %s".
Can't modify data because this server is in read-only mode.
"The read view is aborted".
Can't set option '%s' dynamically.
Replica count limit reached: %u.
"Replica '%s' is not anonymous and cannot register.".
"Replicaset name mismatch: expected %s, got %s".
Reserved66.
Role '%s' already exists.
User '%s' already has role '%s'.
Granting role '%s' to role '%s' would create a loop.
User '%s' does not have role '%s'.
"Rollback called in a nested statement".
"Rowid is overflowed: too many entries in ephemeral space".
RTree: %s must be an array with %u (point) or %u (rectangle/box) numeric coordinates.
"Your schema version is %u.%u.%u while Tarantool %s requires a more recent schema version.
"Schema upgrade is already in progress".
"Sequence '%s' already exists".
"Sequence '%s' is not started".
"Sequence '%s' has overflowed".
Can't initialize server id with a reserved value %u.
"Session setting %s expected a value of type %s"lue.
%s.
%s access denied for user '%s' to space '%s'.
Space '%s' already exists.
Tuple field count %u does not match space '%s' field count %u.
"Space field '%s' is duplicate".
SPLICE error on field %u: %s.
"Split-Brain discovered: %s".
"ANALYZE statement argument %s is not a base table".
"Parameter %s was not found in the statement".
"SQL bind parameter limit reached: %d".
"Bind value type %s for parameter %s is not supported".
"Bind value for parameter %s is out of range for type %s".
"Can't add AUTOINCREMENT: space %s can't feature more than one AUTOINCREMENT field".
"Can’t resolve field '%s'".
"Unequal number of entries in row expression: left side has %u, but right side - %u".
"Failed to create space '%s': space column count %d exceeds the limit (%d)".
"Failed to execute SQL statement: %s".
"SQL expects exactly one argument returned from %s, got %d".
"At line %d at or near position %d: keyword '%.*s' is reserved.
"Pragma '%s' does not exist".
"%s".
"At line %d at or near position %d: %s".
"%s %d exceeds the limit (%d)".
"Failed to prepare SQL statement: %s".
"Failed to expand '*' in SELECT statement without FROM clause".
"Scanning is not allowed for %s".
"Failed to parse SQL statement: parser stack limit reached".
"Failed to execute an empty SQL statement".
"Syntax error at line %d near '%.*s'".
"Syntax error at line %d at or near position %d: %s".
"Type mismatch: can not convert %s to %s".
"At line %d at or near position %d: unrecognized token '%.*s'".
"%s".
"Can not execute a nested statement: nesting limit reached".
"CONFIRM message arrived for an unknown master id %d, expected %d".
"The synchronous transaction queue belongs to other instance with id %u".
"The synchronous transaction queue doesn't belong to any instance".
"Quorum collection for a synchronous transaction is timed out".
"A rollback for a synchronous transaction is received".
"%s".
Timeout exceeded.
"Can't subscribe non-anonymous replica %s until join is done".
Transaction has been aborted by conflict.
"Transaction has been aborted by timeout".
"Transaction has been aborted by a fiber yield".
"Can't truncate a system space, space '%s'".
"Check constraint '%s' failed for tuple".
"Tuple field count limit reached: see box.schema.FIELD_MAX".
Tuple format limit reached: %u.
Duplicate key exists in unique index '%s' in space '%s'.
Tuple is too long %u.
"Can't create tuple: metadata size %u is too big".
Tuple/Key must be MsgPack array.
Tuple doesn't exist in index '%s' in space '%s'.
Tuple reference counter overflow.
"Transaction was rolled back".
"Unable to process %s request in stream".
"Unable to process %s request out of stream"eam.
"Found uncommitted sync transactions from other instance with id %u"xns.
Unknown error.
"Unknown authentication method '%s'".
Unknown request type %u.
Unknown RTREE index distance type %s.
Unknown object type '%s'.
Server %s is not registered with the cluster.
Unknown UPDATE operation.
%s does not support %s.
"Index '%s' (%s) of space '%s' (%s) does not support %s".
Unsupported role privilege '%s'.
"Decimal overflow when performing operation '%c' on field %s".
Field %u UPDATE error: %s.
Integer overflow when performing '%c' operation on field %u.
Space %s has a unique secondary index and does not support UPSERT.
User '%s' already exists.
A limit on the total number of users has been reached: %u.
"View '%s' is read-only".
"Space declared as a view must have SQL statement".
"Failed to allocate %u bytes for tuple: tuple is too large.
"Timed out waiting for Vinyl memory quota".
Failed to write to disk.
"Password doesn't meet security requirements: %s".
"Wrong collation options: %s".
"Wrong _schema version: expected 'major.minor[.patch]'".
"Wrong function options: %s".
Wrong index options (field %u): %s.
Wrong index parts (field %u): %s; expected field1 id (number), field1 type (string), ...
Wrong record in _index space: got {%s}, expected {%s}.
"Prepared statement with id %u does not exist".
Wrong schema version, current: %d, in request: %u.
"Session '%s' is not supported".
"Wrong space format field %u: %s".
"Wrong space options: %s".
"Wrong space upgrade options: %s"s.
"Missing .xlog file between LSN %lld %s and %lld %s".
No description provided by the author
for starting anonymous replication.
No description provided by the author
No description provided by the author
all tuples.
all bits are not set.
all bits from x are set in key.
at least one x's bit is set.
key == x ASC order.
key >= x.
key > x.
key <= x.
key < x.
key == x DESC order.
No description provided by the author
Tarantool >= 1.9.0.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Tarantool >= 2.3.1.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
for leaving anonymous replication (anon => normal replica).
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Tarantool >= 1.9.0.

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ErrBadResult means that query result was of invalid type or length.
ErrConnectionClosed returns when connection is no longer alive.
No description provided by the author
No description provided by the author
ErrNotInReplicaSet means that join operation can not be performed on a replica set due to missing parameters.
ErrNotSupported is returned when an unimplemented query type or operation is encountered.
ErrOldVersionAnon is returns when tarantool version doesn't support anonymous replication.
No description provided by the author
No description provided by the author
ErrUnknownError is returns when ErrorCode isn't OK but Error is nil in Result.
ErrVectorClock is returns in case of bad manipulation with vector clock.
No description provided by the author
No description provided by the author

# Structs

AnonSlave connects to Tarantool >= 2.3.1 instance and subscribes for changes as anonymous replica.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Box is tarantool instance.
No description provided by the author
No description provided by the author
Call17 is available since Tarantool >= 1.7.2.
No description provided by the author
ConnectionError is returned when something have been happened with connection.
No description provided by the author
ContextError is returned when request has been ended with context timeout or cancel.
No description provided by the author
No description provided by the author
No description provided by the author
Eval query.
FetchSnapshot is the FETCH_SNAPSHOT command.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Join is the JOIN command.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
QueryError is returned when query error has been happened.
Register is the REGISTER command.
No description provided by the author
ReplicaSet is used to store params of the Replica Set.
No description provided by the author
No description provided by the author
Slave connects to Tarantool 1.6, 1.7 or 1.10 instance and subscribes for changes.
Subscribe is the SUBSCRIBE command.
No description provided by the author
UnexpectedReplicaSetUUIDError is returned when ReplicaSetUUID set in Options.ReplicaSetUUID is not equal to ReplicaSetUUID received during Join or JoinWithSnap.
No description provided by the author
No description provided by the author
VClock response (in OK).

# Interfaces

Error has Temporary method which returns true if error is temporary.
No description provided by the author
No description provided by the author
PacketIterator is a wrapper around Slave provided iteration over new Packets functionality.
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
VectorClock is used to store logical clocks (direct dependency clock implementation).