]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
5 | #pragma once |
6 | ||
7 | #ifndef ROCKSDB_LITE | |
8 | #include <vector> | |
9 | ||
10 | #include "db/log_reader.h" | |
11 | #include "db/version_set.h" | |
f67539c2 | 12 | #include "file/filename.h" |
1e59de90 | 13 | #include "logging/logging.h" |
7c673cae FG |
14 | #include "options/db_options.h" |
15 | #include "port/port.h" | |
16 | #include "rocksdb/env.h" | |
17 | #include "rocksdb/options.h" | |
18 | #include "rocksdb/transaction_log.h" | |
19 | #include "rocksdb/types.h" | |
7c673cae | 20 | |
f67539c2 | 21 | namespace ROCKSDB_NAMESPACE { |
7c673cae FG |
22 | |
23 | class LogFileImpl : public LogFile { | |
24 | public: | |
25 | LogFileImpl(uint64_t logNum, WalFileType logType, SequenceNumber startSeq, | |
1e59de90 TL |
26 | uint64_t sizeBytes) |
27 | : logNumber_(logNum), | |
28 | type_(logType), | |
29 | startSequence_(startSeq), | |
30 | sizeFileBytes_(sizeBytes) {} | |
7c673cae FG |
31 | |
32 | std::string PathName() const override { | |
33 | if (type_ == kArchivedLogFile) { | |
34 | return ArchivedLogFileName("", logNumber_); | |
35 | } | |
36 | return LogFileName("", logNumber_); | |
37 | } | |
38 | ||
39 | uint64_t LogNumber() const override { return logNumber_; } | |
40 | ||
41 | WalFileType Type() const override { return type_; } | |
42 | ||
43 | SequenceNumber StartSequence() const override { return startSequence_; } | |
44 | ||
45 | uint64_t SizeFileBytes() const override { return sizeFileBytes_; } | |
46 | ||
1e59de90 | 47 | bool operator<(const LogFile& that) const { |
7c673cae FG |
48 | return LogNumber() < that.LogNumber(); |
49 | } | |
50 | ||
51 | private: | |
52 | uint64_t logNumber_; | |
53 | WalFileType type_; | |
54 | SequenceNumber startSequence_; | |
55 | uint64_t sizeFileBytes_; | |
7c673cae FG |
56 | }; |
57 | ||
58 | class TransactionLogIteratorImpl : public TransactionLogIterator { | |
59 | public: | |
60 | TransactionLogIteratorImpl( | |
61 | const std::string& dir, const ImmutableDBOptions* options, | |
62 | const TransactionLogIterator::ReadOptions& read_options, | |
63 | const EnvOptions& soptions, const SequenceNumber seqNum, | |
11fdf7f2 | 64 | std::unique_ptr<VectorLogPtr> files, VersionSet const* const versions, |
20effc67 | 65 | const bool seq_per_batch, const std::shared_ptr<IOTracer>& io_tracer); |
7c673cae FG |
66 | |
67 | virtual bool Valid() override; | |
68 | ||
69 | virtual void Next() override; | |
70 | ||
71 | virtual Status status() override; | |
72 | ||
73 | virtual BatchResult GetBatch() override; | |
74 | ||
75 | private: | |
76 | const std::string& dir_; | |
77 | const ImmutableDBOptions* options_; | |
78 | const TransactionLogIterator::ReadOptions read_options_; | |
79 | const EnvOptions& soptions_; | |
494da23a | 80 | SequenceNumber starting_sequence_number_; |
7c673cae | 81 | std::unique_ptr<VectorLogPtr> files_; |
1e59de90 TL |
82 | // Used only to get latest seq. num |
83 | // TODO(icanadi) can this be just a callback? | |
84 | VersionSet const* const versions_; | |
85 | const bool seq_per_batch_; | |
86 | std::shared_ptr<IOTracer> io_tracer_; | |
87 | ||
88 | // State variables | |
7c673cae | 89 | bool started_; |
494da23a TL |
90 | bool is_valid_; // not valid when it starts of. |
91 | Status current_status_; | |
92 | size_t current_file_index_; | |
93 | std::unique_ptr<WriteBatch> current_batch_; | |
94 | std::unique_ptr<log::Reader> current_log_reader_; | |
f67539c2 | 95 | std::string scratch_; |
494da23a TL |
96 | Status OpenLogFile(const LogFile* log_file, |
97 | std::unique_ptr<SequentialFileReader>* file); | |
7c673cae FG |
98 | |
99 | struct LogReporter : public log::Reader::Reporter { | |
100 | Env* env; | |
101 | Logger* info_log; | |
102 | virtual void Corruption(size_t bytes, const Status& s) override { | |
103 | ROCKS_LOG_ERROR(info_log, "dropping %" ROCKSDB_PRIszt " bytes; %s", bytes, | |
104 | s.ToString().c_str()); | |
105 | } | |
106 | virtual void Info(const char* s) { ROCKS_LOG_INFO(info_log, "%s", s); } | |
107 | } reporter_; | |
108 | ||
494da23a TL |
109 | SequenceNumber |
110 | current_batch_seq_; // sequence number at start of current batch | |
111 | SequenceNumber current_last_seq_; // last sequence in the current batch | |
7c673cae | 112 | // Reads from transaction log only if the writebatch record has been written |
f67539c2 | 113 | bool RestrictedRead(Slice* record); |
1e59de90 TL |
114 | // Seeks to starting_sequence_number_ reading from start_file_index in files_. |
115 | // If strict is set, then must get a batch starting with | |
116 | // starting_sequence_number_. | |
494da23a | 117 | void SeekToStartSequence(uint64_t start_file_index = 0, bool strict = false); |
7c673cae FG |
118 | // Implementation of Next. SeekToStartSequence calls it internally with |
119 | // internal=true to let it find next entry even if it has to jump gaps because | |
120 | // the iterator may start off from the first available entry but promises to | |
121 | // be continuous after that | |
122 | void NextImpl(bool internal = false); | |
123 | // Check if batch is expected, else return false | |
494da23a | 124 | bool IsBatchExpected(const WriteBatch* batch, SequenceNumber expected_seq); |
1e59de90 | 125 | // Update current batch if a continuous batch is found. |
7c673cae FG |
126 | void UpdateCurrentWriteBatch(const Slice& record); |
127 | Status OpenLogReader(const LogFile* file); | |
128 | }; | |
f67539c2 | 129 | } // namespace ROCKSDB_NAMESPACE |
7c673cae | 130 | #endif // ROCKSDB_LITE |