]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
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. | |
11fdf7f2 TL |
22 | template <class TValue = Slice> |
23 | class IteratorWrapperBase { | |
7c673cae | 24 | public: |
11fdf7f2 TL |
25 | IteratorWrapperBase() : iter_(nullptr), valid_(false) {} |
26 | explicit IteratorWrapperBase(InternalIteratorBase<TValue>* _iter) | |
27 | : iter_(nullptr) { | |
7c673cae FG |
28 | Set(_iter); |
29 | } | |
11fdf7f2 TL |
30 | ~IteratorWrapperBase() {} |
31 | InternalIteratorBase<TValue>* iter() const { return iter_; } | |
7c673cae FG |
32 | |
33 | // Set the underlying Iterator to _iter and return | |
34 | // previous underlying Iterator. | |
11fdf7f2 TL |
35 | InternalIteratorBase<TValue>* Set(InternalIteratorBase<TValue>* _iter) { |
36 | InternalIteratorBase<TValue>* old_iter = iter_; | |
7c673cae FG |
37 | |
38 | iter_ = _iter; | |
39 | if (iter_ == nullptr) { | |
40 | valid_ = false; | |
41 | } else { | |
42 | Update(); | |
43 | } | |
44 | return old_iter; | |
45 | } | |
46 | ||
47 | void DeleteIter(bool is_arena_mode) { | |
48 | if (iter_) { | |
49 | if (!is_arena_mode) { | |
50 | delete iter_; | |
51 | } else { | |
11fdf7f2 | 52 | iter_->~InternalIteratorBase<TValue>(); |
7c673cae FG |
53 | } |
54 | } | |
55 | } | |
56 | ||
57 | // Iterator interface methods | |
58 | bool Valid() const { return valid_; } | |
59 | Slice key() const { assert(Valid()); return key_; } | |
11fdf7f2 TL |
60 | TValue value() const { |
61 | assert(Valid()); | |
62 | return iter_->value(); | |
63 | } | |
7c673cae FG |
64 | // Methods below require iter() != nullptr |
65 | Status status() const { assert(iter_); return iter_->status(); } | |
66 | void Next() { assert(iter_); iter_->Next(); Update(); } | |
67 | void Prev() { assert(iter_); iter_->Prev(); Update(); } | |
68 | void Seek(const Slice& k) { assert(iter_); iter_->Seek(k); Update(); } | |
69 | void SeekForPrev(const Slice& k) { | |
70 | assert(iter_); | |
71 | iter_->SeekForPrev(k); | |
72 | Update(); | |
73 | } | |
74 | void SeekToFirst() { assert(iter_); iter_->SeekToFirst(); Update(); } | |
75 | void SeekToLast() { assert(iter_); iter_->SeekToLast(); Update(); } | |
76 | ||
77 | void SetPinnedItersMgr(PinnedIteratorsManager* pinned_iters_mgr) { | |
78 | assert(iter_); | |
79 | iter_->SetPinnedItersMgr(pinned_iters_mgr); | |
80 | } | |
81 | bool IsKeyPinned() const { | |
82 | assert(Valid()); | |
83 | return iter_->IsKeyPinned(); | |
84 | } | |
85 | bool IsValuePinned() const { | |
86 | assert(Valid()); | |
87 | return iter_->IsValuePinned(); | |
88 | } | |
89 | ||
90 | private: | |
91 | void Update() { | |
92 | valid_ = iter_->Valid(); | |
93 | if (valid_) { | |
94 | key_ = iter_->key(); | |
11fdf7f2 | 95 | assert(iter_->status().ok()); |
7c673cae FG |
96 | } |
97 | } | |
98 | ||
11fdf7f2 | 99 | InternalIteratorBase<TValue>* iter_; |
7c673cae FG |
100 | bool valid_; |
101 | Slice key_; | |
102 | }; | |
103 | ||
11fdf7f2 TL |
104 | using IteratorWrapper = IteratorWrapperBase<Slice>; |
105 | ||
7c673cae FG |
106 | class Arena; |
107 | // Return an empty iterator (yields nothing) allocated from arena. | |
11fdf7f2 TL |
108 | template <class TValue = Slice> |
109 | extern InternalIteratorBase<TValue>* NewEmptyInternalIterator(Arena* arena); | |
7c673cae FG |
110 | |
111 | } // namespace rocksdb |