2012-11-30 02:28:37 +01:00
|
|
|
#include "db/transaction_log_iterator_impl.h"
|
|
|
|
#include "db/write_batch_internal.h"
|
|
|
|
#include "db/filename.h"
|
2013-03-21 23:12:35 +01:00
|
|
|
|
2012-11-30 02:28:37 +01:00
|
|
|
namespace leveldb {
|
|
|
|
|
|
|
|
TransactionLogIteratorImpl::TransactionLogIteratorImpl(
|
2013-03-04 19:44:04 +01:00
|
|
|
const std::string& dbname,
|
|
|
|
const Options* options,
|
2013-03-15 01:00:04 +01:00
|
|
|
const StorageOptions& soptions,
|
2013-03-04 19:44:04 +01:00
|
|
|
SequenceNumber& seq,
|
2013-04-09 22:21:11 +02:00
|
|
|
std::unique_ptr<std::vector<LogFile>> files,
|
2013-03-04 19:44:04 +01:00
|
|
|
SequenceNumber const * const lastFlushedSequence) :
|
2012-11-30 02:28:37 +01:00
|
|
|
dbname_(dbname),
|
|
|
|
options_(options),
|
2013-03-15 01:00:04 +01:00
|
|
|
soptions_(soptions),
|
2013-04-09 01:28:09 +02:00
|
|
|
startingSequenceNumber_(seq),
|
2013-04-09 22:21:11 +02:00
|
|
|
files_(std::move(files)),
|
2012-11-30 02:28:37 +01:00
|
|
|
started_(false),
|
2013-04-09 01:28:09 +02:00
|
|
|
isValid_(false),
|
2013-03-04 19:44:04 +01:00
|
|
|
currentFileIndex_(0),
|
|
|
|
lastFlushedSequence_(lastFlushedSequence) {
|
2013-04-09 22:21:11 +02:00
|
|
|
assert(files_.get() != nullptr);
|
2013-03-04 19:44:04 +01:00
|
|
|
assert(lastFlushedSequence_);
|
2012-12-17 06:01:02 +01:00
|
|
|
}
|
2012-11-30 02:28:37 +01:00
|
|
|
|
|
|
|
LogReporter
|
|
|
|
TransactionLogIteratorImpl::NewLogReporter(const uint64_t logNumber) {
|
|
|
|
LogReporter reporter;
|
|
|
|
reporter.env = options_->env;
|
2013-01-20 11:07:13 +01:00
|
|
|
reporter.info_log = options_->info_log.get();
|
2012-11-30 02:28:37 +01:00
|
|
|
reporter.log_number = logNumber;
|
|
|
|
return reporter;
|
|
|
|
}
|
|
|
|
|
2013-01-20 11:07:13 +01:00
|
|
|
Status TransactionLogIteratorImpl::OpenLogFile(
|
|
|
|
const LogFile& logFile,
|
2013-03-04 19:44:04 +01:00
|
|
|
unique_ptr<SequentialFile>* file) {
|
2012-11-30 02:28:37 +01:00
|
|
|
Env* env = options_->env;
|
|
|
|
if (logFile.type == kArchivedLogFile) {
|
|
|
|
std::string fname = ArchivedLogFileName(dbname_, logFile.logNumber);
|
2013-03-15 01:00:04 +01:00
|
|
|
return env->NewSequentialFile(fname, file, soptions_);
|
2012-11-30 02:28:37 +01:00
|
|
|
} else {
|
|
|
|
std::string fname = LogFileName(dbname_, logFile.logNumber);
|
2013-03-15 01:00:04 +01:00
|
|
|
Status status = env->NewSequentialFile(fname, file, soptions_);
|
2012-11-30 02:28:37 +01:00
|
|
|
if (!status.ok()) {
|
|
|
|
// If cannot open file in DB directory.
|
|
|
|
// Try the archive dir, as it could have moved in the meanwhile.
|
|
|
|
fname = ArchivedLogFileName(dbname_, logFile.logNumber);
|
2013-03-15 01:00:04 +01:00
|
|
|
status = env->NewSequentialFile(fname, file, soptions_);
|
2012-11-30 02:28:37 +01:00
|
|
|
if (!status.ok()) {
|
|
|
|
return Status::IOError(" Requested file not present in the dir");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 19:44:04 +01:00
|
|
|
BatchResult TransactionLogIteratorImpl::GetBatch() {
|
2012-11-30 02:28:37 +01:00
|
|
|
assert(isValid_); // cannot call in a non valid state.
|
2013-03-04 19:44:04 +01:00
|
|
|
BatchResult result;
|
|
|
|
result.sequence = currentSequence_;
|
|
|
|
result.writeBatchPtr = std::move(currentBatch_);
|
|
|
|
return result;
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status TransactionLogIteratorImpl::status() {
|
|
|
|
return currentStatus_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TransactionLogIteratorImpl::Valid() {
|
|
|
|
return started_ && isValid_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransactionLogIteratorImpl::Next() {
|
2013-04-09 22:21:11 +02:00
|
|
|
LogFile currentLogFile = files_.get()->at(currentFileIndex_);
|
2012-11-30 02:28:37 +01:00
|
|
|
LogReporter reporter = NewLogReporter(currentLogFile.logNumber);
|
2013-04-09 01:28:09 +02:00
|
|
|
|
|
|
|
// First seek to the given seqNo. in the current file.
|
2012-11-30 02:28:37 +01:00
|
|
|
std::string scratch;
|
|
|
|
Slice record;
|
|
|
|
if (!started_) {
|
2013-04-09 01:28:09 +02:00
|
|
|
started_ = true; // this piece only runs onced.
|
2013-03-04 19:44:04 +01:00
|
|
|
isValid_ = false;
|
2013-04-09 01:28:09 +02:00
|
|
|
if (startingSequenceNumber_ > *lastFlushedSequence_) {
|
2013-03-04 19:44:04 +01:00
|
|
|
currentStatus_ = Status::IOError("Looking for a sequence, "
|
|
|
|
"which is not flushed yet.");
|
|
|
|
return;
|
|
|
|
}
|
2013-04-09 01:28:09 +02:00
|
|
|
Status s = OpenLogReader(currentLogFile);
|
|
|
|
if (!s.ok()) {
|
|
|
|
currentStatus_ = s;
|
|
|
|
isValid_ = false;
|
2012-11-30 02:28:37 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-04-09 01:28:09 +02:00
|
|
|
while (currentLogReader_->ReadRecord(&record, &scratch)) {
|
2012-11-30 02:28:37 +01:00
|
|
|
if (record.size() < 12) {
|
|
|
|
reporter.Corruption(
|
|
|
|
record.size(), Status::Corruption("log record too small"));
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-04 19:44:04 +01:00
|
|
|
UpdateCurrentWriteBatch(record);
|
2013-04-09 01:28:09 +02:00
|
|
|
if (currentSequence_ >= startingSequenceNumber_) {
|
2013-03-21 23:12:35 +01:00
|
|
|
assert(currentSequence_ <= *lastFlushedSequence_);
|
2012-11-30 02:28:37 +01:00
|
|
|
isValid_ = true;
|
|
|
|
break;
|
2013-04-09 01:28:09 +02:00
|
|
|
} else {
|
|
|
|
isValid_ = false;
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
}
|
2013-04-09 01:28:09 +02:00
|
|
|
if (isValid_) {
|
|
|
|
// Done for this iteration
|
|
|
|
return;
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
2013-04-09 01:28:09 +02:00
|
|
|
}
|
|
|
|
bool openNextFile = true;
|
|
|
|
while(openNextFile) {
|
2013-01-20 11:07:13 +01:00
|
|
|
assert(currentLogReader_);
|
2013-03-21 23:12:35 +01:00
|
|
|
if (currentSequence_ < *lastFlushedSequence_) {
|
|
|
|
if (currentLogReader_->IsEOF()) {
|
|
|
|
currentLogReader_->UnmarkEOF();
|
|
|
|
}
|
|
|
|
while (currentLogReader_->ReadRecord(&record, &scratch)) {
|
|
|
|
if (record.size() < 12) {
|
|
|
|
reporter.Corruption(
|
|
|
|
record.size(), Status::Corruption("log record too small"));
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
UpdateCurrentWriteBatch(record);
|
|
|
|
openNextFile = false;
|
|
|
|
break;
|
|
|
|
}
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (openNextFile) {
|
2013-04-09 22:21:11 +02:00
|
|
|
if (currentFileIndex_ < files_.get()->size() - 1) {
|
2012-11-30 02:28:37 +01:00
|
|
|
++currentFileIndex_;
|
2013-04-09 22:21:11 +02:00
|
|
|
Status status = OpenLogReader(files_.get()->at(currentFileIndex_));
|
2012-11-30 02:28:37 +01:00
|
|
|
if (!status.ok()) {
|
|
|
|
isValid_ = false;
|
|
|
|
currentStatus_ = status;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
isValid_ = false;
|
2013-04-09 01:28:09 +02:00
|
|
|
openNextFile = false;
|
|
|
|
if (currentSequence_ == *lastFlushedSequence_) {
|
|
|
|
currentStatus_ = Status::OK();
|
|
|
|
} else {
|
|
|
|
currentStatus_ = Status::IOError(" NO MORE DATA LEFT");
|
|
|
|
}
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-04 19:44:04 +01:00
|
|
|
void TransactionLogIteratorImpl::UpdateCurrentWriteBatch(const Slice& record) {
|
|
|
|
WriteBatch* batch = new WriteBatch();
|
|
|
|
WriteBatchInternal::SetContents(batch, record);
|
|
|
|
currentSequence_ = WriteBatchInternal::Sequence(batch);
|
|
|
|
currentBatch_.reset(batch);
|
2013-03-21 23:12:35 +01:00
|
|
|
isValid_ = true;
|
2013-03-21 23:49:20 +01:00
|
|
|
currentStatus_ = Status::OK();
|
2013-03-04 19:44:04 +01:00
|
|
|
}
|
|
|
|
|
2013-04-09 01:28:09 +02:00
|
|
|
Status TransactionLogIteratorImpl::OpenLogReader(const LogFile& logFile) {
|
|
|
|
LogReporter reporter = NewLogReporter(logFile.logNumber);
|
|
|
|
unique_ptr<SequentialFile> file;
|
|
|
|
Status status = OpenLogFile(logFile, &file);
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
assert(file);
|
|
|
|
currentLogReader_.reset(
|
|
|
|
new log::Reader(std::move(file), &reporter, true, 0)
|
|
|
|
);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2012-11-30 02:28:37 +01:00
|
|
|
} // namespace leveldb
|