op.go 7.0 KB

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