revision.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2015 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 storage
  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. type revision struct {
  21. main int64
  22. sub int64
  23. }
  24. func (a revision) GreaterThan(b revision) bool {
  25. if a.main > b.main {
  26. return true
  27. }
  28. if a.main < b.main {
  29. return false
  30. }
  31. return a.sub > b.sub
  32. }
  33. func newRevBytes() []byte {
  34. return make([]byte, revBytesLen, markedRevBytesLen)
  35. }
  36. func revToBytes(rev revision, bytes []byte) {
  37. binary.BigEndian.PutUint64(bytes, uint64(rev.main))
  38. bytes[8] = '_'
  39. binary.BigEndian.PutUint64(bytes[9:], uint64(rev.sub))
  40. }
  41. func bytesToRev(bytes []byte) revision {
  42. return revision{
  43. main: int64(binary.BigEndian.Uint64(bytes[0:8])),
  44. sub: int64(binary.BigEndian.Uint64(bytes[9:])),
  45. }
  46. }
  47. type revisions []revision
  48. func (a revisions) Len() int { return len(a) }
  49. func (a revisions) Less(i, j int) bool { return a[j].GreaterThan(a[i]) }
  50. func (a revisions) Swap(i, j int) { a[i], a[j] = a[j], a[i] }