comparator.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  4. #ifndef STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
  5. #define STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_
  6. #include <string>
  7. #include "leveldb/export.h"
  8. namespace leveldb {
  9. class Slice;
  10. // A Comparator object provides a total order across slices that are
  11. // used as keys in an sstable or a database. A Comparator implementation
  12. // must be thread-safe since leveldb may invoke its methods concurrently
  13. // from multiple threads.
  14. class LEVELDB_EXPORT Comparator {
  15. public:
  16. virtual ~Comparator();
  17. // Three-way comparison. Returns value:
  18. // < 0 iff "a" < "b",
  19. // == 0 iff "a" == "b",
  20. // > 0 iff "a" > "b"
  21. virtual int Compare(const Slice& a, const Slice& b) const = 0;
  22. // The name of the comparator. Used to check for comparator
  23. // mismatches (i.e., a DB created with one comparator is
  24. // accessed using a different comparator.
  25. //
  26. // The client of this package should switch to a new name whenever
  27. // the comparator implementation changes in a way that will cause
  28. // the relative ordering of any two keys to change.
  29. //
  30. // Names starting with "leveldb." are reserved and should not be used
  31. // by any clients of this package.
  32. virtual const char* Name() const = 0;
  33. // Advanced functions: these are used to reduce the space requirements
  34. // for internal data structures like index blocks.
  35. // If *start < limit, changes *start to a short string in [start,limit).
  36. // Simple comparator implementations may return with *start unchanged,
  37. // i.e., an implementation of this method that does nothing is correct.
  38. virtual void FindShortestSeparator(
  39. std::string* start,
  40. const Slice& limit) const = 0;
  41. // Changes *key to a short string >= *key.
  42. // Simple comparator implementations may return with *key unchanged,
  43. // i.e., an implementation of this method that does nothing is correct.
  44. virtual void FindShortSuccessor(std::string* key) const = 0;
  45. };
  46. // Return a builtin comparator that uses lexicographic byte-wise
  47. // ordering. The result remains the property of this module and
  48. // must not be deleted.
  49. LEVELDB_EXPORT const Comparator* BytewiseComparator();
  50. } // namespace leveldb
  51. #endif // STORAGE_LEVELDB_INCLUDE_COMPARATOR_H_