op.go 2.5 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 OpRange(key, end string, limit, rev int64, sort *SortOption) Op {
  62. return Op{
  63. t: tRange,
  64. key: []byte(key),
  65. end: []byte(end),
  66. limit: limit,
  67. rev: rev,
  68. sort: sort,
  69. }
  70. }
  71. func OpGet(key string, rev int64) Op {
  72. return Op{
  73. t: tRange,
  74. key: []byte(key),
  75. rev: rev,
  76. }
  77. }
  78. func OpDeleteRange(key, end string) Op {
  79. return Op{
  80. t: tDeleteRange,
  81. key: []byte(key),
  82. end: []byte(end),
  83. }
  84. }
  85. func OpDelete(key string) Op {
  86. return Op{
  87. t: tDeleteRange,
  88. key: []byte(key),
  89. }
  90. }
  91. func OpPut(key, val string, leaseID lease.LeaseID) Op {
  92. return Op{
  93. t: tPut,
  94. key: []byte(key),
  95. val: []byte(val),
  96. leaseID: leaseID,
  97. }
  98. }