| 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 "db/db_iter.h" |
| 6 |
|
| |
| 7 |
|
| #include "db/filename.h" |
| 8 |
|
| #include "db/dbformat.h" |
| 9 |
|
| #include "leveldb/env.h" |
| 10 |
|
| #include "leveldb/iterator.h" |
| 11 |
|
| #include "port/port.h" |
| 12 |
|
| #include "util/logging.h" |
| 13 |
|
| #include "util/mutexlock.h" |
| 14 |
|
| |
| 15 |
|
| namespace leveldb { |
| 16 |
|
| |
| 17 |
|
| #if 0 |
| 18 |
|
| static void DumpInternalIter(Iterator* iter) { |
| 19 |
|
| for (iter->SeekToFirst(); iter->Valid(); iter->Next()) { |
| 20 |
|
| ParsedInternalKey k; |
| 21 |
|
| if (!ParseInternalKey(iter->key(), &k)) { |
| 22 |
|
| fprintf(stderr, "Corrupt '%s'\n", EscapeString(iter->key()).c_str()); |
| 23 |
|
| } else { |
| 24 |
|
| fprintf(stderr, "@ '%s'\n", k.DebugString().c_str()); |
| 25 |
|
| } |
| 26 |
|
| } |
| 27 |
|
| } |
| 28 |
|
| #endif |
| 29 |
|
| |
| 30 |
|
| namespace { |
| 31 |
|
| |
| 32 |
|
| // Memtables and sstables that make the DB representation contain |
| 33 |
|
| // (userkey,seq,type) => uservalue entries. DBIter |
| 34 |
|
| // combines multiple entries for the same userkey found in the DB |
| 35 |
|
| // representation into a single entry while accounting for sequence |
| 36 |
|
| // numbers, deletion markers, overwrites, etc. |
| 37 |
|
| class DBIter: public Iterator { |
| 38 |
|
| public: |
| 39 |
|
| // Which direction is the iterator currently moving? |
| 40 |
|
| // (1) When moving forward, the internal iterator is positioned at |
| 41 |
|
| // the exact entry that yields this->key(), this->value() |
| 42 |
|
| // (2) When moving backwards, the internal iterator is positioned |
| 43 |
|
| // just before all entries whose user key == this->key(). |
| 44 |
|
| enum Direction { |
| 45 |
|
| kForward, |
| 46 |
|
| kReverse |
| 47 |
|
| }; |
| 48 |
|
| |
| 49 |
14 |
| DBIter(const std::string* dbname, Env* env, |
| 50 |
|
| const Comparator* cmp, Iterator* iter, SequenceNumber s) |
| 51 |
28 |
| : dbname_(dbname), |
| 52 |
14 |
| env_(env), |
| 53 |
14 |
| user_comparator_(cmp), |
| 54 |
14 |
| iter_(iter), |
| 55 |
14 |
| sequence_(s), |
| 56 |
14 |
| direction_(kForward), |
| 57 |
14 |
| valid_(false) { |
| 58 |
14 |
| } |
| 59 |
28 |
| virtual ~DBIter() { |
| 60 |
14 |
▶ | delete iter_; |
Branch: 1 branch
| Branch | True | False | Total |
| delete iter_; |
14 |
0 |
14 |
|
| 61 |
28 |
| } |
| 62 |
24 |
| virtual bool Valid() const { return valid_; } |
| 63 |
8 |
| virtual Slice key() const { |
| 64 |
8 |
▶ | assert(valid_); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(valid_); |
0 |
8 |
8 |
|
| 65 |
8 |
▶ | return (direction_ == kForward) ? ExtractUserKey(iter_->key()) : saved_key_; |
Branch: 2 branches
| Branch | True | False | Total |
| return (direction_ == kForward) ? ExtractUserKey(iter_->key()) : saved_key_; |
8 |
0 |
8 |
| return (direction_ == kForward) ? ExtractUserKey(iter_->key()) : saved_key_; |
8 |
0 |
8 |
|
| 66 |
|
| } |
| 67 |
0 |
| virtual Slice value() const { |
| 68 |
0 |
▶ | assert(valid_); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(valid_); |
0 |
0 |
0 |
|
| 69 |
0 |
▶ | return (direction_ == kForward) ? iter_->value() : saved_value_; |
Branch: 1 branch
| Branch | True | False | Total |
| return (direction_ == kForward) ? iter_->value() : saved_value_; |
0 |
0 |
0 |
|
| 70 |
|
| } |
| 71 |
0 |
| virtual Status status() const { |
| 72 |
0 |
▶ | if (status_.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (status_.ok()) { |
0 |
0 |
0 |
|
| 73 |
0 |
| return iter_->status(); |
| 74 |
|
| } else { |
| 75 |
0 |
| return status_; |
| 76 |
|
| } |
| 77 |
|
| } |
| 78 |
|
| |
| 79 |
|
| virtual void Next(); |
| 80 |
|
| virtual void Prev(); |
| 81 |
|
| virtual void Seek(const Slice& target); |
| 82 |
|
| virtual void SeekToFirst(); |
| 83 |
|
| virtual void SeekToLast(); |
| 84 |
|
| |
| 85 |
|
| private: |
| 86 |
|
| void FindNextUserEntry(bool skipping, std::string* skip); |
| 87 |
|
| void FindPrevUserEntry(); |
| 88 |
|
| bool ParseKey(ParsedInternalKey* key); |
| 89 |
|
| |
| 90 |
49 |
| inline void SaveKey(const Slice& k, std::string* dst) { |
| 91 |
49 |
| dst->assign(k.data(), k.size()); |
| 92 |
49 |
| } |
| 93 |
|
| |
| 94 |
14 |
| inline void ClearSavedValue() { |
| 95 |
14 |
▶ | if (saved_value_.capacity() > 1048576) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (saved_value_.capacity() > 1048576) { |
0 |
14 |
14 |
|
| 96 |
0 |
| std::string empty; |
| 97 |
0 |
| swap(empty, saved_value_); |
| 98 |
0 |
| } else { |
| 99 |
14 |
| saved_value_.clear(); |
| 100 |
|
| } |
| 101 |
14 |
| } |
| 102 |
|
| |
| 103 |
|
| const std::string* const dbname_; |
| 104 |
|
| Env* const env_; |
| 105 |
|
| const Comparator* const user_comparator_; |
| 106 |
|
| Iterator* const iter_; |
| 107 |
|
| SequenceNumber const sequence_; |
| 108 |
|
| |
| 109 |
|
| Status status_; |
| 110 |
|
| std::string saved_key_; // == current key when direction_==kReverse |
| 111 |
|
| std::string saved_value_; // == current raw value when direction_==kReverse |
| 112 |
|
| Direction direction_; |
| 113 |
|
| bool valid_; |
| 114 |
|
| |
| 115 |
|
| // No copying allowed |
| 116 |
|
| DBIter(const DBIter&); |
| 117 |
|
| void operator=(const DBIter&); |
| 118 |
|
| }; |
| 119 |
|
| |
| 120 |
95 |
| inline bool DBIter::ParseKey(ParsedInternalKey* ikey) { |
| 121 |
95 |
▶ | if (!ParseInternalKey(iter_->key(), ikey)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!ParseInternalKey(iter_->key(), ikey)) { |
95 |
0 |
95 |
| if (!ParseInternalKey(iter_->key(), ikey)) { |
95 |
0 |
95 |
| if (!ParseInternalKey(iter_->key(), ikey)) { |
0 |
95 |
95 |
|
| 122 |
0 |
▶ | status_ = Status::Corruption("corrupted internal key in DBIter"); |
Branch: 2 branches
| Branch | True | False | Total |
| status_ = Status::Corruption("corrupted internal key in DBIter"); |
0 |
0 |
0 |
| status_ = Status::Corruption("corrupted internal key in DBIter"); |
0 |
0 |
0 |
|
| 123 |
0 |
| return false; |
| 124 |
|
| } else { |
| 125 |
95 |
| return true; |
| 126 |
|
| } |
| 127 |
|
| } |
| 128 |
|
| |
| 129 |
10 |
| void DBIter::Next() { |
| 130 |
10 |
▶ | assert(valid_); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(valid_); |
0 |
10 |
10 |
|
| 131 |
|
| |
| 132 |
10 |
▶ | if (direction_ == kReverse) { // Switch directions? |
Branch: 1 branch
| Branch | True | False | Total |
| if (direction_ == kReverse) { // Switch directions? |
0 |
10 |
10 |
|
| 133 |
0 |
| direction_ = kForward; |
| 134 |
|
| // iter_ is pointing just before the entries for this->key(), |
| 135 |
|
| // so advance into the range of entries for this->key() and then |
| 136 |
|
| // use the normal skipping code below. |
| 137 |
0 |
▶ | if (!iter_->Valid()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!iter_->Valid()) { |
0 |
0 |
0 |
|
| 138 |
0 |
| iter_->SeekToFirst(); |
| 139 |
|
| } else { |
| 140 |
0 |
| iter_->Next(); |
| 141 |
|
| } |
| 142 |
0 |
▶ | if (!iter_->Valid()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!iter_->Valid()) { |
0 |
0 |
0 |
|
| 143 |
0 |
| valid_ = false; |
| 144 |
0 |
| saved_key_.clear(); |
| 145 |
0 |
| return; |
| 146 |
|
| } |
| 147 |
|
| } |
| 148 |
|
| |
| 149 |
|
| // Temporarily use saved_key_ as storage for key to skip. |
| 150 |
10 |
| std::string* skip = &saved_key_; |
| 151 |
10 |
▶ | SaveKey(ExtractUserKey(iter_->key()), skip); |
Branch: 2 branches
| Branch | True | False | Total |
| SaveKey(ExtractUserKey(iter_->key()), skip); |
10 |
0 |
10 |
| SaveKey(ExtractUserKey(iter_->key()), skip); |
10 |
0 |
10 |
|
| 152 |
10 |
| FindNextUserEntry(true, skip); |
| 153 |
|
| } |
| 154 |
|
| |
| 155 |
23 |
| void DBIter::FindNextUserEntry(bool skipping, std::string* skip) { |
| 156 |
|
| // Loop until we hit an acceptable entry to yield |
| 157 |
23 |
▶ | assert(iter_->Valid()); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(iter_->Valid()); |
0 |
23 |
23 |
|
| 158 |
23 |
▶ | assert(direction_ == kForward); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(direction_ == kForward); |
0 |
23 |
23 |
|
| 159 |
|
| do { |
| 160 |
95 |
| ParsedInternalKey ikey; |
| 161 |
95 |
▶ | if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
95 |
0 |
95 |
| if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
95 |
0 |
95 |
| if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
95 |
0 |
95 |
| if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
95 |
0 |
95 |
|
| 162 |
95 |
| switch (ikey.type) { |
| 163 |
39 |
| case kTypeDeletion: |
| 164 |
|
| // Arrange to skip all upcoming entries for this key since |
| 165 |
|
| // they are hidden by this deletion. |
| 166 |
39 |
▶ | SaveKey(ikey.user_key, skip); |
Branch: 1 branch
| Branch | True | False | Total |
| SaveKey(ikey.user_key, skip); |
39 |
0 |
39 |
|
| 167 |
39 |
| skipping = true; |
| 168 |
39 |
| break; |
| 169 |
56 |
| case kTypeValue: |
| 170 |
110 |
▶ | if (skipping && |
Branch: 1 branch
| Branch | True | False | Total |
| if (skipping && |
54 |
2 |
56 |
|
| 171 |
110 |
▶ | user_comparator_->Compare(ikey.user_key, *skip) <= 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| user_comparator_->Compare(ikey.user_key, *skip) <= 0) { |
54 |
0 |
54 |
| user_comparator_->Compare(ikey.user_key, *skip) <= 0) { |
44 |
10 |
54 |
| user_comparator_->Compare(ikey.user_key, *skip) <= 0) { |
12 |
44 |
56 |
|
| 172 |
|
| // Entry hidden |
| 173 |
|
| } else { |
| 174 |
12 |
| valid_ = true; |
| 175 |
12 |
| saved_key_.clear(); |
| 176 |
12 |
| return; |
| 177 |
|
| } |
| 178 |
44 |
| break; |
| 179 |
|
| } |
| 180 |
|
| } |
| 181 |
83 |
▶ | iter_->Next(); |
Branch: 1 branch
| Branch | True | False | Total |
| iter_->Next(); |
83 |
0 |
83 |
|
| 182 |
83 |
▶ | } while (iter_->Valid()); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (iter_->Valid()); |
72 |
11 |
83 |
|
| 183 |
11 |
| saved_key_.clear(); |
| 184 |
11 |
| valid_ = false; |
| 185 |
|
| } |
| 186 |
|
| |
| 187 |
0 |
| void DBIter::Prev() { |
| 188 |
0 |
▶ | assert(valid_); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(valid_); |
0 |
0 |
0 |
|
| 189 |
|
| |
| 190 |
0 |
▶ | if (direction_ == kForward) { // Switch directions? |
Branch: 1 branch
| Branch | True | False | Total |
| if (direction_ == kForward) { // Switch directions? |
0 |
0 |
0 |
|
| 191 |
|
| // iter_ is pointing at the current entry. Scan backwards until |
| 192 |
|
| // the key changes so we can use the normal reverse scanning code. |
| 193 |
0 |
▶ | assert(iter_->Valid()); // Otherwise valid_ would have been false |
Branch: 1 branch
| Branch | True | False | Total |
| assert(iter_->Valid()); // Otherwise valid_ would have been false |
0 |
0 |
0 |
|
| 194 |
0 |
▶ | SaveKey(ExtractUserKey(iter_->key()), &saved_key_); |
Branch: 2 branches
| Branch | True | False | Total |
| SaveKey(ExtractUserKey(iter_->key()), &saved_key_); |
0 |
0 |
0 |
| SaveKey(ExtractUserKey(iter_->key()), &saved_key_); |
0 |
0 |
0 |
|
| 195 |
|
| while (true) { |
| 196 |
0 |
| iter_->Prev(); |
| 197 |
0 |
▶ | if (!iter_->Valid()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!iter_->Valid()) { |
0 |
0 |
0 |
|
| 198 |
0 |
| valid_ = false; |
| 199 |
0 |
| saved_key_.clear(); |
| 200 |
0 |
| ClearSavedValue(); |
| 201 |
0 |
| return; |
| 202 |
|
| } |
| 203 |
0 |
▶ | if (user_comparator_->Compare(ExtractUserKey(iter_->key()), |
Branch: 2 branches
| Branch | True | False | Total |
| if (user_comparator_->Compare(ExtractUserKey(iter_->key()), |
0 |
0 |
0 |
| if (user_comparator_->Compare(ExtractUserKey(iter_->key()), |
0 |
0 |
0 |
|
| 204 |
0 |
▶ | saved_key_) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| saved_key_) < 0) { |
0 |
0 |
0 |
|
| 205 |
0 |
| break; |
| 206 |
|
| } |
| 207 |
|
| } |
| 208 |
0 |
| direction_ = kReverse; |
| 209 |
|
| } |
| 210 |
|
| |
| 211 |
0 |
| FindPrevUserEntry(); |
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
0 |
| void DBIter::FindPrevUserEntry() { |
| 215 |
0 |
▶ | assert(direction_ == kReverse); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(direction_ == kReverse); |
0 |
0 |
0 |
|
| 216 |
|
| |
| 217 |
0 |
| ValueType value_type = kTypeDeletion; |
| 218 |
0 |
▶ | if (iter_->Valid()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (iter_->Valid()) { |
0 |
0 |
0 |
|
| 219 |
|
| do { |
| 220 |
0 |
| ParsedInternalKey ikey; |
| 221 |
0 |
▶ | if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
0 |
0 |
0 |
| if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
0 |
0 |
0 |
| if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
0 |
0 |
0 |
| if (ParseKey(&ikey) && ikey.sequence <= sequence_) { |
0 |
0 |
0 |
|
| 222 |
0 |
▶ | if ((value_type != kTypeDeletion) && |
Branch: 1 branch
| Branch | True | False | Total |
| if ((value_type != kTypeDeletion) && |
0 |
0 |
0 |
|
| 223 |
0 |
▶ | user_comparator_->Compare(ikey.user_key, saved_key_) < 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| user_comparator_->Compare(ikey.user_key, saved_key_) < 0) { |
0 |
0 |
0 |
| user_comparator_->Compare(ikey.user_key, saved_key_) < 0) { |
0 |
0 |
0 |
| user_comparator_->Compare(ikey.user_key, saved_key_) < 0) { |
0 |
0 |
0 |
|
| 224 |
|
| // We encountered a non-deleted value in entries for previous keys, |
| 225 |
0 |
| break; |
| 226 |
|
| } |
| 227 |
0 |
| value_type = ikey.type; |
| 228 |
0 |
▶ | if (value_type == kTypeDeletion) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value_type == kTypeDeletion) { |
0 |
0 |
0 |
|
| 229 |
0 |
| saved_key_.clear(); |
| 230 |
0 |
| ClearSavedValue(); |
| 231 |
|
| } else { |
| 232 |
0 |
▶ | Slice raw_value = iter_->value(); |
Branch: 1 branch
| Branch | True | False | Total |
| Slice raw_value = iter_->value(); |
0 |
0 |
0 |
|
| 233 |
0 |
▶ | if (saved_value_.capacity() > raw_value.size() + 1048576) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (saved_value_.capacity() > raw_value.size() + 1048576) { |
0 |
0 |
0 |
|
| 234 |
0 |
| std::string empty; |
| 235 |
0 |
| swap(empty, saved_value_); |
| 236 |
0 |
| } |
| 237 |
0 |
▶ | SaveKey(ExtractUserKey(iter_->key()), &saved_key_); |
Branch: 2 branches
| Branch | True | False | Total |
| SaveKey(ExtractUserKey(iter_->key()), &saved_key_); |
0 |
0 |
0 |
| SaveKey(ExtractUserKey(iter_->key()), &saved_key_); |
0 |
0 |
0 |
|
| 238 |
0 |
▶ | saved_value_.assign(raw_value.data(), raw_value.size()); |
Branch: 1 branch
| Branch | True | False | Total |
| saved_value_.assign(raw_value.data(), raw_value.size()); |
0 |
0 |
0 |
|
| 239 |
|
| } |
| 240 |
|
| } |
| 241 |
0 |
▶ | iter_->Prev(); |
Branch: 1 branch
| Branch | True | False | Total |
| iter_->Prev(); |
0 |
0 |
0 |
|
| 242 |
0 |
▶ | } while (iter_->Valid()); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (iter_->Valid()); |
0 |
0 |
0 |
|
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
0 |
▶ | if (value_type == kTypeDeletion) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value_type == kTypeDeletion) { |
0 |
0 |
0 |
|
| 246 |
|
| // End |
| 247 |
0 |
| valid_ = false; |
| 248 |
0 |
| saved_key_.clear(); |
| 249 |
0 |
| ClearSavedValue(); |
| 250 |
0 |
| direction_ = kForward; |
| 251 |
|
| } else { |
| 252 |
0 |
| valid_ = true; |
| 253 |
|
| } |
| 254 |
0 |
| } |
| 255 |
|
| |
| 256 |
6 |
| void DBIter::Seek(const Slice& target) { |
| 257 |
6 |
| direction_ = kForward; |
| 258 |
6 |
| ClearSavedValue(); |
| 259 |
6 |
| saved_key_.clear(); |
| 260 |
6 |
▶ | AppendInternalKey( |
Branch: 1 branch
| Branch | True | False | Total |
| AppendInternalKey( |
6 |
0 |
6 |
|
| 261 |
6 |
| &saved_key_, ParsedInternalKey(target, sequence_, kValueTypeForSeek)); |
| 262 |
6 |
▶ | iter_->Seek(saved_key_); |
Branch: 1 branch
| Branch | True | False | Total |
| iter_->Seek(saved_key_); |
6 |
0 |
6 |
|
| 263 |
6 |
▶ | if (iter_->Valid()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (iter_->Valid()) { |
5 |
1 |
6 |
|
| 264 |
5 |
| FindNextUserEntry(false, &saved_key_ /* temporary storage */); |
| 265 |
|
| } else { |
| 266 |
1 |
| valid_ = false; |
| 267 |
|
| } |
| 268 |
6 |
| } |
| 269 |
|
| |
| 270 |
8 |
| void DBIter::SeekToFirst() { |
| 271 |
8 |
| direction_ = kForward; |
| 272 |
8 |
| ClearSavedValue(); |
| 273 |
8 |
| iter_->SeekToFirst(); |
| 274 |
8 |
▶ | if (iter_->Valid()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (iter_->Valid()) { |
8 |
0 |
8 |
|
| 275 |
8 |
| FindNextUserEntry(false, &saved_key_ /* temporary storage */); |
| 276 |
|
| } else { |
| 277 |
0 |
| valid_ = false; |
| 278 |
|
| } |
| 279 |
8 |
| } |
| 280 |
|
| |
| 281 |
0 |
| void DBIter::SeekToLast() { |
| 282 |
0 |
| direction_ = kReverse; |
| 283 |
0 |
| ClearSavedValue(); |
| 284 |
0 |
| iter_->SeekToLast(); |
| 285 |
0 |
| FindPrevUserEntry(); |
| 286 |
0 |
| } |
| 287 |
|
| |
| 288 |
|
| } // anonymous namespace |
| 289 |
|
| |
| 290 |
14 |
| Iterator* NewDBIterator( |
| 291 |
|
| const std::string* dbname, |
| 292 |
|
| Env* env, |
| 293 |
|
| const Comparator* user_key_comparator, |
| 294 |
|
| Iterator* internal_iter, |
| 295 |
|
| const SequenceNumber& sequence) { |
| 296 |
14 |
▶ | return new DBIter(dbname, env, user_key_comparator, internal_iter, sequence); |
Branch: 2 branches
| Branch | True | False | Total |
| return new DBIter(dbname, env, user_key_comparator, internal_iter, sequence); |
14 |
0 |
14 |
| return new DBIter(dbname, env, user_key_comparator, internal_iter, sequence); |
0 |
0 |
0 |
|
| 297 |
|
| } |
| 298 |
|
| |
| 299 |
|
| } |