kv.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. "context"
  17. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  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. txn *TxnResponse
  60. }
  61. func (op OpResponse) Put() *PutResponse { return op.put }
  62. func (op OpResponse) Get() *GetResponse { return op.get }
  63. func (op OpResponse) Del() *DeleteResponse { return op.del }
  64. func (op OpResponse) Txn() *TxnResponse { return op.txn }
  65. func (resp *PutResponse) OpResponse() OpResponse {
  66. return OpResponse{put: resp}
  67. }
  68. func (resp *GetResponse) OpResponse() OpResponse {
  69. return OpResponse{get: resp}
  70. }
  71. func (resp *DeleteResponse) OpResponse() OpResponse {
  72. return OpResponse{del: resp}
  73. }
  74. func (resp *TxnResponse) OpResponse() OpResponse {
  75. return OpResponse{txn: resp}
  76. }
  77. type kv struct {
  78. remote pb.KVClient
  79. callOpts []grpc.CallOption
  80. }
  81. func NewKV(c *Client) KV {
  82. api := &kv{remote: RetryKVClient(c)}
  83. if c != nil {
  84. api.callOpts = c.callOpts
  85. }
  86. return api
  87. }
  88. func NewKVFromKVClient(remote pb.KVClient, c *Client) KV {
  89. api := &kv{remote: remote}
  90. if c != nil {
  91. api.callOpts = c.callOpts
  92. }
  93. return api
  94. }
  95. func (kv *kv) Put(ctx context.Context, key, val string, opts ...OpOption) (*PutResponse, error) {
  96. r, err := kv.Do(ctx, OpPut(key, val, opts...))
  97. return r.put, toErr(ctx, err)
  98. }
  99. func (kv *kv) Get(ctx context.Context, key string, opts ...OpOption) (*GetResponse, error) {
  100. r, err := kv.Do(ctx, OpGet(key, opts...))
  101. return r.get, toErr(ctx, err)
  102. }
  103. func (kv *kv) Delete(ctx context.Context, key string, opts ...OpOption) (*DeleteResponse, error) {
  104. r, err := kv.Do(ctx, OpDelete(key, opts...))
  105. return r.del, toErr(ctx, err)
  106. }
  107. func (kv *kv) Compact(ctx context.Context, rev int64, opts ...CompactOption) (*CompactResponse, error) {
  108. resp, err := kv.remote.Compact(ctx, OpCompact(rev, opts...).toRequest(), kv.callOpts...)
  109. if err != nil {
  110. return nil, toErr(ctx, err)
  111. }
  112. return (*CompactResponse)(resp), err
  113. }
  114. func (kv *kv) Txn(ctx context.Context) Txn {
  115. return &txn{
  116. kv: kv,
  117. ctx: ctx,
  118. callOpts: kv.callOpts,
  119. }
  120. }
  121. func (kv *kv) Do(ctx context.Context, op Op) (OpResponse, error) {
  122. var err error
  123. switch op.t {
  124. case tRange:
  125. var resp *pb.RangeResponse
  126. resp, err = kv.remote.Range(ctx, op.toRangeRequest(), kv.callOpts...)
  127. if err == nil {
  128. return OpResponse{get: (*GetResponse)(resp)}, nil
  129. }
  130. case tPut:
  131. var resp *pb.PutResponse
  132. r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV, IgnoreValue: op.ignoreValue, IgnoreLease: op.ignoreLease}
  133. resp, err = kv.remote.Put(ctx, r, kv.callOpts...)
  134. if err == nil {
  135. return OpResponse{put: (*PutResponse)(resp)}, nil
  136. }
  137. case tDeleteRange:
  138. var resp *pb.DeleteRangeResponse
  139. r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
  140. resp, err = kv.remote.DeleteRange(ctx, r, kv.callOpts...)
  141. if err == nil {
  142. return OpResponse{del: (*DeleteResponse)(resp)}, nil
  143. }
  144. case tTxn:
  145. var resp *pb.TxnResponse
  146. resp, err = kv.remote.Txn(ctx, op.toTxnRequest(), kv.callOpts...)
  147. if err == nil {
  148. return OpResponse{txn: (*TxnResponse)(resp)}, nil
  149. }
  150. default:
  151. panic("Unknown op")
  152. }
  153. return OpResponse{}, toErr(ctx, err)
  154. }