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