arena_wrapped_db_iter.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/dbformat.h"
  15. #include "db/range_del_aggregator.h"
  16. #include "memory/arena.h"
  17. #include "options/cf_options.h"
  18. #include "rocksdb/db.h"
  19. #include "rocksdb/iterator.h"
  20. #include "util/autovector.h"
  21. namespace ROCKSDB_NAMESPACE {
  22. class Arena;
  23. // A wrapper iterator which wraps DB Iterator and the arena, with which the DB
  24. // iterator is supposed to be allocated. This class is used as an entry point of
  25. // a iterator hierarchy whose memory can be allocated inline. In that way,
  26. // accessing the iterator tree can be more cache friendly. It is also faster
  27. // to allocate.
  28. // When using the class's Iterator interface, the behavior is exactly
  29. // the same as the inner DBIter.
  30. class ArenaWrappedDBIter : public Iterator {
  31. public:
  32. virtual ~ArenaWrappedDBIter() { db_iter_->~DBIter(); }
  33. // Get the arena to be used to allocate memory for DBIter to be wrapped,
  34. // as well as child iterators in it.
  35. virtual Arena* GetArena() { return &arena_; }
  36. virtual ReadRangeDelAggregator* GetRangeDelAggregator() {
  37. return db_iter_->GetRangeDelAggregator();
  38. }
  39. // Set the internal iterator wrapped inside the DB Iterator. Usually it is
  40. // a merging iterator.
  41. virtual void SetIterUnderDBIter(InternalIterator* iter) {
  42. db_iter_->SetIter(iter);
  43. }
  44. bool Valid() const override { return db_iter_->Valid(); }
  45. void SeekToFirst() override { db_iter_->SeekToFirst(); }
  46. void SeekToLast() override { db_iter_->SeekToLast(); }
  47. void Seek(const Slice& target) override { db_iter_->Seek(target); }
  48. void SeekForPrev(const Slice& target) override {
  49. db_iter_->SeekForPrev(target);
  50. }
  51. void Next() override { db_iter_->Next(); }
  52. void Prev() override { db_iter_->Prev(); }
  53. Slice key() const override { return db_iter_->key(); }
  54. Slice value() const override { return db_iter_->value(); }
  55. Status status() const override { return db_iter_->status(); }
  56. bool IsBlob() const { return db_iter_->IsBlob(); }
  57. Status GetProperty(std::string prop_name, std::string* prop) override;
  58. Status Refresh() override;
  59. void Init(Env* env, const ReadOptions& read_options,
  60. const ImmutableCFOptions& cf_options,
  61. const MutableCFOptions& mutable_cf_options,
  62. const SequenceNumber& sequence,
  63. uint64_t max_sequential_skip_in_iterations, uint64_t version_number,
  64. ReadCallback* read_callback, DBImpl* db_impl, ColumnFamilyData* cfd,
  65. bool allow_blob, bool allow_refresh);
  66. // Store some parameters so we can refresh the iterator at a later point
  67. // with these same params
  68. void StoreRefreshInfo(const ReadOptions& read_options, DBImpl* db_impl,
  69. ColumnFamilyData* cfd, ReadCallback* read_callback,
  70. bool allow_blob) {
  71. read_options_ = read_options;
  72. db_impl_ = db_impl;
  73. cfd_ = cfd;
  74. read_callback_ = read_callback;
  75. allow_blob_ = allow_blob;
  76. }
  77. private:
  78. DBIter* db_iter_;
  79. Arena arena_;
  80. uint64_t sv_number_;
  81. ColumnFamilyData* cfd_ = nullptr;
  82. DBImpl* db_impl_ = nullptr;
  83. ReadOptions read_options_;
  84. ReadCallback* read_callback_;
  85. bool allow_blob_ = false;
  86. bool allow_refresh_ = true;
  87. };
  88. // Generate the arena wrapped iterator class.
  89. // `db_impl` and `cfd` are used for reneweal. If left null, renewal will not
  90. // be supported.
  91. extern ArenaWrappedDBIter* NewArenaWrappedDbIterator(
  92. Env* env, const ReadOptions& read_options,
  93. const ImmutableCFOptions& cf_options,
  94. const MutableCFOptions& mutable_cf_options, const SequenceNumber& sequence,
  95. uint64_t max_sequential_skip_in_iterations, uint64_t version_number,
  96. ReadCallback* read_callback, DBImpl* db_impl = nullptr,
  97. ColumnFamilyData* cfd = nullptr, bool allow_blob = false,
  98. bool allow_refresh = true);
  99. } // namespace ROCKSDB_NAMESPACE