iterator_wrapper.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #pragma once
  10. #include <set>
  11. #include "table/internal_iterator.h"
  12. #include "test_util/sync_point.h"
  13. namespace ROCKSDB_NAMESPACE {
  14. // A internal wrapper class with an interface similar to Iterator that caches
  15. // the valid() and key() results for an underlying iterator.
  16. // This can help avoid virtual function calls and also gives better
  17. // cache locality.
  18. template <class TValue = Slice>
  19. class IteratorWrapperBase {
  20. public:
  21. IteratorWrapperBase() : iter_(nullptr), valid_(false) {}
  22. explicit IteratorWrapperBase(InternalIteratorBase<TValue>* _iter)
  23. : iter_(nullptr) {
  24. Set(_iter);
  25. }
  26. ~IteratorWrapperBase() {}
  27. InternalIteratorBase<TValue>* iter() const { return iter_; }
  28. // Set the underlying Iterator to _iter and return
  29. // previous underlying Iterator.
  30. InternalIteratorBase<TValue>* Set(InternalIteratorBase<TValue>* _iter) {
  31. InternalIteratorBase<TValue>* old_iter = iter_;
  32. iter_ = _iter;
  33. if (iter_ == nullptr) {
  34. valid_ = false;
  35. } else {
  36. Update();
  37. }
  38. return old_iter;
  39. }
  40. void DeleteIter(bool is_arena_mode) {
  41. if (iter_) {
  42. if (!is_arena_mode) {
  43. delete iter_;
  44. } else {
  45. iter_->~InternalIteratorBase<TValue>();
  46. }
  47. }
  48. }
  49. // Iterator interface methods
  50. bool Valid() const { return valid_; }
  51. Slice key() const {
  52. assert(Valid());
  53. return result_.key;
  54. }
  55. TValue value() const {
  56. assert(Valid());
  57. return iter_->value();
  58. }
  59. // Methods below require iter() != nullptr
  60. Status status() const {
  61. assert(iter_);
  62. return iter_->status();
  63. }
  64. void Next() {
  65. assert(iter_);
  66. valid_ = iter_->NextAndGetResult(&result_);
  67. assert(!valid_ || iter_->status().ok());
  68. }
  69. void Prev() {
  70. assert(iter_);
  71. iter_->Prev();
  72. Update();
  73. }
  74. void Seek(const Slice& k) {
  75. assert(iter_);
  76. iter_->Seek(k);
  77. Update();
  78. }
  79. void SeekForPrev(const Slice& k) {
  80. assert(iter_);
  81. iter_->SeekForPrev(k);
  82. Update();
  83. }
  84. void SeekToFirst() {
  85. assert(iter_);
  86. iter_->SeekToFirst();
  87. Update();
  88. }
  89. void SeekToLast() {
  90. assert(iter_);
  91. iter_->SeekToLast();
  92. Update();
  93. }
  94. bool MayBeOutOfLowerBound() {
  95. assert(Valid());
  96. return iter_->MayBeOutOfLowerBound();
  97. }
  98. bool MayBeOutOfUpperBound() {
  99. assert(Valid());
  100. return result_.may_be_out_of_upper_bound;
  101. }
  102. void SetPinnedItersMgr(PinnedIteratorsManager* pinned_iters_mgr) {
  103. assert(iter_);
  104. iter_->SetPinnedItersMgr(pinned_iters_mgr);
  105. }
  106. bool IsKeyPinned() const {
  107. assert(Valid());
  108. return iter_->IsKeyPinned();
  109. }
  110. bool IsValuePinned() const {
  111. assert(Valid());
  112. return iter_->IsValuePinned();
  113. }
  114. private:
  115. void Update() {
  116. valid_ = iter_->Valid();
  117. if (valid_) {
  118. assert(iter_->status().ok());
  119. result_.key = iter_->key();
  120. result_.may_be_out_of_upper_bound = true;
  121. }
  122. }
  123. InternalIteratorBase<TValue>* iter_;
  124. IterateResult result_;
  125. bool valid_;
  126. };
  127. using IteratorWrapper = IteratorWrapperBase<Slice>;
  128. class Arena;
  129. // Return an empty iterator (yields nothing) allocated from arena.
  130. template <class TValue = Slice>
  131. extern InternalIteratorBase<TValue>* NewEmptyInternalIterator(Arena* arena);
  132. } // namespace ROCKSDB_NAMESPACE