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
UserSet.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 SRC_CPP_MAIN_DATABASE_HEADERS_USERSET_H_
20 #define SRC_CPP_MAIN_DATABASE_HEADERS_USERSET_H_
21 
22 #include "PDBDebug.h"
23 #include "PartitionedFile.h"
24 #include "PageCache.h"
25 #include "PDBLogger.h"
26 #include "DataTypes.h"
27 #include "PageIterator.h"
28 #include "PageCircularBuffer.h"
29 #include "SequenceID.h"
30 #include <set>
31 #include <vector>
32 #include <memory>
33 #include "LocalitySet.h"
34 using namespace std;
35 
36 class UserSet;
37 typedef shared_ptr<UserSet> SetPtr;
38 
55 class UserSet : public LocalitySet {
56 public:
61  SharedMemPtr shm,
62  NodeID nodeId,
63  DatabaseID dbId,
64  UserTypeID typeId,
65  SetID setId,
66  string setName,
67  PageCachePtr pageCache,
68  LocalityType localityType = JobData,
70  OperationType operation = Read,
71  DurabilityType durability = TryCache,
72  PersistenceType persistence = Persistent,
73  size_t pageSize = DEFAULT_PAGE_SIZE);
74 
78  UserSet(size_t pageSize,
79  pdb::PDBLoggerPtr logger,
80  SharedMemPtr shm,
81  NodeID nodeId,
82  DatabaseID dbId,
83  UserTypeID typeId,
84  SetID setId,
85  string setName,
86  PartitionedFilePtr file,
87  PageCachePtr pageCache,
88  LocalityType localityType = JobData,
90  OperationType operation = Read,
91  DurabilityType durability = TryCache,
92  PersistenceType persistence = Persistent);
93 
97  ~UserSet();
98 
102  bool addObject(PDBObjectPtr object, PDBPagePtr page);
103 
104 
108  bool addObject(PDBObjectPtr object);
109 
113  void pinBufferPage();
114 
115 
120  if (this->inputBufferPage != nullptr) {
121  this->inputBufferPage->decRefCount();
122  }
123  }
124 
125 
133  PDBPagePtr getPageFromFile(FilePartitionID partitionId, unsigned int pageSeqInPartition);
134 
135 
139  PDBPagePtr addPage();
140 
144  PDBPagePtr addPageByRawBytes(size_t sharedMemOffset);
145 
146 
151  inline void* getNewBytes(size_t size, bool evictWhenUnpin = false) {
152  if (size == 0) {
153  return nullptr;
154  }
155  pthread_mutex_lock(&this->addBytesMutex);
156  if (this->inputBufferPage == nullptr) {
157 
158  this->inputBufferPage = this->addPage();
159  }
160  void* buffer = this->inputBufferPage->addVariableBytes(size);
161  if (buffer == nullptr) {
162  // current inputBufferPage is full
163 
164  // we unpin the inputBufferPage
165  this->inputBufferPage->decRefCount();
166  /*if(this->getDurabilityType() == CacheThrough) {
167  CacheKey key;
168  key.dbId = this->getDbID();
169  key.typeId = this->getTypeID();
170  key.setId = this->getSetID();
171  key.pageId = this->inputBufferPage->getPageID();
172  this->pageCache->flushPageWithoutEviction(key);
173  }*/
174  if (evictWhenUnpin == true) {
175  this->pageCache->evictPage(this->inputBufferPage);
176  }
177 
178 
179  // we add a new page as inputBufferPage
180  this->inputBufferPage = this->addPage();
181  buffer = this->inputBufferPage->addVariableBytes(size);
182  pthread_mutex_unlock(&this->addBytesMutex);
183  // get new bytes in the new page
184  return buffer;
185  }
186  pthread_mutex_unlock(&this->addBytesMutex);
187  return buffer;
188  }
189 
190 
198  virtual vector<PageIteratorPtr>* getIterators();
199 
206  PDBPagePtr getPage(FilePartitionID partitionId, unsigned int pageSeqInPartition, PageID pageId);
207 
211  int getNumPages();
212 
213 
218  return this->dbId;
219  }
220 
221 
226  return this->typeId;
227  }
228 
229 
234  return this->setId;
235  }
236 
240  string getSetName() {
241  return this->setName;
242  }
243 
248  return this->file;
249  }
250 
255  this->file = file;
256  }
257 
262  return this->lastFlushedPageId;
263  }
264 
265 
270  this->lastFlushedPageId = pageId;
271  }
272 
277  lockDirtyPageSet();
278  auto search = dirtyPagesInPageCache->find(pageId);
279  if (search != dirtyPagesInPageCache->end()) {
280  cout << "UserSet: Error in addPageToDirtyPageSet, key exists.\n";
281  } else {
282  FileSearchKey key;
283  key.inCache = true;
284  key.partitionId = (unsigned int)(-1);
285  key.pageSeqInPartition = (unsigned int)(-1);
286  dirtyPagesInPageCache->insert({pageId, key});
287  }
288  unlockDirtyPageSet();
289  }
290 
298  FilePartitionID partitionId,
299  unsigned int pageSeqInPartition) {
300  if (isPinned == false) {
301  PDB_COUT << "the set is not pinned" << std::endl;
302  dirtyPagesInPageCache->erase(pageId);
303  return;
304  }
305  PDB_COUT << "the set is pinned" << std::endl;
306  auto search = dirtyPagesInPageCache->find(pageId);
307  if (search != dirtyPagesInPageCache->end()) {
308  search->second.inCache = false;
309  search->second.partitionId = partitionId;
310  search->second.pageSeqInPartition = pageSeqInPartition;
311  }
312  return;
313  }
314 
318  unordered_map<PageID, FileSearchKey>* getDirtyPageSet() {
319  return dirtyPagesInPageCache;
320  }
321 
323  pthread_mutex_lock(&this->dirtyPageSetMutex);
324  }
325 
327  pthread_mutex_unlock(&this->dirtyPageSetMutex);
328  }
329 
330 
336  void dump(char* buffer);
337 
338  void evictPages();
339 
340  bool getPinned() {
341  return this->isPinned;
342  }
343 
344  /*
345  * TODO: change the name because current name is easy to be confused with pin()/unpin() defined
346  * in LocalitySet class (which is the base class).
347  */
348  void setPinned(bool isPinned) {
349  lockDirtyPageSet();
350  this->isPinned = isPinned;
351  unlockDirtyPageSet();
352  }
353 
355  return this->logger;
356  }
357 
358  void cleanDirtyPageSet();
359 
360  // MUST be used after set->setPinned(true) is invoked
361  void flushDirtyPages();
362 
363  size_t getPageSize() {
364  return this->pageSize;
365  }
366 
367  void setPageSize(size_t pageSize) {
368  this->pageSize = pageSize;
369  }
370 
371  bool getSorted () {
372  return this->isSorted;
373  }
374 
375  void setSorted (bool isSorted) {
376  this->isSorted = isSorted;
377  }
378 
379  bool getPartitioned () {
380  return this->isPartitioned;
381  }
382 
383  void setPartitioned (bool isPartitioned) {
384  this->isPartitioned = isPartitioned;
385  }
386 
387 
388 protected:
389  PartitionedFilePtr file = nullptr;
390  PageCachePtr pageCache = nullptr;
392  pdb::PDBLoggerPtr logger = nullptr;
397  string setName;
400  PDBPagePtr inputBufferPage = nullptr;
402  unordered_map<PageID, FileSearchKey>* dirtyPagesInPageCache = nullptr;
403  pthread_mutex_t dirtyPageSetMutex;
404  bool isPinned = false;
405  int numPages;
406  pthread_mutex_t addBytesMutex;
407  size_t pageSize;
408  bool isSorted = false;
409  bool isPartitioned = false;
410 
411 
412 };
413 
414 
415 #endif /* SRC_CPP_MAIN_DATABASE_HEADERS_PDBSET_H_ */
unsigned int SetID
Definition: DataTypes.h:31
shared_ptr< PDBPage > PDBPagePtr
Definition: PDBPage.h:32
int numPages
Definition: UserSet.h:405
OperationType
Definition: DataTypes.h:57
void unpinBufferPage()
Definition: UserSet.h:119
shared_ptr< PageCache > PageCachePtr
Definition: PageCache.h:39
DatabaseID dbId
Definition: UserSet.h:394
void setLastFlushedPageId(PageID pageId)
Definition: UserSet.h:269
void setFile(PartitionedFilePtr file)
Definition: UserSet.h:254
SharedMemPtr shm
Definition: UserSet.h:391
bool inCache
Definition: DataTypes.h:92
bool getPinned()
Definition: UserSet.h:340
unsigned int NodeID
Definition: DataTypes.h:27
unsigned int pageSeqInPartition
Definition: DataTypes.h:94
bool getSorted()
Definition: UserSet.h:371
LocalityType
Definition: DataTypes.h:50
PageID lastFlushedPageId
Definition: UserSet.h:398
PageID getLastFlushedPageId()
Definition: UserSet.h:261
UserTypeID getTypeID()
Definition: UserSet.h:225
NodeID nodeId
Definition: UserSet.h:393
SetID getSetID()
Definition: UserSet.h:233
DurabilityType
Definition: DataTypes.h:59
LocalitySetReplacementPolicy
Definition: DataTypes.h:52
bool getPartitioned()
Definition: UserSet.h:379
shared_ptr< PartitionedFile > PartitionedFilePtr
shared_ptr< SharedMem > SharedMemPtr
Definition: SharedMem.h:32
Definition: DataTypes.h:52
unsigned int DatabaseID
Definition: DataTypes.h:29
unsigned int PageID
Definition: DataTypes.h:26
FilePartitionID partitionId
Definition: DataTypes.h:93
SequenceID seqId
Definition: UserSet.h:401
size_t pageSize
Definition: UserSet.h:407
void * getNewBytes(size_t size, bool evictWhenUnpin=false)
Definition: UserSet.h:151
pthread_mutex_t addBytesMutex
Definition: UserSet.h:406
string setName
Definition: UserSet.h:397
string getSetName()
Definition: UserSet.h:240
void addPageToDirtyPageSet(PageID pageId)
Definition: UserSet.h:276
DatabaseID getDbID()
Definition: UserSet.h:217
PersistenceType
Definition: DataTypes.h:68
#define PDB_COUT
Definition: PDBDebug.h:31
PageID latestPageId
Definition: UserSet.h:399
void lockDirtyPageSet()
Definition: UserSet.h:322
UserTypeID typeId
Definition: UserSet.h:395
void setPinned(bool isPinned)
Definition: UserSet.h:348
void setSorted(bool isSorted)
Definition: UserSet.h:375
size_t getPageSize()
Definition: UserSet.h:363
pthread_mutex_t dirtyPageSetMutex
Definition: UserSet.h:403
void setPageSize(size_t pageSize)
Definition: UserSet.h:367
std::shared_ptr< PDBLogger > PDBLoggerPtr
Definition: PDBLogger.h:40
void unlockDirtyPageSet()
Definition: UserSet.h:326
shared_ptr< UserSet > SetPtr
Definition: UserSet.h:36
#define DEFAULT_PAGE_SIZE
Definition: Configuration.h:36
pdb::PDBLoggerPtr getLogger()
Definition: UserSet.h:354
SetID setId
Definition: UserSet.h:396
void removePageFromDirtyPageSet(PageID pageId, FilePartitionID partitionId, unsigned int pageSeqInPartition)
Definition: UserSet.h:297
shared_ptr< PDBObject > PDBObjectPtr
Definition: PDBObject.h:27
Definition: DataTypes.h:57
PartitionedFilePtr getFile()
Definition: UserSet.h:247
unordered_map< PageID, FileSearchKey > * getDirtyPageSet()
Definition: UserSet.h:318
unsigned int FilePartitionID
Definition: DataTypes.h:32
unsigned int UserTypeID
Definition: DataTypes.h:25
void setPartitioned(bool isPartitioned)
Definition: UserSet.h:383