1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be
3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors.
4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#ifndef STORAGE_LEVELDB_DB_SNAPSHOT_H_
6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#define STORAGE_LEVELDB_DB_SNAPSHOT_H_
7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
8fbd97aa4c5325eace57d24b89845b9581bac9324jorlow@chromium.org#include "leveldb/db.h"
9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb {
11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass SnapshotList;
13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Snapshots are kept in a doubly-linked list in the DB.
15a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// Each SnapshotImpl corresponds to a particular sequence number.
16a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.orgclass SnapshotImpl : public Snapshot {
17179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org public:
18179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  SequenceNumber number_;  // const after creation
19179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org private:
21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  friend class SnapshotList;
22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
23a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  // SnapshotImpl is kept in a doubly-linked circular list
24a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  SnapshotImpl* prev_;
25a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  SnapshotImpl* next_;
26179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
27179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  SnapshotList* list_;                 // just for sanity checks
28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org};
29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass SnapshotList {
31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org public:
32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  SnapshotList() {
33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    list_.prev_ = &list_;
34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    list_.next_ = &list_;
35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  bool empty() const { return list_.next_ == &list_; }
38a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; }
39a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; }
40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
41a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  const SnapshotImpl* New(SequenceNumber seq) {
42a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org    SnapshotImpl* s = new SnapshotImpl;
43179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    s->number_ = seq;
44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    s->list_ = this;
45179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    s->next_ = &list_;
46179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    s->prev_ = list_.prev_;
47179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    s->prev_->next_ = s;
48179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    s->next_->prev_ = s;
49179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    return s;
50179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
51179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
52a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  void Delete(const SnapshotImpl* s) {
53179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    assert(s->list_ == this);
54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    s->prev_->next_ = s->next_;
55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    s->next_->prev_ = s->prev_;
56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org    delete s;
57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  }
58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org private:
60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Dummy head of doubly-linked list of snapshots
61a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  SnapshotImpl list_;
62179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org};
63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
6445b9940be332834440bd5299419f396e38085ebehans@chromium.org}  // namespace leveldb
65179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
66179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#endif  // STORAGE_LEVELDB_DB_SNAPSHOT_H_
67