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