op.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2016 CoreOS, Inc.
  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 (
  16. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  17. "github.com/coreos/etcd/lease"
  18. )
  19. type opType int
  20. const (
  21. // A default Op has opType 0, which is invalid.
  22. tRange opType = iota + 1
  23. tPut
  24. tDeleteRange
  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. rev int64
  34. sort *SortOption
  35. // for put
  36. val []byte
  37. leaseID lease.LeaseID
  38. }
  39. func (op Op) toRequestUnion() *pb.RequestUnion {
  40. switch op.t {
  41. case tRange:
  42. r := &pb.RangeRequest{Key: op.key, RangeEnd: op.end, Limit: op.limit, Revision: op.rev}
  43. if op.sort != nil {
  44. r.SortOrder = pb.RangeRequest_SortOrder(op.sort.Order)
  45. r.SortTarget = pb.RangeRequest_SortTarget(op.sort.Target)
  46. }
  47. return &pb.RequestUnion{Request: &pb.RequestUnion_RequestRange{RequestRange: r}}
  48. case tPut:
  49. r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID)}
  50. return &pb.RequestUnion{Request: &pb.RequestUnion_RequestPut{RequestPut: r}}
  51. case tDeleteRange:
  52. r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end}
  53. return &pb.RequestUnion{Request: &pb.RequestUnion_RequestDeleteRange{RequestDeleteRange: r}}
  54. default:
  55. panic("Unknown Op")
  56. }
  57. }
  58. func (op Op) isWrite() bool {
  59. return op.t != tRange
  60. }
  61. func OpGet(key string, opts ...OpOption) Op {
  62. ret := Op{t: tRange, key: []byte(key)}
  63. for _, opt := range opts {
  64. opt(&ret)
  65. }
  66. return ret
  67. }
  68. func OpDeleteRange(key, end string) Op {
  69. return Op{
  70. t: tDeleteRange,
  71. key: []byte(key),
  72. end: []byte(end),
  73. }
  74. }
  75. func OpDelete(key string) Op {
  76. return Op{
  77. t: tDeleteRange,
  78. key: []byte(key),
  79. }
  80. }
  81. func OpPut(key, val string, leaseID lease.LeaseID) Op {
  82. return Op{
  83. t: tPut,
  84. key: []byte(key),
  85. val: []byte(val),
  86. leaseID: leaseID,
  87. }
  88. }
  89. type OpOption func(*Op)
  90. func WithLimit(n int64) OpOption { return func(op *Op) { op.limit = n } }
  91. func WithRev(rev int64) OpOption { return func(op *Op) { op.rev = rev } }
  92. func WithSort(tgt SortTarget, order SortOrder) OpOption {
  93. return func(op *Op) {
  94. op.sort = &SortOption{tgt, order}
  95. }
  96. }
  97. func WithRange(endKey string) OpOption {
  98. return func(op *Op) { op.end = []byte(endKey) }
  99. }