internal_iterator.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 "rocksdb/comparator.h"
  10. #include "rocksdb/iterator.h"
  11. #include "rocksdb/status.h"
  12. #include "table/format.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. class PinnedIteratorsManager;
  15. struct IterateResult {
  16. Slice key;
  17. bool may_be_out_of_upper_bound;
  18. };
  19. template <class TValue>
  20. class InternalIteratorBase : public Cleanable {
  21. public:
  22. InternalIteratorBase() {}
  23. // No copying allowed
  24. InternalIteratorBase(const InternalIteratorBase&) = delete;
  25. InternalIteratorBase& operator=(const InternalIteratorBase&) = delete;
  26. virtual ~InternalIteratorBase() {}
  27. // An iterator is either positioned at a key/value pair, or
  28. // not valid. This method returns true iff the iterator is valid.
  29. // Always returns false if !status().ok().
  30. virtual bool Valid() const = 0;
  31. // Position at the first key in the source. The iterator is Valid()
  32. // after this call iff the source is not empty.
  33. virtual void SeekToFirst() = 0;
  34. // Position at the last key in the source. The iterator is
  35. // Valid() after this call iff the source is not empty.
  36. virtual void SeekToLast() = 0;
  37. // Position at the first key in the source that at or past target
  38. // The iterator is Valid() after this call iff the source contains
  39. // an entry that comes at or past target.
  40. // All Seek*() methods clear any error status() that the iterator had prior to
  41. // the call; after the seek, status() indicates only the error (if any) that
  42. // happened during the seek, not any past errors.
  43. virtual void Seek(const Slice& target) = 0;
  44. // Position at the first key in the source that at or before target
  45. // The iterator is Valid() after this call iff the source contains
  46. // an entry that comes at or before target.
  47. virtual void SeekForPrev(const Slice& target) = 0;
  48. // Moves to the next entry in the source. After this call, Valid() is
  49. // true iff the iterator was not positioned at the last entry in the source.
  50. // REQUIRES: Valid()
  51. virtual void Next() = 0;
  52. // Moves to the next entry in the source, and return result. Iterator
  53. // implementation should override this method to help methods inline better,
  54. // or when MayBeOutOfUpperBound() is non-trivial.
  55. // REQUIRES: Valid()
  56. virtual bool NextAndGetResult(IterateResult* result) {
  57. Next();
  58. bool is_valid = Valid();
  59. if (is_valid) {
  60. result->key = key();
  61. // Default may_be_out_of_upper_bound to true to avoid unnecessary virtual
  62. // call. If an implementation has non-trivial MayBeOutOfUpperBound(),
  63. // it should also override NextAndGetResult().
  64. result->may_be_out_of_upper_bound = true;
  65. assert(MayBeOutOfUpperBound());
  66. }
  67. return is_valid;
  68. }
  69. // Moves to the previous entry in the source. After this call, Valid() is
  70. // true iff the iterator was not positioned at the first entry in source.
  71. // REQUIRES: Valid()
  72. virtual void Prev() = 0;
  73. // Return the key for the current entry. The underlying storage for
  74. // the returned slice is valid only until the next modification of
  75. // the iterator.
  76. // REQUIRES: Valid()
  77. virtual Slice key() const = 0;
  78. // Return user key for the current entry.
  79. // REQUIRES: Valid()
  80. virtual Slice user_key() const { return ExtractUserKey(key()); }
  81. // Return the value for the current entry. The underlying storage for
  82. // the returned slice is valid only until the next modification of
  83. // the iterator.
  84. // REQUIRES: Valid()
  85. virtual TValue value() const = 0;
  86. // If an error has occurred, return it. Else return an ok status.
  87. // If non-blocking IO is requested and this operation cannot be
  88. // satisfied without doing some IO, then this returns Status::Incomplete().
  89. virtual Status status() const = 0;
  90. // True if the iterator is invalidated because it reached a key that is above
  91. // the iterator upper bound. Used by LevelIterator to decide whether it should
  92. // stop or move on to the next file.
  93. // Important: if iterator reached the end of the file without encountering any
  94. // keys above the upper bound, IsOutOfBound() must return false.
  95. virtual bool IsOutOfBound() { return false; }
  96. // Keys return from this iterator can be smaller than iterate_lower_bound.
  97. virtual bool MayBeOutOfLowerBound() { return true; }
  98. // Keys return from this iterator can be larger or equal to
  99. // iterate_upper_bound.
  100. virtual bool MayBeOutOfUpperBound() { return true; }
  101. // Pass the PinnedIteratorsManager to the Iterator, most Iterators dont
  102. // communicate with PinnedIteratorsManager so default implementation is no-op
  103. // but for Iterators that need to communicate with PinnedIteratorsManager
  104. // they will implement this function and use the passed pointer to communicate
  105. // with PinnedIteratorsManager.
  106. virtual void SetPinnedItersMgr(PinnedIteratorsManager* /*pinned_iters_mgr*/) {
  107. }
  108. // If true, this means that the Slice returned by key() is valid as long as
  109. // PinnedIteratorsManager::ReleasePinnedData is not called and the
  110. // Iterator is not deleted.
  111. //
  112. // IsKeyPinned() is guaranteed to always return true if
  113. // - Iterator is created with ReadOptions::pin_data = true
  114. // - DB tables were created with BlockBasedTableOptions::use_delta_encoding
  115. // set to false.
  116. virtual bool IsKeyPinned() const { return false; }
  117. // If true, this means that the Slice returned by value() is valid as long as
  118. // PinnedIteratorsManager::ReleasePinnedData is not called and the
  119. // Iterator is not deleted.
  120. virtual bool IsValuePinned() const { return false; }
  121. virtual Status GetProperty(std::string /*prop_name*/, std::string* /*prop*/) {
  122. return Status::NotSupported("");
  123. }
  124. protected:
  125. void SeekForPrevImpl(const Slice& target, const Comparator* cmp) {
  126. Seek(target);
  127. if (!Valid()) {
  128. SeekToLast();
  129. }
  130. while (Valid() && cmp->Compare(target, key()) < 0) {
  131. Prev();
  132. }
  133. }
  134. bool is_mutable_;
  135. };
  136. using InternalIterator = InternalIteratorBase<Slice>;
  137. // Return an empty iterator (yields nothing).
  138. template <class TValue = Slice>
  139. extern InternalIteratorBase<TValue>* NewEmptyInternalIterator();
  140. // Return an empty iterator with the specified status.
  141. template <class TValue = Slice>
  142. extern InternalIteratorBase<TValue>* NewErrorInternalIterator(
  143. const Status& status);
  144. // Return an empty iterator with the specified status, allocated arena.
  145. template <class TValue = Slice>
  146. extern InternalIteratorBase<TValue>* NewErrorInternalIterator(
  147. const Status& status, Arena* arena);
  148. } // namespace ROCKSDB_NAMESPACE