小组成员:谢瑞阳、徐翔宇
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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