op.go 2.4 KB

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