arena_wrapped_db_iter.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <stdint.h>
  11. #include <string>
  12. #include "db/db_impl/db_impl.h"
  13. #include "db/db_iter.h"
  14. #include "db/range_del_aggregator.h"
  15. #include "memory/arena.h"
  16. #include "options/cf_options.h"
  17. #include "rocksdb/db.h"
  18. #include "rocksdb/iterator.h"
  19. namespace ROCKSDB_NAMESPACE {
  20. class Arena;
  21. class Version;
  22. // A wrapper iterator which wraps DB Iterator and the arena, with which the DB
  23. // iterator is supposed to be allocated. This class is used as an entry point of
  24. // a iterator hierarchy whose memory can be allocated inline. In that way,
  25. // accessing the iterator tree can be more cache friendly. It is also faster
  26. // to allocate.
  27. // When using the class's Iterator interface, the behavior is exactly
  28. // the same as the inner DBIter.
  29. class ArenaWrappedDBIter : public Iterator {
  30. public:
  31. ~ArenaWrappedDBIter() override {
  32. if (db_iter_ != nullptr) {
  33. db_iter_->~DBIter();
  34. } else {
  35. assert(false);
  36. }
  37. }
  38. // Get the arena to be used to allocate memory for DBIter to be wrapped,
  39. // as well as child iterators in it.
  40. virtual Arena* GetArena() { return &arena_; }
  41. const ReadOptions& GetReadOptions() { return read_options_; }
  42. // Set the internal iterator wrapped inside the DB Iterator. Usually it is
  43. // a merging iterator.
  44. virtual void SetIterUnderDBIter(InternalIterator* iter) {
  45. db_iter_->SetIter(iter);
  46. }
  47. void SetMemtableRangetombstoneIter(
  48. std::unique_ptr<TruncatedRangeDelIterator>* iter) {
  49. memtable_range_tombstone_iter_ = iter;
  50. }
  51. bool Valid() const override { return db_iter_->Valid(); }
  52. void SeekToFirst() override { db_iter_->SeekToFirst(); }
  53. void SeekToLast() override { db_iter_->SeekToLast(); }
  54. // 'target' does not contain timestamp, even if user timestamp feature is
  55. // enabled.
  56. void Seek(const Slice& target) override {
  57. MaybeAutoRefresh(true /* is_seek */, DBIter::kForward);
  58. db_iter_->Seek(target);
  59. }
  60. void SeekForPrev(const Slice& target) override {
  61. MaybeAutoRefresh(true /* is_seek */, DBIter::kReverse);
  62. db_iter_->SeekForPrev(target);
  63. }
  64. void Next() override {
  65. db_iter_->Next();
  66. MaybeAutoRefresh(false /* is_seek */, DBIter::kForward);
  67. }
  68. void Prev() override {
  69. db_iter_->Prev();
  70. MaybeAutoRefresh(false /* is_seek */, DBIter::kReverse);
  71. }
  72. Slice key() const override { return db_iter_->key(); }
  73. Slice value() const override { return db_iter_->value(); }
  74. const WideColumns& columns() const override { return db_iter_->columns(); }
  75. Status status() const override { return db_iter_->status(); }
  76. Slice timestamp() const override { return db_iter_->timestamp(); }
  77. bool IsBlob() const { return db_iter_->IsBlob(); }
  78. Status GetProperty(std::string prop_name, std::string* prop) override;
  79. Status Refresh() override;
  80. Status Refresh(const Snapshot*) override;
  81. bool PrepareValue() override { return db_iter_->PrepareValue(); }
  82. void Prepare(const MultiScanArgs& scan_opts) override {
  83. db_iter_->Prepare(scan_opts);
  84. }
  85. // FIXME: we could just pass SV in for mutable cf option, version and version
  86. // number, but this is used by SstFileReader which does not have a SV.
  87. void Init(Env* env, const ReadOptions& read_options,
  88. const ImmutableOptions& ioptions,
  89. const MutableCFOptions& mutable_cf_options, const Version* version,
  90. const SequenceNumber& sequence, uint64_t version_number,
  91. ReadCallback* read_callback, ColumnFamilyHandleImpl* cfh,
  92. bool expose_blob_index, bool allow_refresh,
  93. ReadOnlyMemTable* active_mem);
  94. // Store some parameters so we can refresh the iterator at a later point
  95. // with these same params
  96. void StoreRefreshInfo(ColumnFamilyHandleImpl* cfh,
  97. ReadCallback* read_callback, bool expose_blob_index) {
  98. cfh_ = cfh;
  99. read_callback_ = read_callback;
  100. expose_blob_index_ = expose_blob_index;
  101. }
  102. private:
  103. void DoRefresh(const Snapshot* snapshot, uint64_t sv_number);
  104. void MaybeAutoRefresh(bool is_seek, DBIter::Direction direction);
  105. DBIter* db_iter_ = nullptr;
  106. Arena arena_;
  107. uint64_t sv_number_;
  108. ColumnFamilyHandleImpl* cfh_ = nullptr;
  109. ReadOptions read_options_;
  110. ReadCallback* read_callback_;
  111. bool expose_blob_index_ = false;
  112. bool allow_refresh_ = true;
  113. bool allow_mark_memtable_for_flush_ = true;
  114. // If this is nullptr, it means the mutable memtable does not contain range
  115. // tombstone when added under this DBIter.
  116. std::unique_ptr<TruncatedRangeDelIterator>* memtable_range_tombstone_iter_ =
  117. nullptr;
  118. };
  119. ArenaWrappedDBIter* NewArenaWrappedDbIterator(
  120. Env* env, const ReadOptions& read_options, ColumnFamilyHandleImpl* cfh,
  121. SuperVersion* sv, const SequenceNumber& sequence,
  122. ReadCallback* read_callback, DBImpl* db_impl, bool expose_blob_index,
  123. bool allow_refresh, bool allow_mark_memtable_for_flush);
  124. } // namespace ROCKSDB_NAMESPACE