YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/comparator.h
Line
Count
Source
1
// Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
// This source code is licensed under the BSD-style license found in the
3
// LICENSE file in the root directory of this source tree. An additional grant
4
// of patent rights can be found in the PATENTS file in the same 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.
8
//
9
// The following only applies to changes made to this file as part of YugaByte development.
10
//
11
// Portions Copyright (c) YugaByte, Inc.
12
//
13
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
14
// in compliance with the License.  You may obtain a copy of the License at
15
//
16
// http://www.apache.org/licenses/LICENSE-2.0
17
//
18
// Unless required by applicable law or agreed to in writing, software distributed under the License
19
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
20
// or implied.  See the License for the specific language governing permissions and limitations
21
// under the License.
22
//
23
24
#ifndef YB_ROCKSDB_COMPARATOR_H
25
#define YB_ROCKSDB_COMPARATOR_H
26
27
#include <memory>
28
#include <string>
29
30
#include "yb/util/slice.h"
31
32
namespace rocksdb {
33
34
// A Comparator object provides a total order across slices that are
35
// used as keys in an sstable or a database.  A Comparator implementation
36
// must be thread-safe since rocksdb may invoke its methods concurrently
37
// from multiple threads.
38
class Comparator {
39
 public:
40
  virtual ~Comparator();
41
42
  // Three-way comparison.  Returns value:
43
  //   < 0 iff "a" < "b",
44
  //   == 0 iff "a" == "b",
45
  //   > 0 iff "a" > "b"
46
  virtual int Compare(const Slice& a, const Slice& b) const = 0;
47
48
  // Compares two slices for equality. The following invariant should always
49
  // hold (and is the default implementation):
50
  //   Equal(a, b) iff Compare(a, b) == 0
51
  // Overwrite only if equality comparisons can be done more efficiently than
52
  // three-way comparisons.
53
1.15M
  virtual bool Equal(const Slice& a, const Slice& b) const {
54
1.15M
    return Compare(a, b) == 0;
55
1.15M
  }
56
57
  // The name of the comparator.  Used to check for comparator
58
  // mismatches (i.e., a DB created with one comparator is
59
  // accessed using a different comparator.
60
  //
61
  // The client of this package should switch to a new name whenever
62
  // the comparator implementation changes in a way that will cause
63
  // the relative ordering of any two keys to change.
64
  //
65
  // Names starting with "rocksdb." are reserved and should not be used
66
  // by any clients of this package.
67
  virtual const char* Name() const = 0;
68
69
  // Advanced functions: these are used to reduce the space requirements
70
  // for internal data structures like index blocks.
71
72
  // If *start < limit, changes *start to a short string in [start,limit).
73
  // Simple comparator implementations may return with *start unchanged,
74
  // i.e., an implementation of this method that does nothing is correct.
75
  virtual void FindShortestSeparator(
76
      std::string* start,
77
      const Slice& limit) const = 0;
78
79
  // Changes *key to a short string >= *key.
80
  // Simple comparator implementations may return with *key unchanged,
81
  // i.e., an implementation of this method that does nothing is correct.
82
  virtual void FindShortSuccessor(std::string* key) const = 0;
83
};
84
85
typedef std::shared_ptr<const Comparator> ComparatorPtr;
86
87
// Return a builtin comparator that uses lexicographic byte-wise
88
// ordering.  The result remains the property of this module and
89
// must not be deleted.
90
extern const Comparator* BytewiseComparator();
91
92
extern const ComparatorPtr& SharedBytewiseComparator();
93
94
// Return a builtin comparator that uses reverse lexicographic byte-wise
95
// ordering.
96
extern const Comparator* ReverseBytewiseComparator();
97
98
// Returns a user key comparator that can be used for comparing two uint64_t
99
// slices. Instead of comparing slices byte-wise, it compares all the 8 bytes
100
// at once. Assumes same endian-ness is used though the database's lifetime.
101
// Symantics of comparison would differ from Bytewise comparator in little
102
// endian machines.
103
extern const Comparator* Uint64Comparator();
104
105
}  // namespace rocksdb
106
107
#endif // YB_ROCKSDB_COMPARATOR_H