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.cc
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 PDB_Page_C
20 #define PDB_Page_C
21 #include "PDBPage.h"
22 #include <cstring>
23 #include <stdlib.h>
24 #include <iostream>
25 
26 // create an empty page
27 PDBPage::PDBPage(char* dataIn,
28  NodeID dataNodeID,
29  DatabaseID dataDbID,
30  UserTypeID dataTypeID,
31  SetID dataSetID,
32  PageID dataPageID,
33  size_t dataSize,
34  size_t shmOffset,
35  int internalOffset,
36  int numObjectsIn) {
37  rawBytes = dataIn;
38  nodeID = dataNodeID;
39  dbID = dataDbID;
40  typeID = dataTypeID;
41  setID = dataSetID;
42  pageID = dataPageID;
43  size = dataSize;
44  offset = shmOffset;
45  this->numObjects = numObjectsIn;
46  this->curAppendOffset = sizeof(NodeID) + sizeof(DatabaseID) + sizeof(UserTypeID) +
47  sizeof(SetID) + sizeof(PageID) + sizeof(int) + sizeof(size_t);
48  this->refCount = 0;
49  this->pinned = true;
50  this->dirty = false;
51  this->inFlush = false;
52  this->partitionId = (FilePartitionID)(-1);
53  this->pageSeqInPartition = (unsigned int)(-1);
54  pthread_mutex_init(&(this->refCountMutex), nullptr);
55  pthread_rwlock_init(&(this->flushLock), nullptr);
56  this->internalOffset = internalOffset;
57  char* refCountBytes = this->rawBytes +
58  (sizeof(NodeID) + sizeof(DatabaseID) + sizeof(UserTypeID) + sizeof(SetID) + sizeof(PageID));
59  *((int*)refCountBytes) = numObjectsIn;
60  char* pageSizeBytes = refCountBytes + sizeof(int);
61  *((size_t*)pageSizeBytes) = dataSize;
62 }
63 
64 
65 // create a PDBPage instance from a non-empty page.
66 PDBPage::PDBPage(char* dataIn, size_t offset, int internalOffset) {
67  this->rawBytes = dataIn;
68  this->offset = offset;
69  this->internalOffset = internalOffset;
70  this->curAppendOffset = sizeof(NodeID) + sizeof(DatabaseID) + sizeof(UserTypeID) +
71  sizeof(SetID) + sizeof(PageID) + sizeof(int) + sizeof(size_t);
72  char* cur = this->rawBytes;
73  this->nodeID = *((NodeID*)cur);
74  cur = cur + sizeof(NodeID);
75  this->dbID = *((DatabaseID*)cur);
76  cur = cur + sizeof(DatabaseID);
77  this->typeID = *((UserTypeID*)cur);
78  cur = cur + sizeof(UserTypeID);
79  this->setID = *((SetID*)cur);
80  cur = cur + sizeof(SetID);
81  this->pageID = *((PageID*)cur);
82  cur = cur + sizeof(PageID);
83  this->numObjects = *((int*)cur);
84  cur = cur + sizeof(int);
85  this->size = *((size_t*)cur);
86  this->refCount = 0;
87  this->pinned = true;
88  this->dirty = false;
89  this->inFlush = false;
90  this->partitionId = (FilePartitionID)(-1);
91  this->pageSeqInPartition = (unsigned int)(-1);
92  pthread_mutex_init(&(this->refCountMutex), nullptr);
93  pthread_rwlock_init(&(this->flushLock), nullptr);
94 }
95 
96 
98  freePage();
99  pthread_mutex_destroy(&(this->refCountMutex));
100  pthread_rwlock_destroy(&(this->flushLock));
101 }
102 
108  char* cur = this->rawBytes;
109  *((NodeID*)cur) = nodeID;
110  cur = cur + sizeof(NodeID);
111  *((DatabaseID*)cur) = dbID;
112  cur = cur + sizeof(DatabaseID);
113  *((UserTypeID*)cur) = typeID;
114  cur = cur + sizeof(UserTypeID);
115  *((SetID*)cur) = setID;
116  cur = cur + sizeof(SetID);
117  *((PageID*)cur) = pageID;
118  cur = cur + sizeof(PageID);
119  *((int*)cur) = 0;
120  cur = cur + sizeof(int);
121  *((size_t*)cur) = this->size;
122  this->curAppendOffset = sizeof(NodeID) + sizeof(DatabaseID) + sizeof(UserTypeID) +
123  sizeof(SetID) + sizeof(PageID) + sizeof(int) + sizeof(size_t);
124  return;
125 }
126 
127 
129  pthread_rwlock_rdlock(&(this->flushLock));
130 }
131 
133  pthread_rwlock_unlock(&(this->flushLock));
134 }
135 
137  pthread_rwlock_wrlock(&(this->flushLock));
138 }
139 
141  pthread_rwlock_unlock(&(this->flushLock));
142 }
143 
145 
146  return this->rawBytes + sizeof(NodeID) + sizeof(DatabaseID) + sizeof(UserTypeID) +
147  sizeof(SetID) + sizeof(PageID) + sizeof(int) + sizeof(size_t);
148 }
149 
151 
152  return this->size - (sizeof(NodeID) + sizeof(DatabaseID) + sizeof(UserTypeID) + sizeof(SetID) +
153  sizeof(PageID) + sizeof(int) + sizeof(size_t));
154 }
155 
157  this->decRefCount();
158 }
159 
160 
162  if (rawBytes != nullptr) {
163  // we always free page data by SharedMem class when page is flushed or evicted, and we do
164  // not free page data here
165  // If it comes to here, there must be a problem. Shared memory should already be freed,
166  // there could be memory leaks
167  rawBytes = nullptr;
168  }
169  nodeID = -1;
170  dbID = -1;
171  typeID = -1;
172  setID = -1;
173  pageID = -1;
174  offset = 0;
175  size = 0;
176  numObjects = 0;
177  internalOffset = 0;
178 }
179 
180 #endif
bool dirty
Definition: PDBPage.h:420
unsigned int SetID
Definition: DataTypes.h:31
void preparePage()
Definition: PDBPage.cc:107
~PDBPage()
Definition: PDBPage.cc:97
int internalOffset
Definition: PDBPage.h:439
bool inFlush
Definition: PDBPage.h:424
void readLock()
Definition: PDBPage.cc:128
int refCount
Definition: PDBPage.h:418
NodeID nodeID
Definition: PDBPage.h:412
unsigned int NodeID
Definition: DataTypes.h:27
void writeLock()
Definition: PDBPage.cc:136
pthread_rwlock_t flushLock
Definition: PDBPage.h:422
pthread_mutex_t refCountMutex
Definition: PDBPage.h:421
int numObjects
Definition: PDBPage.h:409
PageID pageID
Definition: PDBPage.h:416
unsigned int DatabaseID
Definition: DataTypes.h:29
unsigned int PageID
Definition: DataTypes.h:26
void decRefCount()
Definition: PDBPage.h:93
void unpin()
Definition: PDBPage.cc:156
void readUnlock()
Definition: PDBPage.cc:132
size_t size
Definition: PDBPage.h:417
unsigned int pageSeqInPartition
Definition: PDBPage.h:435
char * rawBytes
Definition: PDBPage.h:405
void writeUnlock()
Definition: PDBPage.cc:140
size_t offset
Definition: PDBPage.h:406
DatabaseID dbID
Definition: PDBPage.h:413
bool pinned
Definition: PDBPage.h:419
FilePartitionID partitionId
Definition: PDBPage.h:434
void * getBytes()
Definition: PDBPage.cc:144
size_t getSize()
Definition: PDBPage.cc:150
SetID setID
Definition: PDBPage.h:415
PDBPage(char *dataIn, NodeID dataNodeID, DatabaseID dataDbID, UserTypeID dataTypeID, SetID setID, PageID pageID, size_t dataSize, size_t offset, int internalOffset=0, int numObjects=0)
Definition: PDBPage.cc:27
unsigned int FilePartitionID
Definition: DataTypes.h:32
void freePage()
Definition: PDBPage.cc:161
UserTypeID typeID
Definition: PDBPage.h:414
unsigned int UserTypeID
Definition: DataTypes.h:25
size_t curAppendOffset
Definition: PDBPage.h:438