2013-10-16 21:59:46 +00: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.
|
2014-04-15 20:39:26 +00:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
2013-10-05 05:32:05 +00:00
|
|
|
#pragma once
|
2012-11-30 01:28:37 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2013-08-23 15:38:13 +00:00
|
|
|
#include "rocksdb/env.h"
|
|
|
|
#include "rocksdb/options.h"
|
|
|
|
#include "rocksdb/types.h"
|
|
|
|
#include "rocksdb/transaction_log.h"
|
2013-10-25 02:09:02 +00:00
|
|
|
#include "db/db_impl.h"
|
2012-11-30 01:28:37 +00:00
|
|
|
#include "db/log_reader.h"
|
2013-08-06 19:54:37 +00:00
|
|
|
#include "db/filename.h"
|
2012-11-30 01:28:37 +00:00
|
|
|
|
2013-10-04 04:49:15 +00:00
|
|
|
namespace rocksdb {
|
2012-11-30 01:28:37 +00:00
|
|
|
|
|
|
|
struct LogReporter : public log::Reader::Reporter {
|
|
|
|
Env* env;
|
|
|
|
Logger* info_log;
|
|
|
|
virtual void Corruption(size_t bytes, const Status& s) {
|
2013-04-29 20:19:24 +00:00
|
|
|
Log(info_log, "dropping %zu bytes; %s", bytes, s.ToString().c_str());
|
2012-11-30 01:28:37 +00:00
|
|
|
}
|
2013-10-21 02:06:19 +00:00
|
|
|
virtual void Info(const char* s) {
|
|
|
|
Log(info_log, "%s", s);
|
|
|
|
}
|
2012-11-30 01:28:37 +00:00
|
|
|
};
|
|
|
|
|
2013-08-06 19:54:37 +00:00
|
|
|
class LogFileImpl : public LogFile {
|
|
|
|
public:
|
|
|
|
LogFileImpl(uint64_t logNum, WalFileType logType, SequenceNumber startSeq,
|
|
|
|
uint64_t sizeBytes) :
|
|
|
|
logNumber_(logNum),
|
|
|
|
type_(logType),
|
|
|
|
startSequence_(startSeq),
|
|
|
|
sizeFileBytes_(sizeBytes) {
|
|
|
|
}
|
|
|
|
|
2013-08-29 21:30:52 +00:00
|
|
|
std::string PathName() const {
|
|
|
|
if (type_ == kArchivedLogFile) {
|
|
|
|
return ArchivedLogFileName("", logNumber_);
|
|
|
|
}
|
|
|
|
return LogFileName("", logNumber_);
|
|
|
|
}
|
2013-08-06 19:54:37 +00:00
|
|
|
|
|
|
|
uint64_t LogNumber() const { return logNumber_; }
|
|
|
|
|
|
|
|
WalFileType Type() const { return type_; }
|
|
|
|
|
|
|
|
SequenceNumber StartSequence() const { return startSequence_; }
|
|
|
|
|
|
|
|
uint64_t SizeFileBytes() const { return sizeFileBytes_; }
|
|
|
|
|
|
|
|
bool operator < (const LogFile& that) const {
|
|
|
|
return LogNumber() < that.LogNumber();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint64_t logNumber_;
|
|
|
|
WalFileType type_;
|
|
|
|
SequenceNumber startSequence_;
|
|
|
|
uint64_t sizeFileBytes_;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2012-11-30 01:28:37 +00:00
|
|
|
class TransactionLogIteratorImpl : public TransactionLogIterator {
|
|
|
|
public:
|
2014-02-28 19:50:36 +00:00
|
|
|
TransactionLogIteratorImpl(
|
2014-03-04 01:54:04 +00:00
|
|
|
const std::string& dir, const DBOptions* options,
|
2014-02-28 19:50:36 +00:00
|
|
|
const TransactionLogIterator::ReadOptions& read_options,
|
|
|
|
const EnvOptions& soptions, const SequenceNumber seqNum,
|
|
|
|
std::unique_ptr<VectorLogPtr> files, DBImpl const* const dbimpl);
|
2013-03-04 18:44:04 +00:00
|
|
|
|
2012-11-30 01:28:37 +00:00
|
|
|
virtual bool Valid();
|
|
|
|
|
|
|
|
virtual void Next();
|
|
|
|
|
|
|
|
virtual Status status();
|
|
|
|
|
2013-03-04 18:44:04 +00:00
|
|
|
virtual BatchResult GetBatch();
|
2012-11-30 01:28:37 +00:00
|
|
|
|
|
|
|
private:
|
2013-10-01 21:46:52 +00:00
|
|
|
const std::string& dir_;
|
2014-02-05 21:12:23 +00:00
|
|
|
const DBOptions* options_;
|
2014-02-28 19:50:36 +00:00
|
|
|
const TransactionLogIterator::ReadOptions read_options_;
|
2013-06-07 22:35:17 +00:00
|
|
|
const EnvOptions& soptions_;
|
2013-10-21 02:06:19 +00:00
|
|
|
SequenceNumber startingSequenceNumber_;
|
2013-08-06 19:54:37 +00:00
|
|
|
std::unique_ptr<VectorLogPtr> files_;
|
2012-11-30 01:28:37 +00:00
|
|
|
bool started_;
|
|
|
|
bool isValid_; // not valid when it starts of.
|
|
|
|
Status currentStatus_;
|
|
|
|
size_t currentFileIndex_;
|
2013-03-04 18:44:04 +00:00
|
|
|
std::unique_ptr<WriteBatch> currentBatch_;
|
2013-01-20 10:07:13 +00:00
|
|
|
unique_ptr<log::Reader> currentLogReader_;
|
2013-08-06 19:54:37 +00:00
|
|
|
Status OpenLogFile(const LogFile* logFile, unique_ptr<SequentialFile>* file);
|
2013-04-29 20:19:24 +00:00
|
|
|
LogReporter reporter_;
|
2013-10-13 22:28:24 +00:00
|
|
|
SequenceNumber currentBatchSeq_; // sequence number at start of current batch
|
2013-10-25 02:09:02 +00:00
|
|
|
SequenceNumber currentLastSeq_; // last sequence in the current batch
|
|
|
|
DBImpl const * const dbimpl_; // The db on whose log files this iterates
|
2013-10-13 22:28:24 +00:00
|
|
|
|
2013-10-21 02:06:19 +00:00
|
|
|
// Reads from transaction log only if the writebatch record has been written
|
|
|
|
bool RestrictedRead(Slice* record, std::string* scratch);
|
|
|
|
// Seeks to startingSequenceNumber reading from startFileIndex in files_.
|
|
|
|
// If strict is set,then must get a batch starting with startingSequenceNumber
|
|
|
|
void SeekToStartSequence(uint64_t startFileIndex = 0, bool strict = false);
|
2013-10-25 02:09:02 +00:00
|
|
|
// Implementation of Next. SeekToStartSequence calls it internally with
|
|
|
|
// internal=true to let it find next entry even if it has to jump gaps because
|
|
|
|
// the iterator may start off from the first available entry but promises to
|
|
|
|
// be continuous after that
|
|
|
|
void NextImpl(bool internal = false);
|
|
|
|
// Check if batch is expected, else return false
|
|
|
|
bool IsBatchExpected(const WriteBatch* batch, SequenceNumber expectedSeq);
|
2013-10-21 02:06:19 +00:00
|
|
|
// Update current batch if a continuous batch is found, else return false
|
2013-03-04 18:44:04 +00:00
|
|
|
void UpdateCurrentWriteBatch(const Slice& record);
|
2013-08-06 19:54:37 +00:00
|
|
|
Status OpenLogReader(const LogFile* file);
|
2012-11-30 01:28:37 +00:00
|
|
|
};
|
2013-10-04 04:49:15 +00:00
|
|
|
} // namespace rocksdb
|
2014-04-15 20:39:26 +00:00
|
|
|
#endif // ROCKSDB_LITE
|