kv.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3
  15. import (
  16. "github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
  17. "github.com/coreos/etcd/Godeps/_workspace/src/google.golang.org/grpc"
  18. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  19. )
  20. type (
  21. PutResponse pb.PutResponse
  22. RangeResponse pb.RangeResponse
  23. GetResponse pb.RangeResponse
  24. DeleteRangeResponse pb.DeleteRangeResponse
  25. DeleteResponse pb.DeleteRangeResponse
  26. TxnResponse pb.TxnResponse
  27. )
  28. type KV interface {
  29. // PUT puts a key-value pair into etcd.
  30. // Note that key,value can be plain bytes array and string is
  31. // an immutable representation of that bytes array.
  32. // To get a string of bytes, do string([]byte(0x10, 0x20)).
  33. Put(key, val string) (*PutResponse, error)
  34. // Range gets the keys [key, end) in the range at rev.
  35. // If revev <=0, range gets the keys at currentRev.
  36. // Limit limits the number of keys returned.
  37. // If the required rev is compacted, ErrCompacted will be returned.
  38. Range(key, end string, limit, rev int64, sort *SortOption) (*RangeResponse, error)
  39. // Get is like Range. A shortcut for ranging single key like [key, key+1).
  40. Get(key, rev int64) (*GetResponse, error)
  41. // DeleteRange deletes the given range [key, end).
  42. DeleteRange(key, end string) (*DeleteRangeResponse, error)
  43. // Delete is like DeleteRange. A shortcut for deleting single key like [key, key+1).
  44. Delete(key string) (*DeleteResponse, error)
  45. // Compact compacts etcd KV history before the given rev.
  46. Compact(rev int64) error
  47. // Txn creates a transaction.
  48. Txn() Txn
  49. }
  50. //
  51. // Tx.If(
  52. // CmpValue(k1, ">", v1),
  53. // CmpVersion(k1, "=", 2)
  54. // ).Then(
  55. // OpPut(k2,v2), OpPut(k3,v3)
  56. // ).Else(
  57. // OpPut(k4,v4), OpPut(k5,v5)
  58. // ).Commit()
  59. type Txn interface {
  60. // If takes a list of comparison. If all comparisons passed in succeed,
  61. // the operations passed into Then() will be executed. Or the operations
  62. // passed into Else() will be executed.
  63. If(cs ...Compare) Txn
  64. // Then takes a list of operations. The Ops list will be executed, if the
  65. // comparisons passed in If() succeed.
  66. Then(ops ...Op) Txn
  67. // Else takes a list of operations. The Ops list will be executed, if the
  68. // comparisons passed in If() fail.
  69. Else(ops ...Op) Txn
  70. // Commit tries to commit the transaction.
  71. Commit() (*TxnResponse, error)
  72. // TODO: add a Do for shortcut the txn without any condition?
  73. }
  74. type kv struct {
  75. conn *grpc.ClientConn // conn in-use
  76. remote pb.KVClient
  77. c *Client
  78. }
  79. func (kv *kv) Range(key, end string, limit, rev int64, sort *SortOption) (*pb.RangeResponse, error) {
  80. r, err := kv.do(OpRange(key, end, limit, rev, sort))
  81. if err != nil {
  82. return nil, err
  83. }
  84. return r.GetResponseRange(), nil
  85. }
  86. func (kv *kv) do(op Op) (*pb.ResponseUnion, error) {
  87. for {
  88. var err error
  89. switch op.t {
  90. // TODO: handle other ops
  91. case tRange:
  92. var resp *pb.RangeResponse
  93. // TODO: setup sorting
  94. r := &pb.RangeRequest{Key: op.key, RangeEnd: op.end, Limit: op.limit, Revision: op.rev}
  95. resp, err = kv.remote.Range(context.TODO(), r)
  96. if err == nil {
  97. return &pb.ResponseUnion{Response: &pb.ResponseUnion_ResponseRange{resp}}, nil
  98. }
  99. default:
  100. panic("Unknown op")
  101. }
  102. if isRPCError(err) {
  103. return nil, err
  104. }
  105. newConn, cerr := kv.c.retryConnection(kv.conn, err)
  106. if cerr != nil {
  107. // TODO: return client lib defined connection error
  108. return nil, cerr
  109. }
  110. kv.conn = newConn
  111. kv.remote = pb.NewKVClient(kv.conn)
  112. }
  113. }