internal_iterator.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #pragma once
  7. #include <string>
  8. #include "db/dbformat.h"
  9. #include "file/readahead_file_info.h"
  10. #include "rocksdb/advanced_iterator.h"
  11. #include "rocksdb/comparator.h"
  12. #include "rocksdb/iterator.h"
  13. #include "rocksdb/status.h"
  14. #include "table/format.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. class PinnedIteratorsManager;
  17. template <class TValue>
  18. class InternalIteratorBase : public Cleanable {
  19. public:
  20. InternalIteratorBase() {}
  21. // No copying allowed
  22. InternalIteratorBase(const InternalIteratorBase&) = delete;
  23. InternalIteratorBase& operator=(const InternalIteratorBase&) = delete;
  24. virtual ~InternalIteratorBase() {}
  25. // This iterator will only process range tombstones with sequence
  26. // number <= `read_seqno`.
  27. // Noop for most child classes.
  28. // For range tombstone iterators (TruncatedRangeDelIterator,
  29. // FragmentedRangeTombstoneIterator), will only return range tombstones with
  30. // sequence number <= `read_seqno`. For LevelIterator, it may open new table
  31. // files and create new range tombstone iterators during scanning. It will use
  32. // `read_seqno` as the sequence number for creating new range tombstone
  33. // iterators.
  34. virtual void SetRangeDelReadSeqno(SequenceNumber /* read_seqno */) {}
  35. // An iterator is either positioned at a key/value pair, or
  36. // not valid. This method returns true iff the iterator is valid.
  37. // Always returns false if !status().ok().
  38. virtual bool Valid() const = 0;
  39. // Position at the first key in the source. The iterator is Valid()
  40. // after this call iff the source is not empty.
  41. virtual void SeekToFirst() = 0;
  42. // Position at the last key in the source. The iterator is
  43. // Valid() after this call iff the source is not empty.
  44. virtual void SeekToLast() = 0;
  45. // Position at the first key in the source that at or past target
  46. // The iterator is Valid() after this call iff the source contains
  47. // an entry that comes at or past target.
  48. // All Seek*() methods clear any error status() that the iterator had prior to
  49. // the call; after the seek, status() indicates only the error (if any) that
  50. // happened during the seek, not any past errors.
  51. // 'target' contains user timestamp if timestamp is enabled.
  52. virtual void Seek(const Slice& target) = 0;
  53. // Position at the first key in the source that at or before target
  54. // The iterator is Valid() after this call iff the source contains
  55. // an entry that comes at or before target.
  56. virtual void SeekForPrev(const Slice& target) = 0;
  57. // Moves to the next entry in the source. After this call, Valid() is
  58. // true iff the iterator was not positioned at the last entry in the source.
  59. // REQUIRES: Valid()
  60. virtual void Next() = 0;
  61. // Moves to the next entry in the source, and return result. Iterator
  62. // implementation should override this method to help methods inline better,
  63. // or when UpperBoundCheckResult() is non-trivial.
  64. // REQUIRES: Valid()
  65. virtual bool NextAndGetResult(IterateResult* result) {
  66. Next();
  67. bool is_valid = Valid();
  68. if (is_valid) {
  69. result->key = key();
  70. // Default may_be_out_of_upper_bound to true to avoid unnecessary virtual
  71. // call. If an implementation has non-trivial UpperBoundCheckResult(),
  72. // it should also override NextAndGetResult().
  73. result->bound_check_result = IterBoundCheck::kUnknown;
  74. result->value_prepared = false;
  75. assert(UpperBoundCheckResult() != IterBoundCheck::kOutOfBound);
  76. }
  77. return is_valid;
  78. }
  79. // Moves to the previous entry in the source. After this call, Valid() is
  80. // true iff the iterator was not positioned at the first entry in source.
  81. // REQUIRES: Valid()
  82. virtual void Prev() = 0;
  83. // Return the key for the current entry. The underlying storage for
  84. // the returned slice is valid only until the next modification of
  85. // the iterator.
  86. // REQUIRES: Valid()
  87. virtual Slice key() const = 0;
  88. // Returns the approximate write time of this entry, which is deduced from
  89. // sequence number if sequence number to time mapping is available.
  90. // The default implementation returns maximum uint64_t and that indicates the
  91. // write time is unknown.
  92. virtual uint64_t write_unix_time() const {
  93. return std::numeric_limits<uint64_t>::max();
  94. }
  95. // Return user key for the current entry.
  96. // REQUIRES: Valid()
  97. virtual Slice user_key() const { return ExtractUserKey(key()); }
  98. // Return the value for the current entry. The underlying storage for
  99. // the returned slice is valid only until the next modification of
  100. // the iterator.
  101. // REQUIRES: Valid()
  102. // REQUIRES: PrepareValue() has been called if needed (see PrepareValue()).
  103. virtual TValue value() const = 0;
  104. // If an error has occurred, return it. Else return an ok status.
  105. // If non-blocking IO is requested and this operation cannot be
  106. // satisfied without doing some IO, then this returns Status::Incomplete().
  107. virtual Status status() const = 0;
  108. // For some types of iterators, sometimes Seek()/Next()/SeekForPrev()/etc may
  109. // load key but not value (to avoid the IO cost of reading the value from disk
  110. // if it won't be not needed). This method loads the value in such situation.
  111. //
  112. // Needs to be called before value() at least once after each iterator
  113. // movement (except if IterateResult::value_prepared = true), for iterators
  114. // created with allow_unprepared_value = true.
  115. //
  116. // Returns false if an error occurred; in this case Valid() is also changed
  117. // to false, and status() is changed to non-ok.
  118. // REQUIRES: Valid()
  119. virtual bool PrepareValue() { return true; }
  120. // Keys return from this iterator can be smaller than iterate_lower_bound.
  121. virtual bool MayBeOutOfLowerBound() { return true; }
  122. // If the iterator has checked the key against iterate_upper_bound, returns
  123. // the result here. The function can be used by user of the iterator to skip
  124. // their own checks. If Valid() = true, IterBoundCheck::kUnknown is always
  125. // a valid value. If Valid() = false, IterBoundCheck::kOutOfBound indicates
  126. // that the iterator is filtered out by upper bound checks.
  127. virtual IterBoundCheck UpperBoundCheckResult() {
  128. return IterBoundCheck::kUnknown;
  129. }
  130. // Pass the PinnedIteratorsManager to the Iterator, most Iterators don't
  131. // communicate with PinnedIteratorsManager so default implementation is no-op
  132. // but for Iterators that need to communicate with PinnedIteratorsManager
  133. // they will implement this function and use the passed pointer to communicate
  134. // with PinnedIteratorsManager.
  135. virtual void SetPinnedItersMgr(PinnedIteratorsManager* /*pinned_iters_mgr*/) {
  136. }
  137. // If true, this means that the Slice returned by key() is valid as long as
  138. // PinnedIteratorsManager::ReleasePinnedData is not called and the
  139. // Iterator is not deleted.
  140. //
  141. // IsKeyPinned() is guaranteed to always return true if
  142. // - Iterator is created with ReadOptions::pin_data = true
  143. // - DB tables were created with BlockBasedTableOptions::use_delta_encoding
  144. // set to false.
  145. virtual bool IsKeyPinned() const { return false; }
  146. // If true, this means that the Slice returned by value() is valid as long as
  147. // PinnedIteratorsManager::ReleasePinnedData is not called and the
  148. // Iterator is not deleted.
  149. // REQUIRES: Same as for value().
  150. virtual bool IsValuePinned() const { return false; }
  151. virtual Status GetProperty(std::string /*prop_name*/, std::string* /*prop*/) {
  152. return Status::NotSupported("");
  153. }
  154. // When iterator moves from one file to another file at same level, new file's
  155. // readahead state (details of last block read) is updated with previous
  156. // file's readahead state. This way internal readahead_size of Prefetch Buffer
  157. // doesn't start from scratch and can fall back to 8KB with no prefetch if
  158. // reads are not sequential.
  159. //
  160. // Default implementation is no-op and its implemented by iterators.
  161. virtual void GetReadaheadState(ReadaheadFileInfo* /*readahead_file_info*/) {}
  162. // Default implementation is no-op and its implemented by iterators.
  163. virtual void SetReadaheadState(ReadaheadFileInfo* /*readahead_file_info*/) {}
  164. // When used under merging iterator, LevelIterator treats file boundaries
  165. // as sentinel keys to prevent it from moving to next SST file before range
  166. // tombstones in the current SST file are no longer needed. This method makes
  167. // it cheap to check if the current key is a sentinel key. This should only be
  168. // used by MergingIterator and LevelIterator for now.
  169. virtual bool IsDeleteRangeSentinelKey() const { return false; }
  170. virtual void Prepare(const MultiScanArgs* /*scan_opts*/) {}
  171. protected:
  172. void SeekForPrevImpl(const Slice& target, const CompareInterface* cmp) {
  173. Seek(target);
  174. if (!Valid()) {
  175. SeekToLast();
  176. }
  177. while (Valid() && cmp->Compare(target, key()) < 0) {
  178. Prev();
  179. }
  180. }
  181. };
  182. using InternalIterator = InternalIteratorBase<Slice>;
  183. // Return an empty iterator (yields nothing).
  184. template <class TValue = Slice>
  185. InternalIteratorBase<TValue>* NewEmptyInternalIterator();
  186. // Return an empty iterator with the specified status.
  187. template <class TValue = Slice>
  188. InternalIteratorBase<TValue>* NewErrorInternalIterator(const Status& status);
  189. // Return an empty iterator with the specified status, allocated arena.
  190. template <class TValue = Slice>
  191. InternalIteratorBase<TValue>* NewErrorInternalIterator(const Status& status,
  192. Arena* arena);
  193. } // namespace ROCKSDB_NAMESPACE