OmniSciDB  bf83d84833
 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 
67 struct EpochedPage {
69  int32_t epoch;
70 };
71 
72 struct MultiPage {
73  size_t pageSize;
74  std::deque<EpochedPage> pageVersions;
75 
77  MultiPage(size_t pageSizeIn) : pageSize(pageSizeIn) {}
78 
81  while (pageVersions.size() > 0) {
82  pop();
83  }
84  }
85 
87  inline EpochedPage current() const {
88  if (pageVersions.size() < 1) {
89  LOG(FATAL) << "No current version of the page exists in this MultiPage.";
90  }
91  return pageVersions.back();
92  }
93 
95  inline void push(const Page& page, const int epoch) {
96  pageVersions.push_back({page, epoch});
97  }
98 
100  inline void pop() {
101  if (pageVersions.size() < 1) {
102  LOG(FATAL) << "No page to pop.";
103  }
104  pageVersions.pop_front();
105  }
106 
107  std::vector<EpochedPage> freePagesBeforeEpoch(const int32_t target_epoch,
108  const int32_t current_epoch) {
109  std::vector<EpochedPage> pagesBeforeEpoch;
110  int32_t next_page_epoch = current_epoch + 1;
111  for (auto pageIt = pageVersions.rbegin(); pageIt != pageVersions.rend(); ++pageIt) {
112  const int32_t epoch_ceiling = next_page_epoch - 1;
113  CHECK_LE(pageIt->epoch, epoch_ceiling);
114  if (epoch_ceiling < target_epoch) {
115  pagesBeforeEpoch.emplace_back(*pageIt);
116  }
117  next_page_epoch = pageIt->epoch;
118  }
119  if (!pagesBeforeEpoch.empty()) {
120  pageVersions.erase(pageVersions.begin(),
121  pageVersions.begin() + pagesBeforeEpoch.size());
122  }
123  return pagesBeforeEpoch;
124  }
125 };
126 
133 struct HeaderInfo {
134  ChunkKey chunkKey; // a vector of ints
135  int32_t pageId;
136  int32_t versionEpoch;
138 
140  const int32_t pageId,
141  const int32_t versionEpoch,
142  const Page& page)
143  : chunkKey(chunkKey), pageId(pageId), versionEpoch(versionEpoch), page(page) {}
144 };
145 
146 } // namespace File_Namespace
147 
148 #endif // DATAMGR_MEMORY_FILE_PAGE_H
std::vector< int > ChunkKey
Definition: types.h:37
~MultiPage()
Destructor – purges all pages.
Definition: Page.h:80
A logical page (Page) belongs to a file on disk.
Definition: Page.h:46
void pop()
Purges the oldest Page.
Definition: Page.h:100
#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:133
bool isValid()
Definition: Page.h:54
HeaderInfo(const ChunkKey &chunkKey, const int32_t pageId, const int32_t versionEpoch, const Page &page)
Definition: Page.h:139
std::deque< EpochedPage > pageVersions
Definition: Page.h:74
int32_t fileId
Definition: Page.h:47
size_t pageNum
unique identifier of the owning file
Definition: Page.h:48
MultiPage(size_t pageSizeIn)
Constructor.
Definition: Page.h:77
#define CHECK_LE(x, y)
Definition: Logger.h:208
void push(const Page &page, const int epoch)
Pushes a new page with epoch value.
Definition: Page.h:95
std::vector< EpochedPage > freePagesBeforeEpoch(const int32_t target_epoch, const int32_t current_epoch)
Definition: Page.h:107
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:87
The MultiPage stores versions of the same logical page in a deque.
Definition: Page.h:72