2013-10-16 23:59:46 +02:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
//
|
2011-03-18 23:37:00 +01: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.
|
|
|
|
|
2013-10-05 07:32:05 +02:00
|
|
|
#pragma once
|
2013-08-23 17:38:13 +02:00
|
|
|
#include "rocksdb/db.h"
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
class SnapshotList;
|
|
|
|
|
|
|
|
// Snapshots are kept in a doubly-linked list in the DB.
|
2011-05-21 04:17:43 +02:00
|
|
|
// Each SnapshotImpl corresponds to a particular sequence number.
|
|
|
|
class SnapshotImpl : public Snapshot {
|
2011-03-18 23:37:00 +01:00
|
|
|
public:
|
|
|
|
SequenceNumber number_; // const after creation
|
|
|
|
|
2015-02-26 20:28:41 +01:00
|
|
|
virtual SequenceNumber GetSequenceNumber() const override { return number_; }
|
2015-01-30 03:22:43 +01:00
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
private:
|
|
|
|
friend class SnapshotList;
|
|
|
|
|
2011-05-21 04:17:43 +02:00
|
|
|
// SnapshotImpl is kept in a doubly-linked circular list
|
|
|
|
SnapshotImpl* prev_;
|
|
|
|
SnapshotImpl* next_;
|
2011-03-18 23:37:00 +01:00
|
|
|
|
|
|
|
SnapshotList* list_; // just for sanity checks
|
2014-12-06 01:12:10 +01:00
|
|
|
|
|
|
|
int64_t unix_time_;
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class SnapshotList {
|
|
|
|
public:
|
|
|
|
SnapshotList() {
|
|
|
|
list_.prev_ = &list_;
|
|
|
|
list_.next_ = &list_;
|
2012-11-27 06:16:21 +01:00
|
|
|
list_.number_ = 0xFFFFFFFFL; // placeholder marker, for debugging
|
2014-12-06 01:12:10 +01:00
|
|
|
count_ = 0;
|
2011-03-18 23:37:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const { return list_.next_ == &list_; }
|
2011-05-21 04:17:43 +02:00
|
|
|
SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; }
|
|
|
|
SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; }
|
2011-03-18 23:37:00 +01:00
|
|
|
|
2014-12-06 01:12:10 +01:00
|
|
|
const SnapshotImpl* New(SequenceNumber seq, uint64_t unix_time) {
|
2011-05-21 04:17:43 +02:00
|
|
|
SnapshotImpl* s = new SnapshotImpl;
|
2011-03-18 23:37:00 +01:00
|
|
|
s->number_ = seq;
|
2014-12-06 01:12:10 +01:00
|
|
|
s->unix_time_ = unix_time;
|
2011-03-18 23:37:00 +01:00
|
|
|
s->list_ = this;
|
|
|
|
s->next_ = &list_;
|
|
|
|
s->prev_ = list_.prev_;
|
|
|
|
s->prev_->next_ = s;
|
|
|
|
s->next_->prev_ = s;
|
2014-12-06 01:12:10 +01:00
|
|
|
count_++;
|
2011-03-18 23:37:00 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-05-21 04:17:43 +02:00
|
|
|
void Delete(const SnapshotImpl* s) {
|
2011-03-18 23:37:00 +01:00
|
|
|
assert(s->list_ == this);
|
|
|
|
s->prev_->next_ = s->next_;
|
|
|
|
s->next_->prev_ = s->prev_;
|
2014-12-06 01:12:10 +01:00
|
|
|
count_--;
|
2011-03-18 23:37:00 +01:00
|
|
|
delete s;
|
|
|
|
}
|
|
|
|
|
2012-11-27 06:16:21 +01:00
|
|
|
// retrieve all snapshot numbers. They are sorted in ascending order.
|
|
|
|
void getAll(std::vector<SequenceNumber>& ret) {
|
|
|
|
if (empty()) return;
|
2013-05-04 00:26:12 +02:00
|
|
|
SnapshotImpl* s = &list_;
|
2012-11-27 06:16:21 +01:00
|
|
|
while (s->next_ != &list_) {
|
|
|
|
ret.push_back(s->next_->number_);
|
|
|
|
s = s ->next_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-28 23:09:30 +01:00
|
|
|
// get the sequence number of the most recent snapshot
|
2014-09-04 16:04:37 +02:00
|
|
|
SequenceNumber GetNewest() {
|
2013-02-28 23:09:30 +01:00
|
|
|
if (empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return newest()->number_;
|
|
|
|
}
|
|
|
|
|
2014-12-06 01:12:10 +01:00
|
|
|
int64_t GetOldestSnapshotTime() const {
|
|
|
|
if (empty()) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return oldest()->unix_time_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t count() const { return count_; }
|
|
|
|
|
2011-03-18 23:37:00 +01:00
|
|
|
private:
|
|
|
|
// Dummy head of doubly-linked list of snapshots
|
2011-05-21 04:17:43 +02:00
|
|
|
SnapshotImpl list_;
|
2014-12-06 01:12:10 +01:00
|
|
|
uint64_t count_;
|
2011-03-18 23:37:00 +01:00
|
|
|
};
|
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|