forward_iterator.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #pragma once
  6. #include <queue>
  7. #include <string>
  8. #include <vector>
  9. #include "memory/arena.h"
  10. #include "rocksdb/comparator.h"
  11. #include "rocksdb/db.h"
  12. #include "rocksdb/iterator.h"
  13. #include "rocksdb/options.h"
  14. #include "table/internal_iterator.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. class DBImpl;
  17. class Env;
  18. struct SuperVersion;
  19. class ColumnFamilyData;
  20. class ForwardLevelIterator;
  21. class VersionStorageInfo;
  22. struct FileMetaData;
  23. class MinIterComparator {
  24. public:
  25. explicit MinIterComparator(const CompareInterface* comparator)
  26. : comparator_(comparator) {}
  27. bool operator()(InternalIterator* a, InternalIterator* b) {
  28. return comparator_->Compare(a->key(), b->key()) > 0;
  29. }
  30. private:
  31. const CompareInterface* comparator_;
  32. };
  33. using MinIterHeap =
  34. std::priority_queue<InternalIterator*, std::vector<InternalIterator*>,
  35. MinIterComparator>;
  36. // TODO: name to TailingIterator
  37. /**
  38. * ForwardIterator is a special type of iterator that only supports Seek()
  39. * and Next(). It is expected to perform better than TailingIterator by
  40. * removing the encapsulation and making all information accessible within
  41. * the iterator. At the current implementation, snapshot is taken at the
  42. * time Seek() is called. The Next() followed do not see new values after.
  43. */
  44. class ForwardIterator : public InternalIterator {
  45. public:
  46. ForwardIterator(DBImpl* db, const ReadOptions& read_options,
  47. ColumnFamilyData* cfd, SuperVersion* current_sv = nullptr,
  48. bool allow_unprepared_value = false);
  49. virtual ~ForwardIterator();
  50. void SeekForPrev(const Slice& /*target*/) override {
  51. status_ = Status::NotSupported("ForwardIterator::SeekForPrev()");
  52. valid_ = false;
  53. }
  54. void SeekToLast() override {
  55. status_ = Status::NotSupported("ForwardIterator::SeekToLast()");
  56. valid_ = false;
  57. }
  58. void Prev() override {
  59. status_ = Status::NotSupported("ForwardIterator::Prev");
  60. valid_ = false;
  61. }
  62. bool Valid() const override;
  63. void SeekToFirst() override;
  64. void Seek(const Slice& target) override;
  65. void Next() override;
  66. Slice key() const override;
  67. Slice value() const override;
  68. uint64_t write_unix_time() const override;
  69. Status status() const override;
  70. bool PrepareValue() override;
  71. Status GetProperty(std::string prop_name, std::string* prop) override;
  72. void SetPinnedItersMgr(PinnedIteratorsManager* pinned_iters_mgr) override;
  73. bool IsKeyPinned() const override;
  74. bool IsValuePinned() const override;
  75. bool TEST_CheckDeletedIters(int* deleted_iters, int* num_iters);
  76. private:
  77. void Cleanup(bool release_sv);
  78. // Unreference and, if needed, clean up the current SuperVersion. This is
  79. // either done immediately or deferred until this iterator is unpinned by
  80. // PinnedIteratorsManager.
  81. void SVCleanup();
  82. static void SVCleanup(DBImpl* db, SuperVersion* sv,
  83. bool background_purge_on_iterator_cleanup);
  84. static void DeferredSVCleanup(void* arg);
  85. void RebuildIterators(bool refresh_sv);
  86. void RenewIterators();
  87. void BuildLevelIterators(const VersionStorageInfo* vstorage,
  88. SuperVersion* sv);
  89. void ResetIncompleteIterators();
  90. void SeekInternal(const Slice& internal_key, bool seek_to_first,
  91. bool seek_after_async_io);
  92. void UpdateCurrent();
  93. bool NeedToSeekImmutable(const Slice& internal_key);
  94. void DeleteCurrentIter();
  95. uint32_t FindFileInRange(const std::vector<FileMetaData*>& files,
  96. const Slice& internal_key, uint32_t left,
  97. uint32_t right);
  98. bool IsOverUpperBound(const Slice& internal_key) const;
  99. // Set PinnedIteratorsManager for all children Iterators, this function should
  100. // be called whenever we update children Iterators or pinned_iters_mgr_.
  101. void UpdateChildrenPinnedItersMgr();
  102. // A helper function that will release iter in the proper manner, or pass it
  103. // to pinned_iters_mgr_ to release it later if pinning is enabled.
  104. void DeleteIterator(InternalIterator* iter, bool is_arena = false);
  105. DBImpl* const db_;
  106. ReadOptions read_options_;
  107. ColumnFamilyData* const cfd_;
  108. const SliceTransform* const prefix_extractor_;
  109. const Comparator* user_comparator_;
  110. const bool allow_unprepared_value_;
  111. MinIterHeap immutable_min_heap_;
  112. SuperVersion* sv_;
  113. InternalIterator* mutable_iter_;
  114. std::vector<InternalIterator*> imm_iters_;
  115. std::vector<InternalIterator*> l0_iters_;
  116. std::vector<ForwardLevelIterator*> level_iters_;
  117. InternalIterator* current_;
  118. bool valid_;
  119. // Internal iterator status; set only by one of the unsupported methods.
  120. Status status_;
  121. // Status of immutable iterators, maintained here to avoid iterating over
  122. // all of them in status().
  123. Status immutable_status_;
  124. // Indicates that at least one of the immutable iterators pointed to a key
  125. // larger than iterate_upper_bound and was therefore destroyed. Seek() may
  126. // need to rebuild such iterators.
  127. bool has_iter_trimmed_for_upper_bound_;
  128. // Is current key larger than iterate_upper_bound? If so, makes Valid()
  129. // return false.
  130. bool current_over_upper_bound_;
  131. // Left endpoint of the range of keys that immutable iterators currently
  132. // cover. When Seek() is called with a key that's within that range, immutable
  133. // iterators don't need to be moved; see NeedToSeekImmutable(). This key is
  134. // included in the range after a Seek(), but excluded when advancing the
  135. // iterator using Next().
  136. IterKey prev_key_;
  137. bool is_prev_set_;
  138. bool is_prev_inclusive_;
  139. PinnedIteratorsManager* pinned_iters_mgr_;
  140. Arena arena_;
  141. };
  142. } // namespace ROCKSDB_NAMESPACE