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