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

66 行
1.5 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. #ifndef STORAGE_LEVELDB_DB_SNAPSHOT_H_
  5. #define STORAGE_LEVELDB_DB_SNAPSHOT_H_
  6. #include "include/db.h"
  7. namespace leveldb {
  8. class SnapshotList;
  9. // Snapshots are kept in a doubly-linked list in the DB.
  10. // Each Snapshot corresponds to a particular sequence number.
  11. class Snapshot {
  12. public:
  13. SequenceNumber number_; // const after creation
  14. private:
  15. friend class SnapshotList;
  16. // Snapshot is kept in a doubly-linked circular list
  17. Snapshot* prev_;
  18. Snapshot* next_;
  19. SnapshotList* list_; // just for sanity checks
  20. };
  21. class SnapshotList {
  22. public:
  23. SnapshotList() {
  24. list_.prev_ = &list_;
  25. list_.next_ = &list_;
  26. }
  27. bool empty() const { return list_.next_ == &list_; }
  28. Snapshot* oldest() const { assert(!empty()); return list_.next_; }
  29. Snapshot* newest() const { assert(!empty()); return list_.prev_; }
  30. const Snapshot* New(SequenceNumber seq) {
  31. Snapshot* s = new Snapshot;
  32. s->number_ = seq;
  33. s->list_ = this;
  34. s->next_ = &list_;
  35. s->prev_ = list_.prev_;
  36. s->prev_->next_ = s;
  37. s->next_->prev_ = s;
  38. return s;
  39. }
  40. void Delete(const Snapshot* s) {
  41. assert(s->list_ == this);
  42. s->prev_->next_ = s->next_;
  43. s->next_->prev_ = s->prev_;
  44. delete s;
  45. }
  46. private:
  47. // Dummy head of doubly-linked list of snapshots
  48. Snapshot list_;
  49. };
  50. }
  51. #endif // STORAGE_LEVELDB_DB_SNAPSHOT_H_