]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/table/iterator_wrapper.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / rocksdb / table / iterator_wrapper.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under the BSD-style license found in the
3 // LICENSE file in the root directory of this source tree. An additional grant
4 // of patent rights can be found in the PATENTS file in the same 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
10 #pragma once
11
12 #include <set>
13
14 #include "table/internal_iterator.h"
15
16 namespace rocksdb {
17
18 // A internal wrapper class with an interface similar to Iterator that caches
19 // the valid() and key() results for an underlying iterator.
20 // This can help avoid virtual function calls and also gives better
21 // cache locality.
22 class IteratorWrapper {
23 public:
24 IteratorWrapper() : iter_(nullptr), valid_(false) {}
25 explicit IteratorWrapper(InternalIterator* _iter) : iter_(nullptr) {
26 Set(_iter);
27 }
28 ~IteratorWrapper() {}
29 InternalIterator* iter() const { return iter_; }
30
31 // Set the underlying Iterator to _iter and return
32 // previous underlying Iterator.
33 InternalIterator* Set(InternalIterator* _iter) {
34 InternalIterator* old_iter = iter_;
35
36 iter_ = _iter;
37 if (iter_ == nullptr) {
38 valid_ = false;
39 } else {
40 Update();
41 }
42 return old_iter;
43 }
44
45 void DeleteIter(bool is_arena_mode) {
46 if (iter_) {
47 if (!is_arena_mode) {
48 delete iter_;
49 } else {
50 iter_->~InternalIterator();
51 }
52 }
53 }
54
55 // Iterator interface methods
56 bool Valid() const { return valid_; }
57 Slice key() const { assert(Valid()); return key_; }
58 Slice value() const { assert(Valid()); return iter_->value(); }
59 // Methods below require iter() != nullptr
60 Status status() const { assert(iter_); return iter_->status(); }
61 void Next() { assert(iter_); iter_->Next(); Update(); }
62 void Prev() { assert(iter_); iter_->Prev(); Update(); }
63 void Seek(const Slice& k) { assert(iter_); iter_->Seek(k); Update(); }
64 void SeekForPrev(const Slice& k) {
65 assert(iter_);
66 iter_->SeekForPrev(k);
67 Update();
68 }
69 void SeekToFirst() { assert(iter_); iter_->SeekToFirst(); Update(); }
70 void SeekToLast() { assert(iter_); iter_->SeekToLast(); Update(); }
71
72 void SetPinnedItersMgr(PinnedIteratorsManager* pinned_iters_mgr) {
73 assert(iter_);
74 iter_->SetPinnedItersMgr(pinned_iters_mgr);
75 }
76 bool IsKeyPinned() const {
77 assert(Valid());
78 return iter_->IsKeyPinned();
79 }
80 bool IsValuePinned() const {
81 assert(Valid());
82 return iter_->IsValuePinned();
83 }
84
85 private:
86 void Update() {
87 valid_ = iter_->Valid();
88 if (valid_) {
89 key_ = iter_->key();
90 }
91 }
92
93 InternalIterator* iter_;
94 bool valid_;
95 Slice key_;
96 };
97
98 class Arena;
99 // Return an empty iterator (yields nothing) allocated from arena.
100 extern InternalIterator* NewEmptyInternalIterator(Arena* arena);
101
102 // Return an empty iterator with the specified status, allocated arena.
103 extern InternalIterator* NewErrorInternalIterator(const Status& status,
104 Arena* arena);
105
106 } // namespace rocksdb