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
PDBPage.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 #ifndef PDBPAGE_H
20 #define PDBPAGE_H
21 
22 #include "PDBDebug.h"
23 #include "PDBObject.h"
24 #include "DataTypes.h"
25 #include "PDBLogger.h"
26 #include <memory>
27 #include <pthread.h>
28 #include <stdlib.h>
29 #include <iostream>
30 using namespace std;
31 // create a smart pointer for PDBBufferPagePtr objects
32 class PDBPage;
33 typedef shared_ptr<PDBPage> PDBPagePtr;
34 
52 class PDBPage {
53 
54 public:
58  PDBPage(char* dataIn,
59  NodeID dataNodeID,
60  DatabaseID dataDbID,
61  UserTypeID dataTypeID,
62  SetID setID,
63  PageID pageID,
64  size_t dataSize,
65  size_t offset,
66  int internalOffset = 0,
67  int numObjects = 0);
68 
72  PDBPage(char* dataIn, size_t offset, int internalOffset = 0);
73 
74  ~PDBPage();
78  void freePage();
79 
83  void preparePage();
84 
85 
86  inline void incRefCount() {
87  pthread_mutex_lock(&this->refCountMutex);
88  this->refCount++;
89  this->pinned = true;
90  pthread_mutex_unlock(&this->refCountMutex);
91  }
92 
93  inline void decRefCount() {
94  pthread_mutex_lock(&this->refCountMutex);
95  this->refCount--;
96  if (this->refCount < 0) {
97  // there is a problem:
98  // reference count should always >= 0
99  this->setPinned(false);
100  this->refCount = 0;
101  } else if (this->refCount == 0) {
102  this->setPinned(false);
103  }
104  pthread_mutex_unlock(&this->refCountMutex);
105  }
106 
107  inline void freeContent() {
108  pthread_mutex_lock(&this->refCountMutex);
109  if (this->rawBytes != nullptr) {
110  free(this->rawBytes);
111  this->rawBytes = nullptr;
112  }
113  pthread_mutex_unlock(&this->refCountMutex);
114  }
115 
116  // this is to increment the reference count embeded in page bytes
117  inline int incEmbeddedNumObjects() {
118  pthread_mutex_lock(&this->refCountMutex);
119  char* refCountBytes =
120  this->rawBytes + (sizeof(NodeID) + sizeof(DatabaseID) + sizeof(UserTypeID) +
121  sizeof(SetID) + sizeof(PageID));
122  *((int*)refCountBytes) = *((int*)refCountBytes) + 1;
123  pthread_mutex_unlock(&this->refCountMutex);
124  return *((int*)refCountBytes);
125  }
126 
127  inline int getEmbeddedNumObjects() {
128  char* refCountBytes =
129  this->rawBytes + (sizeof(NodeID) + sizeof(DatabaseID) + sizeof(UserTypeID) +
130  sizeof(SetID) + sizeof(PageID));
131  return *((int*)refCountBytes);
132  }
133 
134  inline void setNumObjects(int numObjects) {
135  this->numObjects = numObjects;
136  }
137 
138  inline int getNumObjects() {
139  return this->numObjects;
140  }
141 
146  inline void* addVariableBytes(size_t size) {
147  size_t remainSize = this->size - this->curAppendOffset;
148  if (remainSize < size + sizeof(size_t)) {
149  // no room in the current page
150  return nullptr;
151  }
152 
153  // write data
154  // get pointer to append position
155  char* cur = this->rawBytes + this->curAppendOffset;
156  // write the size
157  *((size_t*)cur) = size;
158  void* retPos = cur + sizeof(size_t);
159  cur = (char*)retPos + size;
160  this->curAppendOffset = cur - this->rawBytes;
161  this->incEmbeddedNumObjects();
162  int myNumObjects = this->getEmbeddedNumObjects();
163  return retPos;
164  }
165 
166 
167  /*****To Comply with Chris' interfaces******/
168 
169  void* getBytes();
170 
171  size_t getSize();
172 
173  void unpin();
174 
175  /********************Mutexes*****************/
176 
177  // To lock page for read.
178  void readLock();
179 
180  // To release the read lock.
181  void readUnlock();
182 
183  // To lock page for write.
184  void writeLock();
185 
186  // To release the write lock.
187  void writeUnlock();
188 
189 
190  /************Simple getters/setters**********/
191 
192  // To return NodeID
193  NodeID getNodeID() const {
194  return nodeID;
195  }
196 
197  // To return DatabaseID.
198  DatabaseID getDbID() const {
199  return dbID;
200  }
201 
202  // To return UserTypeID.
204  return typeID;
205  }
206 
207  // To return SetID.
208  SetID getSetID() const {
209  return setID;
210  }
211 
212  // To return PageID
213  PageID getPageID() const {
214  return pageID;
215  }
216 
217  // To return raw data.
218  char* getRawBytes() const {
219  return rawBytes;
220  }
221 
222  // To return size of raw data.
223  size_t getRawSize() const {
224  return size;
225  }
226 
227  // To return offset in shared memory.
228  size_t getOffset() const {
229  return offset;
230  }
231 
232  // To return number of miniPages used to write page header.
233  int getNumHeadMiniPages() const {
234  return numHeadMiniPages;
235  }
236 
237  // To return number of miniPages that has been written to the page.
238  int getNumMiniPages() const {
239  return numMiniPages;
240  }
241 
242 
243  // To return the miniPage size.
244  size_t getMiniPageSize() const {
245  return miniPageSize;
246  }
247 
248  // To return page header size
249  //(net size, not the size that is aligned with miniPage).
250  size_t getHeadSize() const {
251  return headSize;
252  }
253 
254  // To return last accessed sequence Id of this page.
255  long getAccessSequenceId() const {
256  return accessSequenceId;
257  }
258 
259  // To return the reference count of this page.
260  int getRefCount() {
261  return this->refCount;
262  }
263 
264  // To reset the reference count of this page.
265  void resetRefCount() {
266  this->refCount = 0;
267  }
268 
269 
270  // Return whether page is pinned.
271  // Page is pinned if reference count > 0.
272  // Once page is unpinned, we can flush the page to disk, or evict the page from cache.
273  bool isPinned() {
274  return this->pinned;
275  }
276 
277  // Return whether page is dirty (i.e. hasn't been flushed to disk yet).
278  bool isDirty() {
279  return this->dirty;
280  }
281 
282 
283  // Return whether page is in flush
284  bool isInFlush() {
285  return this->inFlush;
286  }
287 
288  // Return whether page is in eviction
289  bool isInEviction() {
290  return this->inEviction;
291  }
292 
293  unsigned int getPageSeqInPartition() const {
294  return pageSeqInPartition;
295  }
296 
298  return partitionId;
299  }
300 
301  // To set NodeID.
302  void setNodeID(NodeID nodeID) {
303  this->nodeID = nodeID;
304  }
305 
306  // To set DatabaseID.
307  void setDbID(DatabaseID dbID) {
308  this->dbID = dbID;
309  }
310 
311  // To set TypeID.
312  void setTypeID(UserTypeID typeID) {
313  this->typeID = typeID;
314  }
315 
316  // To set SetID.
317  void setSetID(SetID setID) {
318  this->setID = setID;
319  }
320 
321  // To set PageID.
322  void setPageID(PageID pageID) {
323  this->pageID = pageID;
324  }
325 
326  // To set raw data.
327  void setRawBytes(char* rawBytes) {
328  this->rawBytes = rawBytes;
329  }
330 
331  // To set raw data size.
332  void setSize(size_t size) {
333  this->size = size;
334  }
335 
336  // To set offset of raw data in shared memory.
337  void setOffset(size_t offset) {
338  this->offset = offset;
339  }
340 
341 
342  // To set number of miniPages to store page header.
343  void setNumHeadMiniPages(int numHeadMiniPages) {
344  this->numHeadMiniPages = numHeadMiniPages;
345  }
346 
347  // To set number of miniPages that has been written to the page.
348  void setNumMiniPages(int numMiniPages) {
349  this->numMiniPages = numMiniPages;
350  }
351 
352 
353  // To set miniPage size.
354  void setMiniPageSize(size_t miniPageSize) {
355  this->miniPageSize = miniPageSize;
356  }
357 
358  // To set the page header size (net size, not the size that is aligned with miniPage)
359  void setHeadSize(size_t headSize) {
360  this->headSize = headSize;
361  }
362 
363  // To set last accessed sequence Id for the page.
364  void setAccessSequenceId(long accessSequenceId) {
365  this->accessSequenceId = accessSequenceId;
366  }
367 
368  // To set whether the page is pinned or not.
369  void setPinned(bool isPinned) {
370  this->pinned = isPinned;
371  }
372 
373  // To set whether the page is dirty or not.
374  void setDirty(bool dirty) {
375  this->dirty = dirty;
376  }
377 
378  // To set whether the page is in flush or not.
379  void setInFlush(bool inFlush) {
380  this->inFlush = inFlush;
381  }
382 
383  // To set whether the page is in eviction or not
384  void setInEviction(bool inEviction) {
385  this->inEviction = inEviction;
386  }
387 
388  void setPageSeqInPartition(unsigned int pageSeqInPartition) {
389  this->pageSeqInPartition = pageSeqInPartition;
390  }
391 
392  void setPartitionId(FilePartitionID partitionId) {
393  this->partitionId = partitionId;
394  }
395 
396  void setInternalOffset(int offset) {
397  this->internalOffset = offset;
398  }
399 
401  return this->internalOffset;
402  }
403 
404 private:
405  char* rawBytes;
406  size_t offset; // used in shared memory
409  int numObjects = 0;
410  size_t miniPageSize;
411  size_t headSize;
417  size_t size;
418  int refCount;
419  bool pinned;
420  bool dirty;
421  pthread_mutex_t refCountMutex;
422  pthread_rwlock_t flushLock;
424  bool inFlush;
426 
427  // Below info can only be filled when the page has been loaded to cache after flushed to a disk
428  // file.
429  // Otherwise, they will be unsigned int(-1).
430  // When backend wants to pin a page, it will use pageId to check whether the page is already in
431  // cache.
432  // If the page is not in cache, then it will be loaded to cache using partitionId and
433  // pageSeqInPartition.
435  unsigned int pageSeqInPartition;
436 
437  // Offset from last append position to start of raw bytes
440 };
441 
442 
443 #endif /* PDBPAGE_H */
void setNumHeadMiniPages(int numHeadMiniPages)
Definition: PDBPage.h:343
bool dirty
Definition: PDBPage.h:420
void setNodeID(NodeID nodeID)
Definition: PDBPage.h:302
DatabaseID getDbID() const
Definition: PDBPage.h:198
unsigned int SetID
Definition: DataTypes.h:31
shared_ptr< PDBPage > PDBPagePtr
Definition: PDBPage.h:32
void resetRefCount()
Definition: PDBPage.h:265
int getNumMiniPages() const
Definition: PDBPage.h:238
size_t headSize
Definition: PDBPage.h:411
void setPinned(bool isPinned)
Definition: PDBPage.h:369
int numHeadMiniPages
Definition: PDBPage.h:407
void setInEviction(bool inEviction)
Definition: PDBPage.h:384
int internalOffset
Definition: PDBPage.h:439
bool inFlush
Definition: PDBPage.h:424
int numMiniPages
Definition: PDBPage.h:408
size_t miniPageSize
Definition: PDBPage.h:410
int refCount
Definition: PDBPage.h:418
void setPageSeqInPartition(unsigned int pageSeqInPartition)
Definition: PDBPage.h:388
int getRefCount()
Definition: PDBPage.h:260
int getInternalOffset()
Definition: PDBPage.h:400
SetID getSetID() const
Definition: PDBPage.h:208
void setAccessSequenceId(long accessSequenceId)
Definition: PDBPage.h:364
NodeID nodeID
Definition: PDBPage.h:412
unsigned int NodeID
Definition: DataTypes.h:27
int getNumHeadMiniPages() const
Definition: PDBPage.h:233
bool isDirty()
Definition: PDBPage.h:278
size_t getRawSize() const
Definition: PDBPage.h:223
void setInternalOffset(int offset)
Definition: PDBPage.h:396
void setHeadSize(size_t headSize)
Definition: PDBPage.h:359
void setSetID(SetID setID)
Definition: PDBPage.h:317
pthread_rwlock_t flushLock
Definition: PDBPage.h:422
void setPageID(PageID pageID)
Definition: PDBPage.h:322
void setInFlush(bool inFlush)
Definition: PDBPage.h:379
bool isInFlush()
Definition: PDBPage.h:284
bool isPinned()
Definition: PDBPage.h:273
size_t getHeadSize() const
Definition: PDBPage.h:250
pthread_mutex_t refCountMutex
Definition: PDBPage.h:421
PageID getPageID() const
Definition: PDBPage.h:213
PageID pageID
Definition: PDBPage.h:416
unsigned int DatabaseID
Definition: DataTypes.h:29
void freeContent()
Definition: PDBPage.h:107
unsigned int PageID
Definition: DataTypes.h:26
FilePartitionID getPartitionId() const
Definition: PDBPage.h:297
void decRefCount()
Definition: PDBPage.h:93
bool isInEviction()
Definition: PDBPage.h:289
void setNumObjects(int numObjects)
Definition: PDBPage.h:134
void setTypeID(UserTypeID typeID)
Definition: PDBPage.h:312
size_t size
Definition: PDBPage.h:417
unsigned int pageSeqInPartition
Definition: PDBPage.h:435
UserTypeID getTypeID() const
Definition: PDBPage.h:203
char * rawBytes
Definition: PDBPage.h:405
size_t offset
Definition: PDBPage.h:406
size_t getMiniPageSize() const
Definition: PDBPage.h:244
DatabaseID dbID
Definition: PDBPage.h:413
void incRefCount()
Definition: PDBPage.h:86
void setNumMiniPages(int numMiniPages)
Definition: PDBPage.h:348
bool inEviction
Definition: PDBPage.h:425
int getNumObjects()
Definition: PDBPage.h:138
void setDirty(bool dirty)
Definition: PDBPage.h:374
bool pinned
Definition: PDBPage.h:419
void setMiniPageSize(size_t miniPageSize)
Definition: PDBPage.h:354
void setDbID(DatabaseID dbID)
Definition: PDBPage.h:307
long accessSequenceId
Definition: PDBPage.h:423
void setOffset(size_t offset)
Definition: PDBPage.h:337
void setPartitionId(FilePartitionID partitionId)
Definition: PDBPage.h:392
unsigned int getPageSeqInPartition() const
Definition: PDBPage.h:293
FilePartitionID partitionId
Definition: PDBPage.h:434
int incEmbeddedNumObjects()
Definition: PDBPage.h:117
NodeID getNodeID() const
Definition: PDBPage.h:193
SetID setID
Definition: PDBPage.h:415
size_t getOffset() const
Definition: PDBPage.h:228
void setRawBytes(char *rawBytes)
Definition: PDBPage.h:327
unsigned int FilePartitionID
Definition: DataTypes.h:32
char * getRawBytes() const
Definition: PDBPage.h:218
void setSize(size_t size)
Definition: PDBPage.h:332
UserTypeID typeID
Definition: PDBPage.h:414
unsigned int UserTypeID
Definition: DataTypes.h:25
long getAccessSequenceId() const
Definition: PDBPage.h:255
void * addVariableBytes(size_t size)
Definition: PDBPage.h:146
size_t curAppendOffset
Definition: PDBPage.h:438
int getEmbeddedNumObjects()
Definition: PDBPage.h:127