2011-03-19 06:37:00 +08:00
|
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
|
|
|
#ifndef STORAGE_LEVELDB_DB_SNAPSHOT_H_
|
|
|
|
#define STORAGE_LEVELDB_DB_SNAPSHOT_H_
|
|
|
|
|
2014-12-12 00:02:45 +08:00
|
|
|
#include "db/dbformat.h"
|
2011-03-31 02:35:40 +08:00
|
|
|
#include "leveldb/db.h"
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
class SnapshotList;
|
|
|
|
|
|
|
|
// Snapshots are kept in a doubly-linked list in the DB.
|
2011-05-21 10:17:43 +08:00
|
|
|
// Each SnapshotImpl corresponds to a particular sequence number.
|
|
|
|
class SnapshotImpl : public Snapshot {
|
2011-03-19 06:37:00 +08:00
|
|
|
public:
|
|
|
|
SequenceNumber number_; // const after creation
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class SnapshotList;
|
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
// SnapshotImpl is kept in a doubly-linked circular list
|
|
|
|
SnapshotImpl* prev_;
|
|
|
|
SnapshotImpl* next_;
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
SnapshotList* list_; // just for sanity checks
|
|
|
|
};
|
|
|
|
|
|
|
|
class SnapshotList {
|
|
|
|
public:
|
|
|
|
SnapshotList() {
|
|
|
|
list_.prev_ = &list_;
|
|
|
|
list_.next_ = &list_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const { return list_.next_ == &list_; }
|
2011-05-21 10:17:43 +08:00
|
|
|
SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; }
|
|
|
|
SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; }
|
2011-03-19 06:37:00 +08:00
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
const SnapshotImpl* New(SequenceNumber seq) {
|
|
|
|
SnapshotImpl* s = new SnapshotImpl;
|
2011-03-19 06:37:00 +08:00
|
|
|
s->number_ = seq;
|
|
|
|
s->list_ = this;
|
|
|
|
s->next_ = &list_;
|
|
|
|
s->prev_ = list_.prev_;
|
|
|
|
s->prev_->next_ = s;
|
|
|
|
s->next_->prev_ = s;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-05-21 10:17:43 +08:00
|
|
|
void Delete(const SnapshotImpl* s) {
|
2011-03-19 06:37:00 +08:00
|
|
|
assert(s->list_ == this);
|
|
|
|
s->prev_->next_ = s->next_;
|
|
|
|
s->next_->prev_ = s->prev_;
|
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Dummy head of doubly-linked list of snapshots
|
2011-05-21 10:17:43 +08:00
|
|
|
SnapshotImpl list_;
|
2011-03-19 06:37:00 +08:00
|
|
|
};
|
|
|
|
|
2011-11-01 01:22:06 +08:00
|
|
|
} // namespace leveldb
|
2011-03-19 06:37:00 +08:00
|
|
|
|
|
|
|
#endif // STORAGE_LEVELDB_DB_SNAPSHOT_H_
|