Bitcoin
iterator_wrapper.h
Go to the documentation of this file.
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.
18  public:
19  IteratorWrapper(): iter_(NULL), valid_(false) { }
20  explicit IteratorWrapper(Iterator* iter): iter_(NULL) {
21  Set(iter);
22  }
23  ~IteratorWrapper() { delete iter_; }
24  Iterator* iter() const { return iter_; }
25 
26  // Takes ownership of "iter" and will delete it when destroyed, or
27  // when Set() is invoked again.
28  void Set(Iterator* iter) {
29  delete iter_;
30  iter_ = iter;
31  if (iter_ == NULL) {
32  valid_ = false;
33  } else {
34  Update();
35  }
36  }
37 
38 
39  // Iterator interface methods
40  bool Valid() const { return valid_; }
41  Slice key() const { assert(Valid()); return key_; }
42  Slice value() const { assert(Valid()); return iter_->value(); }
43  // Methods below require iter() != NULL
44  Status status() const { assert(iter_); return iter_->status(); }
45  void Next() { assert(iter_); iter_->Next(); Update(); }
46  void Prev() { assert(iter_); iter_->Prev(); Update(); }
47  void Seek(const Slice& k) { assert(iter_); iter_->Seek(k); Update(); }
48  void SeekToFirst() { assert(iter_); iter_->SeekToFirst(); Update(); }
49  void SeekToLast() { assert(iter_); iter_->SeekToLast(); Update(); }
50 
51  private:
52  void Update() {
53  valid_ = iter_->Valid();
54  if (valid_) {
55  key_ = iter_->key();
56  }
57  }
58 
60  bool valid_;
62 };
63 
64 } // namespace leveldb
65 
66 #endif // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
Slice key_
Definition: iterator_wrapper.h:61
virtual Status status() const =0
Definition: autocompact_test.cc:11
Definition: iterator.h:23
virtual Slice key() const =0
virtual Slice value() const =0
Status status() const
Definition: iterator_wrapper.h:44
virtual void SeekToFirst()=0
void SeekToFirst()
Definition: iterator_wrapper.h:48
void Set(Iterator *iter)
Definition: iterator_wrapper.h:28
virtual void Seek(const Slice &target)=0
void Seek(const Slice &k)
Definition: iterator_wrapper.h:47
virtual void Next()=0
virtual void SeekToLast()=0
void Next()
Definition: iterator_wrapper.h:45
Iterator * iter() const
Definition: iterator_wrapper.h:24
void Prev()
Definition: iterator_wrapper.h:46
void Update()
Definition: iterator_wrapper.h:52
bool valid_
Definition: iterator_wrapper.h:60
virtual void Prev()=0
~IteratorWrapper()
Definition: iterator_wrapper.h:23
IteratorWrapper(Iterator *iter)
Definition: iterator_wrapper.h:20
void SeekToLast()
Definition: iterator_wrapper.h:49
Definition: slice.h:25
Slice value() const
Definition: iterator_wrapper.h:42
Definition: status.h:21
Definition: iterator_wrapper.h:17
bool Valid() const
Definition: iterator_wrapper.h:40
virtual bool Valid() const =0
Iterator * iter_
Definition: iterator_wrapper.h:59
Slice key() const
Definition: iterator_wrapper.h:41
IteratorWrapper()
Definition: iterator_wrapper.h:19