xref: /aosp_15_r20/external/leveldb/table/iterator.cc (revision 9507f98c5f32dee4b5f9e4a38cd499f3ff5c4490)
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 
7 namespace leveldb {
8 
Iterator()9 Iterator::Iterator() {
10   cleanup_head_.function = nullptr;
11   cleanup_head_.next = nullptr;
12 }
13 
~Iterator()14 Iterator::~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 
RegisterCleanup(CleanupFunction func,void * arg1,void * arg2)26 void 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 
41 namespace {
42 
43 class EmptyIterator : public Iterator {
44  public:
EmptyIterator(const Status & s)45   EmptyIterator(const Status& s) : status_(s) {}
46   ~EmptyIterator() override = default;
47 
Valid() const48   bool Valid() const override { return false; }
Seek(const Slice & target)49   void Seek(const Slice& target) override {}
SeekToFirst()50   void SeekToFirst() override {}
SeekToLast()51   void SeekToLast() override {}
Next()52   void Next() override { assert(false); }
Prev()53   void Prev() override { assert(false); }
key() const54   Slice key() const override {
55     assert(false);
56     return Slice();
57   }
value() const58   Slice value() const override {
59     assert(false);
60     return Slice();
61   }
status() const62   Status status() const override { return status_; }
63 
64  private:
65   Status status_;
66 };
67 
68 }  // anonymous namespace
69 
NewEmptyIterator()70 Iterator* NewEmptyIterator() { return new EmptyIterator(Status::OK()); }
71 
NewErrorIterator(const Status & status)72 Iterator* NewErrorIterator(const Status& status) {
73   return new EmptyIterator(status);
74 }
75 
76 }  // namespace leveldb
77