op.go 7.9 KB

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