comparer.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (c) 2012, Suryandaru Triandana <syndtr@gmail.com>
  2. // All rights reserved.
  3. //
  4. // Use of this source code is governed by a BSD-style license that can be
  5. // found in the LICENSE file.
  6. package leveldb
  7. import (
  8. "github.com/syndtr/goleveldb/leveldb/comparer"
  9. )
  10. type iComparer struct {
  11. ucmp comparer.Comparer
  12. }
  13. func (icmp *iComparer) uName() string {
  14. return icmp.ucmp.Name()
  15. }
  16. func (icmp *iComparer) uCompare(a, b []byte) int {
  17. return icmp.ucmp.Compare(a, b)
  18. }
  19. func (icmp *iComparer) uSeparator(dst, a, b []byte) []byte {
  20. return icmp.ucmp.Separator(dst, a, b)
  21. }
  22. func (icmp *iComparer) uSuccessor(dst, b []byte) []byte {
  23. return icmp.ucmp.Successor(dst, b)
  24. }
  25. func (icmp *iComparer) Name() string {
  26. return icmp.uName()
  27. }
  28. func (icmp *iComparer) Compare(a, b []byte) int {
  29. x := icmp.uCompare(internalKey(a).ukey(), internalKey(b).ukey())
  30. if x == 0 {
  31. if m, n := internalKey(a).num(), internalKey(b).num(); m > n {
  32. return -1
  33. } else if m < n {
  34. return 1
  35. }
  36. }
  37. return x
  38. }
  39. func (icmp *iComparer) Separator(dst, a, b []byte) []byte {
  40. ua, ub := internalKey(a).ukey(), internalKey(b).ukey()
  41. dst = icmp.uSeparator(dst, ua, ub)
  42. if dst != nil && len(dst) < len(ua) && icmp.uCompare(ua, dst) < 0 {
  43. // Append earliest possible number.
  44. return append(dst, keyMaxNumBytes...)
  45. }
  46. return nil
  47. }
  48. func (icmp *iComparer) Successor(dst, b []byte) []byte {
  49. ub := internalKey(b).ukey()
  50. dst = icmp.uSuccessor(dst, ub)
  51. if dst != nil && len(dst) < len(ub) && icmp.uCompare(ub, dst) < 0 {
  52. // Append earliest possible number.
  53. return append(dst, keyMaxNumBytes...)
  54. }
  55. return nil
  56. }