Coverage Report

Created: 2026-09-13 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/leveldb/util/comparator.cc
Line
Count
Source
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
5
#include "leveldb/comparator.h"
6
7
#include <algorithm>
8
#include <cstdint>
9
#include <string>
10
#include <type_traits>
11
12
#include "leveldb/slice.h"
13
#include "util/logging.h"
14
#include "util/no_destructor.h"
15
16
namespace leveldb {
17
18
730
Comparator::~Comparator() = default;
19
20
namespace {
21
class BytewiseComparatorImpl : public Comparator {
22
 public:
23
1
  BytewiseComparatorImpl() = default;
24
25
265
  const char* Name() const override { return "leveldb.BytewiseComparator"; }
26
27
6.89k
  int Compare(const Slice& a, const Slice& b) const override {
28
6.89k
    return a.compare(b);
29
6.89k
  }
30
31
  void FindShortestSeparator(std::string* start,
32
468
                             const Slice& limit) const override {
33
    // Find length of common prefix
34
468
    size_t min_length = std::min(start->size(), limit.size());
35
468
    size_t diff_index = 0;
36
3.94k
    while ((diff_index < min_length) &&
37
3.94k
           ((*start)[diff_index] == limit[diff_index])) {
38
3.48k
      diff_index++;
39
3.48k
    }
40
41
468
    if (diff_index >= min_length) {
42
      // Do not shorten if one string is a prefix of the other
43
468
    } else {
44
468
      uint8_t diff_byte = static_cast<uint8_t>((*start)[diff_index]);
45
468
      if (diff_byte < static_cast<uint8_t>(0xff) &&
46
468
          diff_byte + 1 < static_cast<uint8_t>(limit[diff_index])) {
47
405
        (*start)[diff_index]++;
48
405
        start->resize(diff_index + 1);
49
405
        assert(Compare(*start, limit) < 0);
50
405
      }
51
468
    }
52
468
  }
53
54
52
  void FindShortSuccessor(std::string* key) const override {
55
    // Find first character that can be incremented
56
52
    size_t n = key->size();
57
76
    for (size_t i = 0; i < n; i++) {
58
75
      const uint8_t byte = (*key)[i];
59
75
      if (byte != static_cast<uint8_t>(0xff)) {
60
51
        (*key)[i] = byte + 1;
61
51
        key->resize(i + 1);
62
51
        return;
63
51
      }
64
75
    }
65
    // *key is a run of 0xffs.  Leave it alone.
66
52
  }
67
};
68
}  // namespace
69
70
196
const Comparator* BytewiseComparator() {
71
196
  static NoDestructor<BytewiseComparatorImpl> singleton;
72
196
  return singleton.get();
73
196
}
74
75
}  // namespace leveldb