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