1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be
3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors.
4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#ifndef STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#define STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb {
9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// A internal wrapper class with an interface similar to Iterator that
11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// caches the valid() and key() results for an underlying iterator.
12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// This can help avoid virtual function calls and also gives better
13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// cache locality.
14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass IteratorWrapper {
15179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org public:
16179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  IteratorWrapper(): iter_(NULL), valid_(false) { }
17179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  explicit IteratorWrapper(Iterator* iter): iter_(NULL) {
18179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    Set(iter);
19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  ~IteratorWrapper() { delete iter_; }
21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  Iterator* iter() const { return iter_; }
22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Takes ownership of "iter" and will delete it when destroyed, or
24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // when Set() is invoked again.
25179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void Set(Iterator* iter) {
26179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    delete iter_;
27179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    iter_ = iter;
28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    if (iter_ == NULL) {
29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org      valid_ = false;
30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    } else {
31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org      Update();
32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    }
33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Iterator interface methods
37ce5ba3cdead24077a2f07212d93f867a45dd79dadgrogan@chromium.org  bool Valid() const        { return valid_; }
38ce5ba3cdead24077a2f07212d93f867a45dd79dadgrogan@chromium.org  Slice key() const         { assert(Valid()); return key_; }
39ce5ba3cdead24077a2f07212d93f867a45dd79dadgrogan@chromium.org  Slice value() const       { assert(Valid()); return iter_->value(); }
40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Methods below require iter() != NULL
41ce5ba3cdead24077a2f07212d93f867a45dd79dadgrogan@chromium.org  Status status() const     { assert(iter_); return iter_->status(); }
42ce5ba3cdead24077a2f07212d93f867a45dd79dadgrogan@chromium.org  void Next()               { assert(iter_); iter_->Next();        Update(); }
43ce5ba3cdead24077a2f07212d93f867a45dd79dadgrogan@chromium.org  void Prev()               { assert(iter_); iter_->Prev();        Update(); }
44ce5ba3cdead24077a2f07212d93f867a45dd79dadgrogan@chromium.org  void Seek(const Slice& k) { assert(iter_); iter_->Seek(k);       Update(); }
45ce5ba3cdead24077a2f07212d93f867a45dd79dadgrogan@chromium.org  void SeekToFirst()        { assert(iter_); iter_->SeekToFirst(); Update(); }
46ce5ba3cdead24077a2f07212d93f867a45dd79dadgrogan@chromium.org  void SeekToLast()         { assert(iter_); iter_->SeekToLast();  Update(); }
47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
48179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org private:
49179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void Update() {
50179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    valid_ = iter_->Valid();
51179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    if (valid_) {
52179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org      key_ = iter_->key();
53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    }
54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
56c6ac22e779e5135e494ddeb1d8e2b6008e9b619edgrogan@chromium.org  Iterator* iter_;
57c6ac22e779e5135e494ddeb1d8e2b6008e9b619edgrogan@chromium.org  bool valid_;
58c6ac22e779e5135e494ddeb1d8e2b6008e9b619edgrogan@chromium.org  Slice key_;
59c6ac22e779e5135e494ddeb1d8e2b6008e9b619edgrogan@chromium.org};
60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
61c6ac22e779e5135e494ddeb1d8e2b6008e9b619edgrogan@chromium.org}  // namespace leveldb
62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#endif  // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_
64