作者: 韩晨旭 10225101440 李畅 10225102463
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

66 行
2.0 KiB

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