revision.go 1.4 KB

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