op.go 8.2 KB

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