2013-08-06 21:54:37 +02:00
|
|
|
#include "db/transaction_log_impl.h"
|
2012-11-30 02:28:37 +01:00
|
|
|
#include "db/write_batch_internal.h"
|
2013-03-21 23:12:35 +01:00
|
|
|
|
2013-10-04 06:49:15 +02:00
|
|
|
namespace rocksdb {
|
2012-11-30 02:28:37 +01:00
|
|
|
|
|
|
|
TransactionLogIteratorImpl::TransactionLogIteratorImpl(
|
2013-03-04 19:44:04 +01:00
|
|
|
const std::string& dbname,
|
|
|
|
const Options* options,
|
2013-06-08 00:35:17 +02:00
|
|
|
const EnvOptions& soptions,
|
2013-08-06 21:54:37 +02:00
|
|
|
const SequenceNumber seq,
|
|
|
|
std::unique_ptr<VectorLogPtr> files,
|
2013-03-04 19:44:04 +01:00
|
|
|
SequenceNumber const * const lastFlushedSequence) :
|
2013-08-06 21:54:37 +02:00
|
|
|
dbname_(dbname),
|
|
|
|
options_(options),
|
|
|
|
soptions_(soptions),
|
|
|
|
startingSequenceNumber_(seq),
|
|
|
|
files_(std::move(files)),
|
|
|
|
started_(false),
|
|
|
|
isValid_(false),
|
|
|
|
currentFileIndex_(0),
|
|
|
|
lastFlushedSequence_(lastFlushedSequence) {
|
|
|
|
assert(startingSequenceNumber_ <= *lastFlushedSequence_);
|
2013-04-09 22:21:11 +02:00
|
|
|
assert(files_.get() != nullptr);
|
2012-11-30 02:28:37 +01:00
|
|
|
|
2013-04-29 22:19:24 +02:00
|
|
|
reporter_.env = options_->env;
|
|
|
|
reporter_.info_log = options_->info_log.get();
|
2012-11-30 02:28:37 +01:00
|
|
|
}
|
|
|
|
|
2013-01-20 11:07:13 +01:00
|
|
|
Status TransactionLogIteratorImpl::OpenLogFile(
|
2013-08-06 21:54:37 +02:00
|
|
|
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;
|
2013-08-06 21:54:37 +02:00
|
|
|
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 {
|
2013-08-06 21:54:37 +02:00
|
|
|
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.
|
2013-08-06 21:54:37 +02:00
|
|
|
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-08-06 21:54:37 +02:00
|
|
|
LogFile* currentLogFile = files_.get()->at(currentFileIndex_).get();
|
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) {
|
2013-04-29 22:19:24 +02:00
|
|
|
reporter_.Corruption(
|
2012-11-30 02:28:37 +01:00
|
|
|
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) {
|
2013-04-29 22:19:24 +02:00
|
|
|
reporter_.Corruption(
|
2013-03-21 23:12:35 +01:00
|
|
|
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-08-06 21:54:37 +02:00
|
|
|
Status status =OpenLogReader(files_.get()->at(currentFileIndex_).get());
|
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-08-06 21:54:37 +02:00
|
|
|
Status TransactionLogIteratorImpl::OpenLogReader(const LogFile* logFile) {
|
2013-04-09 01:28:09 +02:00
|
|
|
unique_ptr<SequentialFile> file;
|
|
|
|
Status status = OpenLogFile(logFile, &file);
|
|
|
|
if (!status.ok()) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
assert(file);
|
|
|
|
currentLogReader_.reset(
|
2013-04-29 22:19:24 +02:00
|
|
|
new log::Reader(std::move(file), &reporter_, true, 0)
|
2013-04-09 01:28:09 +02:00
|
|
|
);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2013-10-04 06:49:15 +02:00
|
|
|
} // namespace rocksdb
|