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
PartitionedHashSet.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 #ifndef PARTITIONED_HASH_SET
19 #define PARTITIONED_HASH_SET
20 
21 
22 #include "AbstractHashSet.h"
23 #include <pthread.h>
24 
25 namespace pdb {
26 
28 typedef std::shared_ptr<PartitionedHashSet> PartitionedHashSetPtr;
29 
30 
31 /*
32  * This class encapsulates a partitioned hash set, which is a collection of managed blocks, and each
33  * block contains a PDBMap.
34  * In the end, this class will be removed and replaced by Pangea hash set
35  */
36 
38 
39 private:
40  // the name of this partitioned hash set
41  std::string setName;
42 
43  // the set of partition pages
44  std::vector<void*> partitionPages;
45 
46  // the size of each partition page
47  size_t pageSize;
48 
49  // whether this partitioned hash set has been cleaned
50  bool isCleaned;
51 
52  // mutex
53  pthread_mutex_t myMutex;
54 
55 public:
56  // constructor
57  PartitionedHashSet(std::string myName, size_t pageSize) {
58  this->setName = myName;
59  this->pageSize = pageSize;
60  this->isCleaned = false;
61  pthread_mutex_init(&myMutex, nullptr);
62  }
63 
64  // destructor
66  if (isCleaned == false) {
67  this->cleanup();
68  }
69  pthread_mutex_destroy(&myMutex);
70  }
71 
72  // get type of this hash set
73  std::string getHashSetType() override {
74  return "PartitionedHashSet";
75  }
76 
77  // get name of this hash set
78  std::string getHashSetName() {
79  return setName;
80  }
81 
82  // get page size
83  size_t getPageSize() {
84  return pageSize;
85  }
86 
87  // get page for a particular partition
88  void* getPage(unsigned int partitionId) {
89  void* retPtr = nullptr;
90  pthread_mutex_lock(&myMutex);
91  if (partitionId < partitionPages.size()) {
92  retPtr = partitionPages[partitionId];
93  }
94  pthread_mutex_unlock(&myMutex);
95  return retPtr;
96  }
97 
98  // get number of pages
99  size_t getNumPages() {
100  size_t retNum;
101  pthread_mutex_lock(&myMutex);
102  retNum = partitionPages.size();
103  pthread_mutex_unlock(&myMutex);
104  return retNum;
105  }
106 
107  // add page
108  void* addPage() {
109  void* block = (void*)malloc(sizeof(char) * pageSize);
110  if (block != nullptr) {
111  pthread_mutex_lock(&myMutex);
112  partitionPages.push_back(block);
113  pthread_mutex_unlock(&myMutex);
114  }
115  return block;
116  }
117 
118  // clean up all pages
119  void cleanup() override {
120  if (isCleaned == false) {
121  for (int i = 0; i < partitionPages.size(); i++) {
122  free(partitionPages[i]);
123  }
124  isCleaned = true;
125 #ifdef PROFILING
126  std::cout << "partitioned hash set: " << this->setName << " is removed" << std::endl;
127 #endif
128  }
129  }
130 
131  // get size
132  size_t getSize() override {
133  return pageSize * partitionPages.size();
134  }
135 };
136 }
137 
138 
139 #endif
std::vector< void * > partitionPages
void * getPage(unsigned int partitionId)
std::shared_ptr< PartitionedHashSet > PartitionedHashSetPtr
PartitionedHashSet(std::string myName, size_t pageSize)
std::string getHashSetType() override