comparer.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 comparer provides interface and implementation for ordering
  7. // sets of data.
  8. package comparer
  9. // BasicComparer is the interface that wraps the basic Compare method.
  10. type BasicComparer interface {
  11. // Compare returns -1, 0, or +1 depending on whether a is 'less than',
  12. // 'equal to' or 'greater than' b. The two arguments can only be 'equal'
  13. // if their contents are exactly equal. Furthermore, the empty slice
  14. // must be 'less than' any non-empty slice.
  15. Compare(a, b []byte) int
  16. }
  17. // Comparer defines a total ordering over the space of []byte keys: a 'less
  18. // than' relationship.
  19. type Comparer interface {
  20. BasicComparer
  21. // Name returns name of the comparer.
  22. //
  23. // The Level-DB on-disk format stores the comparer name, and opening a
  24. // database with a different comparer from the one it was created with
  25. // will result in an error.
  26. //
  27. // An implementation to a new name whenever the comparer implementation
  28. // changes in a way that will cause the relative ordering of any two keys
  29. // to change.
  30. //
  31. // Names starting with "leveldb." are reserved and should not be used
  32. // by any users of this package.
  33. Name() string
  34. // Bellow are advanced functions used to reduce the space requirements
  35. // for internal data structures such as index blocks.
  36. // Separator appends a sequence of bytes x to dst such that a <= x && x < b,
  37. // where 'less than' is consistent with Compare. An implementation should
  38. // return nil if x equal to a.
  39. //
  40. // Either contents of a or b should not by any means modified. Doing so
  41. // may cause corruption on the internal state.
  42. Separator(dst, a, b []byte) []byte
  43. // Successor appends a sequence of bytes x to dst such that x >= b, where
  44. // 'less than' is consistent with Compare. An implementation should return
  45. // nil if x equal to b.
  46. //
  47. // Contents of b should not by any means modified. Doing so may cause
  48. // corruption on the internal state.
  49. Successor(dst, b []byte) []byte
  50. }