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