作者: 谢瑞阳 10225101483 徐翔宇 10225101535
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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