Coverage Report

Created: 2023-01-25 06:32

/src/leveldb/table/iterator_wrapper.h
Line
Count
Source (jump to first uncovered line)
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
547k
  IteratorWrapper() : iter_(nullptr), valid_(false) {}
20
1.58M
  explicit IteratorWrapper(Iterator* iter) : iter_(nullptr) { Set(iter); }
21
2.12M
  ~IteratorWrapper() { delete iter_; }
22
10.2M
  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
3.92M
  void Set(Iterator* iter) {
27
3.92M
    delete iter_;
28
3.92M
    iter_ = iter;
29
3.92M
    if (iter_ == nullptr) {
30
1.81M
      valid_ = false;
31
2.11M
    } else {
32
2.11M
      Update();
33
2.11M
    }
34
3.92M
  }
35
36
  // Iterator interface methods
37
28.6M
  bool Valid() const { return valid_; }
38
21.0M
  Slice key() const {
39
21.0M
    assert(Valid());
40
21.0M
    return key_;
41
21.0M
  }
42
7.98M
  Slice value() const {
43
7.98M
    assert(Valid());
44
7.98M
    return iter_->value();
45
7.98M
  }
46
  // Methods below require iter() != nullptr
47
1.29M
  Status status() const {
48
1.29M
    assert(iter_);
49
1.29M
    return iter_->status();
50
1.29M
  }
51
9.53M
  void Next() {
52
9.53M
    assert(iter_);
53
9.53M
    iter_->Next();
54
9.53M
    Update();
55
9.53M
  }
56
0
  void Prev() {
57
0
    assert(iter_);
58
0
    iter_->Prev();
59
0
    Update();
60
0
  }
61
0
  void Seek(const Slice& k) {
62
0
    assert(iter_);
63
0
    iter_->Seek(k);
64
0
    Update();
65
0
  }
66
1.56M
  void SeekToFirst() {
67
1.56M
    assert(iter_);
68
1.56M
    iter_->SeekToFirst();
69
1.56M
    Update();
70
1.56M
  }
71
0
  void SeekToLast() {
72
0
    assert(iter_);
73
0
    iter_->SeekToLast();
74
0
    Update();
75
0
  }
76
77
 private:
78
13.1M
  void Update() {
79
13.1M
    valid_ = iter_->Valid();
80
13.1M
    if (valid_) {
81
9.54M
      key_ = iter_->key();
82
9.54M
    }
83
13.1M
  }
84
85
  Iterator* iter_;
86
  bool valid_;
87
  Slice key_;
88
};
89
90
}  // namespace leveldb
91
92
#endif  // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_