kv.go 5.3 KB

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