Squashed 'src/leveldb/' changes from 20ca81f..a31c8aa
[bitcoinplatinum.git] / db / snapshot.h
blob6ed413c42d4f4a8d531fba2d53617605741046dc
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.
5 #ifndef STORAGE_LEVELDB_DB_SNAPSHOT_H_
6 #define STORAGE_LEVELDB_DB_SNAPSHOT_H_
8 #include "db/dbformat.h"
9 #include "leveldb/db.h"
11 namespace leveldb {
13 class SnapshotList;
15 // Snapshots are kept in a doubly-linked list in the DB.
16 // Each SnapshotImpl corresponds to a particular sequence number.
17 class SnapshotImpl : public Snapshot {
18 public:
19 SequenceNumber number_; // const after creation
21 private:
22 friend class SnapshotList;
24 // SnapshotImpl is kept in a doubly-linked circular list
25 SnapshotImpl* prev_;
26 SnapshotImpl* next_;
28 SnapshotList* list_; // just for sanity checks
31 class SnapshotList {
32 public:
33 SnapshotList() {
34 list_.prev_ = &list_;
35 list_.next_ = &list_;
38 bool empty() const { return list_.next_ == &list_; }
39 SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; }
40 SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; }
42 const SnapshotImpl* New(SequenceNumber seq) {
43 SnapshotImpl* s = new SnapshotImpl;
44 s->number_ = seq;
45 s->list_ = this;
46 s->next_ = &list_;
47 s->prev_ = list_.prev_;
48 s->prev_->next_ = s;
49 s->next_->prev_ = s;
50 return s;
53 void Delete(const SnapshotImpl* s) {
54 assert(s->list_ == this);
55 s->prev_->next_ = s->next_;
56 s->next_->prev_ = s->prev_;
57 delete s;
60 private:
61 // Dummy head of doubly-linked list of snapshots
62 SnapshotImpl list_;
65 } // namespace leveldb
67 #endif // STORAGE_LEVELDB_DB_SNAPSHOT_H_