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