]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
2 | // This source code is licensed under both the GPLv2 (found in the |
3 | // COPYING file in the root directory) and Apache 2.0 License | |
4 | // (found in the LICENSE.Apache file in the root directory). | |
7c673cae FG |
5 | // |
6 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. | |
7 | // Use of this source code is governed by a BSD-style license that can be | |
8 | // found in the LICENSE file. See the AUTHORS file for names of contributors. | |
9 | ||
10 | #include <algorithm> | |
11 | #include <memory> | |
12 | #include <stdint.h> | |
13 | #include "rocksdb/comparator.h" | |
14 | #include "rocksdb/slice.h" | |
15 | #include "port/port.h" | |
16 | #include "util/logging.h" | |
17 | ||
18 | namespace rocksdb { | |
19 | ||
7c673cae FG |
20 | namespace { |
21 | class BytewiseComparatorImpl : public Comparator { | |
22 | public: | |
23 | BytewiseComparatorImpl() { } | |
24 | ||
494da23a | 25 | const char* Name() const override { return "leveldb.BytewiseComparator"; } |
7c673cae | 26 | |
494da23a | 27 | int Compare(const Slice& a, const Slice& b) const override { |
7c673cae FG |
28 | return a.compare(b); |
29 | } | |
30 | ||
494da23a | 31 | bool Equal(const Slice& a, const Slice& b) const override { return a == b; } |
7c673cae | 32 | |
494da23a TL |
33 | void FindShortestSeparator(std::string* start, |
34 | const Slice& limit) const override { | |
7c673cae FG |
35 | // Find length of common prefix |
36 | size_t min_length = std::min(start->size(), limit.size()); | |
37 | size_t diff_index = 0; | |
38 | while ((diff_index < min_length) && | |
39 | ((*start)[diff_index] == limit[diff_index])) { | |
40 | diff_index++; | |
41 | } | |
42 | ||
43 | if (diff_index >= min_length) { | |
44 | // Do not shorten if one string is a prefix of the other | |
45 | } else { | |
46 | uint8_t start_byte = static_cast<uint8_t>((*start)[diff_index]); | |
47 | uint8_t limit_byte = static_cast<uint8_t>(limit[diff_index]); | |
11fdf7f2 | 48 | if (start_byte >= limit_byte) { |
7c673cae FG |
49 | // Cannot shorten since limit is smaller than start or start is |
50 | // already the shortest possible. | |
51 | return; | |
52 | } | |
53 | assert(start_byte < limit_byte); | |
54 | ||
55 | if (diff_index < limit.size() - 1 || start_byte + 1 < limit_byte) { | |
56 | (*start)[diff_index]++; | |
57 | start->resize(diff_index + 1); | |
58 | } else { | |
59 | // v | |
60 | // A A 1 A A A | |
61 | // A A 2 | |
62 | // | |
63 | // Incrementing the current byte will make start bigger than limit, we | |
64 | // will skip this byte, and find the first non 0xFF byte in start and | |
65 | // increment it. | |
66 | diff_index++; | |
67 | ||
68 | while (diff_index < start->size()) { | |
69 | // Keep moving until we find the first non 0xFF byte to | |
70 | // increment it | |
71 | if (static_cast<uint8_t>((*start)[diff_index]) < | |
72 | static_cast<uint8_t>(0xff)) { | |
73 | (*start)[diff_index]++; | |
74 | start->resize(diff_index + 1); | |
75 | break; | |
76 | } | |
77 | diff_index++; | |
78 | } | |
79 | } | |
80 | assert(Compare(*start, limit) < 0); | |
81 | } | |
82 | } | |
83 | ||
494da23a | 84 | void FindShortSuccessor(std::string* key) const override { |
7c673cae FG |
85 | // Find first character that can be incremented |
86 | size_t n = key->size(); | |
87 | for (size_t i = 0; i < n; i++) { | |
88 | const uint8_t byte = (*key)[i]; | |
89 | if (byte != static_cast<uint8_t>(0xff)) { | |
90 | (*key)[i] = byte + 1; | |
91 | key->resize(i+1); | |
92 | return; | |
93 | } | |
94 | } | |
95 | // *key is a run of 0xffs. Leave it alone. | |
96 | } | |
11fdf7f2 | 97 | |
494da23a TL |
98 | bool IsSameLengthImmediateSuccessor(const Slice& s, |
99 | const Slice& t) const override { | |
11fdf7f2 TL |
100 | if (s.size() != t.size() || s.size() == 0) { |
101 | return false; | |
102 | } | |
103 | size_t diff_ind = s.difference_offset(t); | |
104 | // same slice | |
105 | if (diff_ind >= s.size()) return false; | |
106 | uint8_t byte_s = static_cast<uint8_t>(s[diff_ind]); | |
107 | uint8_t byte_t = static_cast<uint8_t>(t[diff_ind]); | |
108 | // first different byte must be consecutive, and remaining bytes must be | |
109 | // 0xff for s and 0x00 for t | |
110 | if (byte_s != uint8_t{0xff} && byte_s + 1 == byte_t) { | |
111 | for (size_t i = diff_ind + 1; i < s.size(); ++i) { | |
112 | byte_s = static_cast<uint8_t>(s[i]); | |
113 | byte_t = static_cast<uint8_t>(t[i]); | |
114 | if (byte_s != uint8_t{0xff} || byte_t != uint8_t{0x00}) { | |
115 | return false; | |
116 | } | |
117 | } | |
118 | return true; | |
119 | } else { | |
120 | return false; | |
121 | } | |
122 | } | |
123 | ||
494da23a | 124 | bool CanKeysWithDifferentByteContentsBeEqual() const override { |
11fdf7f2 TL |
125 | return false; |
126 | } | |
7c673cae FG |
127 | }; |
128 | ||
129 | class ReverseBytewiseComparatorImpl : public BytewiseComparatorImpl { | |
130 | public: | |
131 | ReverseBytewiseComparatorImpl() { } | |
132 | ||
494da23a | 133 | const char* Name() const override { |
7c673cae FG |
134 | return "rocksdb.ReverseBytewiseComparator"; |
135 | } | |
136 | ||
494da23a | 137 | int Compare(const Slice& a, const Slice& b) const override { |
7c673cae FG |
138 | return -a.compare(b); |
139 | } | |
7c673cae | 140 | |
11fdf7f2 TL |
141 | void FindShortestSeparator(std::string* start, |
142 | const Slice& limit) const override { | |
143 | // Find length of common prefix | |
144 | size_t min_length = std::min(start->size(), limit.size()); | |
145 | size_t diff_index = 0; | |
146 | while ((diff_index < min_length) && | |
147 | ((*start)[diff_index] == limit[diff_index])) { | |
148 | diff_index++; | |
149 | } | |
150 | ||
151 | assert(diff_index <= min_length); | |
152 | if (diff_index == min_length) { | |
153 | // Do not shorten if one string is a prefix of the other | |
154 | // | |
155 | // We could handle cases like: | |
156 | // V | |
157 | // A A 2 X Y | |
158 | // A A 2 | |
159 | // in a similar way as BytewiseComparator::FindShortestSeparator(). | |
160 | // We keep it simple by not implementing it. We can come back to it | |
161 | // later when needed. | |
162 | } else { | |
163 | uint8_t start_byte = static_cast<uint8_t>((*start)[diff_index]); | |
164 | uint8_t limit_byte = static_cast<uint8_t>(limit[diff_index]); | |
165 | if (start_byte > limit_byte && diff_index < start->size() - 1) { | |
166 | // Case like | |
167 | // V | |
168 | // A A 3 A A | |
169 | // A A 1 B B | |
170 | // | |
171 | // or | |
172 | // v | |
173 | // A A 2 A A | |
174 | // A A 1 B B | |
175 | // In this case "AA2" will be good. | |
176 | #ifndef NDEBUG | |
177 | std::string old_start = *start; | |
178 | #endif | |
179 | start->resize(diff_index + 1); | |
180 | #ifndef NDEBUG | |
181 | assert(old_start >= *start); | |
182 | #endif | |
183 | assert(Slice(*start).compare(limit) > 0); | |
184 | } | |
185 | } | |
186 | } | |
187 | ||
188 | void FindShortSuccessor(std::string* /*key*/) const override { | |
189 | // Don't do anything for simplicity. | |
190 | } | |
191 | ||
494da23a | 192 | bool CanKeysWithDifferentByteContentsBeEqual() const override { |
11fdf7f2 TL |
193 | return false; |
194 | } | |
195 | }; | |
7c673cae FG |
196 | }// namespace |
197 | ||
198 | const Comparator* BytewiseComparator() { | |
199 | static BytewiseComparatorImpl bytewise; | |
200 | return &bytewise; | |
201 | } | |
202 | ||
203 | const Comparator* ReverseBytewiseComparator() { | |
204 | static ReverseBytewiseComparatorImpl rbytewise; | |
205 | return &rbytewise; | |
206 | } | |
207 | ||
208 | } // namespace rocksdb |