作者: 谢瑞阳 10225101483 徐翔宇 10225101535
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
1.8 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. #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 {
  47. assert(false);
  48. return Slice();
  49. }
  50. Slice value() const override {
  51. assert(false);
  52. return Slice();
  53. }
  54. Status status() const override { return status_; }
  55. private:
  56. Status status_;
  57. };
  58. } // anonymous namespace
  59. Iterator* NewEmptyIterator() { return new EmptyIterator(Status::OK()); }
  60. Iterator* NewErrorIterator(const Status& status) {
  61. return new EmptyIterator(status);
  62. }
  63. } // namespace leveldb