1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
10 #include "db/log_reader.h"
11 #include "db/version_set.h"
12 #include "options/db_options.h"
13 #include "port/port.h"
14 #include "rocksdb/env.h"
15 #include "rocksdb/options.h"
16 #include "rocksdb/transaction_log.h"
17 #include "rocksdb/types.h"
18 #include "util/filename.h"
22 class LogFileImpl
: public LogFile
{
24 LogFileImpl(uint64_t logNum
, WalFileType logType
, SequenceNumber startSeq
,
28 startSequence_(startSeq
),
29 sizeFileBytes_(sizeBytes
) {
32 std::string
PathName() const override
{
33 if (type_
== kArchivedLogFile
) {
34 return ArchivedLogFileName("", logNumber_
);
36 return LogFileName("", logNumber_
);
39 uint64_t LogNumber() const override
{ return logNumber_
; }
41 WalFileType
Type() const override
{ return type_
; }
43 SequenceNumber
StartSequence() const override
{ return startSequence_
; }
45 uint64_t SizeFileBytes() const override
{ return sizeFileBytes_
; }
47 bool operator < (const LogFile
& that
) const {
48 return LogNumber() < that
.LogNumber();
54 SequenceNumber startSequence_
;
55 uint64_t sizeFileBytes_
;
59 class TransactionLogIteratorImpl
: public TransactionLogIterator
{
61 TransactionLogIteratorImpl(
62 const std::string
& dir
, const ImmutableDBOptions
* options
,
63 const TransactionLogIterator::ReadOptions
& read_options
,
64 const EnvOptions
& soptions
, const SequenceNumber seqNum
,
65 std::unique_ptr
<VectorLogPtr
> files
, VersionSet
const* const versions
,
66 const bool seq_per_batch
);
68 virtual bool Valid() override
;
70 virtual void Next() override
;
72 virtual Status
status() override
;
74 virtual BatchResult
GetBatch() override
;
77 const std::string
& dir_
;
78 const ImmutableDBOptions
* options_
;
79 const TransactionLogIterator::ReadOptions read_options_
;
80 const EnvOptions
& soptions_
;
81 SequenceNumber startingSequenceNumber_
;
82 std::unique_ptr
<VectorLogPtr
> files_
;
84 bool isValid_
; // not valid when it starts of.
85 Status currentStatus_
;
86 size_t currentFileIndex_
;
87 std::unique_ptr
<WriteBatch
> currentBatch_
;
88 unique_ptr
<log::Reader
> currentLogReader_
;
89 Status
OpenLogFile(const LogFile
* logFile
,
90 unique_ptr
<SequentialFileReader
>* file
);
92 struct LogReporter
: public log::Reader::Reporter
{
95 virtual void Corruption(size_t bytes
, const Status
& s
) override
{
96 ROCKS_LOG_ERROR(info_log
, "dropping %" ROCKSDB_PRIszt
" bytes; %s", bytes
,
97 s
.ToString().c_str());
99 virtual void Info(const char* s
) { ROCKS_LOG_INFO(info_log
, "%s", s
); }
102 SequenceNumber currentBatchSeq_
; // sequence number at start of current batch
103 SequenceNumber currentLastSeq_
; // last sequence in the current batch
104 // Used only to get latest seq. num
105 // TODO(icanadi) can this be just a callback?
106 VersionSet
const* const versions_
;
107 const bool seq_per_batch_
;
108 // Reads from transaction log only if the writebatch record has been written
109 bool RestrictedRead(Slice
* record
, std::string
* scratch
);
110 // Seeks to startingSequenceNumber reading from startFileIndex in files_.
111 // If strict is set,then must get a batch starting with startingSequenceNumber
112 void SeekToStartSequence(uint64_t startFileIndex
= 0, bool strict
= false);
113 // Implementation of Next. SeekToStartSequence calls it internally with
114 // internal=true to let it find next entry even if it has to jump gaps because
115 // the iterator may start off from the first available entry but promises to
116 // be continuous after that
117 void NextImpl(bool internal
= false);
118 // Check if batch is expected, else return false
119 bool IsBatchExpected(const WriteBatch
* batch
, SequenceNumber expectedSeq
);
120 // Update current batch if a continuous batch is found, else return false
121 void UpdateCurrentWriteBatch(const Slice
& record
);
122 Status
OpenLogReader(const LogFile
* file
);
124 } // namespace rocksdb
125 #endif // ROCKSDB_LITE