/bitcoin/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 | 166k | Comparator::~Comparator() = default; |
19 | | |
20 | | namespace { |
21 | | class BytewiseComparatorImpl : public Comparator { |
22 | | public: |
23 | 11.0k | BytewiseComparatorImpl() = default; |
24 | | |
25 | 99.8k | const char* Name() const override { return "leveldb.BytewiseComparator"; } |
26 | | |
27 | 74.7M | int Compare(const Slice& a, const Slice& b) const override { |
28 | 74.7M | return a.compare(b); |
29 | 74.7M | } |
30 | | |
31 | | void FindShortestSeparator(std::string* start, |
32 | 601 | const Slice& limit) const override { |
33 | | // Find length of common prefix |
34 | 601 | size_t min_length = std::min(start->size(), limit.size()); |
35 | 601 | size_t diff_index = 0; |
36 | 1.66k | while ((diff_index < min_length) && Branch (36:12): [True: 1.18k, False: 476]
|
37 | 1.66k | ((*start)[diff_index] == limit[diff_index])) { Branch (37:12): [True: 1.06k, False: 125]
|
38 | 1.06k | diff_index++; |
39 | 1.06k | } |
40 | | |
41 | 601 | if (diff_index >= min_length) { Branch (41:9): [True: 476, False: 125]
|
42 | | // Do not shorten if one string is a prefix of the other |
43 | 476 | } else { |
44 | 125 | uint8_t diff_byte = static_cast<uint8_t>((*start)[diff_index]); |
45 | 125 | if (diff_byte < static_cast<uint8_t>(0xff) && Branch (45:11): [True: 125, False: 0]
|
46 | 125 | diff_byte + 1 < static_cast<uint8_t>(limit[diff_index])) { Branch (46:11): [True: 4, False: 121]
|
47 | 4 | (*start)[diff_index]++; |
48 | 4 | start->resize(diff_index + 1); |
49 | 4 | assert(Compare(*start, limit) < 0); Branch (49:9): [True: 4, False: 0]
|
50 | 4 | } |
51 | 125 | } |
52 | 601 | } |
53 | | |
54 | 24 | void FindShortSuccessor(std::string* key) const override { |
55 | | // Find first character that can be incremented |
56 | 24 | size_t n = key->size(); |
57 | 24 | for (size_t i = 0; i < n; i++) { Branch (57:24): [True: 24, False: 0]
|
58 | 24 | const uint8_t byte = (*key)[i]; |
59 | 24 | if (byte != static_cast<uint8_t>(0xff)) { Branch (59:11): [True: 24, False: 0]
|
60 | 24 | (*key)[i] = byte + 1; |
61 | 24 | key->resize(i + 1); |
62 | 24 | return; |
63 | 24 | } |
64 | 24 | } |
65 | | // *key is a run of 0xffs. Leave it alone. |
66 | 24 | } |
67 | | }; |
68 | | } // namespace |
69 | | |
70 | 66.6k | const Comparator* BytewiseComparator() { |
71 | 66.6k | static NoDestructor<BytewiseComparatorImpl> singleton; |
72 | 66.6k | return singleton.get(); |
73 | 66.6k | } |
74 | | |
75 | | } // namespace leveldb |