kv.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 creating arbitrary operations to be issued at a
  48. // later time; the user can range over the operations, calling Do to
  49. // execute them. Get/Put/Delete, on the other hand, are best suited
  50. // for when the operation should be issued at the time of declaration.
  51. Do(ctx context.Context, op Op) (OpResponse, error)
  52. // Txn creates a transaction.
  53. Txn(ctx context.Context) Txn
  54. }
  55. type OpResponse struct {
  56. put *PutResponse
  57. get *GetResponse
  58. del *DeleteResponse
  59. }
  60. func (op OpResponse) Put() *PutResponse { return op.put }
  61. func (op OpResponse) Get() *GetResponse { return op.get }
  62. func (op OpResponse) Del() *DeleteResponse { return op.del }
  63. type kv struct {
  64. remote pb.KVClient
  65. }
  66. func NewKV(c *Client) KV {
  67. return &kv{remote: RetryKVClient(c)}
  68. }
  69. func NewKVFromKVClient(remote pb.KVClient) KV {
  70. return &kv{remote: remote}
  71. }
  72. func (kv *kv) Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error) {
  73. r, err := kv.Do(ctx, OpPut(key, val, opts...))
  74. return r.put, toErr(ctx, err)
  75. }
  76. func (kv *kv) Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, error) {
  77. r, err := kv.Do(ctx, OpGet(key, opts...))
  78. return r.get, toErr(ctx, err)
  79. }
  80. func (kv *kv) Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, error) {
  81. r, err := kv.Do(ctx, OpDelete(key, opts...))
  82. return r.del, toErr(ctx, err)
  83. }
  84. func (kv *kv) Compact(ctx context.Context, rev int64, opts ...CompactOption) (*CompactResponse, error) {
  85. resp, err := kv.remote.Compact(ctx, OpCompact(rev, opts...).toRequest())
  86. if err != nil {
  87. return nil, toErr(ctx, err)
  88. }
  89. return (*CompactResponse)(resp), err
  90. }
  91. func (kv *kv) Txn(ctx context.Context) Txn {
  92. return &txn{
  93. kv: kv,
  94. ctx: ctx,
  95. }
  96. }
  97. func (kv *kv) Do(ctx context.Context, op Op) (OpResponse, error) {
  98. for {
  99. resp, err := kv.do(ctx, op)
  100. if err == nil {
  101. return resp, nil
  102. }
  103. if isHaltErr(ctx, err) {
  104. return resp, toErr(ctx, err)
  105. }
  106. // do not retry on modifications
  107. if op.isWrite() {
  108. return resp, toErr(ctx, err)
  109. }
  110. }
  111. }
  112. func (kv *kv) do(ctx context.Context, op Op) (OpResponse, error) {
  113. var err error
  114. switch op.t {
  115. // TODO: handle other ops
  116. case tRange:
  117. var resp *pb.RangeResponse
  118. resp, err = kv.remote.Range(ctx, op.toRangeRequest(), grpc.FailFast(false))
  119. if err == nil {
  120. return OpResponse{get: (*GetResponse)(resp)}, nil
  121. }
  122. case tPut:
  123. var resp *pb.PutResponse
  124. r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV, IgnoreValue: op.ignoreValue, IgnoreLease: op.ignoreLease}
  125. resp, err = kv.remote.Put(ctx, r)
  126. if err == nil {
  127. return OpResponse{put: (*PutResponse)(resp)}, nil
  128. }
  129. case tDeleteRange:
  130. var resp *pb.DeleteRangeResponse
  131. r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
  132. resp, err = kv.remote.DeleteRange(ctx, r)
  133. if err == nil {
  134. return OpResponse{del: (*DeleteResponse)(resp)}, nil
  135. }
  136. default:
  137. panic("Unknown op")
  138. }
  139. return OpResponse{}, err
  140. }