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 "leveldb/iterator.h"
6
7namespace leveldb {
8
9Iterator::Iterator() {
10 cleanup_head_.function = nullptr;
11 cleanup_head_.next = nullptr;
12}
13
14Iterator::~Iterator() {
15 if (!cleanup_head_.IsEmpty()) {
16 cleanup_head_.Run();
17 for (CleanupNode* node = cleanup_head_.next; node != nullptr;) {
18 node->Run();
19 CleanupNode* next_node = node->next;
20 delete node;
21 node = next_node;
22 }
23 }
24}
25
26void Iterator::RegisterCleanup(CleanupFunction func, void* arg1, void* arg2) {
27 assert(func != nullptr);
28 CleanupNode* node;
29 if (cleanup_head_.IsEmpty()) {
30 node = &cleanup_head_;
31 } else {
32 node = new CleanupNode();
33 node->next = cleanup_head_.next;
34 cleanup_head_.next = node;
35 }
36 node->function = func;
37 node->arg1 = arg1;
38 node->arg2 = arg2;
39}
40
41namespace {
42
43class EmptyIterator : public Iterator {
44 public:
45 EmptyIterator(const Status& s) : status_(s) {}
46 ~EmptyIterator() override = default;
47
48 bool Valid() const override { return false; }
49 void Seek(const Slice& target) override {}
50 void SeekToFirst() override {}
51 void SeekToLast() override {}
52 void Next() override { assert(false); }
53 void Prev() override { assert(false); }
54 Slice key() const override {
55 assert(false);
56 return Slice();
57 }
58 Slice value() const override {
59 assert(false);
60 return Slice();
61 }
62 Status status() const override { return status_; }
63
64 private:
65 Status status_;
66};
67
68} // anonymous namespace
69
70Iterator* NewEmptyIterator() { return new EmptyIterator(Status::OK()); }
71
72Iterator* NewErrorIterator(const Status& status) {
73 return new EmptyIterator(status);
74}
75
76} // namespace leveldb
77