2017-10-18 02:24:25 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2017-11-28 20:42:28 +01:00
|
|
|
#include "monitoring/statistics.h"
|
2017-10-18 02:24:25 +02:00
|
|
|
#include "rocksdb/iterator.h"
|
2017-11-28 20:42:28 +01:00
|
|
|
#include "util/stop_watch.h"
|
2017-10-18 02:24:25 +02:00
|
|
|
#include "utilities/blob_db/blob_db_impl.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
namespace blob_db {
|
|
|
|
|
|
|
|
using rocksdb::ManagedSnapshot;
|
|
|
|
|
|
|
|
class BlobDBIterator : public Iterator {
|
|
|
|
public:
|
|
|
|
BlobDBIterator(ManagedSnapshot* snapshot, ArenaWrappedDBIter* iter,
|
2017-11-28 20:42:28 +01:00
|
|
|
BlobDBImpl* blob_db, Env* env, Statistics* statistics)
|
|
|
|
: snapshot_(snapshot),
|
|
|
|
iter_(iter),
|
|
|
|
blob_db_(blob_db),
|
|
|
|
env_(env),
|
|
|
|
statistics_(statistics) {}
|
2017-10-18 02:24:25 +02:00
|
|
|
|
|
|
|
virtual ~BlobDBIterator() = default;
|
|
|
|
|
|
|
|
bool Valid() const override {
|
|
|
|
if (!iter_->Valid()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return status_.ok();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status status() const override {
|
|
|
|
if (!iter_->status().ok()) {
|
|
|
|
return iter_->status();
|
|
|
|
}
|
|
|
|
return status_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SeekToFirst() override {
|
2017-11-28 20:42:28 +01:00
|
|
|
StopWatch seek_sw(env_, statistics_, BLOB_DB_SEEK_MICROS);
|
|
|
|
RecordTick(statistics_, BLOB_DB_NUM_SEEK);
|
2017-10-18 02:24:25 +02:00
|
|
|
iter_->SeekToFirst();
|
2018-03-06 20:46:20 +01:00
|
|
|
while (UpdateBlobValue()) {
|
|
|
|
iter_->Next();
|
|
|
|
}
|
2017-10-18 02:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SeekToLast() override {
|
2017-11-28 20:42:28 +01:00
|
|
|
StopWatch seek_sw(env_, statistics_, BLOB_DB_SEEK_MICROS);
|
|
|
|
RecordTick(statistics_, BLOB_DB_NUM_SEEK);
|
2017-10-18 02:24:25 +02:00
|
|
|
iter_->SeekToLast();
|
2018-03-06 20:46:20 +01:00
|
|
|
while (UpdateBlobValue()) {
|
|
|
|
iter_->Prev();
|
|
|
|
}
|
2017-10-18 02:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Seek(const Slice& target) override {
|
2017-11-28 20:42:28 +01:00
|
|
|
StopWatch seek_sw(env_, statistics_, BLOB_DB_SEEK_MICROS);
|
|
|
|
RecordTick(statistics_, BLOB_DB_NUM_SEEK);
|
2017-10-18 02:24:25 +02:00
|
|
|
iter_->Seek(target);
|
2018-03-06 20:46:20 +01:00
|
|
|
while (UpdateBlobValue()) {
|
|
|
|
iter_->Next();
|
|
|
|
}
|
2017-10-18 02:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SeekForPrev(const Slice& target) override {
|
2017-11-28 20:42:28 +01:00
|
|
|
StopWatch seek_sw(env_, statistics_, BLOB_DB_SEEK_MICROS);
|
|
|
|
RecordTick(statistics_, BLOB_DB_NUM_SEEK);
|
2017-10-18 02:24:25 +02:00
|
|
|
iter_->SeekForPrev(target);
|
2018-03-06 20:46:20 +01:00
|
|
|
while (UpdateBlobValue()) {
|
|
|
|
iter_->Prev();
|
|
|
|
}
|
2017-10-18 02:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Next() override {
|
|
|
|
assert(Valid());
|
2017-11-28 20:42:28 +01:00
|
|
|
StopWatch next_sw(env_, statistics_, BLOB_DB_NEXT_MICROS);
|
|
|
|
RecordTick(statistics_, BLOB_DB_NUM_NEXT);
|
2017-10-18 02:24:25 +02:00
|
|
|
iter_->Next();
|
2018-03-06 20:46:20 +01:00
|
|
|
while (UpdateBlobValue()) {
|
|
|
|
iter_->Next();
|
|
|
|
}
|
2017-10-18 02:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Prev() override {
|
|
|
|
assert(Valid());
|
2017-11-28 20:42:28 +01:00
|
|
|
StopWatch prev_sw(env_, statistics_, BLOB_DB_PREV_MICROS);
|
|
|
|
RecordTick(statistics_, BLOB_DB_NUM_PREV);
|
2017-10-18 02:24:25 +02:00
|
|
|
iter_->Prev();
|
2018-03-06 20:46:20 +01:00
|
|
|
while (UpdateBlobValue()) {
|
|
|
|
iter_->Prev();
|
|
|
|
}
|
2017-10-18 02:24:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Slice key() const override {
|
|
|
|
assert(Valid());
|
|
|
|
return iter_->key();
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice value() const override {
|
|
|
|
assert(Valid());
|
|
|
|
if (!iter_->IsBlob()) {
|
|
|
|
return iter_->value();
|
|
|
|
}
|
|
|
|
return value_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator::Refresh() not supported.
|
|
|
|
|
|
|
|
private:
|
2018-03-06 20:46:20 +01:00
|
|
|
// Return true if caller should continue to next value.
|
|
|
|
bool UpdateBlobValue() {
|
2017-10-18 02:24:25 +02:00
|
|
|
TEST_SYNC_POINT("BlobDBIterator::UpdateBlobValue:Start:1");
|
|
|
|
TEST_SYNC_POINT("BlobDBIterator::UpdateBlobValue:Start:2");
|
|
|
|
value_.Reset();
|
2018-03-06 20:46:20 +01:00
|
|
|
if (iter_->Valid() && iter_->status().ok() && iter_->IsBlob()) {
|
|
|
|
Status s = blob_db_->GetBlobValue(iter_->key(), iter_->value(), &value_);
|
|
|
|
if (s.IsNotFound()) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
if (!s.ok()) {
|
|
|
|
status_ = s;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
2017-10-18 02:24:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<ManagedSnapshot> snapshot_;
|
|
|
|
std::unique_ptr<ArenaWrappedDBIter> iter_;
|
|
|
|
BlobDBImpl* blob_db_;
|
2017-11-28 20:42:28 +01:00
|
|
|
Env* env_;
|
|
|
|
Statistics* statistics_;
|
2017-10-18 02:24:25 +02:00
|
|
|
Status status_;
|
|
|
|
PinnableSlice value_;
|
|
|
|
};
|
|
|
|
} // namespace blob_db
|
|
|
|
} // namespace rocksdb
|
|
|
|
#endif // !ROCKSDB_LITE
|