A platform for high-performance distributed tool and library development written in C++. It can be deployed in two different cluster modes: standalone or distributed. API for v0.5.0, released on June 13, 2018.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
PartitionedFileMetaData.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * *
3  * Copyright 2018 Rice University *
4  * *
5  * Licensed under the Apache License, Version 2.0 (the "License"); *
6  * you may not use this file except in compliance with the License. *
7  * You may obtain a copy of the License at *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14  * See the License for the specific language governing permissions and *
15  * limitations under the License. *
16  * *
17  *****************************************************************************/
18 /*
19  * PartitionedFileMetaData.h
20  *
21  * Created on: Dec 20, 2015
22  * Author: Jia
23  */
24 
25 #ifndef SRC_CPP_MAIN_DATABASE_HEADERS_PARTITIONEDFILEMETADATA_H_
26 #define SRC_CPP_MAIN_DATABASE_HEADERS_PARTITIONEDFILEMETADATA_H_
27 
28 #include "DataTypes.h"
29 #include <string>
30 #include <vector>
31 #include <unordered_map>
32 #include <memory>
33 #include <iostream>
34 #include <pthread.h>
35 using namespace std;
36 
38 typedef shared_ptr<PartitionedFileMetaData> PartitionedFileMetaDataPtr;
39 
41 typedef shared_ptr<PartitionMetaData> PartitionMetaDataPtr;
42 
43 struct PageIndexHash {
44  std::size_t operator()(const PageIndex& index) const {
45  return (index.partitionId << 16) + index.pageSeqInPartition;
46  }
47 };
48 
50  bool operator()(const PageIndex& lIndex, const PageIndex& rIndex) const {
51  if (lIndex.partitionId != rIndex.partitionId) {
52  return false;
53  } else {
54  if (lIndex.pageSeqInPartition != rIndex.pageSeqInPartition) {
55  return false;
56  } else {
57  return true;
58  }
59  }
60  }
61 };
62 
67 public:
68  // Create an empty PartitionedFileMetaData instance.
70  this->version = 0;
71  this->partitions = nullptr;
72  this->pageSize = 0;
73  this->numFlushedPages = 0;
74  this->pageIndexes = new unordered_map<PageID, PageIndex>();
75  this->pageIds = new unordered_map<PageIndex, PageID, PageIndexHash, PageIndexEqual>();
76  pthread_mutex_init(&(this->metaMutex), nullptr);
77  pthread_mutex_init(&(this->indexMutex), nullptr);
78  }
79 
80  // Destructor, it will NOT delete the meta partition or any other file partitions.
82  if (partitions != nullptr) {
83  partitions->clear();
84  delete partitions;
85  }
86  if (pageIndexes != nullptr) {
87  pageIndexes->clear();
88  delete pageIndexes;
89  }
90  if (pageIds != nullptr) {
91  pageIds->clear();
92  delete pageIds;
93  }
94  }
95 
96  // Return total number of flushed pages in all data partitions of this PartitionedFile instance
97  unsigned int getNumFlushedPages() const {
98  return this->numFlushedPages;
99  }
100 
101  // Set total number of flushed pages in all data partitions of this PartitionedFile instance
102  void setNumFlushedPages(unsigned int numFlushedPages) {
103  this->numFlushedPages = numFlushedPages;
104  }
105 
106  void addPageIndex(PageID pageId, FilePartitionID partitionId, unsigned int pageSeqInPartition) {
107  pthread_mutex_lock(&indexMutex);
108  PageIndex pageIndex;
109  pageIndex.partitionId = partitionId;
110  pageIndex.pageSeqInPartition = pageSeqInPartition;
111  this->pageIndexes->insert(pair<PageID, PageIndex>(pageId, pageIndex));
112  this->pageIds->insert(pair<PageIndex, PageID>(pageIndex, pageId));
113  pthread_mutex_unlock(&indexMutex);
114  }
115 
117 
118  PageIndex pageIndex;
119  if (this->pageIndexes->find(pageId) == this->pageIndexes->end()) {
120  pageIndex.partitionId = (unsigned int)(-1);
121  pageIndex.pageSeqInPartition = (unsigned int)(-1);
122  } else {
123  pageIndex = this->pageIndexes->at(pageId);
124  }
125  return pageIndex;
126  }
127 
128  PageID getPageId(FilePartitionID partitionId, unsigned int pageSeqInPartition) {
129  PageIndex pageIndex;
130  pageIndex.partitionId = partitionId;
131  pageIndex.pageSeqInPartition = pageSeqInPartition;
132  PageID pageId;
133  if (this->pageIds->find(pageIndex) == this->pageIds->end()) {
134  pageId = (unsigned int)(-1);
135  } else {
136  pageId = this->pageIds->at(pageIndex);
137  }
138  return pageId;
139  }
140 
141  // Increment total number of flushed pages in all data partitions of this PartitionedFile
142  // instance
144  pthread_mutex_lock(&metaMutex);
145  this->numFlushedPages++;
146  pthread_mutex_unlock(&metaMutex);
147  }
148 
149  // Return the page size
150  size_t getPageSize() const {
151  return pageSize;
152  }
153 
154  // Return the latest pageId that has been allocated for this set
156  return latestPageId;
157  }
158 
159  // Set the page size
160  void setPageSize(size_t pageSize) {
161  this->pageSize = pageSize;
162  }
163 
164  // Return all partitions
165  vector<PartitionMetaDataPtr>* getPartitions() const {
166  return partitions;
167  }
168 
169  // Return partition specified by Id
171  return partitions->at(partitionId);
172  }
173 
174  // Set all partitions
175  void setPartitions(vector<PartitionMetaDataPtr>* partitions = nullptr) {
176  this->partitions = partitions;
177  }
178 
179  // Add a new partition
181  if (this->partitions == nullptr) {
182  this->partitions = new vector<PartitionMetaDataPtr>();
183  }
184  partitions->push_back(partition);
185  }
186 
187  // Return current metadata version
188  unsigned short getVersion() const {
189  return version;
190  }
191 
192  // Set current metadata version
193  void setVersion(unsigned short version) {
194  this->version = version;
195  }
196 
197 
198  // Set latest pageId
199  void setLatestPageId(PageID pageId) {
200  this->latestPageId = pageId;
201  }
202 
203  unordered_map<PageID, PageIndex>* getPageIndexes() {
204  return pageIndexes;
205  }
206 
207 private:
208  // Metadata version
209  unsigned short version;
210  // A vector of partition meta data
211  vector<PartitionMetaDataPtr>* partitions = nullptr;
212  // Page size
213  size_t pageSize;
214  // Number of flushed pages
215  unsigned int numFlushedPages;
216  // Last PageID (The largest pageId that has been allocated for this set)
218  // a map of PageID to PageIndex
219  unordered_map<PageID, PageIndex>* pageIndexes = nullptr;
220  unordered_map<PageIndex, PageID, PageIndexHash, PageIndexEqual>* pageIds = nullptr;
221  pthread_mutex_t metaMutex;
222  pthread_mutex_t indexMutex;
223 };
224 
229 public:
230  // Create an empty PartitionMetaData instance
232 
233  // Create a new PartitionMetaData instance
234  PartitionMetaData(string path, FilePartitionID partitionId) {
235  this->numPages = 0;
236  this->path = path;
237  this->partitionId = partitionId;
238  }
239 
241 
245  unsigned int getNumPages() const {
246  return numPages;
247  }
248 
252  void setNumPages(unsigned int numPages) {
253  this->numPages = numPages;
254  }
255 
259  void incNumPages() {
260  this->numPages++;
261  }
262 
267  return partitionId;
268  }
269 
273  void setPartitionId(FilePartitionID partitionId) {
274  this->partitionId = partitionId;
275  }
276 
280  string getPath() const {
281  return path;
282  }
283 
287  void setPath(string path) {
288  this->path = path;
289  }
290 
291 private:
296 
300  unsigned int numPages;
301 
305  string path;
306 };
307 
308 #endif /* SRC_CPP_MAIN_DATABASE_HEADERS_PARTITIONEDFILEMETADATA_H_ */
void setPageSize(size_t pageSize)
PartitionMetaData(string path, FilePartitionID partitionId)
PageID getPageId(FilePartitionID partitionId, unsigned int pageSeqInPartition)
unsigned int pageSeqInPartition
Definition: DataTypes.h:99
void addPageIndex(PageID pageId, FilePartitionID partitionId, unsigned int pageSeqInPartition)
bool operator()(const PageIndex &lIndex, const PageIndex &rIndex) const
void setPath(string path)
PageIndex getPageIndex(PageID pageId)
unsigned int getNumFlushedPages() const
void addPartition(PartitionMetaDataPtr partition)
unsigned int PageID
Definition: DataTypes.h:26
FilePartitionID partitionId
Definition: DataTypes.h:98
void setPartitionId(FilePartitionID partitionId)
void setPartitions(vector< PartitionMetaDataPtr > *partitions=nullptr)
vector< PartitionMetaDataPtr > * getPartitions() const
shared_ptr< PartitionMetaData > PartitionMetaDataPtr
unsigned int getNumPages() const
void setLatestPageId(PageID pageId)
shared_ptr< PartitionedFileMetaData > PartitionedFileMetaDataPtr
void setNumPages(unsigned int numPages)
void setNumFlushedPages(unsigned int numFlushedPages)
void setVersion(unsigned short version)
FilePartitionID getPartitionId() const
unordered_map< PageID, PageIndex > * getPageIndexes()
std::size_t operator()(const PageIndex &index) const
unsigned short getVersion() const
unsigned int FilePartitionID
Definition: DataTypes.h:32
PartitionMetaDataPtr getPartition(FilePartitionID partitionId) const