client

package
v0.0.0-...-9f035b8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2015 License: Apache-2.0 Imports: 30 Imported by: 0

Documentation

Overview

Package client provides clients for accessing the various externally-facing Cockroach database endpoints.

DB Client

The DB client is a fully-featured client of Cockroach's key-value database. It provides a simple, synchronous interface well-suited to parallel updates and queries.

The simplest way to use the client is through the Run method. Run synchronously invokes the call, fills in the the reply and returns an error. The example below shows a get and a put.

db, err := client.Open("https://root@localhost:8080")
if err != nil {
	log.Fatal(err)
}
if err := db.Put("a", "hello"); err != nil {
	log.Fatal(err)
}
if gr, err := db.Get("a"); err != nil {
	log.Fatal(err)
} else {
	log.Printf("%s", gr.ValueBytes())  // "hello"
}

The API is synchronous, but accommodates efficient parallel updates and queries using Batch objects. An arbitrary number of calls may be added to a Batch which is executed using DB.Run. Note however that the individual calls within a batch are not guaranteed to have atomic semantics. A transaction must be used to guarantee atomicity. A simple example of using a Batch which does two scans in parallel and then sends a sequence of puts in parallel:

db, err := client.Open("https://root@localhost:8080")
if err != nil {
	log.Fatal(err)
}

b1 := &client.Batch{}
b1.Scan("a", "c\x00", 1000)
b1.Scan("x", "z\x00", 1000)

// Run sends both scans in parallel and returns the first error or nil.
if err := db.Run(b1); err != nil {
	log.Fatal(err)
}

acResult := b1.Results[0]
xzResult := b1.Results[1]

// Append maximum value from "a"-"c" to all values from "x"-"z".
max := []byte(nil)
for _, row := range acResult.Rows {
	if bytes.Compare(max, row.ValueBytes()) < 0 {
		max = row.ValueBytes()
	}
}

b2 := &client.Batch{}
for _, row := range xzResult.Rows {
	b2.Put(row.Key, bytes.Join([][]byte{row.ValueBytes(), max}, []byte(nil)))
}

// Run all puts for parallel execution.
if err := db.Run(b2); err != nil {
	log.Fatal(err)
}

Transactions are supported through the DB.Txn() method, which takes a retryable function, itself composed of the same simple mix of API calls typical of a non-transactional operation. Within the context of the Txn() call, all method invocations are transparently given necessary transactional details, and conflicts are handled with backoff/retry loops and transaction restarts as necessary. An example of using transactions with parallel writes:

db, err := client.Open("https://root@localhost:8080")
if err != nil {
	log.Fatal(err)
}

err := db.Txn(func(txn *client.Txn) error {
	b := &client.Batch{}
	for i := 0; i < 100; i++ {
		key := fmt.Sprintf("testkey-%02d", i)
		b.Put(key, "test value")
	}

	// Note that the Txn client is flushed automatically when this function
	// returns success (i.e. nil). Calling Commit explicitly can sometimes
	// reduce the number of RPCs.
	return txn.Commit(b)
})
if err != nil {
	log.Fatal(err)
}

Note that with Cockroach's lock-free transactions, clients should expect retries as a matter of course. This is why the transaction functionality is exposed through a retryable function. The retryable function should have no side effects which are not idempotent.

Transactions should endeavor to use batches to perform multiple operations in a single RPC. In addition to the reduced number of RPCs to the server, this allows writes to the same range to be batched together. In cases where the entire transaction affects only a single range, transactions can commit in a single round trip.

Index

Examples

Constants

View Source
const (
	// KVDBEndpoint is the URL path prefix which accepts incoming
	// HTTP requests for the KV API.
	KVDBEndpoint = "/kv/db/"
	// StatusTooManyRequests indicates client should retry due to
	// server having too many requests.
	StatusTooManyRequests = 429
)

Variables

View Source
var (
	// DefaultTxnRetryOptions are the standard retry options used
	// for transactions.
	// This is exported for testing purposes only.
	DefaultTxnRetryOptions = retry.Options{
		Backoff:     50 * time.Millisecond,
		MaxBackoff:  5 * time.Second,
		Constant:    2,
		MaxAttempts: 0,
		UseV1Info:   true,
	}
)

Functions

func RegisterSender

func RegisterSender(scheme string, f NewSenderFunc)

RegisterSender registers the specified function to be used for creation of a new sender when the specified scheme is encountered.

func SchemaFromModel

func SchemaFromModel(obj interface{}) (proto.TableSchema, error)

SchemaFromModel allows the easy construction of a TableSchema from a Go struct. Columns are created for each exported field in the struct. The "db" struct tag is used to control the mapping of field name to column name and to indicate exported fields which should be skipped.

type User struct {
  ID      int
  Name    string `db:"old_name"`
  Ignored int    `db:"-"`
}

Indexes are specified using the "roach" struct tag declaration.

type User struct {
  ID   int    `roach:"primary key"`
  Name string `db:"old_name" roach:"index"`
}

The following "roach" options are supported:

"primary key [(columns...)]" - creates a unique index on <columns> and
marks it as the primary key for the table. If <columns> is not specified
it defaults to the name of the column the option is associated with.

"index" [(columns...)]" - creates an index on <columns>.

"unique index" [(columns...)]" - creates a unique index on <columns>.

Types

type Batch

type Batch struct {
	// The DB the batch is associated with. This field may be nil if the batch
	// was not created via DB.NewBatch or Txn.NewBatch.
	DB *DB
	// Results contains an entry for each operation added to the batch. The order
	// of the results matches the order the operations were added to the
	// batch. For example:
	//
	//   b := db.NewBatch()
	//   b.Put("a", "1")
	//   b.Put("b", "2")
	//   _ = db.Run(b)
	//   // string(b.Results[0].Rows[0].Key) == "a"
	//   // string(b.Results[1].Rows[0].Key) == "b"
	Results []Result
	// contains filtered or unexported fields
}

Batch provides for the parallel execution of a number of database operations. Operations are added to the Batch and then the Batch is executed via either DB.Run, Txn.Run or Txn.Commit.

TODO(pmattis): Allow a timestamp to be specified which is applied to all operations within the batch.

Example
package main

import (
	"fmt"
	"log"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/server"
)

func setup() (*server.TestServer, *client.DB) {
	s := server.StartTestServer(nil)
	db, err := client.Open("https://root@" + s.ServingAddr() + "?certs=test_certs")
	if err != nil {
		log.Fatal(err)
	}
	return s, db
}

func main() {
	s, db := setup()
	defer s.Stop()

	b := &client.Batch{}
	b.Get("aa")
	b.Put("bb", "2")
	if err := db.Run(b); err != nil {
		panic(err)
	}
	for _, result := range b.Results {
		for _, row := range result.Rows {
			fmt.Printf("%s=%s\n", row.Key, row.ValueBytes())
		}
	}

}
Output:

aa=
bb=2

func (*Batch) CPut

func (b *Batch) CPut(key, value, expValue interface{})

CPut conditionally sets the value for a key if the existing value is equal to expValue. To conditionally set a value only if there is no existing entry pass nil for expValue.

A new result will be appended to the batch which will contain a single row and Result.Err will indicate success or failure.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler. value can be any key type or a proto.Message.

func (*Batch) Del

func (b *Batch) Del(keys ...interface{})

Del deletes one or more keys.

A new result will be appended to the batch and each key will have a corresponding row in the returned Result.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Batch) DelRange

func (b *Batch) DelRange(s, e interface{})

DelRange deletes the rows between begin (inclusive) and end (exclusive).

A new result will be appended to the batch which will contain 0 rows and Result.Err will indicate success or failure.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Batch) DelStruct

func (b *Batch) DelStruct(obj interface{}, columns ...string)

DelStruct deletes the specified columns from the structured table identified by obj. The primary key columns within obj are used to identify which row to modify. The obj type must have previously been bound to a table using BindModel. If columns is empty the entire row is deleted.

TODO(pmattis): If "obj" is a pointer, should we clear the columns in "obj" that are being deleted?

func (*Batch) Get

func (b *Batch) Get(key interface{})

Get retrieves the value for a key. A new result will be appended to the batch which will contain a single row.

r, err := db.Get("a")
// string(r.Rows[0].Key) == "a"

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Batch) GetProto

func (b *Batch) GetProto(key interface{}, msg gogoproto.Message)

GetProto retrieves the value for a key and decodes the result as a proto message. A new result will be appended to the batch which will contain a single row. Note that the proto will not be decoded until after the batch is executed using DB.Run or Txn.Run.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Batch) GetStruct

func (b *Batch) GetStruct(obj interface{}, columns ...string)

GetStruct retrieves the specified columns in the structured table identified by obj. The primary key columns within obj are used to identify which row to retrieve. The obj type must have previously been bound to a table using BindModel. If columns is empty all of the columns are retrieved. Obj must be a pointer to the model type.

func (*Batch) Inc

func (b *Batch) Inc(key interface{}, value int64)

Inc increments the integer value at key. If the key does not exist it will be created with an initial value of 0 which will then be incremented. If the key exists but was set using Put or CPut an error will be returned.

A new result will be appended to the batch which will contain a single row and Result.Err will indicate success or failure.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Batch) IncStruct

func (b *Batch) IncStruct(obj interface{}, value int64, column string)

IncStruct increments the specified column in the structured table identify by obj. The primary key columns within obj are used to identify which row to modify. The obj type must have previously been bound to a table using BindModel.

func (*Batch) InternalAddCall

func (b *Batch) InternalAddCall(call proto.Call)

InternalAddCall adds the specified call to the batch. It is intended for internal use only.

func (*Batch) Put

func (b *Batch) Put(key, value interface{})

Put sets the value for a key.

A new result will be appended to the batch which will contain a single row and Result.Err will indicate success or failure.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler. value can be any key type or a proto.Message.

func (*Batch) PutStruct

func (b *Batch) PutStruct(obj interface{}, columns ...string)

PutStruct sets the specified columns in the structured table identified by obj. The primary key columns within obj are used to identify which row to modify. The obj type must have previously been bound to a table using BindModel. If columns is empty all of the columns are set.

func (*Batch) Scan

func (b *Batch) Scan(s, e interface{}, maxRows int64)

Scan retrieves the rows between begin (inclusive) and end (exclusive).

A new result will be appended to the batch which will contain up to maxRows rows and Result.Err will indicate success or failure.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Batch) ScanStruct

func (b *Batch) ScanStruct(dest, start, end interface{}, maxRows int64, columns ...string)

ScanStruct scans the specified columns from the structured table identified by the destination slice. The slice element type, start and end key types must be identical. The primary key columns within start and end are used to identify which rows to scan. The type must have previously been bound to a table using BindModel. If columns is empty all of the columns in the table are scanned.

type DB

type DB struct {
	Sender Sender
	// contains filtered or unexported fields
}

DB is a database handle to a single cockroach cluster. A DB is safe for concurrent use by multiple goroutines.

func Open

func Open(addr string, opts ...Option) (*DB, error)

Open creates a new database handle to the cockroach cluster specified by addr. The cluster is identified by a URL with the format:

[<sender>:]//[<user>@]<host>:<port>[?certs=<dir>,priority=<val>]

The URL scheme (<sender>) specifies which transport to use for talking to the cockroach cluster. Currently allowable values are: http, https, rpc, rpcs. The rpc and rpcs senders use a variant of Go's builtin rpc library for communication with the cluster. This protocol is lower overhead and more efficient than http. The decision between the encrypted (https, rpcs) and unencrypted senders (http, rpc) depends on the settings of the cluster. A given cluster supports either encrypted or unencrypted traffic, but not both. The <sender> can be left unspecified in the URL and set by passing client.SenderOpt.

If not specified, the <user> field defaults to "root".

The certs parameter can be used to override the default directory to use for client certificates. In tests, the directory "test_certs" uses the embedded test certificates.

The priority parameter can be used to override the default priority for operations.

func (*DB) AdminMerge

func (db *DB) AdminMerge(key interface{}) error

AdminMerge merges the range containing key and the subsequent range. After the merge operation is complete, the range containing key will contain all of the key/value pairs of the subsequent range and the subsequent range will no longer exist.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*DB) AdminSplit

func (db *DB) AdminSplit(splitKey interface{}) error

AdminSplit splits the range at splitkey.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*DB) BindModel

func (db *DB) BindModel(name string, obj interface{}) error

BindModel binds the supplied interface with the named table. You must bind the model for any type you wish to perform operations on. It is an error to bind the same model type more than once and a single model type can only be bound to a single table. The primaryKey arguments specify the columns that make up the primary key.

func (*DB) CPut

func (db *DB) CPut(key, value, expValue interface{}) error

CPut conditionally sets the value for a key if the existing value is equal to expValue. To conditionally set a value only if there is no existing entry pass nil for expValue.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler. value can be any key type or a proto.Message.

Example
package main

import (
	"fmt"
	"log"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/server"
)

func setup() (*server.TestServer, *client.DB) {
	s := server.StartTestServer(nil)
	db, err := client.Open("https://root@" + s.ServingAddr() + "?certs=test_certs")
	if err != nil {
		log.Fatal(err)
	}
	return s, db
}

func main() {
	s, db := setup()
	defer s.Stop()

	if err := db.Put("aa", "1"); err != nil {
		panic(err)
	}
	if err := db.CPut("aa", "2", "1"); err != nil {
		panic(err)
	}
	result, err := db.Get("aa")
	if err != nil {
		panic(err)
	}
	fmt.Printf("aa=%s\n", result.ValueBytes())

	if err = db.CPut("aa", "3", "1"); err == nil {
		panic("expected error from conditional put")
	}
	result, err = db.Get("aa")
	if err != nil {
		panic(err)
	}
	fmt.Printf("aa=%s\n", result.ValueBytes())

	if err = db.CPut("bb", "4", "1"); err == nil {
		panic("expected error from conditional put")
	}
	result, err = db.Get("bb")
	if err != nil {
		panic(err)
	}
	fmt.Printf("bb=%s\n", result.ValueBytes())
	if err = db.CPut("bb", "4", nil); err != nil {
		panic(err)
	}
	result, err = db.Get("bb")
	if err != nil {
		panic(err)
	}
	fmt.Printf("bb=%s\n", result.ValueBytes())

}
Output:

aa=2
aa=2
bb=
bb=4

func (*DB) CreateTable

func (db *DB) CreateTable(schema proto.TableSchema) error

CreateTable creates a table from the specified schema. Table creation will fail if the table name is already in use.

func (*DB) Del

func (db *DB) Del(keys ...interface{}) error

Del deletes one or more keys.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

Example
package main

import (
	"fmt"
	"log"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/server"
)

func setup() (*server.TestServer, *client.DB) {
	s := server.StartTestServer(nil)
	db, err := client.Open("https://root@" + s.ServingAddr() + "?certs=test_certs")
	if err != nil {
		log.Fatal(err)
	}
	return s, db
}

func main() {
	s, db := setup()
	defer s.Stop()

	b := &client.Batch{}
	b.Put("aa", "1")
	b.Put("ab", "2")
	b.Put("ac", "3")
	if err := db.Run(b); err != nil {
		panic(err)
	}
	if err := db.Del("ab"); err != nil {
		panic(err)
	}
	rows, err := db.Scan("a", "b", 100)
	if err != nil {
		panic(err)
	}
	for i, row := range rows {
		fmt.Printf("%d: %s=%s\n", i, row.Key, row.ValueBytes())
	}

}
Output:

0: aa=1
1: ac=3

func (*DB) DelRange

func (db *DB) DelRange(begin, end interface{}) error

DelRange deletes the rows between begin (inclusive) and end (exclusive).

TODO(pmattis): Perhaps the result should return which rows were deleted.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*DB) DelStruct

func (db *DB) DelStruct(obj interface{}, columns ...string) error

DelStruct ...

func (*DB) DeleteTable

func (db *DB) DeleteTable(name string) error

DeleteTable deletes the specified table.

func (*DB) DescribeTable

func (db *DB) DescribeTable(name string) (*proto.TableSchema, error)

DescribeTable retrieves the table schema for the specified table.

func (*DB) Get

func (db *DB) Get(key interface{}) (KeyValue, error)

Get retrieves the value for a key, returning the retrieved key/value or an error.

r, err := db.Get("a")
// string(r.Key) == "a"

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

Example
package main

import (
	"fmt"
	"log"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/server"
)

func setup() (*server.TestServer, *client.DB) {
	s := server.StartTestServer(nil)
	db, err := client.Open("https://root@" + s.ServingAddr() + "?certs=test_certs")
	if err != nil {
		log.Fatal(err)
	}
	return s, db
}

func main() {
	s, db := setup()
	defer s.Stop()

	result, err := db.Get("aa")
	if err != nil {
		panic(err)
	}
	fmt.Printf("aa=%s\n", result.ValueBytes())

}
Output:

aa=

func (*DB) GetProto

func (db *DB) GetProto(key interface{}, msg gogoproto.Message) error

GetProto retrieves the value for a key and decodes the result as a proto message.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*DB) GetStruct

func (db *DB) GetStruct(obj interface{}, columns ...string) error

GetStruct ...

func (*DB) Inc

func (db *DB) Inc(key interface{}, value int64) (KeyValue, error)

Inc increments the integer value at key. If the key does not exist it will be created with an initial value of 0 which will then be incremented. If the key exists but was set using Put or CPut an error will be returned.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

Example
package main

import (
	"fmt"
	"log"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/server"
)

func setup() (*server.TestServer, *client.DB) {
	s := server.StartTestServer(nil)
	db, err := client.Open("https://root@" + s.ServingAddr() + "?certs=test_certs")
	if err != nil {
		log.Fatal(err)
	}
	return s, db
}

func main() {
	s, db := setup()
	defer s.Stop()

	if _, err := db.Inc("aa", 100); err != nil {
		panic(err)
	}
	result, err := db.Get("aa")
	if err != nil {
		panic(err)
	}
	fmt.Printf("aa=%d\n", result.ValueInt())

}
Output:

aa=100

func (*DB) IncStruct

func (db *DB) IncStruct(obj interface{}, value int64, column string) error

IncStruct ...

func (*DB) ListTables

func (db *DB) ListTables() ([]string, error)

ListTables lists the tables.

func (*DB) NewBatch

func (db *DB) NewBatch() *Batch

NewBatch creates and returns a new empty batch object for use with the DB.

func (*DB) Put

func (db *DB) Put(key, value interface{}) error

Put sets the value for a key.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler. value can be any key type or a proto.Message.

Example
package main

import (
	"fmt"
	"log"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/server"
)

func setup() (*server.TestServer, *client.DB) {
	s := server.StartTestServer(nil)
	db, err := client.Open("https://root@" + s.ServingAddr() + "?certs=test_certs")
	if err != nil {
		log.Fatal(err)
	}
	return s, db
}

func main() {
	s, db := setup()
	defer s.Stop()

	if err := db.Put("aa", "1"); err != nil {
		panic(err)
	}
	result, err := db.Get("aa")
	if err != nil {
		panic(err)
	}
	fmt.Printf("aa=%s\n", result.ValueBytes())

}
Output:

aa=1

func (*DB) PutStruct

func (db *DB) PutStruct(obj interface{}, columns ...string) error

PutStruct ...

func (*DB) RenameTable

func (db *DB) RenameTable(oldName, newName string) error

RenameTable renames a table.

func (*DB) Run

func (db *DB) Run(b *Batch) error

Run executes the operations queued up within a batch. Before executing any of the operations the batch is first checked to see if there were any errors during its construction (e.g. failure to marshal a proto message).

The operations within a batch are run in parallel and the order is non-deterministic. It is an unspecified behavior to modify and retrieve the same key within a batch.

Upon completion, Batch.Results will contain the results for each operation. The order of the results matches the order the operations were added to the batch.

func (*DB) Scan

func (db *DB) Scan(begin, end interface{}, maxRows int64) ([]KeyValue, error)

Scan retrieves the rows between begin (inclusive) and end (exclusive).

The returned []KeyValue will contain up to maxRows elements.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

Example
package main

import (
	"fmt"
	"log"

	"github.com/cockroachdb/cockroach/client"
	"github.com/cockroachdb/cockroach/server"
)

func setup() (*server.TestServer, *client.DB) {
	s := server.StartTestServer(nil)
	db, err := client.Open("https://root@" + s.ServingAddr() + "?certs=test_certs")
	if err != nil {
		log.Fatal(err)
	}
	return s, db
}

func main() {
	s, db := setup()
	defer s.Stop()

	b := &client.Batch{}
	b.Put("aa", "1")
	b.Put("ab", "2")
	b.Put("bb", "3")
	if err := db.Run(b); err != nil {
		panic(err)
	}
	rows, err := db.Scan("a", "b", 100)
	if err != nil {
		panic(err)
	}
	for i, row := range rows {
		fmt.Printf("%d: %s=%s\n", i, row.Key, row.ValueBytes())
	}

}
Output:

0: aa=1
1: ab=2

func (*DB) ScanStruct

func (db *DB) ScanStruct(dest, start, end interface{}, maxRows int64, columns ...string) error

ScanStruct ...

func (*DB) Txn

func (db *DB) Txn(retryable func(txn *Txn) error) error

Txn executes retryable in the context of a distributed transaction. The transaction is automatically aborted if retryable returns any error aside from recoverable internal errors, and is automatically committed otherwise. The retryable function should have no side effects which could cause problems in the event it must be run more than once.

TODO(pmattis): Allow transaction options to be specified.

type KeyValue

type KeyValue struct {
	Key       []byte
	Value     interface{}
	Timestamp time.Time
}

KeyValue represents a single key/value pair and corresponding timestamp.

func (*KeyValue) Exists

func (kv *KeyValue) Exists() bool

Exists returns true iff the value exists.

func (*KeyValue) String

func (kv *KeyValue) String() string

func (*KeyValue) ValueBytes

func (kv *KeyValue) ValueBytes() []byte

ValueBytes returns the value as a byte slice. This method will panic if the value's type is not a byte slice.

func (*KeyValue) ValueInt

func (kv *KeyValue) ValueInt() int64

ValueInt returns the value decoded as an int64. This method will panic if the value cannot be decoded as an int64.

func (*KeyValue) ValueProto

func (kv *KeyValue) ValueProto(msg gogoproto.Message) error

ValueProto parses the byte slice value as a proto message.

type NewSenderFunc

type NewSenderFunc func(u *url.URL, ctx *base.Context, retryOpts retry.Options) (Sender, error)

NewSenderFunc creates a new sender for the registered scheme.

type Option

type Option func(*DB)

Option is the signature for a function which applies an option to a DB.

func SenderOpt

func SenderOpt(sender Sender) Option

SenderOpt sets the sender for a DB.

type Result

type Result struct {

	// Err contains any error encountered when performing the operation.
	Err error
	// Rows contains the key/value pairs for the operation. The number of rows
	// returned varies by operation. For Get, Put, CPut, Inc and Del the number
	// of rows returned is the number of keys operated on. For Scan the number of
	// rows returned is the number or rows matching the scan capped by the
	// maxRows parameter. For DelRange Rows is nil.
	Rows []KeyValue
	// contains filtered or unexported fields
}

Result holds the result for a single DB or Txn operation (e.g. Get, Put, etc).

func (Result) String

func (r Result) String() string

type Runner

type Runner interface {
	Run(b *Batch) error
}

Runner only exports the Run method on a batch of operations.

type Sender

type Sender interface {
	// Send invokes the Call.Method with Call.Args and sets the result
	// in Call.Reply.
	Send(context.Context, proto.Call)
}

Sender is an interface for sending a request to a Key-Value database backend.

type SenderFunc

type SenderFunc func(context.Context, proto.Call)

SenderFunc is an adapter to allow the use of ordinary functions as Senders.

func (SenderFunc) Send

func (f SenderFunc) Send(ctx context.Context, c proto.Call)

Send calls f(ctx, c).

type Txn

type Txn struct {
	// contains filtered or unexported fields
}

Txn is an in-progress distributed database transaction. A Txn is not safe for concurrent use by multiple goroutines.

func (*Txn) CPut

func (txn *Txn) CPut(key, value, expValue interface{}) error

CPut conditionally sets the value for a key if the existing value is equal to expValue. To conditionally set a value only if there is no existing entry pass nil for expValue.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler. value can be any key type or a proto.Message.

func (*Txn) Commit

func (txn *Txn) Commit(b *Batch) error

Commit executes the operations queued up within a batch and commits the transaction. Explicitly committing a transaction is optional, but more efficient than relying on the implicit commit performed when the transaction function returns without error.

func (*Txn) DebugName

func (txn *Txn) DebugName() string

DebugName returns the debug name associated with the transaction.

func (*Txn) Del

func (txn *Txn) Del(keys ...interface{}) error

Del deletes one or more keys.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Txn) DelRange

func (txn *Txn) DelRange(begin, end interface{}) error

DelRange deletes the rows between begin (inclusive) and end (exclusive).

The returned Result will contain 0 rows and Result.Err will indicate success or failure.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Txn) DelStruct

func (txn *Txn) DelStruct(obj interface{}, columns ...string) error

DelStruct ...

func (*Txn) Get

func (txn *Txn) Get(key interface{}) (KeyValue, error)

Get retrieves the value for a key, returning the retrieved key/value or an error.

r, err := db.Get("a")
// string(r.Key) == "a"

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Txn) GetProto

func (txn *Txn) GetProto(key interface{}, msg gogoproto.Message) error

GetProto retrieves the value for a key and decodes the result as a proto message.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Txn) GetStruct

func (txn *Txn) GetStruct(obj interface{}, columns ...string) error

GetStruct ...

func (*Txn) Inc

func (txn *Txn) Inc(key interface{}, value int64) (KeyValue, error)

Inc increments the integer value at key. If the key does not exist it will be created with an initial value of 0 which will then be incremented. If the key exists but was set using Put or CPut an error will be returned.

The returned Result will contain a single row and Result.Err will indicate success or failure.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Txn) IncStruct

func (txn *Txn) IncStruct(obj interface{}, value int64, column string) error

IncStruct ...

func (*Txn) InternalSetPriority

func (txn *Txn) InternalSetPriority(priority int32)

InternalSetPriority sets the transaction priority. It is intended for internal (testing) use only.

func (*Txn) NewBatch

func (txn *Txn) NewBatch() *Batch

NewBatch creates and returns a new empty batch object for use with the Txn.

func (*Txn) Put

func (txn *Txn) Put(key, value interface{}) error

Put sets the value for a key

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler. value can be any key type or a proto.Message.

func (*Txn) PutStruct

func (txn *Txn) PutStruct(obj interface{}, columns ...string) error

PutStruct ...

func (*Txn) Run

func (txn *Txn) Run(b *Batch) error

Run executes the operations queued up within a batch. Before executing any of the operations the batch is first checked to see if there were any errors during its construction (e.g. failure to marshal a proto message).

The operations within a batch are run in parallel and the order is non-deterministic. It is an unspecified behavior to modify and retrieve the same key within a batch.

Upon completion, Batch.Results will contain the results for each operation. The order of the results matches the order the operations were added to the batch.

func (*Txn) Scan

func (txn *Txn) Scan(begin, end interface{}, maxRows int64) ([]KeyValue, error)

Scan retrieves the rows between begin (inclusive) and end (exclusive).

The returned []KeyValue will contain up to maxRows elements.

key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.

func (*Txn) ScanStruct

func (txn *Txn) ScanStruct(dest, start, end interface{}, maxRows int64, columns ...string) error

ScanStruct ...

func (*Txn) SetDebugName

func (txn *Txn) SetDebugName(name string)

SetDebugName sets the debug name associated with the transaction which will appear in log files and the web UI. Each transaction starts out with an automatically assigned debug name composed of the file and line number where the transaction was created.

func (*Txn) SetSnapshotIsolation

func (txn *Txn) SetSnapshotIsolation()

SetSnapshotIsolation sets the transaction's isolation type to snapshot. Transactions default to serializable isolation. The isolation must be set before any operations are performed on the transaction.

TODO(pmattis): This isn't tested yet but will be as part of the conversion of client_test.go.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL