merging_iterator.h 4.3 KB

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