transaction_log_impl.h 4.3 KB

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