YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/comparator.cc
Line
Count
Source (jump to first uncovered line)
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
//
6
// The following only applies to changes made to this file as part of YugaByte development.
7
//
8
// Portions Copyright (c) YugaByte, Inc.
9
//
10
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
11
// in compliance with the License.  You may obtain a copy of the License at
12
//
13
// http://www.apache.org/licenses/LICENSE-2.0
14
//
15
// Unless required by applicable law or agreed to in writing, software distributed under the License
16
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
17
// or implied.  See the License for the specific language governing permissions and limitations
18
// under the License.
19
//
20
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
21
// Use of this source code is governed by a BSD-style license that can be
22
// found in the LICENSE file. See the AUTHORS file for names of contributors.
23
#include <stdint.h>
24
25
#include <memory>
26
27
#include <glog/logging.h>
28
29
#include "yb/rocksdb/comparator.h"
30
#include "yb/util/slice.h"
31
32
namespace rocksdb {
33
34
1.69M
Comparator::~Comparator() { }
35
36
namespace {
37
class BytewiseComparatorImpl : public Comparator {
38
 public:
39
15.3k
  BytewiseComparatorImpl() { }
40
41
4.16M
  const char* Name() const override {
42
4.16M
    return "leveldb.BytewiseComparator";
43
4.16M
  }
44
45
29.2G
  int Compare(const Slice& a, const Slice& b) const override {
46
29.2G
    return a.compare(b);
47
29.2G
  }
48
49
263M
  bool Equal(const Slice& a, const Slice& b) const override {
50
263M
    return a == b;
51
263M
  }
52
53
2.79M
  virtual void FindShortestSeparator(std::string* start, const Slice& limit) const override {
54
2.79M
    const uint8_t* start_bytes = pointer_cast<const uint8_t*>(start->data());
55
2.79M
    const uint8_t* limit_bytes = limit.data();
56
    // Find length of common prefix
57
2.79M
    size_t min_length = std::min(start->size(), limit.size());
58
2.79M
    size_t diff_index = 0;
59
135M
    while ((diff_index < min_length) &&
60
135M
           
(start_bytes[diff_index] == limit_bytes[diff_index])135M
) {
61
132M
      diff_index++;
62
132M
    }
63
64
2.79M
    if (diff_index >= min_length) {
65
      // Do not shorten if one string is a prefix of the other
66
2.78M
    } else {
67
2.78M
      uint8_t start_byte = start_bytes[diff_index];
68
2.78M
      uint8_t limit_byte = limit_bytes[diff_index];
69
2.78M
      if (start_byte > limit_byte) {
70
        // Cannot shorten since limit is smaller than start.
71
2
        return;
72
2
      }
73
2.78M
      DCHECK_LT(start_byte, limit_byte);
74
75
2.78M
      if (diff_index == limit.size() - 1 && 
start_byte + 1 == limit_byte1.25M
) {
76
        //     v
77
        // A A 1 A A A
78
        // A A 2
79
        //
80
        // Incrementing the current byte will make start bigger than limit, we
81
        // will skip this byte, and find the first non 0xFF byte in start and
82
        // increment it.
83
1.15M
        ++diff_index;
84
1.15M
        while (diff_index < start->size() && 
start_bytes[diff_index] == 0xffU7.77k
)
{ ++diff_index; }764
85
1.15M
        if (diff_index == start->size()) {
86
1.14M
          return;
87
1.14M
        }
88
1.15M
      }
89
1.64M
      (*start)[diff_index]++;
90
1.64M
      start->resize(diff_index + 1);
91
1.64M
      DCHECK_LT(Compare(*start, limit), 0);
92
1.64M
    }
93
2.79M
  }
94
95
76.7k
  void FindShortSuccessor(std::string* key) const override {
96
    // Find first character that can be incremented
97
76.7k
    size_t n = key->size();
98
86.3k
    for (size_t i = 0; i < n; 
i++9.59k
) {
99
85.4k
      const uint8_t byte = (*key)[i];
100
85.4k
      if (byte != static_cast<uint8_t>(0xff)) {
101
75.8k
        (*key)[i] = byte + 1;
102
75.8k
        key->resize(i+1);
103
75.8k
        return;
104
75.8k
      }
105
85.4k
    }
106
    // *key is a run of 0xffs.  Leave it alone.
107
76.7k
  }
108
};
109
110
class ReverseBytewiseComparatorImpl : public BytewiseComparatorImpl {
111
 public:
112
3
  ReverseBytewiseComparatorImpl() { }
113
114
0
  const char* Name() const override {
115
0
    return "rocksdb.ReverseBytewiseComparator";
116
0
  }
117
118
87
  int Compare(const Slice& a, const Slice& b) const override {
119
87
    return -a.compare(b);
120
87
  }
121
};
122
123
}  // namespace
124
125
138M
const ComparatorPtr& SharedBytewiseComparator() {
126
  // Comparator should be shared ptr, because we use shared_from_this to store it in index reader.
127
138M
  static ComparatorPtr bytewise = std::make_shared<BytewiseComparatorImpl>();
128
138M
  return bytewise;
129
138M
}
130
131
138M
const Comparator* BytewiseComparator() {
132
  // Comparator should be shared ptr, because we use shared_from_this to store it in index reader.
133
138M
  return SharedBytewiseComparator().get();
134
138M
}
135
136
4
const Comparator* ReverseBytewiseComparator() {
137
4
  static ComparatorPtr rbytewise = std::make_shared<ReverseBytewiseComparatorImpl>();
138
4
  return rbytewise.get();
139
4
}
140
141
class Uint64ComparatorImpl : public Comparator {
142
 public:
143
1
  Uint64ComparatorImpl() { }
144
145
76
  const char* Name() const override {
146
76
    return "rocksdb.Uint64Comparator";
147
76
  }
148
149
514k
  int Compare(const Slice& a, const Slice& b) const override {
150
514k
    assert(a.size() == sizeof(uint64_t) && b.size() == sizeof(uint64_t));
151
0
    const uint64_t* left = reinterpret_cast<const uint64_t*>(a.data());
152
514k
    const uint64_t* right = reinterpret_cast<const uint64_t*>(b.data());
153
514k
    if (*left == *right) {
154
81.5k
      return 0;
155
432k
    } else if (*left < *right) {
156
300k
      return -1;
157
300k
    } else {
158
131k
      return 1;
159
131k
    }
160
514k
  }
161
162
  virtual void FindShortestSeparator(std::string* start,
163
0
      const Slice& limit) const override {
164
0
    return;
165
0
  }
166
167
45
  void FindShortSuccessor(std::string* key) const override {
168
45
    return;
169
45
  }
170
};
171
172
1
const Comparator* Uint64Comparator() {
173
1
  static ComparatorPtr uint64comparator = std::make_shared<Uint64ComparatorImpl>();
174
1
  return uint64comparator.get();
175
1
}
176
177
}  // namespace rocksdb