LCOV - code coverage report
Current view: top level - src/leveldb/table - iterator_wrapper.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 22 27 81.5 %
Date: 2015-10-12 22:39:14 Functions: 9 19 47.4 %
Legend: Lines: hit not hit

          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             : 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         155 :   IteratorWrapper(): iter_(NULL), valid_(false) { }
      17         436 :   explicit IteratorWrapper(Iterator* iter): iter_(NULL) {
      18         436 :     Set(iter);
      19           0 :   }
      20         591 :   ~IteratorWrapper() { delete iter_; }
      21           0 :   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        1075 :   void Set(Iterator* iter) {
      26        1075 :     delete iter_;
      27        1075 :     iter_ = iter;
      28        1075 :     if (iter_ == NULL) {
      29         254 :       valid_ = false;
      30             :     } else {
      31         821 :       Update();
      32             :     }
      33        1075 :   }
      34             : 
      35             : 
      36             :   // Iterator interface methods
      37           0 :   bool Valid() const        { return valid_; }
      38       69169 :   Slice key() const         { assert(Valid()); return key_; }
      39       70438 :   Slice value() const       { assert(Valid()); return iter_->value(); }
      40             :   // Methods below require iter() != NULL
      41         564 :   Status status() const     { assert(iter_); return iter_->status(); }
      42       25728 :   void Next()               { assert(iter_); iter_->Next();        Update(); }
      43           0 :   void Prev()               { assert(iter_); iter_->Prev();        Update(); }
      44         285 :   void Seek(const Slice& k) { assert(iter_); iter_->Seek(k);       Update(); }
      45         414 :   void SeekToFirst()        { assert(iter_); iter_->SeekToFirst(); Update(); }
      46           0 :   void SeekToLast()         { assert(iter_); iter_->SeekToLast();  Update(); }
      47             : 
      48             :  private:
      49       27248 :   void Update() {
      50       27248 :     valid_ = iter_->Valid();
      51       27248 :     if (valid_) {
      52       25962 :       key_ = iter_->key();
      53             :     }
      54       27248 :   }
      55             : 
      56             :   Iterator* iter_;
      57             :   bool valid_;
      58             :   Slice key_;
      59             : };
      60             : 
      61             : }  // namespace leveldb
      62             : 
      63             : #endif  // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_

Generated by: LCOV version 1.11