forward_iterator.h 5.5 KB

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