]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/include/rocksdb/comparator.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style license that can be
7 // found in the LICENSE file. See the AUTHORS file for names of contributors.
13 #include "rocksdb/rocksdb_namespace.h"
15 namespace ROCKSDB_NAMESPACE
{
19 // A Comparator object provides a total order across slices that are
20 // used as keys in an sstable or a database. A Comparator implementation
21 // must be thread-safe since rocksdb may invoke its methods concurrently
22 // from multiple threads.
25 Comparator() : timestamp_size_(0) {}
27 Comparator(size_t ts_sz
) : timestamp_size_(ts_sz
) {}
29 Comparator(const Comparator
& orig
) : timestamp_size_(orig
.timestamp_size_
) {}
31 Comparator
& operator=(const Comparator
& rhs
) {
33 timestamp_size_
= rhs
.timestamp_size_
;
38 virtual ~Comparator() {}
40 static const char* Type() { return "Comparator"; }
41 // Three-way comparison. Returns value:
43 // == 0 iff "a" == "b",
45 virtual int Compare(const Slice
& a
, const Slice
& b
) const = 0;
47 // Compares two slices for equality. The following invariant should always
48 // hold (and is the default implementation):
49 // Equal(a, b) iff Compare(a, b) == 0
50 // Overwrite only if equality comparisons can be done more efficiently than
51 // three-way comparisons.
52 virtual bool Equal(const Slice
& a
, const Slice
& b
) const {
53 return Compare(a
, b
) == 0;
56 // The name of the comparator. Used to check for comparator
57 // mismatches (i.e., a DB created with one comparator is
58 // accessed using a different comparator.
60 // The client of this package should switch to a new name whenever
61 // the comparator implementation changes in a way that will cause
62 // the relative ordering of any two keys to change.
64 // Names starting with "rocksdb." are reserved and should not be used
65 // by any clients of this package.
66 virtual const char* Name() const = 0;
68 // Advanced functions: these are used to reduce the space requirements
69 // for internal data structures like index blocks.
71 // If *start < limit, changes *start to a short string in [start,limit).
72 // Simple comparator implementations may return with *start unchanged,
73 // i.e., an implementation of this method that does nothing is correct.
74 virtual void FindShortestSeparator(std::string
* start
,
75 const Slice
& limit
) const = 0;
77 // Changes *key to a short string >= *key.
78 // Simple comparator implementations may return with *key unchanged,
79 // i.e., an implementation of this method that does nothing is correct.
80 virtual void FindShortSuccessor(std::string
* key
) const = 0;
82 // if it is a wrapped comparator, may return the root one.
83 // return itself it is not wrapped.
84 virtual const Comparator
* GetRootComparator() const { return this; }
86 // given two keys, determine if t is the successor of s
87 virtual bool IsSameLengthImmediateSuccessor(const Slice
& /*s*/,
88 const Slice
& /*t*/) const {
92 // return true if two keys with different byte sequences can be regarded
93 // as equal by this comparator.
94 // The major use case is to determine if DataBlockHashIndex is compatible
95 // with the customized comparator.
96 virtual bool CanKeysWithDifferentByteContentsBeEqual() const { return true; }
98 inline size_t timestamp_size() const { return timestamp_size_
; }
100 virtual int CompareWithoutTimestamp(const Slice
& a
, const Slice
& b
) const {
101 return Compare(a
, b
);
104 virtual int CompareTimestamp(const Slice
& /*ts1*/,
105 const Slice
& /*ts2*/) const {
110 size_t timestamp_size_
;
113 // Return a builtin comparator that uses lexicographic byte-wise
114 // ordering. The result remains the property of this module and
115 // must not be deleted.
116 extern const Comparator
* BytewiseComparator();
118 // Return a builtin comparator that uses reverse lexicographic byte-wise
120 extern const Comparator
* ReverseBytewiseComparator();
122 } // namespace ROCKSDB_NAMESPACE