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 
6 #pragma once
7 
8 #include <memory>
9 #include <vector>
10 #include "rocksdb/status.h"
11 #include "rocksdb/types.h"
12 #include "rocksdb/write_batch.h"
13 
14 namespace ROCKSDB_NAMESPACE {
15 
16 class LogFile;
17 typedef std::vector<std::unique_ptr<LogFile>> VectorLogPtr;
18 
19 enum WalFileType {
20   /* Indicates that WAL file is in archive directory. WAL files are moved from
21    * the main db directory to archive directory once they are not live and stay
22    * there until cleaned up. Files are cleaned depending on archive size
23    * (Options::WAL_size_limit_MB) and time since last cleaning
24    * (Options::WAL_ttl_seconds).
25    */
26   kArchivedLogFile = 0,
27 
28   /* Indicates that WAL file is live and resides in the main db directory */
29   kAliveLogFile = 1
30 };
31 
32 class LogFile {
33  public:
LogFile()34   LogFile() {}
~LogFile()35   virtual ~LogFile() {}
36 
37   // Returns log file's pathname relative to the main db dir
38   // Eg. For a live-log-file = /000003.log
39   //     For an archived-log-file = /archive/000003.log
40   virtual std::string PathName() const = 0;
41 
42   // Primary identifier for log file.
43   // This is directly proportional to creation time of the log file
44   virtual uint64_t LogNumber() const = 0;
45 
46   // Log file can be either alive or archived
47   virtual WalFileType Type() const = 0;
48 
49   // Starting sequence number of writebatch written in this log file
50   virtual SequenceNumber StartSequence() const = 0;
51 
52   // Size of log file on disk in Bytes
53   virtual uint64_t SizeFileBytes() const = 0;
54 };
55 
56 struct BatchResult {
57   SequenceNumber sequence = 0;
58   std::unique_ptr<WriteBatch> writeBatchPtr;
59 
60   // Add empty __ctor and __dtor for the rule of five
61   // However, preserve the original semantics and prohibit copying
62   // as the std::unique_ptr member does not copy.
BatchResultBatchResult63   BatchResult() {}
64 
~BatchResultBatchResult65   ~BatchResult() {}
66 
67   BatchResult(const BatchResult&) = delete;
68 
69   BatchResult& operator=(const BatchResult&) = delete;
70 
BatchResultBatchResult71   BatchResult(BatchResult&& bResult)
72       : sequence(std::move(bResult.sequence)),
73         writeBatchPtr(std::move(bResult.writeBatchPtr)) {}
74 
75   BatchResult& operator=(BatchResult&& bResult) {
76     sequence = std::move(bResult.sequence);
77     writeBatchPtr = std::move(bResult.writeBatchPtr);
78     return *this;
79   }
80 };
81 
82 // A TransactionLogIterator is used to iterate over the transactions in a db.
83 // One run of the iterator is continuous, i.e. the iterator will stop at the
84 // beginning of any gap in sequences
85 class TransactionLogIterator {
86  public:
TransactionLogIterator()87   TransactionLogIterator() {}
~TransactionLogIterator()88   virtual ~TransactionLogIterator() {}
89 
90   // An iterator is either positioned at a WriteBatch or not valid.
91   // This method returns true if the iterator is valid.
92   // Can read data from a valid iterator.
93   virtual bool Valid() = 0;
94 
95   // Moves the iterator to the next WriteBatch.
96   // REQUIRES: Valid() to be true.
97   virtual void Next() = 0;
98 
99   // Returns ok if the iterator is valid.
100   // Returns the Error when something has gone wrong.
101   virtual Status status() = 0;
102 
103   // If valid return's the current write_batch and the sequence number of the
104   // earliest transaction contained in the batch.
105   // ONLY use if Valid() is true and status() is OK.
106   virtual BatchResult GetBatch() = 0;
107 
108   // The read options for TransactionLogIterator.
109   struct ReadOptions {
110     // If true, all data read from underlying storage will be
111     // verified against corresponding checksums.
112     // Default: true
113     bool verify_checksums_;
114 
ReadOptionsReadOptions115     ReadOptions() : verify_checksums_(true) {}
116 
ReadOptionsReadOptions117     explicit ReadOptions(bool verify_checksums)
118         : verify_checksums_(verify_checksums) {}
119   };
120 };
121 }  // namespace ROCKSDB_NAMESPACE
122