2011-03-18 22:37:00 +00:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#include "db/db_iter.h"
|
|
|
|
|
2013-08-21 11:12:47 -07:00
|
|
|
#include "db/db_impl.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "db/dbformat.h"
|
2019-05-02 11:01:00 -07:00
|
|
|
#include "db/filename.h"
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/env.h"
|
|
|
|
#include "leveldb/iterator.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
#include "port/port.h"
|
|
|
|
#include "util/logging.h"
|
|
|
|
#include "util/mutexlock.h"
|
2013-08-21 11:12:47 -07:00
|
|
|
#include "util/random.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static void DumpInternalIter(Iterator* iter) {
|
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
ParsedInternalKey k;
|
|
|
|
if (!ParseInternalKey(iter->key(), &k)) {
|
2020-04-29 22:31:41 +00:00
|
|
|
std::fprintf(stderr, "Corrupt '%s'\n", EscapeString(iter->key()).c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
2020-04-29 22:31:41 +00:00
|
|
|
std::fprintf(stderr, "@ '%s'\n", k.DebugString().c_str());
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Memtables and sstables that make the DB representation contain
|
|
|
|
// (userkey,seq,type) => uservalue entries. DBIter
|
|
|
|
// combines multiple entries for the same userkey found in the DB
|
|
|
|
// representation into a single entry while accounting for sequence
|
|
|
|
// numbers, deletion markers, overwrites, etc.
|
2019-05-02 11:01:00 -07:00
|
|
|
class DBIter : public Iterator {
|
2011-03-18 22:37:00 +00:00
|
|
|
public:
|
2011-03-25 20:27:43 +00:00
|
|
|
// Which direction is the iterator currently moving?
|
|
|
|
// (1) When moving forward, the internal iterator is positioned at
|
|
|
|
// the exact entry that yields this->key(), this->value()
|
|
|
|
// (2) When moving backwards, the internal iterator is positioned
|
|
|
|
// just before all entries whose user key == this->key().
|
2019-05-02 11:01:00 -07:00
|
|
|
enum Direction { kForward, kReverse };
|
2011-03-25 20:27:43 +00:00
|
|
|
|
2013-08-21 11:12:47 -07:00
|
|
|
DBIter(DBImpl* db, const Comparator* cmp, Iterator* iter, SequenceNumber s,
|
|
|
|
uint32_t seed)
|
|
|
|
: db_(db),
|
2011-03-18 22:37:00 +00:00
|
|
|
user_comparator_(cmp),
|
|
|
|
iter_(iter),
|
|
|
|
sequence_(s),
|
2011-03-25 20:27:43 +00:00
|
|
|
direction_(kForward),
|
2013-08-21 11:12:47 -07:00
|
|
|
valid_(false),
|
|
|
|
rnd_(seed),
|
2019-05-02 11:01:00 -07:00
|
|
|
bytes_until_read_sampling_(RandomCompactionPeriod()) {}
|
2019-05-03 09:31:18 -07:00
|
|
|
|
|
|
|
DBIter(const DBIter&) = delete;
|
|
|
|
DBIter& operator=(const DBIter&) = delete;
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
~DBIter() override { delete iter_; }
|
|
|
|
bool Valid() const override { return valid_; }
|
|
|
|
Slice key() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(valid_);
|
2011-03-25 20:27:43 +00:00
|
|
|
return (direction_ == kForward) ? ExtractUserKey(iter_->key()) : saved_key_;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2019-05-09 14:00:07 -07:00
|
|
|
Slice value() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
assert(valid_);
|
2011-04-20 22:48:11 +00:00
|
|
|
return (direction_ == kForward) ? iter_->value() : saved_value_;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2019-05-09 14:00:07 -07:00
|
|
|
Status status() const override {
|
2011-03-18 22:37:00 +00:00
|
|
|
if (status_.ok()) {
|
|
|
|
return iter_->status();
|
|
|
|
} else {
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-09 14:00:07 -07:00
|
|
|
void Next() override;
|
|
|
|
void Prev() override;
|
|
|
|
void Seek(const Slice& target) override;
|
|
|
|
void SeekToFirst() override;
|
|
|
|
void SeekToLast() override;
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
private:
|
|
|
|
void FindNextUserEntry(bool skipping, std::string* skip);
|
|
|
|
void FindPrevUserEntry();
|
|
|
|
bool ParseKey(ParsedInternalKey* key);
|
|
|
|
|
|
|
|
inline void SaveKey(const Slice& k, std::string* dst) {
|
|
|
|
dst->assign(k.data(), k.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void ClearSavedValue() {
|
|
|
|
if (saved_value_.capacity() > 1048576) {
|
|
|
|
std::string empty;
|
|
|
|
swap(empty, saved_value_);
|
|
|
|
} else {
|
|
|
|
saved_value_.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-04 09:44:56 -07:00
|
|
|
// Picks the number of bytes that can be read until a compaction is scheduled.
|
|
|
|
size_t RandomCompactionPeriod() {
|
2019-05-02 11:01:00 -07:00
|
|
|
return rnd_.Uniform(2 * config::kReadBytesPeriod);
|
2013-08-21 11:12:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
DBImpl* db_;
|
2011-03-18 22:37:00 +00:00
|
|
|
const Comparator* const user_comparator_;
|
|
|
|
Iterator* const iter_;
|
|
|
|
SequenceNumber const sequence_;
|
|
|
|
Status status_;
|
2019-05-02 11:01:00 -07:00
|
|
|
std::string saved_key_; // == current key when direction_==kReverse
|
|
|
|
std::string saved_value_; // == current raw value when direction_==kReverse
|
2011-03-25 20:27:43 +00:00
|
|
|
Direction direction_;
|
2011-03-18 22:37:00 +00:00
|
|
|
bool valid_;
|
2013-08-21 11:12:47 -07:00
|
|
|
Random rnd_;
|
2018-09-04 09:44:56 -07:00
|
|
|
size_t bytes_until_read_sampling_;
|
2011-03-18 22:37:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline bool DBIter::ParseKey(ParsedInternalKey* ikey) {
|
2013-08-21 11:12:47 -07:00
|
|
|
Slice k = iter_->key();
|
2018-09-04 09:44:56 -07:00
|
|
|
|
|
|
|
size_t bytes_read = k.size() + iter_->value().size();
|
|
|
|
while (bytes_until_read_sampling_ < bytes_read) {
|
|
|
|
bytes_until_read_sampling_ += RandomCompactionPeriod();
|
2013-08-21 11:12:47 -07:00
|
|
|
db_->RecordReadSample(k);
|
|
|
|
}
|
2018-09-04 09:44:56 -07:00
|
|
|
assert(bytes_until_read_sampling_ >= bytes_read);
|
|
|
|
bytes_until_read_sampling_ -= bytes_read;
|
|
|
|
|
2013-08-21 11:12:47 -07:00
|
|
|
if (!ParseInternalKey(k, ikey)) {
|
2011-03-18 22:37:00 +00:00
|
|
|
status_ = Status::Corruption("corrupted internal key in DBIter");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::Next() {
|
|
|
|
assert(valid_);
|
|
|
|
|
|
|
|
if (direction_ == kReverse) { // Switch directions?
|
|
|
|
direction_ = kForward;
|
|
|
|
// iter_ is pointing just before the entries for this->key(),
|
|
|
|
// so advance into the range of entries for this->key() and then
|
|
|
|
// use the normal skipping code below.
|
|
|
|
if (!iter_->Valid()) {
|
|
|
|
iter_->SeekToFirst();
|
|
|
|
} else {
|
2011-03-18 22:37:00 +00:00
|
|
|
iter_->Next();
|
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
if (!iter_->Valid()) {
|
|
|
|
valid_ = false;
|
|
|
|
saved_key_.clear();
|
|
|
|
return;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2013-09-19 13:42:22 -07:00
|
|
|
// saved_key_ already contains the key to skip past.
|
|
|
|
} else {
|
|
|
|
// Store in saved_key_ the current key so we skip it below.
|
|
|
|
SaveKey(ExtractUserKey(iter_->key()), &saved_key_);
|
2019-04-25 09:44:07 +08:00
|
|
|
|
|
|
|
// iter_ is pointing to current key. We can now safely move to the next to
|
|
|
|
// avoid checking current key.
|
|
|
|
iter_->Next();
|
|
|
|
if (!iter_->Valid()) {
|
|
|
|
valid_ = false;
|
|
|
|
saved_key_.clear();
|
|
|
|
return;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
|
2013-09-19 13:42:22 -07:00
|
|
|
FindNextUserEntry(true, &saved_key_);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::FindNextUserEntry(bool skipping, std::string* skip) {
|
|
|
|
// Loop until we hit an acceptable entry to yield
|
|
|
|
assert(iter_->Valid());
|
|
|
|
assert(direction_ == kForward);
|
|
|
|
do {
|
2011-03-18 22:37:00 +00:00
|
|
|
ParsedInternalKey ikey;
|
2011-03-25 20:27:43 +00:00
|
|
|
if (ParseKey(&ikey) && ikey.sequence <= sequence_) {
|
|
|
|
switch (ikey.type) {
|
|
|
|
case kTypeDeletion:
|
|
|
|
// Arrange to skip all upcoming entries for this key since
|
|
|
|
// they are hidden by this deletion.
|
|
|
|
SaveKey(ikey.user_key, skip);
|
|
|
|
skipping = true;
|
|
|
|
break;
|
|
|
|
case kTypeValue:
|
|
|
|
if (skipping &&
|
|
|
|
user_comparator_->Compare(ikey.user_key, *skip) <= 0) {
|
|
|
|
// Entry hidden
|
|
|
|
} else {
|
|
|
|
valid_ = true;
|
|
|
|
saved_key_.clear();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
iter_->Next();
|
2011-03-25 20:27:43 +00:00
|
|
|
} while (iter_->Valid());
|
|
|
|
saved_key_.clear();
|
|
|
|
valid_ = false;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::Prev() {
|
|
|
|
assert(valid_);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
if (direction_ == kForward) { // Switch directions?
|
|
|
|
// iter_ is pointing at the current entry. Scan backwards until
|
|
|
|
// the key changes so we can use the normal reverse scanning code.
|
|
|
|
assert(iter_->Valid()); // Otherwise valid_ would have been false
|
|
|
|
SaveKey(ExtractUserKey(iter_->key()), &saved_key_);
|
|
|
|
while (true) {
|
|
|
|
iter_->Prev();
|
2011-03-18 22:37:00 +00:00
|
|
|
if (!iter_->Valid()) {
|
2011-03-25 20:27:43 +00:00
|
|
|
valid_ = false;
|
|
|
|
saved_key_.clear();
|
|
|
|
ClearSavedValue();
|
2011-03-18 22:37:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-05-02 11:01:00 -07:00
|
|
|
if (user_comparator_->Compare(ExtractUserKey(iter_->key()), saved_key_) <
|
|
|
|
0) {
|
2011-03-25 20:27:43 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
direction_ = kReverse;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2011-03-25 20:27:43 +00:00
|
|
|
|
|
|
|
FindPrevUserEntry();
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::FindPrevUserEntry() {
|
|
|
|
assert(direction_ == kReverse);
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
ValueType value_type = kTypeDeletion;
|
|
|
|
if (iter_->Valid()) {
|
|
|
|
do {
|
|
|
|
ParsedInternalKey ikey;
|
|
|
|
if (ParseKey(&ikey) && ikey.sequence <= sequence_) {
|
|
|
|
if ((value_type != kTypeDeletion) &&
|
|
|
|
user_comparator_->Compare(ikey.user_key, saved_key_) < 0) {
|
|
|
|
// We encountered a non-deleted value in entries for previous keys,
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
value_type = ikey.type;
|
|
|
|
if (value_type == kTypeDeletion) {
|
2011-08-16 01:21:01 +00:00
|
|
|
saved_key_.clear();
|
2011-03-25 20:27:43 +00:00
|
|
|
ClearSavedValue();
|
|
|
|
} else {
|
|
|
|
Slice raw_value = iter_->value();
|
|
|
|
if (saved_value_.capacity() > raw_value.size() + 1048576) {
|
|
|
|
std::string empty;
|
|
|
|
swap(empty, saved_value_);
|
|
|
|
}
|
2011-08-16 01:21:01 +00:00
|
|
|
SaveKey(ExtractUserKey(iter_->key()), &saved_key_);
|
2011-03-25 20:27:43 +00:00
|
|
|
saved_value_.assign(raw_value.data(), raw_value.size());
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
iter_->Prev();
|
2011-03-25 20:27:43 +00:00
|
|
|
} while (iter_->Valid());
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
if (value_type == kTypeDeletion) {
|
|
|
|
// End
|
|
|
|
valid_ = false;
|
|
|
|
saved_key_.clear();
|
|
|
|
ClearSavedValue();
|
|
|
|
direction_ = kForward;
|
|
|
|
} else {
|
|
|
|
valid_ = true;
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::Seek(const Slice& target) {
|
|
|
|
direction_ = kForward;
|
|
|
|
ClearSavedValue();
|
|
|
|
saved_key_.clear();
|
2019-05-02 11:01:00 -07:00
|
|
|
AppendInternalKey(&saved_key_,
|
|
|
|
ParsedInternalKey(target, sequence_, kValueTypeForSeek));
|
2011-03-25 20:27:43 +00:00
|
|
|
iter_->Seek(saved_key_);
|
|
|
|
if (iter_->Valid()) {
|
|
|
|
FindNextUserEntry(false, &saved_key_ /* temporary storage */);
|
|
|
|
} else {
|
|
|
|
valid_ = false;
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::SeekToFirst() {
|
|
|
|
direction_ = kForward;
|
|
|
|
ClearSavedValue();
|
|
|
|
iter_->SeekToFirst();
|
|
|
|
if (iter_->Valid()) {
|
|
|
|
FindNextUserEntry(false, &saved_key_ /* temporary storage */);
|
|
|
|
} else {
|
|
|
|
valid_ = false;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-25 20:27:43 +00:00
|
|
|
void DBIter::SeekToLast() {
|
|
|
|
direction_ = kReverse;
|
|
|
|
ClearSavedValue();
|
|
|
|
iter_->SeekToLast();
|
|
|
|
FindPrevUserEntry();
|
|
|
|
}
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Iterator* NewDBIterator(DBImpl* db, const Comparator* user_key_comparator,
|
|
|
|
Iterator* internal_iter, SequenceNumber sequence,
|
|
|
|
uint32_t seed) {
|
2013-08-21 11:12:47 -07:00
|
|
|
return new DBIter(db, user_key_comparator, internal_iter, sequence, seed);
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|