| 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 <stdio.h> |
| 6 |
|
| #include "db/dbformat.h" |
| 7 |
|
| #include "port/port.h" |
| 8 |
|
| #include "util/coding.h" |
| 9 |
|
| |
| 10 |
|
| namespace leveldb { |
| 11 |
|
| |
| 12 |
15 |
| static uint64_t PackSequenceAndType(uint64_t seq, ValueType t) { |
| 13 |
15 |
▶ | assert(seq <= kMaxSequenceNumber); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(seq <= kMaxSequenceNumber); |
0 |
15 |
15 |
|
| 14 |
15 |
▶ | assert(t <= kValueTypeForSeek); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(t <= kValueTypeForSeek); |
0 |
15 |
15 |
|
| 15 |
15 |
| return (seq << 8) | t; |
| 16 |
|
| } |
| 17 |
|
| |
| 18 |
6 |
| void AppendInternalKey(std::string* result, const ParsedInternalKey& key) { |
| 19 |
6 |
| result->append(key.user_key.data(), key.user_key.size()); |
| 20 |
6 |
| PutFixed64(result, PackSequenceAndType(key.sequence, key.type)); |
| 21 |
6 |
| } |
| 22 |
|
| |
| 23 |
0 |
| std::string ParsedInternalKey::DebugString() const { |
| 24 |
|
| char buf[50]; |
| 25 |
0 |
| snprintf(buf, sizeof(buf), "' @ %llu : %d", |
| 26 |
0 |
| (unsigned long long) sequence, |
| 27 |
0 |
| int(type)); |
| 28 |
0 |
▶ | std::string result = "'"; |
Branch: 1 branch
| Branch | True | False | Total |
| std::string result = "'"; |
0 |
0 |
0 |
|
| 29 |
0 |
▶ | result += user_key.ToString(); |
Branch: 2 branches
| Branch | True | False | Total |
| result += user_key.ToString(); |
0 |
0 |
0 |
| result += user_key.ToString(); |
0 |
0 |
0 |
|
| 30 |
0 |
▶ | result += buf; |
Branch: 1 branch
| Branch | True | False | Total |
| result += buf; |
0 |
0 |
0 |
|
| 31 |
0 |
| return result; |
| 32 |
|
| } |
| 33 |
|
| |
| 34 |
0 |
| const char* InternalKeyComparator::Name() const { |
| 35 |
0 |
| return "leveldb.InternalKeyComparator"; |
| 36 |
|
| } |
| 37 |
|
| |
| 38 |
160 |
| int InternalKeyComparator::Compare(const Slice& akey, const Slice& bkey) const { |
| 39 |
|
| // Order by: |
| 40 |
|
| // increasing user key (according to user-supplied comparator) |
| 41 |
|
| // decreasing sequence number |
| 42 |
|
| // decreasing type (though sequence# should be enough to disambiguate) |
| 43 |
160 |
▶ | int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey)); |
Branch: 1 branch
| Branch | True | False | Total |
| int r = user_comparator_->Compare(ExtractUserKey(akey), ExtractUserKey(bkey)); |
160 |
0 |
160 |
|
| 44 |
160 |
▶ | if (r == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r == 0) { |
81 |
79 |
160 |
|
| 45 |
81 |
| const uint64_t anum = DecodeFixed64(akey.data() + akey.size() - 8); |
| 46 |
81 |
| const uint64_t bnum = DecodeFixed64(bkey.data() + bkey.size() - 8); |
| 47 |
81 |
▶ | if (anum > bnum) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (anum > bnum) { |
17 |
64 |
81 |
|
| 48 |
17 |
| r = -1; |
| 49 |
64 |
▶ | } else if (anum < bnum) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (anum < bnum) { |
57 |
7 |
64 |
|
| 50 |
57 |
| r = +1; |
| 51 |
|
| } |
| 52 |
|
| } |
| 53 |
160 |
| return r; |
| 54 |
|
| } |
| 55 |
|
| |
| 56 |
0 |
| void InternalKeyComparator::FindShortestSeparator( |
| 57 |
|
| std::string* start, |
| 58 |
|
| const Slice& limit) const { |
| 59 |
|
| // Attempt to shorten the user portion of the key |
| 60 |
0 |
| Slice user_start = ExtractUserKey(*start); |
| 61 |
0 |
| Slice user_limit = ExtractUserKey(limit); |
| 62 |
0 |
▶ | std::string tmp(user_start.data(), user_start.size()); |
Branch: 1 branch
| Branch | True | False | Total |
| std::string tmp(user_start.data(), user_start.size()); |
0 |
0 |
0 |
|
| 63 |
0 |
▶ | user_comparator_->FindShortestSeparator(&tmp, user_limit); |
Branch: 1 branch
| Branch | True | False | Total |
| user_comparator_->FindShortestSeparator(&tmp, user_limit); |
0 |
0 |
0 |
|
| 64 |
0 |
▶ | if (user_comparator_->Compare(*start, tmp) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (user_comparator_->Compare(*start, tmp) < 0) { |
0 |
0 |
0 |
| if (user_comparator_->Compare(*start, tmp) < 0) { |
0 |
0 |
0 |
|
| 65 |
|
| // User key has become larger. Tack on the earliest possible |
| 66 |
|
| // number to the shortened user key. |
| 67 |
0 |
▶ | PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek)); |
Branch: 1 branch
| Branch | True | False | Total |
| PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek)); |
0 |
0 |
0 |
|
| 68 |
0 |
▶ | assert(this->Compare(*start, tmp) < 0); |
Branch: 2 branches
| Branch | True | False | Total |
| assert(this->Compare(*start, tmp) < 0); |
0 |
0 |
0 |
| assert(this->Compare(*start, tmp) < 0); |
0 |
0 |
0 |
|
| 69 |
0 |
▶ | assert(this->Compare(tmp, limit) < 0); |
Branch: 2 branches
| Branch | True | False | Total |
| assert(this->Compare(tmp, limit) < 0); |
0 |
0 |
0 |
| assert(this->Compare(tmp, limit) < 0); |
0 |
0 |
0 |
|
| 70 |
0 |
| start->swap(tmp); |
| 71 |
|
| } |
| 72 |
0 |
| } |
| 73 |
|
| |
| 74 |
1 |
| void InternalKeyComparator::FindShortSuccessor(std::string* key) const { |
| 75 |
1 |
| Slice user_key = ExtractUserKey(*key); |
| 76 |
1 |
▶ | std::string tmp(user_key.data(), user_key.size()); |
Branch: 1 branch
| Branch | True | False | Total |
| std::string tmp(user_key.data(), user_key.size()); |
1 |
0 |
1 |
|
| 77 |
1 |
▶ | user_comparator_->FindShortSuccessor(&tmp); |
Branch: 1 branch
| Branch | True | False | Total |
| user_comparator_->FindShortSuccessor(&tmp); |
1 |
0 |
1 |
|
| 78 |
1 |
▶ | if (user_comparator_->Compare(user_key, tmp) < 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (user_comparator_->Compare(user_key, tmp) < 0) { |
1 |
0 |
1 |
| if (user_comparator_->Compare(user_key, tmp) < 0) { |
1 |
0 |
1 |
|
| 79 |
|
| // User key has become larger. Tack on the earliest possible |
| 80 |
|
| // number to the shortened user key. |
| 81 |
1 |
▶ | PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek)); |
Branch: 1 branch
| Branch | True | False | Total |
| PutFixed64(&tmp, PackSequenceAndType(kMaxSequenceNumber,kValueTypeForSeek)); |
1 |
0 |
1 |
|
| 82 |
1 |
▶ | assert(this->Compare(*key, tmp) < 0); |
Branch: 2 branches
| Branch | True | False | Total |
| assert(this->Compare(*key, tmp) < 0); |
1 |
0 |
1 |
| assert(this->Compare(*key, tmp) < 0); |
0 |
1 |
1 |
|
| 83 |
1 |
| key->swap(tmp); |
| 84 |
|
| } |
| 85 |
1 |
| } |
| 86 |
|
| |
| 87 |
8 |
| LookupKey::LookupKey(const Slice& user_key, SequenceNumber s) { |
| 88 |
8 |
| size_t usize = user_key.size(); |
| 89 |
8 |
| size_t needed = usize + 13; // A conservative estimate |
| 90 |
|
| char* dst; |
| 91 |
8 |
▶ | if (needed <= sizeof(space_)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (needed <= sizeof(space_)) { |
8 |
0 |
8 |
|
| 92 |
8 |
| dst = space_; |
| 93 |
|
| } else { |
| 94 |
0 |
| dst = new char[needed]; |
| 95 |
|
| } |
| 96 |
8 |
| start_ = dst; |
| 97 |
8 |
| dst = EncodeVarint32(dst, usize + 8); |
| 98 |
8 |
| kstart_ = dst; |
| 99 |
8 |
| memcpy(dst, user_key.data(), usize); |
| 100 |
8 |
| dst += usize; |
| 101 |
8 |
| EncodeFixed64(dst, PackSequenceAndType(s, kValueTypeForSeek)); |
| 102 |
8 |
| dst += 8; |
| 103 |
8 |
| end_ = dst; |
| 104 |
8 |
| } |
| 105 |
|
| |
| 106 |
|
| } |