Coverage Report

Created: 2026-02-14 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/db/arena_wrapped_db_iter.h
Line
Count
Source
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
//
6
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7
// Use of this source code is governed by a BSD-style license that can be
8
// found in the LICENSE file. See the AUTHORS file for names of contributors.
9
10
#pragma once
11
#include <stdint.h>
12
13
#include <string>
14
15
#include "db/db_impl/db_impl.h"
16
#include "db/db_iter.h"
17
#include "db/range_del_aggregator.h"
18
#include "memory/arena.h"
19
#include "options/cf_options.h"
20
#include "rocksdb/db.h"
21
#include "rocksdb/iterator.h"
22
23
namespace ROCKSDB_NAMESPACE {
24
25
class Arena;
26
class Version;
27
28
// A wrapper iterator which wraps DB Iterator and the arena, with which the DB
29
// iterator is supposed to be allocated. This class is used as an entry point of
30
// a iterator hierarchy whose memory can be allocated inline. In that way,
31
// accessing the iterator tree can be more cache friendly. It is also faster
32
// to allocate.
33
// When using the class's Iterator interface, the behavior is exactly
34
// the same as the inner DBIter.
35
class ArenaWrappedDBIter : public Iterator {
36
 public:
37
4.68k
  ~ArenaWrappedDBIter() override {
38
4.68k
    if (db_iter_ != nullptr) {
39
4.68k
      db_iter_->~DBIter();
40
4.68k
    } else {
41
0
      assert(false);
42
0
    }
43
4.68k
  }
44
45
  // Get the arena to be used to allocate memory for DBIter to be wrapped,
46
  // as well as child iterators in it.
47
4.68k
  virtual Arena* GetArena() { return &arena_; }
48
49
4.68k
  const ReadOptions& GetReadOptions() { return read_options_; }
50
51
  // Set the internal iterator wrapped inside the DB Iterator. Usually it is
52
  // a merging iterator.
53
4.68k
  virtual void SetIterUnderDBIter(InternalIterator* iter) {
54
4.68k
    db_iter_->SetIter(iter);
55
4.68k
  }
56
57
  void SetMemtableRangetombstoneIter(
58
1.69k
      std::unique_ptr<TruncatedRangeDelIterator>* iter) {
59
1.69k
    memtable_range_tombstone_iter_ = iter;
60
1.69k
  }
61
62
2.67k
  bool Valid() const override { return db_iter_->Valid(); }
63
1.41k
  void SeekToFirst() override { db_iter_->SeekToFirst(); }
64
0
  void SeekToLast() override { db_iter_->SeekToLast(); }
65
  // 'target' does not contain timestamp, even if user timestamp feature is
66
  // enabled.
67
0
  void Seek(const Slice& target) override {
68
0
    MaybeAutoRefresh(true /* is_seek */, DBIter::kForward);
69
0
    db_iter_->Seek(target);
70
0
  }
71
72
1.51k
  void SeekForPrev(const Slice& target) override {
73
1.51k
    MaybeAutoRefresh(true /* is_seek */, DBIter::kReverse);
74
1.51k
    db_iter_->SeekForPrev(target);
75
1.51k
  }
76
77
1.25k
  void Next() override {
78
1.25k
    db_iter_->Next();
79
1.25k
    MaybeAutoRefresh(false /* is_seek */, DBIter::kForward);
80
1.25k
  }
81
82
0
  void Prev() override {
83
0
    db_iter_->Prev();
84
0
    MaybeAutoRefresh(false /* is_seek */, DBIter::kReverse);
85
0
  }
86
87
36
  Slice key() const override { return db_iter_->key(); }
88
36
  Slice value() const override { return db_iter_->value(); }
89
0
  const WideColumns& columns() const override { return db_iter_->columns(); }
90
0
  Status status() const override { return db_iter_->status(); }
91
0
  Slice timestamp() const override { return db_iter_->timestamp(); }
92
0
  bool IsBlob() const { return db_iter_->IsBlob(); }
93
94
  Status GetProperty(std::string prop_name, std::string* prop) override;
95
96
  Status Refresh() override;
97
  Status Refresh(const Snapshot*) override;
98
99
0
  bool PrepareValue() override { return db_iter_->PrepareValue(); }
100
101
0
  void Prepare(const MultiScanArgs& scan_opts) override {
102
0
    db_iter_->Prepare(scan_opts);
103
0
  }
104
105
  // FIXME: we could just pass SV in for mutable cf option, version and version
106
  // number, but this is used by SstFileReader which does not have a SV.
107
  void Init(Env* env, const ReadOptions& read_options,
108
            const ImmutableOptions& ioptions,
109
            const MutableCFOptions& mutable_cf_options, const Version* version,
110
            const SequenceNumber& sequence, uint64_t version_number,
111
            ReadCallback* read_callback, ColumnFamilyHandleImpl* cfh,
112
            bool expose_blob_index, bool allow_refresh,
113
            ReadOnlyMemTable* active_mem);
114
115
  // Store some parameters so we can refresh the iterator at a later point
116
  // with these same params
117
  void StoreRefreshInfo(ColumnFamilyHandleImpl* cfh,
118
4.68k
                        ReadCallback* read_callback, bool expose_blob_index) {
119
4.68k
    cfh_ = cfh;
120
4.68k
    read_callback_ = read_callback;
121
4.68k
    expose_blob_index_ = expose_blob_index;
122
4.68k
  }
123
124
 private:
125
  void DoRefresh(const Snapshot* snapshot, uint64_t sv_number);
126
  void MaybeAutoRefresh(bool is_seek, DBIter::Direction direction);
127
128
  DBIter* db_iter_ = nullptr;
129
  Arena arena_;
130
  uint64_t sv_number_;
131
  ColumnFamilyHandleImpl* cfh_ = nullptr;
132
  ReadOptions read_options_;
133
  ReadCallback* read_callback_;
134
  bool expose_blob_index_ = false;
135
  bool allow_refresh_ = true;
136
  bool allow_mark_memtable_for_flush_ = true;
137
  // If this is nullptr, it means the mutable memtable does not contain range
138
  // tombstone when added under this DBIter.
139
  std::unique_ptr<TruncatedRangeDelIterator>* memtable_range_tombstone_iter_ =
140
      nullptr;
141
};
142
143
ArenaWrappedDBIter* NewArenaWrappedDbIterator(
144
    Env* env, const ReadOptions& read_options, ColumnFamilyHandleImpl* cfh,
145
    SuperVersion* sv, const SequenceNumber& sequence,
146
    ReadCallback* read_callback, DBImpl* db_impl, bool expose_blob_index,
147
    bool allow_refresh, bool allow_mark_memtable_for_flush);
148
}  // namespace ROCKSDB_NAMESPACE