Documentation ¶
Overview ¶
Package client provides clients for accessing the various externally-facing Cockroach database endpoints.
TODO(pmattis): Update these docs to talk about the DB interface.
KV Client ¶
The KV 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 and fills in the the reply and returns an error. The example below shows a get and a put.
kv := client.newKV(nil, client.newHTTPSender("localhost:8080", httpClient)) getCall := client.Get(proto.Key("a")) getResp := getCall.Reply.(*proto.GetResponse) if err := kv.Run(getCall); err != nil { log.Fatal(err) } if err := kv.Run(client.Put(proto.Key("b"), getResp.Value.Bytes)) err != nil { log.Fatal(err) }
The API is synchronous, but accommodates efficient parallel updates and queries using the variadic Run method. An arbitrary number of calls may be passed to Run which are sent to Cockroach as part of a batch. Note however that such the individual API calls within a batch are not guaranteed to have atomic semantics. A transaction must be used to guarantee atomicity. A simple example of using the API which does two scans in parallel and then sends a sequence of puts in parallel:
kv := client.newKV(nil, client.newHTTPSender("localhost:8080", httpClient)) acScan := client.Scan(proto.Key("a"), proto.Key("c\x00"), 1000) xzScan := client.Scan(proto.Key("x"), proto.Key("z\x00"), 1000) // Run sends both scans in parallel and returns first error or nil. if err := kv.Run(acScan, xzScan); err != nil { log.Fatal(err) } acResp := acScan.Reply.(*proto.ScanResponse) xzResp := xzScan.Reply.(*proto.ScanResponse) // Append maximum value from "a"-"c" to all values from "x"-"z". max := []byte(nil) for _, keyVal := range acResp.Rows { if bytes.Compare(max, keyVal.Value.Bytes) < 0 { max = keyVal.Value.Bytes } } var calls []*client.Call for keyVal := range xzResp.Rows { putCall := client.Put(keyVal.Key, bytes.Join([][]byte{keyVal.Value.Bytes, max}, []byte(nil))) calls = append(calls, putCall) } // Run all puts for parallel execution. if err := kv.Run(calls...); err != nil { log.Fatal(err) }
Transactions are supported through the RunTransaction() 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 RunTransaction 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:
kv := client.newKV(nil, client.newHTTPSender("localhost:8080", httpClient)) opts := &client.TransactionOptions{Name: "test", Isolation: proto.SERIALIZABLE} err := kv.RunTransaction(opts, func(txn *client.Txn) error { for i := 0; i < 100; i++ { key := proto.Key(fmt.Sprintf("testkey-%02d", i)) txn.Prepare(client.Put(key, []byte("test value"))) } // Note that the Txn client is flushed automatically on transaction // commit. Invoking Flush after individual API methods is only // required if the result needs to be received to take conditional // action. return nil }) 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 write using KV.Prepare calls. 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 ¶
- Constants
- Variables
- func RegisterSender(scheme string, f NewSenderFunc)
- type Batch
- func (b *Batch) CPut(key, value, expValue interface{}) *Batch
- func (b *Batch) Del(keys ...interface{}) *Batch
- func (b *Batch) DelRange(s, e interface{}) *Batch
- func (b *Batch) Get(key interface{}) *Batch
- func (b *Batch) Inc(key interface{}, value int64) *Batch
- func (b *Batch) InternalAddCall(call Call)
- func (b *Batch) Put(key, value interface{}) *Batch
- func (b *Batch) Scan(s, e interface{}, maxRows int64) *Batch
- type Call
- func ConditionalPut(key proto.Key, valueBytes, expValueBytes []byte) Call
- func Delete(key proto.Key) Call
- func DeleteRange(startKey, endKey proto.Key) Call
- func Get(key proto.Key) Call
- func GetProto(key proto.Key, msg gogoproto.Message) Call
- func Increment(key proto.Key, increment int64) Call
- func Put(key proto.Key, valueBytes []byte) Call
- func PutProto(key proto.Key, msg gogoproto.Message) Call
- func Scan(key, endKey proto.Key, maxResults int64) Call
- type Clock
- type DB
- func (db *DB) AdminMerge(key interface{}) error
- func (db *DB) AdminSplit(splitKey interface{}) error
- func (db *DB) CPut(key, value, expValue interface{}) error
- func (db *DB) Del(keys ...interface{}) error
- func (db *DB) DelRange(begin, end interface{}) error
- func (db *DB) Get(key interface{}) (KeyValue, error)
- func (db *DB) GetProto(key interface{}, msg gogoproto.Message) error
- func (db *DB) Inc(key interface{}, value int64) (KeyValue, error)
- func (db *DB) InternalKV() *KV
- func (db *DB) Put(key, value interface{}) error
- func (db *DB) Run(b *Batch) error
- func (db *DB) Scan(begin, end interface{}, maxRows int64) ([]KeyValue, error)
- func (db *DB) Tx(retryable func(tx *Tx) error) error
- type KV
- type KeyValue
- type NewSenderFunc
- type Option
- type Result
- type Runner
- type Sender
- type SenderFunc
- type TransactionOptions
- type Tx
- func (tx *Tx) CPut(key, value, expValue interface{}) error
- func (tx *Tx) Commit(b *Batch) error
- func (tx *Tx) DebugName() string
- func (tx *Tx) Del(keys ...interface{}) error
- func (tx *Tx) DelRange(begin, end interface{}) error
- func (tx *Tx) Get(key interface{}) (KeyValue, error)
- func (tx *Tx) GetProto(key interface{}, msg gogoproto.Message) error
- func (tx *Tx) Inc(key interface{}, value int64) (KeyValue, error)
- func (tx *Tx) InternalSetPriority(priority int32)
- func (tx *Tx) Put(key, value interface{}) error
- func (tx *Tx) Run(b *Batch) error
- func (tx *Tx) Scan(begin, end interface{}, maxRows int64) ([]KeyValue, error)
- func (tx *Tx) SetDebugName(name string)
- func (tx *Tx) SetSnapshotIsolation()
- type Txn
Examples ¶
Constants ¶
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 ¶
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.
Types ¶
type Batch ¶
type Batch struct { // 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 := client.B.Put("a", "1").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, Tx.Run or Tx.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 := db.B.Get("aa").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 ¶
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 ¶
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 ¶
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) Get ¶
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) Inc ¶
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) InternalAddCall ¶
InternalAddCall adds the specified call to the batch. It is intended for internal use only. It is an error to use InternalAddCall to execute operations that are available via the Batch methods (e.g. Get).
func (*Batch) Put ¶
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) Scan ¶
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.
type Call ¶
type Call struct { Args proto.Request // The argument to the command Reply proto.Response // The reply from the command Err error // Error during call creation Post func() error // Function to be called after successful completion }
A Call is a pending database API call.
func ConditionalPut ¶
ConditionalPut returns a Call object initialized to put value as a byte slice at key if the existing value at key equals expValueBytes.
func DeleteRange ¶
DeleteRange returns a Call object initialized to delete the values in the given key range (excluding the endpoint).
func GetProto ¶
GetProto returns a Call object initialized to get the value at key and then to decode it as a protobuf message.
func Increment ¶
Increment returns a Call object initialized to increment the value at key by increment.
func PutProto ¶
PutProto returns a Call object initialized to put the proto message as a byte slice at key.
type Clock ¶
type Clock interface { // Now returns nanoseconds since the Jan 1, 1970 GMT. Now() int64 }
A Clock is an interface which provides the current time.
type DB ¶
type DB struct { // B is a helper to make creating a new batch and performing an // operation on it easer: // // err := db.Run(db.B.Put("a", "1").Put("b", "2")) B batcher // 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 ¶
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 ¶
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 ¶
AdminSplit splits the range at splitkey.
key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.
func (*DB) CPut ¶
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) Del ¶
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() if err := db.Run(db.B.Put("aa", "1").Put("ab", "2").Put("ac", "3")); 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 ¶
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) Get ¶
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 ¶
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) Inc ¶
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) InternalKV ¶
InternalKV returns the internal KV. It is intended for internal use only.
func (*DB) Put ¶
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) Run ¶
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 ¶
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 := db.B.Put("aa", "1").Put("ab", "2").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) Tx ¶
Tx 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 KV ¶
type KV struct { // User is the default user to set on API calls. If User is set to // non-empty in call arguments, this value is ignored. User string // UserPriority is the default user priority to set on API calls. If // UserPriority is set non-zero in call arguments, this value is // ignored. UserPriority int32 TxnRetryOptions retry.Options Sender Sender // contains filtered or unexported fields }
KV provides access to a KV store. A KV instance is safe for concurrent use by multiple goroutines.
func (*KV) Run ¶
Run runs the specified calls synchronously in a single batch and returns any errors.
func (*KV) RunTransaction ¶
func (kv *KV) RunTransaction(opts *TransactionOptions, retryable func(txn *Txn) error) error
RunTransaction 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. retryable should have no side effects which could cause problems in the event it must be run more than once. The opts struct contains transaction settings.
Example ¶
This is an example for using the RunTransaction() method to submit multiple Key Value API operations inside a transaction.
package main import ( "bytes" "fmt" "github.com/cockroachdb/cockroach/client" "github.com/cockroachdb/cockroach/proto" "github.com/cockroachdb/cockroach/security" "github.com/cockroachdb/cockroach/server" "github.com/cockroachdb/cockroach/util/log" ) func main() { // Using built-in test server for this example code. serv := server.StartTestServer(nil) defer serv.Stop() // Key Value Client initialization. db, err := client.Open("https://root@" + serv.ServingAddr() + "?certs=" + security.EmbeddedCertsDir) if err != nil { log.Fatal(err) } kvClient := db.InternalKV() // Create test data. numKVPairs := 10 keys := make([]string, numKVPairs) values := make([][]byte, numKVPairs) for i := 0; i < numKVPairs; i++ { keys[i] = fmt.Sprintf("testkey-%03d", i) values[i] = []byte(fmt.Sprintf("testvalue-%03d", i)) } // Insert all KV pairs inside a transaction. putOpts := client.TransactionOptions{Name: "example put"} err = kvClient.RunTransaction(&putOpts, func(txn *client.Txn) error { for i := 0; i < numKVPairs; i++ { txn.Prepare(client.Put(proto.Key(keys[i]), values[i])) } // Note that the KV client is flushed automatically on transaction // commit. Invoking Flush after individual API methods is only // required if the result needs to be received to take conditional // action. return nil }) if err != nil { log.Fatal(err) } // Read back KV pairs inside a transaction. getResponses := make([]*proto.GetResponse, numKVPairs) getOpts := client.TransactionOptions{Name: "example get"} err = kvClient.RunTransaction(&getOpts, func(txn *client.Txn) error { for i := 0; i < numKVPairs; i++ { call := client.Get(proto.Key(keys[i])) getResponses[i] = call.Reply.(*proto.GetResponse) txn.Prepare(call) } return nil }) if err != nil { log.Fatal(err) } // Check results. for i, getResp := range getResponses { if getResp.Value == nil { log.Fatal("No value returned for ", keys[i]) } else { if !bytes.Equal(values[i], getResp.Value.Bytes) { log.Fatal("Data mismatch for ", keys[i], ", got: ", getResp.Value.Bytes) } } } fmt.Println("Transaction example done.") }
Output: Transaction example done.
type KeyValue ¶
KeyValue represents a single key/value pair and corresponding timestamp.
func (*KeyValue) ValueBytes ¶
ValueBytes returns the value as a byte slice. This method will panic if the value's type is not a byte slice.
type NewSenderFunc ¶
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.
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 Tx operation (e.g. Get, Put, etc).
type Sender ¶
type Sender interface { // Send invokes the Call.Method with Call.Args and sets the result // in Call.Reply. Send(context.Context, Call) }
Sender is an interface for sending a request to a Key-Value database backend.
type SenderFunc ¶
SenderFunc is an adapter to allow the use of ordinary functions as Senders.
type TransactionOptions ¶
type TransactionOptions struct { Name string // Concise desc of txn for debugging Isolation proto.IsolationType UserPriority int32 }
TransactionOptions are parameters for use with KV.RunTransaction.
type Tx ¶
type Tx struct { // B is a helper to make creating a new batch and performing an // operation on it easer: // // err := db.Tx(func(tx *Tx) error { // return tx.Commit(tx.B.Put("a", "1").Put("b", "2")) // }) B batcher // contains filtered or unexported fields }
Tx is an in-progress distributed database transaction. A Tx is not safe for concurrent use by multiple goroutines.
func (*Tx) CPut ¶
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 (*Tx) Commit ¶
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.
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() err := db.Tx(func(tx *client.Tx) error { return tx.Commit(tx.B.Put("aa", "1").Put("ab", "2")) }) if err != nil { panic(err) } b := db.B.Get("aa").Get("ab") if err := db.Run(b); err != nil { panic(err) } for i, result := range b.Results { for j, row := range result.Rows { fmt.Printf("%d/%d: %s=%s\n", i, j, row.Key, row.ValueBytes()) } } }
Output: 0/0: aa=1 1/0: ab=2
func (*Tx) Del ¶
Del deletes one or more keys.
key can be either a byte slice, a string, a fmt.Stringer or an encoding.BinaryMarshaler.
func (*Tx) DelRange ¶
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 (*Tx) Get ¶
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 (*Tx) GetProto ¶
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 (*Tx) Inc ¶
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 (*Tx) InternalSetPriority ¶
InternalSetPriority sets the transaction priority. It is intended for internal (testing) use only.
func (*Tx) Put ¶
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 (*Tx) Run ¶
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 (*Tx) Scan ¶
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 (*Tx) SetDebugName ¶
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 (*Tx) SetSnapshotIsolation ¶
func (tx *Tx) 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.
type Txn ¶
type Txn struct {
// contains filtered or unexported fields
}
Txn provides serial access to a KV store via Run and parallel access via Prepare and Flush. On receipt of TransactionRestartError, the transaction epoch is incremented and error passed to caller. On receipt of TransactionAbortedError, the transaction is re-created and the error passed to caller.
A Txn instance is not thread safe.
func (*Txn) Flush ¶
Flush sends all previously prepared calls, buffered by invocations of Prepare(). The calls are organized into a single batch command and sent together. Flush returns nil if all prepared calls are executed successfully. Otherwise, Flush returns the first error, where calls are executed in the order in which they were prepared. After Flush returns, all prepared reply structs will be valid.
func (*Txn) Prepare ¶
Prepare accepts a KV API call, specified by arguments and a reply struct. The call will be buffered locally until the first call to Flush(), at which time it will be sent for execution as part of a batch call. Using Prepare/Flush parallelizes queries and updates and should be used where possible for efficiency.
For clients using an HTTP sender, Prepare/Flush allows multiple commands to be sent over the same connection. Prepare/Flush can dramatically improve efficiency by compressing multiple writes into a single atomic update in the event that the writes are to keys within a single range.
TODO(pmattis): Can Prepare/Flush be replaced with a Batch struct? Doing so could potentially make the Txn interface more symmetric with the KV interface, but potentially removes the optimization to send the EndTransaction in the same batch as the final set of prepared calls.