作者: 韩晨旭 10225101440 李畅 10225102463
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

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