op.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright 2016 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. )
  18. type opType int
  19. const (
  20. // A default Op has opType 0, which is invalid.
  21. tRange opType = iota + 1
  22. tPut
  23. tDeleteRange
  24. )
  25. var (
  26. noPrefixEnd = []byte{0}
  27. )
  28. // Op represents an Operation that kv can execute.
  29. type Op struct {
  30. t opType
  31. key []byte
  32. end []byte
  33. // for range
  34. limit int64
  35. sort *SortOption
  36. serializable bool
  37. keysOnly bool
  38. // for range, watch
  39. rev int64
  40. // progressNotify is for progress updates.
  41. progressNotify bool
  42. // for put
  43. val []byte
  44. leaseID LeaseID
  45. }
  46. func (op Op) toRequestOp() *pb.RequestOp {
  47. switch op.t {
  48. case tRange:
  49. r := &pb.RangeRequest{Key: op.key, RangeEnd: op.end, Limit: op.limit, Revision: op.rev, Serializable: op.serializable}
  50. if op.sort != nil {
  51. r.SortOrder = pb.RangeRequest_SortOrder(op.sort.Order)
  52. r.SortTarget = pb.RangeRequest_SortTarget(op.sort.Target)
  53. }
  54. return &pb.RequestOp{Request: &pb.RequestOp_RequestRange{RequestRange: r}}
  55. case tPut:
  56. r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID)}
  57. return &pb.RequestOp{Request: &pb.RequestOp_RequestPut{RequestPut: r}}
  58. case tDeleteRange:
  59. r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end}
  60. return &pb.RequestOp{Request: &pb.RequestOp_RequestDeleteRange{RequestDeleteRange: r}}
  61. default:
  62. panic("Unknown Op")
  63. }
  64. }
  65. func (op Op) isWrite() bool {
  66. return op.t != tRange
  67. }
  68. func OpGet(key string, opts ...OpOption) Op {
  69. ret := Op{t: tRange, key: []byte(key)}
  70. ret.applyOpts(opts)
  71. return ret
  72. }
  73. func OpDelete(key string, opts ...OpOption) Op {
  74. ret := Op{t: tDeleteRange, key: []byte(key)}
  75. ret.applyOpts(opts)
  76. switch {
  77. case ret.leaseID != 0:
  78. panic("unexpected lease in delete")
  79. case ret.limit != 0:
  80. panic("unexpected limit in delete")
  81. case ret.rev != 0:
  82. panic("unexpected revision in delete")
  83. case ret.sort != nil:
  84. panic("unexpected sort in delete")
  85. case ret.serializable:
  86. panic("unexpected serializable in delete")
  87. }
  88. return ret
  89. }
  90. func OpPut(key, val string, opts ...OpOption) Op {
  91. ret := Op{t: tPut, key: []byte(key), val: []byte(val)}
  92. ret.applyOpts(opts)
  93. switch {
  94. case ret.end != nil:
  95. panic("unexpected range in put")
  96. case ret.limit != 0:
  97. panic("unexpected limit in put")
  98. case ret.rev != 0:
  99. panic("unexpected revision in put")
  100. case ret.sort != nil:
  101. panic("unexpected sort in put")
  102. case ret.serializable:
  103. panic("unexpected serializable in put")
  104. }
  105. return ret
  106. }
  107. func opWatch(key string, opts ...OpOption) Op {
  108. ret := Op{t: tRange, key: []byte(key)}
  109. ret.applyOpts(opts)
  110. switch {
  111. case ret.leaseID != 0:
  112. panic("unexpected lease in watch")
  113. case ret.limit != 0:
  114. panic("unexpected limit in watch")
  115. case ret.sort != nil:
  116. panic("unexpected sort in watch")
  117. case ret.serializable:
  118. panic("unexpected serializable in watch")
  119. }
  120. return ret
  121. }
  122. func (op *Op) applyOpts(opts []OpOption) {
  123. for _, opt := range opts {
  124. opt(op)
  125. }
  126. }
  127. // OpOption configures Operations like Get, Put, Delete.
  128. type OpOption func(*Op)
  129. // WithLease attaches a lease ID to a key in 'Put' request.
  130. func WithLease(leaseID LeaseID) OpOption {
  131. return func(op *Op) { op.leaseID = leaseID }
  132. }
  133. // WithLimit limits the number of results to return from 'Get' request.
  134. func WithLimit(n int64) OpOption { return func(op *Op) { op.limit = n } }
  135. // WithRev specifies the store revision for 'Get' request.
  136. // Or the start revision of 'Watch' request.
  137. func WithRev(rev int64) OpOption { return func(op *Op) { op.rev = rev } }
  138. // WithSort specifies the ordering in 'Get' request. It requires
  139. // 'WithRange' and/or 'WithPrefix' to be specified too.
  140. // 'target' specifies the target to sort by: key, version, revisions, value.
  141. // 'order' can be either 'SortNone', 'SortAscend', 'SortDescend'.
  142. func WithSort(target SortTarget, order SortOrder) OpOption {
  143. return func(op *Op) {
  144. op.sort = &SortOption{target, order}
  145. }
  146. }
  147. func getPrefix(key []byte) []byte {
  148. end := make([]byte, len(key))
  149. copy(end, key)
  150. for i := len(end) - 1; i >= 0; i-- {
  151. if end[i] < 0xff {
  152. end[i] = end[i] + 1
  153. end = end[:i+1]
  154. return end
  155. }
  156. }
  157. // next prefix does not exist (e.g., 0xffff);
  158. // default to WithFromKey policy
  159. return noPrefixEnd
  160. }
  161. // WithPrefix enables 'Get', 'Delete', or 'Watch' requests to operate
  162. // on the keys with matching prefix. For example, 'Get(foo, WithPrefix())'
  163. // can return 'foo1', 'foo2', and so on.
  164. func WithPrefix() OpOption {
  165. return func(op *Op) {
  166. op.end = getPrefix(op.key)
  167. }
  168. }
  169. // WithRange specifies the range of 'Get' or 'Delete' requests.
  170. // For example, 'Get' requests with 'WithRange(end)' returns
  171. // the keys in the range [key, end).
  172. func WithRange(endKey string) OpOption {
  173. return func(op *Op) { op.end = []byte(endKey) }
  174. }
  175. // WithFromKey specifies the range of 'Get' or 'Delete' requests
  176. // to be equal or greater than the key in the argument.
  177. func WithFromKey() OpOption { return WithRange("\x00") }
  178. // WithSerializable makes 'Get' request serializable. By default,
  179. // it's linearizable. Serializable requests are better for lower latency
  180. // requirement.
  181. func WithSerializable() OpOption {
  182. return func(op *Op) { op.serializable = true }
  183. }
  184. // WithKeysOnly makes the 'Get' request return only the keys and the corresponding
  185. // values will be omitted.
  186. func WithKeysOnly() OpOption {
  187. return func(op *Op) { op.keysOnly = true }
  188. }
  189. // WithFirstCreate gets the key with the oldest creation revision in the request range.
  190. func WithFirstCreate() []OpOption { return withTop(SortByCreateRevision, SortAscend) }
  191. // WithLastCreate gets the key with the latest creation revision in the request range.
  192. func WithLastCreate() []OpOption { return withTop(SortByCreateRevision, SortDescend) }
  193. // WithFirstKey gets the lexically first key in the request range.
  194. func WithFirstKey() []OpOption { return withTop(SortByKey, SortAscend) }
  195. // WithLastKey gets the lexically last key in the request range.
  196. func WithLastKey() []OpOption { return withTop(SortByKey, SortDescend) }
  197. // WithFirstRev gets the key with the oldest modification revision in the request range.
  198. func WithFirstRev() []OpOption { return withTop(SortByModRevision, SortAscend) }
  199. // WithLastRev gets the key with the latest modification revision in the request range.
  200. func WithLastRev() []OpOption { return withTop(SortByModRevision, SortDescend) }
  201. // withTop gets the first key over the get's prefix given a sort order
  202. func withTop(target SortTarget, order SortOrder) []OpOption {
  203. return []OpOption{WithPrefix(), WithSort(target, order), WithLimit(1)}
  204. }
  205. // WithProgressNotify makes watch server send periodic progress updates.
  206. // Progress updates have zero events in WatchResponse.
  207. func WithProgressNotify() OpOption {
  208. return func(op *Op) {
  209. op.progressNotify = true
  210. }
  211. }