#pragma once
#include "rocksdb/db.h"
namespace
rocksdb {
class
SnapshotList;
class
SnapshotImpl :
public
Snapshot {
public
:
SequenceNumber number_;
private
:
friend
class
SnapshotList;
SnapshotImpl* prev_;
SnapshotImpl* next_;
SnapshotList* list_;
};
class
SnapshotList {
public
:
SnapshotList() {
list_.prev_ = &list_;
list_.next_ = &list_;
list_.number_ = 0xFFFFFFFFL;
}
bool
empty()
const
{
return
list_.next_ == &list_; }
SnapshotImpl* oldest()
const
{
assert
(!empty());
return
list_.next_; }
SnapshotImpl* newest()
const
{
assert
(!empty());
return
list_.prev_; }
const
SnapshotImpl* New(SequenceNumber seq) {
SnapshotImpl* s =
new
SnapshotImpl;
s->number_ = seq;
s->list_ =
this
;
s->next_ = &list_;
s->prev_ = list_.prev_;
s->prev_->next_ = s;
s->next_->prev_ = s;
return
s;
}
void
Delete(
const
SnapshotImpl* s) {
assert
(s->list_ ==
this
);
s->prev_->next_ = s->next_;
s->next_->prev_ = s->prev_;
delete
s;
}
void
getAll(std::vector<SequenceNumber>& ret) {
if
(empty())
return
;
SnapshotImpl* s = &list_;
while
(s->next_ != &list_) {
ret.push_back(s->next_->number_);
s = s ->next_;
}
}
const
SequenceNumber GetNewest() {
if
(empty()) {
return
0;
}
return
newest()->number_;
}
private
:
SnapshotImpl list_;
};
}