snapshot.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "leveldb/db.h"
  7. namespace leveldb {
  8. class SnapshotList;
  9. // Snapshots are kept in a doubly-linked list in the DB.
  10. // Each SnapshotImpl corresponds to a particular sequence number.
  11. class SnapshotImpl : public Snapshot {
  12. public:
  13. SequenceNumber number_; // const after creation
  14. private:
  15. friend class SnapshotList;
  16. // SnapshotImpl is kept in a doubly-linked circular list
  17. SnapshotImpl* prev_;
  18. SnapshotImpl* 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. SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; }
  29. SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; }
  30. const SnapshotImpl* New(SequenceNumber seq) {
  31. SnapshotImpl* s = new SnapshotImpl;
  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 SnapshotImpl* 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. SnapshotImpl list_;
  49. };
  50. } // namespace leveldb
  51. #endif // STORAGE_LEVELDB_DB_SNAPSHOT_H_