Coverage Report

Created: 2024-09-08 07:17

/src/rocksdb/table/merging_iterator.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
12
#include "db/range_del_aggregator.h"
13
#include "rocksdb/slice.h"
14
#include "rocksdb/types.h"
15
#include "table/iterator_wrapper.h"
16
17
namespace ROCKSDB_NAMESPACE {
18
19
class Arena;
20
class ArenaWrappedDBIter;
21
class InternalKeyComparator;
22
23
template <class TValue>
24
class InternalIteratorBase;
25
using InternalIterator = InternalIteratorBase<Slice>;
26
27
// Return an iterator that provides the union of the data in
28
// children[0,n-1].  Takes ownership of the child iterators and
29
// will delete them when the result iterator is deleted.
30
//
31
// The result does no duplicate suppression.  I.e., if a particular
32
// key is present in K child iterators, it will be yielded K times.
33
//
34
// REQUIRES: n >= 0
35
InternalIterator* NewMergingIterator(const InternalKeyComparator* comparator,
36
                                     InternalIterator** children, int n,
37
                                     Arena* arena = nullptr,
38
                                     bool prefix_seek_mode = false);
39
40
// The iterator returned by NewMergingIterator() and
41
// MergeIteratorBuilder::Finish(). MergingIterator handles the merging of data
42
// from different point and/or range tombstone iterators.
43
class MergingIterator;
44
45
// A builder class to for an iterator that provides the union of data
46
// of input iterators. Two APIs are provided to add input iterators. User should
47
// only call one of them exclusively depending on if range tombstone should be
48
// processed.
49
class MergeIteratorBuilder {
50
 public:
51
  // comparator: the comparator used in merging comparator
52
  // arena: where the merging iterator needs to be allocated from.
53
  explicit MergeIteratorBuilder(const InternalKeyComparator* comparator,
54
                                Arena* arena, bool prefix_seek_mode = false,
55
                                const Slice* iterate_upper_bound = nullptr);
56
  ~MergeIteratorBuilder();
57
58
  // Add point key iterator `iter` to the merging iterator.
59
  void AddIterator(InternalIterator* iter);
60
61
  // Add a point key iterator and a range tombstone iterator.
62
  // `tombstone_iter_ptr` should and only be set by LevelIterator.
63
  // *tombstone_iter_ptr will be set to where the merging iterator stores
64
  // `tombstone_iter` when MergeIteratorBuilder::Finish() is called. This is
65
  // used by LevelIterator to update range tombstone iters when switching to a
66
  // different SST file. If a single point iterator with a nullptr range
67
  // tombstone iterator is provided, and the point iterator is not a level
68
  // iterator, then this builder will return the point iterator directly,
69
  // instead of creating a merging iterator on top of it. Internally, if all
70
  // point iterators are not LevelIterator, then range tombstone iterator is
71
  // only added to the merging iter if there is a non-null `tombstone_iter`.
72
  void AddPointAndTombstoneIterator(
73
      InternalIterator* point_iter,
74
      std::unique_ptr<TruncatedRangeDelIterator>&& tombstone_iter,
75
      std::unique_ptr<TruncatedRangeDelIterator>** tombstone_iter_ptr =
76
          nullptr);
77
78
  // Get arena used to build the merging iterator. It is called one a child
79
  // iterator needs to be allocated.
80
6.23k
  Arena* GetArena() { return arena; }
81
82
  // Return the result merging iterator.
83
  // If db_iter is not nullptr, then db_iter->SetMemtableRangetombstoneIter()
84
  // will be called with pointer to where the merging iterator
85
  // stores the memtable range tombstone iterator.
86
  // This is used for DB iterator to refresh memtable range tombstones.
87
  InternalIterator* Finish(ArenaWrappedDBIter* db_iter = nullptr);
88
89
 private:
90
  MergingIterator* merge_iter;
91
  InternalIterator* first_iter;
92
  bool use_merging_iter;
93
  Arena* arena;
94
  // Used to set LevelIterator.range_tombstone_iter_.
95
  // See AddRangeTombstoneIterator() implementation for more detail.
96
  std::vector<std::pair<size_t, std::unique_ptr<TruncatedRangeDelIterator>**>>
97
      range_del_iter_ptrs_;
98
};
99
100
}  // namespace ROCKSDB_NAMESPACE