OmniSciDB  85c2d10cdc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Page.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
23 #ifndef DATAMGR_MEMORY_FILE_PAGE_H
24 #define DATAMGR_MEMORY_FILE_PAGE_H
25 
26 #include "Logger/Logger.h"
27 
28 #include <cassert>
29 #include <deque>
30 #include <stdexcept>
31 #include <vector>
32 #include "../../Shared/types.h"
33 
34 namespace File_Namespace {
35 
46 struct Page {
47  int32_t fileId;
48  size_t pageNum;
49 
51  Page(int32_t fileId, size_t pageNum) : fileId(fileId), pageNum(pageNum) {}
52  Page() : fileId(-1), pageNum(0) {}
53 
54  inline bool isValid() { return fileId >= 0; }
55 
56  bool operator<(const Page other) const {
57  if (fileId != other.fileId) {
58  return fileId < other.fileId;
59  }
60  return pageNum < other.pageNum;
61  }
62 };
63 
74 struct EpochedPage {
76  int32_t epoch;
77 };
78 
79 struct MultiPage {
80  size_t pageSize;
81  std::deque<EpochedPage> pageVersions;
82 
84  MultiPage(size_t pageSizeIn) : pageSize(pageSizeIn) {}
85 
88  while (pageVersions.size() > 0) {
89  pop();
90  }
91  }
92 
94  inline EpochedPage current() const {
95  if (pageVersions.size() < 1) {
96  LOG(FATAL) << "No current version of the page exists in this MultiPage.";
97  }
98  return pageVersions.back();
99  }
100 
102  inline void push(const Page& page, const int epoch) {
103  if (!pageVersions.empty()) {
104  CHECK_GT(epoch, pageVersions.back().epoch);
105  }
106  pageVersions.push_back({page, epoch});
107  }
108 
110  inline void pop() {
111  if (pageVersions.size() < 1) {
112  LOG(FATAL) << "No page to pop.";
113  }
114  pageVersions.pop_front();
115  }
116 
117  std::vector<EpochedPage> freePagesBeforeEpoch(const int32_t target_epoch,
118  const int32_t current_epoch) {
119  std::vector<EpochedPage> pagesBeforeEpoch;
120  int32_t next_page_epoch = current_epoch + 1;
121  for (auto pageIt = pageVersions.rbegin(); pageIt != pageVersions.rend(); ++pageIt) {
122  const int32_t epoch_ceiling = next_page_epoch - 1;
123  CHECK_LE(pageIt->epoch, epoch_ceiling);
124  if (epoch_ceiling < target_epoch) {
125  pagesBeforeEpoch.emplace_back(*pageIt);
126  }
127  next_page_epoch = pageIt->epoch;
128  }
129  if (!pagesBeforeEpoch.empty()) {
130  pageVersions.erase(pageVersions.begin(),
131  pageVersions.begin() + pagesBeforeEpoch.size());
132  }
133  return pagesBeforeEpoch;
134  }
135 };
136 
143 struct HeaderInfo {
144  ChunkKey chunkKey; // a vector of ints
145  int32_t pageId;
146  int32_t versionEpoch;
148 
150  const int32_t pageId,
151  const int32_t versionEpoch,
152  const Page& page)
153  : chunkKey(chunkKey), pageId(pageId), versionEpoch(versionEpoch), page(page) {}
154 
155  bool operator<(const HeaderInfo& other) {
156  if (chunkKey != other.chunkKey) {
157  return chunkKey < other.chunkKey;
158  }
159  if (pageId != other.pageId) {
160  return pageId < other.pageId;
161  }
162  return versionEpoch < other.versionEpoch;
163  }
164 };
165 
166 } // namespace File_Namespace
167 
168 #endif // DATAMGR_MEMORY_FILE_PAGE_H
std::vector< int > ChunkKey
Definition: types.h:37
~MultiPage()
Destructor – purges all pages.
Definition: Page.h:87
A logical page (Page) belongs to a file on disk.
Definition: Page.h:46
void pop()
Purges the oldest Page.
Definition: Page.h:110
#define LOG(tag)
Definition: Logger.h:188
Stores Pair of ChunkKey and Page id and version, in a pair with a Page struct itself (File id and Pag...
Definition: Page.h:143
bool isValid()
Definition: Page.h:54
#define CHECK_GT(x, y)
Definition: Logger.h:209
HeaderInfo(const ChunkKey &chunkKey, const int32_t pageId, const int32_t versionEpoch, const Page &page)
Definition: Page.h:149
std::deque< EpochedPage > pageVersions
Definition: Page.h:81
int32_t fileId
Definition: Page.h:47
size_t pageNum
unique identifier of the owning file
Definition: Page.h:48
bool operator<(const HeaderInfo &other)
Definition: Page.h:155
MultiPage(size_t pageSizeIn)
Constructor.
Definition: Page.h:84
#define CHECK_LE(x, y)
Definition: Logger.h:208
bool operator<(const Page other) const
Definition: Page.h:56
void push(const Page &page, const int epoch)
Pushes a new page with epoch value.
Definition: Page.h:102
std::vector< EpochedPage > freePagesBeforeEpoch(const int32_t target_epoch, const int32_t current_epoch)
Definition: Page.h:117
Page(int32_t fileId, size_t pageNum)
page number
Definition: Page.h:51
EpochedPage current() const
Returns a reference to the most recent version of the page.
Definition: Page.h:94
The MultiPage stores versions of the same logical page in a deque.
Definition: Page.h:79