kv.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright 2015 The etcd Authors
  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. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  17. "golang.org/x/net/context"
  18. "google.golang.org/grpc"
  19. )
  20. type (
  21. CompactResponse pb.CompactionResponse
  22. PutResponse pb.PutResponse
  23. GetResponse pb.RangeResponse
  24. DeleteResponse pb.DeleteRangeResponse
  25. TxnResponse pb.TxnResponse
  26. )
  27. type KV interface {
  28. // Put puts a key-value pair into etcd.
  29. // Note that key,value can be plain bytes array and string is
  30. // an immutable representation of that bytes array.
  31. // To get a string of bytes, do string([]byte(0x10, 0x20)).
  32. Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error)
  33. // Get retrieves keys.
  34. // By default, Get will return the value for "key", if any.
  35. // When passed WithRange(end), Get will return the keys in the range [key, end).
  36. // When passed WithFromKey(), Get returns keys greater than or equal to key.
  37. // When passed WithRev(rev) with rev > 0, Get retrieves keys at the given revision;
  38. // if the required revision is compacted, the request will fail with ErrCompacted .
  39. // When passed WithLimit(limit), the number of returned keys is bounded by limit.
  40. // When passed WithSort(), the keys will be sorted.
  41. Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, error)
  42. // Delete deletes a key, or optionally using WithRange(end), [key, end).
  43. Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, error)
  44. // Compact compacts etcd KV history before the given rev.
  45. Compact(ctx context.Context, rev int64, opts ...CompactOption) (*CompactResponse, error)
  46. // Do applies a single Op on KV without a transaction.
  47. // Do is useful when declaring operations to be issued at a later time
  48. // whereas Get/Put/Delete are for better suited for when the operation
  49. // should be immediately issued at time of declaration.
  50. // Do applies a single Op on KV without a transaction.
  51. // Do is useful when creating arbitrary operations to be issued at a
  52. // later time; the user can range over the operations, calling Do to
  53. // execute them. Get/Put/Delete, on the other hand, are best suited
  54. // for when the operation should be issued at the time of declaration.
  55. Do(ctx context.Context, op Op) (OpResponse, error)
  56. // Txn creates a transaction.
  57. Txn(ctx context.Context) Txn
  58. }
  59. type OpResponse struct {
  60. put *PutResponse
  61. get *GetResponse
  62. del *DeleteResponse
  63. }
  64. func (op OpResponse) Put() *PutResponse { return op.put }
  65. func (op OpResponse) Get() *GetResponse { return op.get }
  66. func (op OpResponse) Del() *DeleteResponse { return op.del }
  67. type kv struct {
  68. remote pb.KVClient
  69. }
  70. func NewKV(c *Client) KV {
  71. return &kv{remote: RetryKVClient(c)}
  72. }
  73. func (kv *kv) Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error) {
  74. r, err := kv.Do(ctx, OpPut(key, val, opts...))
  75. return r.put, toErr(ctx, err)
  76. }
  77. func (kv *kv) Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, error) {
  78. r, err := kv.Do(ctx, OpGet(key, opts...))
  79. return r.get, toErr(ctx, err)
  80. }
  81. func (kv *kv) Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, error) {
  82. r, err := kv.Do(ctx, OpDelete(key, opts...))
  83. return r.del, toErr(ctx, err)
  84. }
  85. func (kv *kv) Compact(ctx context.Context, rev int64, opts ...CompactOption) (*CompactResponse, error) {
  86. resp, err := kv.remote.Compact(ctx, OpCompact(rev, opts...).toRequest(), grpc.FailFast(false))
  87. if err != nil {
  88. return nil, toErr(ctx, err)
  89. }
  90. return (*CompactResponse)(resp), err
  91. }
  92. func (kv *kv) Txn(ctx context.Context) Txn {
  93. return &txn{
  94. kv: kv,
  95. ctx: ctx,
  96. }
  97. }
  98. func (kv *kv) Do(ctx context.Context, op Op) (OpResponse, error) {
  99. for {
  100. resp, err := kv.do(ctx, op)
  101. if err == nil {
  102. return resp, nil
  103. }
  104. if isHaltErr(ctx, err) {
  105. return resp, toErr(ctx, err)
  106. }
  107. // do not retry on modifications
  108. if op.isWrite() {
  109. return resp, toErr(ctx, err)
  110. }
  111. }
  112. }
  113. func (kv *kv) do(ctx context.Context, op Op) (OpResponse, error) {
  114. var err error
  115. switch op.t {
  116. // TODO: handle other ops
  117. case tRange:
  118. var resp *pb.RangeResponse
  119. r := &pb.RangeRequest{
  120. Key: op.key,
  121. RangeEnd: op.end,
  122. Limit: op.limit,
  123. Revision: op.rev,
  124. Serializable: op.serializable,
  125. KeysOnly: op.keysOnly,
  126. CountOnly: op.countOnly,
  127. }
  128. if op.sort != nil {
  129. r.SortOrder = pb.RangeRequest_SortOrder(op.sort.Order)
  130. r.SortTarget = pb.RangeRequest_SortTarget(op.sort.Target)
  131. }
  132. resp, err = kv.remote.Range(ctx, r, grpc.FailFast(false))
  133. if err == nil {
  134. return OpResponse{get: (*GetResponse)(resp)}, nil
  135. }
  136. case tPut:
  137. var resp *pb.PutResponse
  138. r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV}
  139. resp, err = kv.remote.Put(ctx, r)
  140. if err == nil {
  141. return OpResponse{put: (*PutResponse)(resp)}, nil
  142. }
  143. case tDeleteRange:
  144. var resp *pb.DeleteRangeResponse
  145. r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
  146. resp, err = kv.remote.DeleteRange(ctx, r)
  147. if err == nil {
  148. return OpResponse{del: (*DeleteResponse)(resp)}, nil
  149. }
  150. default:
  151. panic("Unknown op")
  152. }
  153. return OpResponse{}, err
  154. }