revision.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2015 The etcd Authors
  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 mvcc
  15. import "encoding/binary"
  16. // revBytesLen is the byte length of a normal revision.
  17. // First 8 bytes is the revision.main in big-endian format. The 9th byte
  18. // is a '_'. The last 8 bytes is the revision.sub in big-endian format.
  19. const revBytesLen = 8 + 1 + 8
  20. // A revision indicates modification of the key-value space.
  21. // The set of changes that share same main revision changes the key-value space atomically.
  22. type revision struct {
  23. // main is the main revision of a set of changes that happen atomically.
  24. main int64
  25. // sub is the sub revision of a change in a set of changes that happen
  26. // atomically. Each change has different increasing sub revision in that
  27. // set.
  28. sub int64
  29. }
  30. func (a revision) GreaterThan(b revision) bool {
  31. if a.main > b.main {
  32. return true
  33. }
  34. if a.main < b.main {
  35. return false
  36. }
  37. return a.sub > b.sub
  38. }
  39. func newRevBytes() []byte {
  40. return make([]byte, revBytesLen, markedRevBytesLen)
  41. }
  42. func revToBytes(rev revision, bytes []byte) {
  43. binary.BigEndian.PutUint64(bytes, uint64(rev.main))
  44. bytes[8] = '_'
  45. binary.BigEndian.PutUint64(bytes[9:], uint64(rev.sub))
  46. }
  47. func bytesToRev(bytes []byte) revision {
  48. return revision{
  49. main: int64(binary.BigEndian.Uint64(bytes[0:8])),
  50. sub: int64(binary.BigEndian.Uint64(bytes[9:])),
  51. }
  52. }
  53. type revisions []revision
  54. func (a revisions) Len() int { return len(a) }
  55. func (a revisions) Less(i, j int) bool { return a[j].GreaterThan(a[i]) }
  56. func (a revisions) Swap(i, j int) { a[i], a[j] = a[j], a[i] }