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