Coverage Report

Created: 2026-07-14 18:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/bitcoin/src/leveldb/table/iterator_wrapper.h
Line
Count
Source
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
#ifndef STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
6
#define STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
7
8
#include "leveldb/iterator.h"
9
#include "leveldb/slice.h"
10
11
namespace leveldb {
12
13
// A internal wrapper class with an interface similar to Iterator that
14
// caches the valid() and key() results for an underlying iterator.
15
// This can help avoid virtual function calls and also gives better
16
// cache locality.
17
class IteratorWrapper {
18
 public:
19
0
  IteratorWrapper() : iter_(nullptr), valid_(false) {}
20
32
  explicit IteratorWrapper(Iterator* iter) : iter_(nullptr) { Set(iter); }
21
32
  ~IteratorWrapper() { delete iter_; }
22
16
  Iterator* iter() const { return iter_; }
23
24
  // Takes ownership of "iter" and will delete it when destroyed, or
25
  // when Set() is invoked again.
26
32
  void Set(Iterator* iter) {
27
32
    delete iter_;
28
32
    iter_ = iter;
29
32
    if (iter_ == nullptr) {
  Branch (29:9): [True: 16, False: 16]
30
16
      valid_ = false;
31
16
    } else {
32
16
      Update();
33
16
    }
34
32
  }
35
36
  // Iterator interface methods
37
0
  bool Valid() const { return valid_; }
38
0
  Slice key() const {
39
0
    assert(Valid());
  Branch (39:5): [True: 0, False: 0]
40
0
    return key_;
41
0
  }
42
0
  Slice value() const {
43
0
    assert(Valid());
  Branch (43:5): [True: 0, False: 0]
44
0
    return iter_->value();
45
0
  }
46
  // Methods below require iter() != nullptr
47
16
  Status status() const {
48
16
    assert(iter_);
  Branch (48:5): [True: 16, False: 0]
49
16
    return iter_->status();
50
16
  }
51
0
  void Next() {
52
0
    assert(iter_);
  Branch (52:5): [True: 0, False: 0]
53
0
    iter_->Next();
54
0
    Update();
55
0
  }
56
0
  void Prev() {
57
0
    assert(iter_);
  Branch (57:5): [True: 0, False: 0]
58
0
    iter_->Prev();
59
0
    Update();
60
0
  }
61
0
  void Seek(const Slice& k) {
62
0
    assert(iter_);
  Branch (62:5): [True: 0, False: 0]
63
0
    iter_->Seek(k);
64
0
    Update();
65
0
  }
66
0
  void SeekToFirst() {
67
0
    assert(iter_);
  Branch (67:5): [True: 0, False: 0]
68
0
    iter_->SeekToFirst();
69
0
    Update();
70
0
  }
71
0
  void SeekToLast() {
72
0
    assert(iter_);
  Branch (72:5): [True: 0, False: 0]
73
0
    iter_->SeekToLast();
74
0
    Update();
75
0
  }
76
77
 private:
78
16
  void Update() {
79
16
    valid_ = iter_->Valid();
80
16
    if (valid_) {
  Branch (80:9): [True: 0, False: 16]
81
0
      key_ = iter_->key();
82
0
    }
83
16
  }
84
85
  Iterator* iter_;
86
  bool valid_;
87
  Slice key_;
88
};
89
90
}  // namespace leveldb
91
92
#endif  // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_