kv.go 5.3 KB

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