| 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 |
|
| namespace leveldb { |
| 9 |
|
| |
| 10 |
|
| // A internal wrapper class with an interface similar to Iterator that |
| 11 |
|
| // caches the valid() and key() results for an underlying iterator. |
| 12 |
|
| // This can help avoid virtual function calls and also gives better |
| 13 |
|
| // cache locality. |
| 14 |
|
| class IteratorWrapper { |
| 15 |
|
| public: |
| 16 |
2 |
| IteratorWrapper(): iter_(NULL), valid_(false) { } |
| 17 |
|
| explicit IteratorWrapper(Iterator* iter): iter_(NULL) { |
| 18 |
|
| Set(iter); |
| 19 |
|
| } |
| 20 |
6 |
▶ | ~IteratorWrapper() { delete iter_; } |
Branch: 1 branch
| Branch | True | False | Total |
| ~IteratorWrapper() { delete iter_; } |
4 |
2 |
6 |
|
| 21 |
|
| Iterator* iter() const { return iter_; } |
| 22 |
|
| |
| 23 |
|
| // Takes ownership of "iter" and will delete it when destroyed, or |
| 24 |
|
| // when Set() is invoked again. |
| 25 |
9 |
| void Set(Iterator* iter) { |
| 26 |
9 |
▶ | delete iter_; |
Branch: 1 branch
| Branch | True | False | Total |
| delete iter_; |
1 |
8 |
9 |
|
| 27 |
9 |
| iter_ = iter; |
| 28 |
9 |
▶ | if (iter_ == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (iter_ == NULL) { |
4 |
5 |
9 |
|
| 29 |
4 |
| valid_ = false; |
| 30 |
|
| } else { |
| 31 |
5 |
| Update(); |
| 32 |
|
| } |
| 33 |
9 |
| } |
| 34 |
|
| |
| 35 |
|
| |
| 36 |
|
| // Iterator interface methods |
| 37 |
143 |
| bool Valid() const { return valid_; } |
| 38 |
49 |
▶ | Slice key() const { assert(Valid()); return key_; } |
Branch: 1 branch
| Branch | True | False | Total |
| Slice key() const { assert(Valid()); return key_; } |
0 |
49 |
49 |
|
| 39 |
1 |
▶ | Slice value() const { assert(Valid()); return iter_->value(); } |
Branch: 1 branch
| Branch | True | False | Total |
| Slice value() const { assert(Valid()); return iter_->value(); } |
0 |
1 |
1 |
|
| 40 |
|
| // Methods below require iter() != NULL |
| 41 |
2 |
▶ | Status status() const { assert(iter_); return iter_->status(); } |
Branch: 1 branch
| Branch | True | False | Total |
| Status status() const { assert(iter_); return iter_->status(); } |
0 |
2 |
2 |
|
| 42 |
30 |
▶ | void Next() { assert(iter_); iter_->Next(); Update(); } |
Branch: 1 branch
| Branch | True | False | Total |
| void Next() { assert(iter_); iter_->Next(); Update(); } |
0 |
30 |
30 |
|
| 43 |
0 |
▶ | void Prev() { assert(iter_); iter_->Prev(); Update(); } |
Branch: 1 branch
| Branch | True | False | Total |
| void Prev() { assert(iter_); iter_->Prev(); Update(); } |
0 |
0 |
0 |
|
| 44 |
0 |
▶ | void Seek(const Slice& k) { assert(iter_); iter_->Seek(k); Update(); } |
Branch: 1 branch
| Branch | True | False | Total |
| void Seek(const Slice& k) { assert(iter_); iter_->Seek(k); Update(); } |
0 |
0 |
0 |
|
| 45 |
4 |
▶ | void SeekToFirst() { assert(iter_); iter_->SeekToFirst(); Update(); } |
Branch: 1 branch
| Branch | True | False | Total |
| void SeekToFirst() { assert(iter_); iter_->SeekToFirst(); Update(); } |
0 |
4 |
4 |
|
| 46 |
0 |
▶ | void SeekToLast() { assert(iter_); iter_->SeekToLast(); Update(); } |
Branch: 1 branch
| Branch | True | False | Total |
| void SeekToLast() { assert(iter_); iter_->SeekToLast(); Update(); } |
0 |
0 |
0 |
|
| 47 |
|
| |
| 48 |
|
| private: |
| 49 |
39 |
| void Update() { |
| 50 |
39 |
| valid_ = iter_->Valid(); |
| 51 |
39 |
▶ | if (valid_) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (valid_) { |
30 |
9 |
39 |
|
| 52 |
30 |
| key_ = iter_->key(); |
| 53 |
|
| } |
| 54 |
39 |
| } |
| 55 |
|
| |
| 56 |
|
| Iterator* iter_; |
| 57 |
|
| bool valid_; |
| 58 |
|
| Slice key_; |
| 59 |
|
| }; |
| 60 |
|
| |
| 61 |
|
| } // namespace leveldb |
| 62 |
|
| |
| 63 |
|
| #endif // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_ |