Zero  0.1.0
logarchive_scanner.h
Go to the documentation of this file.
1 #ifndef __LOGARCHIVE_SCANNER_H
2 #define __LOGARCHIVE_SCANNER_H
3 
4 #include <iostream>
5 #include <memory>
6 #include <vector>
7 
8 #include "basics.h"
9 #include "lsn.h"
10 #include "logarchive_index.h"
11 
12 class ArchiveIndex;
13 class RunFile;
14 class logrec_t;
15 
16 struct MergeInput {
18 
19  size_t pos;
20 
22 
24 
26 
27  logrec_t* logrec();
28 
29  bool open(PageID startPID);
30 
31  bool finished();
32 
33  void next();
34 
35  friend bool mergeInputCmpGt(const MergeInput& a, const MergeInput& b);
36 };
37 
38 
39 // Merge input should be exactly 1/2 of a cacheline
40 static_assert(sizeof(MergeInput) == 32, "Misaligned MergeInput");
41 
42 class ArchiveScan {
43 public:
44  ArchiveScan(std::shared_ptr<ArchiveIndex>);
45 
46  ~ArchiveScan();
47 
48  void open(PageID startPID, PageID endPID, lsn_t startLSN,
49  lsn_t endLSN = lsn_t::null);
50 
51  bool next(logrec_t*&);
52 
53  bool finished();
54 
55  template<class Iter>
56  void openForMerge(Iter begin, Iter end);
57 
58  void dumpHeap();
59 
60 private:
61  // Thread-local storage for merge inputs
62  static thread_local std::vector<MergeInput> _mergeInputVector;
63 
64  std::vector<MergeInput>::iterator heapBegin;
65 
66  std::vector<MergeInput>::iterator heapEnd;
67 
68  std::shared_ptr<ArchiveIndex> archIndex;
69 
71 
73 
74  bool singlePage;
75 
76  void clear();
77 };
78 
79 bool mergeInputCmpGt(const MergeInput& a, const MergeInput& b);
80 
81 template<class Iter>
82 void ArchiveScan::openForMerge(Iter begin, Iter end) {
84  clear();
85  auto& inputs = _mergeInputVector;
86 
87  for (Iter it = begin; it != end; it++) {
88  MergeInput input;
89  auto runid = *it;
90  input.pos = 0;
91  input.runFile = archIndex->openForScan(*it);
92  inputs.push_back(input);
93  }
94 
95  heapBegin = inputs.begin();
96  auto it = inputs.rbegin();
97  while (it != inputs.rend()) {
98  constexpr PageID startPID = 0;
99  if (it->open(startPID)) {
100  it++;
101  } else {
102  std::advance(it, 1);
103  inputs.erase(it.base());
104  }
105  }
106 
107  heapEnd = inputs.end();
108  std::make_heap(heapBegin, heapEnd, mergeInputCmpGt);
109 }
110 
111 #endif // __LOGARCHIVE_SCANNER_H
PageID prevPID
Definition: logarchive_scanner.h:72
bool finished()
Definition: logarchive_scanner.cpp:154
bool open(PageID startPID)
Definition: logarchive_scanner.cpp:127
void next()
Definition: logarchive_scanner.cpp:162
static thread_local std::vector< MergeInput > _mergeInputVector
Definition: logarchive_scanner.h:62
RunFile * runFile
Definition: logarchive_scanner.h:17
static const lsn_t null
Definition: lsn.h:371
shared_ptr< ArchiveIndex > archIndex
Definition: archstats.cpp:5
Definition: logarchive_scanner.h:42
lsn_t keyLSN
Definition: logarchive_scanner.h:21
Encapsulates all file and I/O operations on the log archive.
Definition: logarchive_index.h:94
void openForMerge(Iter begin, Iter end)
Definition: logarchive_scanner.h:82
Represents a transactional log record.
Definition: logrec.h:143
friend bool mergeInputCmpGt(const MergeInput &a, const MergeInput &b)
Definition: logarchive_scanner.cpp:16
std::vector< MergeInput >::iterator heapEnd
Definition: logarchive_scanner.h:66
uint32_t PageID
Definition: basics.h:45
bool singlePage
Definition: logarchive_scanner.h:74
#define w_assert0(x)
Default assert/debug level is 0.
Definition: w_base.h:175
lsn_t prevLSN
Definition: logarchive_scanner.h:70
Log Sequence Number. See Log Sequence Numbers (LSN).
Definition: lsn.h:243
std::shared_ptr< ArchiveIndex > archIndex
Definition: logarchive_scanner.h:68
Definition: logarchive_index.h:35
std::vector< MergeInput >::iterator heapBegin
Definition: logarchive_scanner.h:64
PageID keyPID
Definition: logarchive_scanner.h:23
logrec_t * logrec()
Definition: logarchive_scanner.cpp:123
size_t pos
Definition: logarchive_scanner.h:19
PageID endPID
Definition: logarchive_scanner.h:25
Definition: logarchive_scanner.h:16