op.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. sort *SortOption
  34. serializable bool
  35. // for range, watch
  36. rev int64
  37. // for put
  38. val []byte
  39. leaseID lease.LeaseID
  40. }
  41. func (op Op) toRequestUnion() *pb.RequestUnion {
  42. switch op.t {
  43. case tRange:
  44. r := &pb.RangeRequest{Key: op.key, RangeEnd: op.end, Limit: op.limit, Revision: op.rev}
  45. if op.sort != nil {
  46. r.SortOrder = pb.RangeRequest_SortOrder(op.sort.Order)
  47. r.SortTarget = pb.RangeRequest_SortTarget(op.sort.Target)
  48. }
  49. return &pb.RequestUnion{Request: &pb.RequestUnion_RequestRange{RequestRange: r}}
  50. case tPut:
  51. r := &pb.PutRequest{Key: op.key, Value: op.val, Lease: int64(op.leaseID)}
  52. return &pb.RequestUnion{Request: &pb.RequestUnion_RequestPut{RequestPut: r}}
  53. case tDeleteRange:
  54. r := &pb.DeleteRangeRequest{Key: op.key, RangeEnd: op.end}
  55. return &pb.RequestUnion{Request: &pb.RequestUnion_RequestDeleteRange{RequestDeleteRange: r}}
  56. default:
  57. panic("Unknown Op")
  58. }
  59. }
  60. func (op Op) isWrite() bool {
  61. return op.t != tRange
  62. }
  63. func OpGet(key string, opts ...OpOption) Op {
  64. ret := Op{t: tRange, key: []byte(key)}
  65. ret.applyOpts(opts)
  66. return ret
  67. }
  68. func OpDelete(key string, opts ...OpOption) Op {
  69. ret := Op{t: tDeleteRange, key: []byte(key)}
  70. ret.applyOpts(opts)
  71. switch {
  72. case ret.leaseID != 0:
  73. panic("unexpected lease in delete")
  74. case ret.limit != 0:
  75. panic("unexpected limit in delete")
  76. case ret.rev != 0:
  77. panic("unexpected revision in delete")
  78. case ret.sort != nil:
  79. panic("unexpected sort in delete")
  80. case ret.serializable != false:
  81. panic("unexpected serializable in delete")
  82. }
  83. return ret
  84. }
  85. func OpPut(key, val string, opts ...OpOption) Op {
  86. ret := Op{t: tPut, key: []byte(key), val: []byte(val)}
  87. ret.applyOpts(opts)
  88. switch {
  89. case ret.end != nil:
  90. panic("unexpected range in put")
  91. case ret.limit != 0:
  92. panic("unexpected limit in put")
  93. case ret.rev != 0:
  94. panic("unexpected revision in put")
  95. case ret.sort != nil:
  96. panic("unexpected sort in put")
  97. case ret.serializable != false:
  98. panic("unexpected serializable in delete")
  99. }
  100. return ret
  101. }
  102. func opWatch(key string, opts ...OpOption) Op {
  103. ret := Op{t: tRange, key: []byte(key)}
  104. ret.applyOpts(opts)
  105. switch {
  106. case ret.leaseID != 0:
  107. panic("unexpected lease in watch")
  108. case ret.limit != 0:
  109. panic("unexpected limit in watch")
  110. case ret.sort != nil:
  111. panic("unexpected sort in watch")
  112. case ret.serializable != false:
  113. panic("unexpected serializable in watch")
  114. }
  115. return ret
  116. }
  117. func (op *Op) applyOpts(opts []OpOption) {
  118. for _, opt := range opts {
  119. opt(op)
  120. }
  121. }
  122. // OpOption configures Operations like Get, Put, Delete.
  123. type OpOption func(*Op)
  124. // WithLease attaches a lease ID to a key in 'Put' request.
  125. func WithLease(leaseID lease.LeaseID) OpOption {
  126. return func(op *Op) { op.leaseID = leaseID }
  127. }
  128. // WithLimit limits the number of results to return from 'Get' request.
  129. func WithLimit(n int64) OpOption { return func(op *Op) { op.limit = n } }
  130. // WithRev specifies the store revision for 'Get' request.
  131. // Or the start revision of 'Watch' request.
  132. func WithRev(rev int64) OpOption { return func(op *Op) { op.rev = rev } }
  133. // WithSort specifies the ordering in 'Get' request. It requires
  134. // 'WithRange' and/or 'WithPrefix' to be specified too.
  135. // 'target' specifies the target to sort by: key, version, revisions, value.
  136. // 'order' can be either 'SortNone', 'SortAscend', 'SortDescend'.
  137. func WithSort(target SortTarget, order SortOrder) OpOption {
  138. return func(op *Op) {
  139. op.sort = &SortOption{target, order}
  140. }
  141. }
  142. func getPrefix(key []byte) []byte {
  143. end := make([]byte, len(key))
  144. copy(end, key)
  145. for i := len(end) - 1; i >= 0; i-- {
  146. if end[i] < 0xff {
  147. end[i] = end[i] + 1
  148. end = end[:i+1]
  149. return end
  150. }
  151. }
  152. // next prefix does not exist (e.g., 0xffff);
  153. // default to WithFromKey policy
  154. end = []byte{0}
  155. return end
  156. }
  157. // WithPrefix enables 'Get', 'Delete', or 'Watch' requests to operate
  158. // on the keys with matching prefix. For example, 'Get(foo, WithPrefix())'
  159. // can return 'foo1', 'foo2', and so on.
  160. func WithPrefix() OpOption {
  161. return func(op *Op) {
  162. op.end = getPrefix(op.key)
  163. }
  164. }
  165. // WithRange specifies the range of 'Get' or 'Delete' requests.
  166. // For example, 'Get' requests with 'WithRange(end)' returns
  167. // the keys in the range [key, end).
  168. func WithRange(endKey string) OpOption {
  169. return func(op *Op) { op.end = []byte(endKey) }
  170. }
  171. // WithFromKey specifies the range of 'Get' or 'Delete' requests
  172. // to be equal or greater than they key in the argument.
  173. func WithFromKey() OpOption { return WithRange("\x00") }
  174. // WithSerializable makes 'Get' request serializable. By default,
  175. // it's linearizable. Serializable requests are better for lower latency
  176. // requirement.
  177. func WithSerializable() OpOption {
  178. return func(op *Op) { op.serializable = true }
  179. }
  180. // WithFirstCreate gets the key with the oldest creation revision in the request range.
  181. func WithFirstCreate() []OpOption { return withTop(SortByCreatedRev, SortAscend) }
  182. // WithLastCreate gets the key with the latest creation revision in the request range.
  183. func WithLastCreate() []OpOption { return withTop(SortByCreatedRev, SortDescend) }
  184. // WithFirstKey gets the lexically first key in the request range.
  185. func WithFirstKey() []OpOption { return withTop(SortByKey, SortAscend) }
  186. // WithLastKey gets the lexically last key in the request range.
  187. func WithLastKey() []OpOption { return withTop(SortByKey, SortDescend) }
  188. // WithFirstRev gets the key with the oldest modification revision in the request range.
  189. func WithFirstRev() []OpOption { return withTop(SortByModifiedRev, SortAscend) }
  190. // WithLastRev gets the key with the latest modification revision in the request range.
  191. func WithLastRev() []OpOption { return withTop(SortByModifiedRev, SortDescend) }
  192. // withTop gets the first key over the get's prefix given a sort order
  193. func withTop(target SortTarget, order SortOrder) []OpOption {
  194. return []OpOption{WithPrefix(), WithSort(target, order), WithLimit(1)}
  195. }