Branch data Line data Source code
1 : : // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2 : : // Use of this source code is governed by a BSD-style license that can be
3 : : // found in the LICENSE file. See the AUTHORS file for names of contributors.
4 : :
5 : : #ifndef STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
6 : : #define STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
7 : :
8 : : #include "leveldb/iterator.h"
9 : : #include "leveldb/slice.h"
10 : :
11 : : namespace leveldb {
12 : :
13 : : // A internal wrapper class with an interface similar to Iterator that
14 : : // caches the valid() and key() results for an underlying iterator.
15 : : // This can help avoid virtual function calls and also gives better
16 : : // cache locality.
17 : : class IteratorWrapper {
18 : : public:
19 : 8238 : IteratorWrapper() : iter_(nullptr), valid_(false) {}
20 [ + - + - ]: 8517 : explicit IteratorWrapper(Iterator* iter) : iter_(nullptr) { Set(iter); }
21 [ + + ]: 25272 : ~IteratorWrapper() { delete iter_; }
22 [ + + + + : 686464 : Iterator* iter() const { return iter_; }
- - - - +
+ + + - -
+ - + + -
+ ]
23 : :
24 : : // Takes ownership of "iter" and will delete it when destroyed, or
25 : : // when Set() is invoked again.
26 : 48058 : void Set(Iterator* iter) {
27 [ + + ]: 48058 : delete iter_;
28 : 48058 : iter_ = iter;
29 [ + + ]: 48058 : if (iter_ == nullptr) {
30 : 17409 : valid_ = false;
31 : : } else {
32 : 30649 : Update();
33 : : }
34 : 48058 : }
35 : :
36 : : // Iterator interface methods
37 [ + + - - : 2241143 : bool Valid() const { return valid_; }
- - + + +
+ ][ - - +
+ - - -
- ]
38 : 3218132 : Slice key() const {
39 [ - + ]: 3218132 : assert(Valid());
40 : 3218132 : return key_;
41 : : }
42 : 1968532 : Slice value() const {
43 [ - + ]: 1968532 : assert(Valid());
44 : 1968532 : return iter_->value();
45 : : }
46 : : // Methods below require iter() != nullptr
47 : 18773 : Status status() const {
48 [ - + ]: 18773 : assert(iter_);
49 : 18773 : return iter_->status();
50 : : }
51 : 1240127 : void Next() {
52 [ - + ]: 1240127 : assert(iter_);
53 : 1240127 : iter_->Next();
54 : 1240127 : Update();
55 : 1240127 : }
56 : 0 : void Prev() {
57 [ # # ]: 0 : assert(iter_);
58 : 0 : iter_->Prev();
59 : 0 : Update();
60 : 0 : }
61 : 15088 : void Seek(const Slice& k) {
62 [ - + ]: 15088 : assert(iter_);
63 : 15088 : iter_->Seek(k);
64 : 15088 : Update();
65 : 15088 : }
66 : 13369 : void SeekToFirst() {
67 [ - + ]: 13369 : assert(iter_);
68 : 13369 : iter_->SeekToFirst();
69 : 13369 : Update();
70 : 13369 : }
71 : 0 : void SeekToLast() {
72 [ # # ]: 0 : assert(iter_);
73 : 0 : iter_->SeekToLast();
74 : 0 : Update();
75 : 0 : }
76 : :
77 : : private:
78 : 1299233 : void Update() {
79 : 1299233 : valid_ = iter_->Valid();
80 [ + + ]: 1299233 : if (valid_) {
81 : 1245598 : key_ = iter_->key();
82 : : }
83 : 1299233 : }
84 : :
85 : : Iterator* iter_;
86 : : bool valid_;
87 : : Slice key_;
88 : : };
89 : :
90 : : } // namespace leveldb
91 : :
92 : : #endif // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
|