iterator.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "leveldb/iterator.h"
  5. namespace leveldb {
  6. Iterator::Iterator() {
  7. cleanup_head_.function = nullptr;
  8. cleanup_head_.next = nullptr;
  9. }
  10. Iterator::~Iterator() {
  11. if (!cleanup_head_.IsEmpty()) {
  12. cleanup_head_.Run();
  13. for (CleanupNode* node = cleanup_head_.next; node != nullptr; ) {
  14. node->Run();
  15. CleanupNode* next_node = node->next;
  16. delete node;
  17. node = next_node;
  18. }
  19. }
  20. }
  21. void Iterator::RegisterCleanup(CleanupFunction func, void* arg1, void* arg2) {
  22. assert(func != nullptr);
  23. CleanupNode* node;
  24. if (cleanup_head_.IsEmpty()) {
  25. node = &cleanup_head_;
  26. } else {
  27. node = new CleanupNode();
  28. node->next = cleanup_head_.next;
  29. cleanup_head_.next = node;
  30. }
  31. node->function = func;
  32. node->arg1 = arg1;
  33. node->arg2 = arg2;
  34. }
  35. namespace {
  36. class EmptyIterator : public Iterator {
  37. public:
  38. EmptyIterator(const Status& s) : status_(s) { }
  39. ~EmptyIterator() override = default;
  40. bool Valid() const override { return false; }
  41. void Seek(const Slice& target) override { }
  42. void SeekToFirst() override { }
  43. void SeekToLast() override { }
  44. void Next() override { assert(false); }
  45. void Prev() override { assert(false); }
  46. Slice key() const override { assert(false); return Slice(); }
  47. Slice value() const override { assert(false); return Slice(); }
  48. Status status() const override { return status_; }
  49. private:
  50. Status status_;
  51. };
  52. } // anonymous namespace
  53. Iterator* NewEmptyIterator() {
  54. return new EmptyIterator(Status::OK());
  55. }
  56. Iterator* NewErrorIterator(const Status& status) {
  57. return new EmptyIterator(status);
  58. }
  59. } // namespace leveldb