作者: 谢瑞阳 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.

67 lines
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 = NULL;
  8. cleanup_.next = NULL;
  9. }
  10. Iterator::~Iterator() {
  11. if (cleanup_.function != NULL) {
  12. (*cleanup_.function)(cleanup_.arg1, cleanup_.arg2);
  13. for (Cleanup* c = cleanup_.next; c != NULL; ) {
  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 != NULL);
  23. Cleanup* c;
  24. if (cleanup_.function == NULL) {
  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