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.
|
|
|
|
|
2011-03-30 18:35:40 +00:00
|
|
|
#include "leveldb/iterator.h"
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
Iterator::Iterator() {
|
2018-06-04 12:29:13 -07:00
|
|
|
cleanup_head_.function = nullptr;
|
|
|
|
cleanup_head_.next = nullptr;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Iterator::~Iterator() {
|
2018-06-04 12:29:13 -07:00
|
|
|
if (!cleanup_head_.IsEmpty()) {
|
|
|
|
cleanup_head_.Run();
|
2019-05-02 11:01:00 -07:00
|
|
|
for (CleanupNode* node = cleanup_head_.next; node != nullptr;) {
|
2018-06-04 12:29:13 -07:00
|
|
|
node->Run();
|
|
|
|
CleanupNode* next_node = node->next;
|
|
|
|
delete node;
|
|
|
|
node = next_node;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Iterator::RegisterCleanup(CleanupFunction func, void* arg1, void* arg2) {
|
2018-04-10 16:18:06 -07:00
|
|
|
assert(func != nullptr);
|
2018-06-04 12:29:13 -07:00
|
|
|
CleanupNode* node;
|
|
|
|
if (cleanup_head_.IsEmpty()) {
|
|
|
|
node = &cleanup_head_;
|
2011-03-18 22:37:00 +00:00
|
|
|
} else {
|
2018-06-04 12:29:13 -07:00
|
|
|
node = new CleanupNode();
|
|
|
|
node->next = cleanup_head_.next;
|
|
|
|
cleanup_head_.next = node;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
2018-06-04 12:29:13 -07:00
|
|
|
node->function = func;
|
|
|
|
node->arg1 = arg1;
|
|
|
|
node->arg2 = arg2;
|
2011-03-18 22:37:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2018-06-04 12:29:13 -07:00
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
class EmptyIterator : public Iterator {
|
|
|
|
public:
|
2019-05-02 11:01:00 -07:00
|
|
|
EmptyIterator(const Status& s) : status_(s) {}
|
2018-06-04 12:29:13 -07:00
|
|
|
~EmptyIterator() override = default;
|
|
|
|
|
|
|
|
bool Valid() const override { return false; }
|
2019-05-02 11:01:00 -07:00
|
|
|
void Seek(const Slice& target) override {}
|
|
|
|
void SeekToFirst() override {}
|
|
|
|
void SeekToLast() override {}
|
2018-06-04 12:29:13 -07:00
|
|
|
void Next() override { assert(false); }
|
|
|
|
void Prev() override { assert(false); }
|
2019-05-02 11:01:00 -07:00
|
|
|
Slice key() const override {
|
|
|
|
assert(false);
|
|
|
|
return Slice();
|
|
|
|
}
|
|
|
|
Slice value() const override {
|
|
|
|
assert(false);
|
|
|
|
return Slice();
|
|
|
|
}
|
2018-06-04 12:29:13 -07:00
|
|
|
Status status() const override { return status_; }
|
|
|
|
|
2011-03-18 22:37:00 +00:00
|
|
|
private:
|
|
|
|
Status status_;
|
|
|
|
};
|
2018-06-04 12:29:13 -07:00
|
|
|
|
|
|
|
} // anonymous namespace
|
2011-03-18 22:37:00 +00:00
|
|
|
|
2019-05-02 11:01:00 -07:00
|
|
|
Iterator* NewEmptyIterator() { return new EmptyIterator(Status::OK()); }
|
2011-03-18 22:37:00 +00:00
|
|
|
|
|
|
|
Iterator* NewErrorIterator(const Status& status) {
|
|
|
|
return new EmptyIterator(status);
|
|
|
|
}
|
|
|
|
|
2011-10-31 17:22:06 +00:00
|
|
|
} // namespace leveldb
|