op.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. tTxn
  23. )
  24. var (
  25. noPrefixEnd = []byte{0}
  26. )
  27. // Op represents an Operation that kv can execute.
  28. type Op struct {
  29. t opType
  30. key []byte
  31. end []byte
  32. // for range
  33. limit int64
  34. sort *SortOption
  35. serializable bool
  36. keysOnly bool
  37. countOnly bool
  38. minModRev int64
  39. maxModRev int64
  40. minCreateRev int64
  41. maxCreateRev int64
  42. // for range, watch
  43. rev int64
  44. // for watch, put, delete
  45. prevKV bool
  46. // for put
  47. ignoreValue bool
  48. ignoreLease bool
  49. // progressNotify is for progress updates.
  50. progressNotify bool
  51. // createdNotify is for created event
  52. createdNotify bool
  53. // filters for watchers
  54. filterPut bool
  55. filterDelete bool
  56. // for put
  57. val []byte
  58. leaseID LeaseID
  59. // txn
  60. cmps []Cmp
  61. thenOps []Op
  62. elseOps []Op
  63. }
  64. // accesors / mutators
  65. func (op Op) IsTxn() bool { return op.t == tTxn }
  66. func (op Op) Txn() ([]Cmp, []Op, []Op) { return op.cmps, op.thenOps, op.elseOps }
  67. // KeyBytes returns the byte slice holding the Op's key.
  68. func (op Op) KeyBytes() []byte { return op.key }
  69. // WithKeyBytes sets the byte slice for the Op's key.
  70. func (op *Op) WithKeyBytes(key []byte) { op.key = key }
  71. // RangeBytes returns the byte slice holding with the Op's range end, if any.
  72. func (op Op) RangeBytes() []byte { return op.end }
  73. // Rev returns the requested revision, if any.
  74. func (op Op) Rev() int64 { return op.rev }
  75. // IsPut returns true iff the operation is a Put.
  76. func (op Op) IsPut() bool { return op.t == tPut }
  77. // IsGet returns true iff the operation is a Get.
  78. func (op Op) IsGet() bool { return op.t == tRange }
  79. // IsDelete returns true iff the operation is a Delete.
  80. func (op Op) IsDelete() bool { return op.t == tDeleteRange }
  81. // IsSerializable returns true if the serializable field is true.
  82. func (op Op) IsSerializable() bool { return op.serializable == true }
  83. // IsKeysOnly returns true if the keysonly field is true.
  84. func (op Op) IsKeysOnly() bool { return op.keysOnly == true }
  85. // IsCountOnly returns true if the countonly field is true.
  86. func (op Op) IsCountOnly() bool { return op.countOnly == true }
  87. // MinModRev returns if field is populated.
  88. func (op Op) MinModRev() int64 { return op.minModRev }
  89. // MaxModRev returns if field is populated.
  90. func (op Op) MaxModRev() int64 { return op.maxModRev }
  91. // MinCreateRev returns if field is populated.
  92. func (op Op) MinCreateRev() int64 { return op.minCreateRev }
  93. // MaxCreateRev returns if field is populated.
  94. func (op Op) MaxCreateRev() int64 { return op.maxCreateRev }
  95. // Limit returns if field is populated.
  96. func (op Op) retLimit() int64 { return op.limit }
  97. // Sort returns if field is populated.
  98. func (op Op) retSort() bool { return op.sort != nil }
  99. // WithRangeBytes sets the byte slice for the Op's range end.
  100. func (op *Op) WithRangeBytes(end []byte) { op.end = end }
  101. // ValueBytes returns the byte slice holding the Op's value, if any.
  102. func (op Op) ValueBytes() []byte { return op.val }
  103. // WithValueBytes sets the byte slice for the Op's value.
  104. func (op *Op) WithValueBytes(v []byte) { op.val = v }
  105. func (op Op) toRangeRequest() *pb.RangeRequest {
  106. if op.t != tRange {
  107. panic("op.t != tRange")
  108. }
  109. r := &pb.RangeRequest{
  110. Key: op.key,
  111. RangeEnd: op.end,
  112. Limit: op.limit,
  113. Revision: op.rev,
  114. Serializable: op.serializable,
  115. KeysOnly: op.keysOnly,
  116. CountOnly: op.countOnly,
  117. MinModRevision: op.minModRev,
  118. MaxModRevision: op.maxModRev,
  119. MinCreateRevision: op.minCreateRev,
  120. MaxCreateRevision: op.maxCreateRev,
  121. }
  122. if op.sort != nil {
  123. r.SortOrder = pb.RangeRequest_SortOrder(op.sort.Order)
  124. r.SortTarget = pb.RangeRequest_SortTarget(op.sort.Target)
  125. }
  126. return r
  127. }
  128. func (op Op) toTxnRequest() *pb.TxnRequest {
  129. thenOps := make([]*pb.RequestOp, len(op.thenOps))
  130. for i, tOp := range op.thenOps {
  131. thenOps[i] = tOp.toRequestOp()
  132. }
  133. elseOps := make([]*pb.RequestOp, len(op.elseOps))
  134. for i, eOp := range op.elseOps {
  135. elseOps[i] = eOp.toRequestOp()
  136. }
  137. cmps := make([]*pb.Compare, len(op.cmps))
  138. for i := range op.cmps {
  139. cmps[i] = (*pb.Compare)(&op.cmps[i])
  140. }
  141. return &pb.TxnRequest{Compare: cmps, Success: thenOps, Failure: elseOps}
  142. }
  143. func (op Op) toRequestOp() *pb.RequestOp {
  144. switch op.t {
  145. case tRange:
  146. return &pb.RequestOp{Request: &pb.RequestOp_RequestRange{RequestRange: op.toRangeRequest()}}
  147. case tPut:
  148. r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID), PrevKv: op.prevKV, IgnoreValue: op.ignoreValue, IgnoreLease: op.ignoreLease}
  149. return &pb.RequestOp{Request: &pb.RequestOp_RequestPut{RequestPut: r}}
  150. case tDeleteRange:
  151. r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end, PrevKv: op.prevKV}
  152. return &pb.RequestOp{Request: &pb.RequestOp_RequestDeleteRange{RequestDeleteRange: r}}
  153. case tTxn:
  154. return &pb.RequestOp{Request: &pb.RequestOp_RequestTxn{RequestTxn: op.toTxnRequest()}}
  155. default:
  156. panic("Unknown Op")
  157. }
  158. }
  159. func (op Op) isWrite() bool {
  160. if op.t == tTxn {
  161. for _, tOp := range op.thenOps {
  162. if tOp.isWrite() {
  163. return true
  164. }
  165. }
  166. for _, tOp := range op.elseOps {
  167. if tOp.isWrite() {
  168. return true
  169. }
  170. }
  171. return false
  172. }
  173. return op.t != tRange
  174. }
  175. func OpGet(key string, opts ...OpOption) Op {
  176. ret := Op{t: tRange, key: []byte(key)}
  177. ret.applyOpts(opts)
  178. return ret
  179. }
  180. func OpDelete(key string, opts ...OpOption) Op {
  181. ret := Op{t: tDeleteRange, key: []byte(key)}
  182. ret.applyOpts(opts)
  183. switch {
  184. case ret.leaseID != 0:
  185. panic("unexpected lease in delete")
  186. case ret.limit != 0:
  187. panic("unexpected limit in delete")
  188. case ret.rev != 0:
  189. panic("unexpected revision in delete")
  190. case ret.sort != nil:
  191. panic("unexpected sort in delete")
  192. case ret.serializable:
  193. panic("unexpected serializable in delete")
  194. case ret.countOnly:
  195. panic("unexpected countOnly in delete")
  196. case ret.minModRev != 0, ret.maxModRev != 0:
  197. panic("unexpected mod revision filter in delete")
  198. case ret.minCreateRev != 0, ret.maxCreateRev != 0:
  199. panic("unexpected create revision filter in delete")
  200. case ret.filterDelete, ret.filterPut:
  201. panic("unexpected filter in delete")
  202. case ret.createdNotify:
  203. panic("unexpected createdNotify in delete")
  204. }
  205. return ret
  206. }
  207. func OpPut(key, val string, opts ...OpOption) Op {
  208. ret := Op{t: tPut, key: []byte(key), val: []byte(val)}
  209. ret.applyOpts(opts)
  210. switch {
  211. case ret.end != nil:
  212. panic("unexpected range in put")
  213. case ret.limit != 0:
  214. panic("unexpected limit in put")
  215. case ret.rev != 0:
  216. panic("unexpected revision in put")
  217. case ret.sort != nil:
  218. panic("unexpected sort in put")
  219. case ret.serializable:
  220. panic("unexpected serializable in put")
  221. case ret.countOnly:
  222. panic("unexpected countOnly in put")
  223. case ret.minModRev != 0, ret.maxModRev != 0:
  224. panic("unexpected mod revision filter in put")
  225. case ret.minCreateRev != 0, ret.maxCreateRev != 0:
  226. panic("unexpected create revision filter in put")
  227. case ret.filterDelete, ret.filterPut:
  228. panic("unexpected filter in put")
  229. case ret.createdNotify:
  230. panic("unexpected createdNotify in put")
  231. }
  232. return ret
  233. }
  234. func OpTxn(cmps []Cmp, thenOps []Op, elseOps []Op) Op {
  235. return Op{t: tTxn, cmps: cmps, thenOps: thenOps, elseOps: elseOps}
  236. }
  237. func opWatch(key string, opts ...OpOption) Op {
  238. ret := Op{t: tRange, key: []byte(key)}
  239. ret.applyOpts(opts)
  240. switch {
  241. case ret.leaseID != 0:
  242. panic("unexpected lease in watch")
  243. case ret.limit != 0:
  244. panic("unexpected limit in watch")
  245. case ret.sort != nil:
  246. panic("unexpected sort in watch")
  247. case ret.serializable:
  248. panic("unexpected serializable in watch")
  249. case ret.countOnly:
  250. panic("unexpected countOnly in watch")
  251. case ret.minModRev != 0, ret.maxModRev != 0:
  252. panic("unexpected mod revision filter in watch")
  253. case ret.minCreateRev != 0, ret.maxCreateRev != 0:
  254. panic("unexpected create revision filter in watch")
  255. }
  256. return ret
  257. }
  258. func (op *Op) applyOpts(opts []OpOption) {
  259. for _, opt := range opts {
  260. opt(op)
  261. }
  262. }
  263. // OpOption configures Operations like Get, Put, Delete.
  264. type OpOption func(*Op)
  265. // WithLease attaches a lease ID to a key in 'Put' request.
  266. func WithLease(leaseID LeaseID) OpOption {
  267. return func(op *Op) { op.leaseID = leaseID }
  268. }
  269. // WithLimit limits the number of results to return from 'Get' request.
  270. // If WithLimit is given a 0 limit, it is treated as no limit.
  271. func WithLimit(n int64) OpOption { return func(op *Op) { op.limit = n } }
  272. // WithRev specifies the store revision for 'Get' request.
  273. // Or the start revision of 'Watch' request.
  274. func WithRev(rev int64) OpOption { return func(op *Op) { op.rev = rev } }
  275. // WithSort specifies the ordering in 'Get' request. It requires
  276. // 'WithRange' and/or 'WithPrefix' to be specified too.
  277. // 'target' specifies the target to sort by: key, version, revisions, value.
  278. // 'order' can be either 'SortNone', 'SortAscend', 'SortDescend'.
  279. func WithSort(target SortTarget, order SortOrder) OpOption {
  280. return func(op *Op) {
  281. if target == SortByKey && order == SortAscend {
  282. // If order != SortNone, server fetches the entire key-space,
  283. // and then applies the sort and limit, if provided.
  284. // Since current mvcc.Range implementation returns results
  285. // sorted by keys in lexicographically ascending order,
  286. // client should ignore SortOrder if the target is SortByKey.
  287. order = SortNone
  288. }
  289. op.sort = &SortOption{target, order}
  290. }
  291. }
  292. // GetPrefixRangeEnd gets the range end of the prefix.
  293. // 'Get(foo, WithPrefix())' is equal to 'Get(foo, WithRange(GetPrefixRangeEnd(foo))'.
  294. func GetPrefixRangeEnd(prefix string) string {
  295. return string(getPrefix([]byte(prefix)))
  296. }
  297. func getPrefix(key []byte) []byte {
  298. end := make([]byte, len(key))
  299. copy(end, key)
  300. for i := len(end) - 1; i >= 0; i-- {
  301. if end[i] < 0xff {
  302. end[i] = end[i] + 1
  303. end = end[:i+1]
  304. return end
  305. }
  306. }
  307. // next prefix does not exist (e.g., 0xffff);
  308. // default to WithFromKey policy
  309. return noPrefixEnd
  310. }
  311. // WithPrefix enables 'Get', 'Delete', or 'Watch' requests to operate
  312. // on the keys with matching prefix. For example, 'Get(foo, WithPrefix())'
  313. // can return 'foo1', 'foo2', and so on.
  314. func WithPrefix() OpOption {
  315. return func(op *Op) {
  316. if len(op.key) == 0 {
  317. op.key, op.end = []byte{0}, []byte{0}
  318. return
  319. }
  320. op.end = getPrefix(op.key)
  321. }
  322. }
  323. // WithRange specifies the range of 'Get', 'Delete', 'Watch' requests.
  324. // For example, 'Get' requests with 'WithRange(end)' returns
  325. // the keys in the range [key, end).
  326. // endKey must be lexicographically greater than start key.
  327. func WithRange(endKey string) OpOption {
  328. return func(op *Op) { op.end = []byte(endKey) }
  329. }
  330. // WithFromKey specifies the range of 'Get', 'Delete', 'Watch' requests
  331. // to be equal or greater than the key in the argument.
  332. func WithFromKey() OpOption { return WithRange("\x00") }
  333. // WithSerializable makes 'Get' request serializable. By default,
  334. // it's linearizable. Serializable requests are better for lower latency
  335. // requirement.
  336. func WithSerializable() OpOption {
  337. return func(op *Op) { op.serializable = true }
  338. }
  339. // WithKeysOnly makes the 'Get' request return only the keys and the corresponding
  340. // values will be omitted.
  341. func WithKeysOnly() OpOption {
  342. return func(op *Op) { op.keysOnly = true }
  343. }
  344. // WithCountOnly makes the 'Get' request return only the count of keys.
  345. func WithCountOnly() OpOption {
  346. return func(op *Op) { op.countOnly = true }
  347. }
  348. // WithMinModRev filters out keys for Get with modification revisions less than the given revision.
  349. func WithMinModRev(rev int64) OpOption { return func(op *Op) { op.minModRev = rev } }
  350. // WithMaxModRev filters out keys for Get with modification revisions greater than the given revision.
  351. func WithMaxModRev(rev int64) OpOption { return func(op *Op) { op.maxModRev = rev } }
  352. // WithMinCreateRev filters out keys for Get with creation revisions less than the given revision.
  353. func WithMinCreateRev(rev int64) OpOption { return func(op *Op) { op.minCreateRev = rev } }
  354. // WithMaxCreateRev filters out keys for Get with creation revisions greater than the given revision.
  355. func WithMaxCreateRev(rev int64) OpOption { return func(op *Op) { op.maxCreateRev = rev } }
  356. // WithFirstCreate gets the key with the oldest creation revision in the request range.
  357. func WithFirstCreate() []OpOption { return withTop(SortByCreateRevision, SortAscend) }
  358. // WithLastCreate gets the key with the latest creation revision in the request range.
  359. func WithLastCreate() []OpOption { return withTop(SortByCreateRevision, SortDescend) }
  360. // WithFirstKey gets the lexically first key in the request range.
  361. func WithFirstKey() []OpOption { return withTop(SortByKey, SortAscend) }
  362. // WithLastKey gets the lexically last key in the request range.
  363. func WithLastKey() []OpOption { return withTop(SortByKey, SortDescend) }
  364. // WithFirstRev gets the key with the oldest modification revision in the request range.
  365. func WithFirstRev() []OpOption { return withTop(SortByModRevision, SortAscend) }
  366. // WithLastRev gets the key with the latest modification revision in the request range.
  367. func WithLastRev() []OpOption { return withTop(SortByModRevision, SortDescend) }
  368. // withTop gets the first key over the get's prefix given a sort order
  369. func withTop(target SortTarget, order SortOrder) []OpOption {
  370. return []OpOption{WithPrefix(), WithSort(target, order), WithLimit(1)}
  371. }
  372. // WithProgressNotify makes watch server send periodic progress updates
  373. // every 10 minutes when there is no incoming events.
  374. // Progress updates have zero events in WatchResponse.
  375. func WithProgressNotify() OpOption {
  376. return func(op *Op) {
  377. op.progressNotify = true
  378. }
  379. }
  380. // WithCreatedNotify makes watch server sends the created event.
  381. func WithCreatedNotify() OpOption {
  382. return func(op *Op) {
  383. op.createdNotify = true
  384. }
  385. }
  386. // WithFilterPut discards PUT events from the watcher.
  387. func WithFilterPut() OpOption {
  388. return func(op *Op) { op.filterPut = true }
  389. }
  390. // WithFilterDelete discards DELETE events from the watcher.
  391. func WithFilterDelete() OpOption {
  392. return func(op *Op) { op.filterDelete = true }
  393. }
  394. // WithPrevKV gets the previous key-value pair before the event happens. If the previous KV is already compacted,
  395. // nothing will be returned.
  396. func WithPrevKV() OpOption {
  397. return func(op *Op) {
  398. op.prevKV = true
  399. }
  400. }
  401. // WithIgnoreValue updates the key using its current value.
  402. // Empty value should be passed when ignore_value is set.
  403. // Returns an error if the key does not exist.
  404. func WithIgnoreValue() OpOption {
  405. return func(op *Op) {
  406. op.ignoreValue = true
  407. }
  408. }
  409. // WithIgnoreLease updates the key using its current lease.
  410. // Empty lease should be passed when ignore_lease is set.
  411. // Returns an error if the key does not exist.
  412. func WithIgnoreLease() OpOption {
  413. return func(op *Op) {
  414. op.ignoreLease = true
  415. }
  416. }
  417. // LeaseOp represents an Operation that lease can execute.
  418. type LeaseOp struct {
  419. id LeaseID
  420. // for TimeToLive
  421. attachedKeys bool
  422. }
  423. // LeaseOption configures lease operations.
  424. type LeaseOption func(*LeaseOp)
  425. func (op *LeaseOp) applyOpts(opts []LeaseOption) {
  426. for _, opt := range opts {
  427. opt(op)
  428. }
  429. }
  430. // WithAttachedKeys requests lease timetolive API to return
  431. // attached keys of given lease ID.
  432. func WithAttachedKeys() LeaseOption {
  433. return func(op *LeaseOp) { op.attachedKeys = true }
  434. }
  435. func toLeaseTimeToLiveRequest(id LeaseID, opts ...LeaseOption) *pb.LeaseTimeToLiveRequest {
  436. ret := &LeaseOp{id: id}
  437. ret.applyOpts(opts)
  438. return &pb.LeaseTimeToLiveRequest{ID: int64(id), Keys: ret.attachedKeys}
  439. }