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.

64 Zeilen
1.9 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. private:
  13. Iterator* iter_;
  14. bool valid_;
  15. Slice key_;
  16. public:
  17. IteratorWrapper(): iter_(NULL), valid_(false) { }
  18. explicit IteratorWrapper(Iterator* iter): iter_(NULL) {
  19. Set(iter);
  20. }
  21. ~IteratorWrapper() { delete iter_; }
  22. Iterator* iter() const { return iter_; }
  23. // Takes ownership of "iter" and will delete it when destroyed, or
  24. // when Set() is invoked again.
  25. void Set(Iterator* iter) {
  26. delete iter_;
  27. iter_ = iter;
  28. if (iter_ == NULL) {
  29. valid_ = false;
  30. } else {
  31. Update();
  32. }
  33. }
  34. // Iterator interface methods
  35. bool Valid() const { return valid_; }
  36. Slice key() const { assert(Valid()); return key_; }
  37. Slice value() const { assert(Valid()); return iter_->value(); }
  38. // Methods below require iter() != NULL
  39. Status status() const { assert(iter_); return iter_->status(); }
  40. void Next() { assert(iter_); iter_->Next(); Update(); }
  41. void Prev() { assert(iter_); iter_->Prev(); Update(); }
  42. void Seek(const Slice& k) { assert(iter_); iter_->Seek(k); Update(); }
  43. void SeekToFirst() { assert(iter_); iter_->SeekToFirst(); Update(); }
  44. void SeekToLast() { assert(iter_); iter_->SeekToLast(); Update(); }
  45. private:
  46. void Update() {
  47. valid_ = iter_->Valid();
  48. if (valid_) {
  49. key_ = iter_->key();
  50. }
  51. }
  52. };
  53. }
  54. #endif // STORAGE_LEVELDB_TABLE_ITERATOR_WRAPPER_H_