From ff0c6ed52f7d6bd8c3b4d7d773cf25c87278a2be Mon Sep 17 00:00:00 2001 From: James Graves Date: Tue, 18 Nov 2014 18:03:37 -0600 Subject: [PATCH] Update KV get/put and prepared batch examples. The basic key/value example has been updated to reflect the current client and proto APIs. The prepared batch example has also been updated to query the correct number of rows. Both examples were tested against a local cluster. --- client/doc.go | 85 +++++++++++++++++++++++++++++---------------------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/client/doc.go b/client/doc.go index 246efbace3fe..9afd4d5df506 100644 --- a/client/doc.go +++ b/client/doc.go @@ -29,15 +29,23 @@ The simplest way to use the client is through the Call method. Call synchronously invokes the method and returns the reply and an error. The example below shows a get and a put. - kv := client.NewKV("localhost:8080", tlsConfig) - - getResp := &proto.GetResponse{} - if err := kv.Call(proto.Get, proto.GetArgs(proto.Key("a")), getResp); err != nil { - log.Fatal(err) - } - if _, err := kv.Call(proto.Put, proto.PutArgs(proto.Key("b"), getResp.Value.Bytes)) err != nil { - log.Fatal(err) - } + host := "localhost:8080" + sender := client.NewHTTPSender(host, &http.Transport{ + TLSClientConfig: rpc.LoadInsecureTLSConfig().Config(), + }) + kv := client.NewKV(sender, nil) + kv.User = storage.UserRoot + + getResp := &proto.GetResponse{} + if err := kv.Call(proto.Get, proto.GetArgs(proto.Key("a")), getResp); err != nil { + log.Fatal(err) + } + + value := getResp.Value.Bytes + putResp := &proto.PutResponse{} + if err := kv.Call(proto.Put, proto.PutArgs(proto.Key("b"), value), putResp); err != nil { + log.Fatal(err) + } The API is synchronous, but accommodates efficient parallel updates and queries using the Prepare method. An arbitrary number of Prepare @@ -50,33 +58,38 @@ 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("localhost:8080", tlsConfig) - - acResp, xzResp := &proto.ScanResponse{}, &proto.ScanResponse{} - kv.Prepare(proto.Scan, proto.ScanArgs(proto.Key("a"), proto.Key("c")), acResp) - kv.Prepare(proto.Scan, proto.ScanArgs(proto.Key("x"), proto.Key("z")), xzResp) - - // Flush sends both scans in parallel and returns first error or nil. - if err := kv.Flush(); err != nil { - log.Fatal(err) - } - - // 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 - } - } - for keyVal := range xzResp.Rows { - putReq := proto.PutArgs(keyVal.Key, bytes.Join([][]byte{keyVal.Value.Bytes, max}, []byte(nil))) - kv.Prepare(proto.Put, putReq, &proto.PutReponse{}) - } - - // Flush all puts for parallel execution. - if _, err := kv.Flush(); err != nil { - log.Fatal(err) - } + host := "localhost:8080" + sender := client.NewHTTPSender(host, &http.Transport{ + TLSClientConfig: rpc.LoadInsecureTLSConfig().Config(), + }) + kv := client.NewKV(sender, nil) + kv.User = storage.UserRoot + + acResp, xzResp := &proto.ScanResponse{}, &proto.ScanResponse{} + kv.Prepare(proto.Scan, proto.ScanArgs(proto.Key("a"), proto.Key("c").Next(), 3), acResp) + kv.Prepare(proto.Scan, proto.ScanArgs(proto.Key("x"), proto.Key("z").Next(), 3), xzResp) + + // Flush sends both scans in parallel and returns first error or nil. + if err := kv.Flush(); err != nil { + log.Fatal(err) + } + + // 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 + } + } + for _, keyVal := range xzResp.Rows { + putReq := proto.PutArgs(keyVal.Key, bytes.Join([][]byte{keyVal.Value.Bytes, max}, []byte(nil))) + kv.Prepare(proto.Put, putReq, &proto.PutResponse{}) + } + + // Flush all puts for parallel execution. + if err := kv.Flush(); 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