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
CatalogDatabaseMetadata.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  * CatalogDatabaseMetadata.h
20  *
21  */
22 
23 #ifndef CATALOG_DATABASE_METADATA_H_
24 #define CATALOG_DATABASE_METADATA_H_
25 
26 #include <iostream>
27 #include <map>
28 
29 #include "Handle.h"
30 #include "PDBDebug.h"
31 #include "PDBVector.h"
32 #include "PDBString.h"
33 #include "CatalogNodeMetadata.h"
35 #include "CatalogSetMetadata.h"
37 #include "Object.h"
38 
39 // PRELOAD %CatalogDatabaseMetadata%
40 
41 using namespace std;
42 
43 namespace pdb {
44 
45 // This class serves to store information about the databases in a given instance of PDB
46 // and provide methods for maintaining their associated metadata.
47 // Clients of this class will access this information using a handler to the catalog.
48 
50 public:
52 
54  pdb::String dbNameIn,
55  pdb::String userCreatorIn,
56  pdb::String createdOnIn,
57  pdb::String lastModifiedIn)
58  : dbId(dbIdIn),
59  dbName(dbNameIn),
60  userCreator(userCreatorIn),
61  createdOn(createdOnIn),
62  lastModified(lastModifiedIn) {}
63 
65  dbId = pdbDatabaseToCopy.dbId;
66  dbName = pdbDatabaseToCopy.dbName;
67  userCreator = pdbDatabaseToCopy.userCreator;
68  createdOn = pdbDatabaseToCopy.createdOn;
69  lastModified = pdbDatabaseToCopy.lastModified;
70  listOfNodes = pdbDatabaseToCopy.listOfNodes;
71  listOfSets = pdbDatabaseToCopy.listOfSets;
72  listOfTypes = pdbDatabaseToCopy.listOfTypes;
73  setsInDB = pdbDatabaseToCopy.setsInDB;
74  nodesInDB = pdbDatabaseToCopy.nodesInDB;
75  }
76 
78  dbId = pdbDatabaseToCopy->dbId;
79  dbName = pdbDatabaseToCopy->dbName;
80  userCreator = pdbDatabaseToCopy->userCreator;
81  createdOn = pdbDatabaseToCopy->createdOn;
82  lastModified = pdbDatabaseToCopy->lastModified;
83  listOfNodes = pdbDatabaseToCopy->listOfNodes;
84  listOfSets = pdbDatabaseToCopy->listOfSets;
85  listOfTypes = pdbDatabaseToCopy->listOfTypes;
86  setsInDB = pdbDatabaseToCopy->setsInDB;
87  nodesInDB = pdbDatabaseToCopy->nodesInDB;
88  }
89 
90 
91  void setValues(String dbIdIn,
92  pdb::String dbNameIn,
93  pdb::String userCreatorIn,
94  pdb::String createdOnIn,
95  pdb::String lastModifiedIn) {
96  dbId = dbIdIn;
97  dbName = dbNameIn;
98  userCreator = userCreatorIn;
99  createdOn = createdOnIn;
100  lastModified = lastModifiedIn;
101  }
102 
104 
106  listOfPermissions->push_back(permissionsIn);
107  }
108 
109  void addNode(pdb::String& nodeIn) {
110  PDB_COUT << "Adding node " << nodeIn.c_str() << endl;
111  listOfNodes->push_back(nodeIn);
112  }
113 
114  void addSet(pdb::String& setIn) {
115  PDB_COUT << "Adding node " << setIn.c_str() << endl;
116  listOfSets->push_back(setIn);
117  }
118 
119  void addSetToMap(String& setName, String& nodeIP) {
120  PDB_COUT << "key: " << setName.c_str() << " push_back node: " << nodeIP.c_str();
121  (*setsInDB)[setName].push_back(nodeIP);
122  }
123 
124  void addNodeToMap(String& nodeIP, String& setName) {
125  PDB_COUT << "key: " << nodeIP.c_str() << " push_back set: " << setName.c_str();
126  (*nodesInDB)[nodeIP].push_back(setName);
127  }
128 
129  void addType(pdb::String& typeIn) {
130  listOfTypes->push_back(typeIn);
131  }
132 
134  listOfSets = newList;
135  }
136 
138  listOfNodes = newList;
139  }
140 
142  setsInDB = newMap;
143  }
144 
146  nodesInDB = newMap;
147  }
148 
153  void deleteSet(String setName) {
154  deleteSetFromSetList(setName);
155  deleteSetFromSetMap(setName);
156  deleteSetFromNodeMap(setName);
157  }
158 
159  void removeNodeFromSet(String node, String set) {
160  deleteNodeFromSingleSet(node, set);
161  deleteSetFromSingleNode(set, node);
162  }
163 
164  void deleteNodeFromMap(String& nodeIP, String& setName) {
165  pdb::Handle<pdb::Vector<String>> tempListOfNodes = makeObject<Vector<String>>();
166 
167  for (int i = 0; i < (*getListOfNodes()).size(); i++) {
168  String itemValue = (*getListOfNodes())[i];
169  if (itemValue != setName) {
170  tempListOfNodes->push_back(itemValue);
171  }
172  }
173  replaceListOfNodes(tempListOfNodes);
174  }
175 
176  void deleteType(void* typeIn) {
177  (*listOfTypes).deleteObject(typeIn);
178  }
179 
181  return listOfNodes;
182  }
183 
185  return listOfSets;
186  }
187 
189  return listOfTypes;
190  }
191 
193  return listOfPermissions;
194  }
195 
197  return dbId;
198  }
199 
201  return dbName;
202  }
203 
205  return dbName;
206  }
207 
209  return userCreator;
210  }
211 
213  return createdOn;
214  }
215 
217  return lastModified;
218  }
219 
220  void setItemKey(String& itemKeyIn) {
221  dbName = itemKeyIn;
222  }
223 
224  void setItemId(String& idIn) {
225  dbId = idIn;
226  }
227 
228  void setItemName(String& itemNameIn) {
229  dbName = itemNameIn;
230  }
231 
233  return setsInDB;
234  }
235 
237  return nodesInDB;
238  }
239 
240  string printShort() {
241  string output;
242  string spaces("");
243  output = " \nDB ";
244  output.append(getItemId().c_str()).append(":").append(getItemKey().c_str());
245 
246  int i = 0;
247  output.append("\n is stored in (")
248  .append(to_string((*nodesInDB).size()))
249  .append(")nodes: [ ");
250  for (auto& item : (*nodesInDB)) {
251  if (i > 0)
252  output.append(", ").append(spaces).append(item.key.c_str());
253  else
254  output.append(item.key.c_str());
255  i++;
256  }
257 
258  output.append(" ]\n and has (").append(to_string((*setsInDB).size())).append(")sets: [ ");
259  i = 0;
260  for (auto& item : (*setsInDB)) {
261  if (i > 0)
262  output.append(", ").append(spaces).append(item.key.c_str());
263  else
264  output.append(item.key.c_str());
265  i++;
266  }
267  output.append(" ]");
268 
269  for (auto& item : (*setsInDB)) {
270  output.append("\n * Set: ")
271  .append(item.key.c_str())
272  .append(" is stored in (")
273  .append(to_string(item.value.size()))
274  .append(")nodes: [ ");
275  for (int i = 0; i < item.value.size(); i++) {
276  if (i > 0)
277  output.append(", ").append(spaces).append(item.value[i].c_str());
278  else
279  output.append(item.value[i].c_str());
280  }
281  output.append(" ]");
282  }
283 
284  return output;
285  }
286 
287  friend std::ostream& operator<<(std::ostream& out, CatalogDatabaseMetadata& database) {
288  out << "\nCatalog Database Metadata" << endl;
289  out << "-------------------" << endl;
290  out << " DB Id: " << database.getItemId().c_str() << endl;
291  out << " DB Key: " << database.getItemKey().c_str() << endl;
292  out << " DB Name: " << database.getItemName().c_str() << endl;
293  out << "\nThis Database is stored in the following nodes: " << endl;
294  for (int i = 0; i < database.getListOfNodes()->size(); i++) {
295  // out << " IP: " << database.getListOfNodes()->[i] << endl;
296  }
297  out << "\nThis Database has the following sets: " << endl;
298  for (int i = 0; i < database.getListOfSets()->size(); i++) {
299  out << " Set: " << (*database.getListOfSets())[i].c_str() << endl;
300  }
301 
302  out << "-------------------\n" << endl;
303  return out;
304  }
305 
307 
308 private:
314 
315  // a map where the key is the name of a set and the value is a vector with
316  // all nodes where that set has information stored
317  Handle<Map<String, Vector<String>>> setsInDB = makeObject<Map<String, Vector<String>>>();
318 
319  // a map where the key is the IP of a node and the value is a vector with
320  // all sets in that node that contain data for this database
321  Handle<Map<String, Vector<String>>> nodesInDB = makeObject<Map<String, Vector<String>>>();
322 
323  // Contains information about nodes in the cluster with data for a given database
324  pdb::Handle<pdb::Vector<String>> listOfNodes = makeObject<Vector<String>>();
325 
326  // Contains information about sets in the cluster containing data for a given database
327  pdb::Handle<pdb::Vector<String>> listOfSets = makeObject<Vector<String>>();
328 
329  // Contains information about types in the cluster containing data for a given database
330  pdb::Handle<pdb::Vector<String>> listOfTypes = makeObject<Vector<String>>();
331 
332  // Contains all users' permissions for a given database
334  makeObject<Vector<CatalogPermissionsMetadata>>();
335 
336  void deleteSetFromSetList(String& setName) {
337  pdb::Handle<pdb::Vector<String>> tempListOfSets = makeObject<Vector<String>>();
338  for (int i = 0; i < (*getListOfSets()).size(); i++) {
339  String itemValue = (*getListOfSets())[i];
340  if (itemValue != setName) {
341  tempListOfSets->push_back(itemValue);
342  }
343  }
344  replaceListOfSets(tempListOfSets);
345  }
346 
347  void deleteSetFromSetMap(String& setName) {
348  Handle<Map<String, Vector<String>>> tempSetsInDB =
349  makeObject<Map<String, Vector<String>>>();
350  for (auto& a : *getSetsInDB()) {
351  if (a.key != setName) {
352  (*tempSetsInDB)[a.key] = a.value;
353  }
354  }
355  replaceMapOfSets(tempSetsInDB);
356  }
357 
358  void deleteNodeFromSingleSet(String& node, String& setName) {
359  Handle<Map<String, Vector<String>>> tempSetsInDB =
360  makeObject<Map<String, Vector<String>>>();
361  for (auto& a : *getSetsInDB()) {
362  if (a.key != setName) {
363  (*tempSetsInDB)[a.key] = a.value;
364  } else {
365  auto nodes = a.value;
366  auto newNodes = (*tempSetsInDB)[a.key];
367  for (int i = 0; i < nodes.size(); i++) {
368  if (nodes[i] != node) {
369  newNodes.push_back(nodes[i]);
370  }
371  }
372  }
373  }
374  replaceMapOfSets(tempSetsInDB);
375  }
376 
377  void deleteSetFromSingleNode(String& setName, String& node) {
378  Handle<Map<String, Vector<String>>> tempNodesInDB =
379  makeObject<Map<String, Vector<String>>>();
380  for (auto& a : *getNodesInDB()) {
381  if (a.key != node) {
382  (*tempNodesInDB)[a.key] = a.value;
383  } else {
384  auto sets = a.value;
385  auto newSets = (*tempNodesInDB)[a.key];
386  for (int i = 0; i < sets.size(); i++) {
387  if (sets[i] != setName) {
388  newSets.push_back(sets[i]);
389  }
390  }
391  }
392  }
393  }
394 
395  void deleteSetFromNodeMap(String& setName) {
396  Handle<Map<String, Vector<String>>> tempNodesInDB =
397  makeObject<Map<String, Vector<String>>>();
398  for (const auto& setsInNode : (*nodesInDB)) {
399  auto node = setsInNode.key;
400  auto sets = setsInNode.value;
401  auto newSetsInNode = (*tempNodesInDB)[node];
402  for (int i = 0; i < sets.size(); i++) {
403  if (sets[i] != setName) {
404  newSetsInNode.push_back(sets[i]);
405  }
406  }
407  }
408  replaceMapOfNodes(tempNodesInDB);
409  }
410 };
411 
412 } /* namespace pdb */
413 
414 #endif /* CATALOG_DATABASE_METADATA_H_ */
#define ENABLE_DEEP_COPY
Definition: DeepCopy.h:52
void removeNodeFromSet(String node, String set)
pdb::Handle< pdb::Vector< pdb::CatalogPermissionsMetadata > > getListOfPermissions()
void setItemName(String &itemNameIn)
pdb::Handle< pdb::Vector< String > > listOfNodes
void replaceMapOfSets(Handle< Map< String, Vector< String >>> &newMap)
void replaceMapOfNodes(Handle< Map< String, Vector< String >>> &newMap)
void deleteSetFromSetList(String &setName)
pdb::Handle< pdb::Vector< String > > listOfTypes
void deleteSetFromSingleNode(String &setName, String &node)
void deleteSetFromSetMap(String &setName)
void addNodeToMap(String &nodeIP, String &setName)
CatalogDatabaseMetadata(pdb::String dbIdIn, pdb::String dbNameIn, pdb::String userCreatorIn, pdb::String createdOnIn, pdb::String lastModifiedIn)
CatalogDatabaseMetadata(const CatalogDatabaseMetadata &pdbDatabaseToCopy)
void addType(pdb::String &typeIn)
void addNode(pdb::String &nodeIn)
void addPermission(CatalogPermissionsMetadata &permissionsIn)
friend std::ostream & operator<<(std::ostream &out, CatalogDatabaseMetadata &database)
Handle< Map< String, Vector< String > > > getSetsInDB()
pdb::Handle< pdb::Vector< String > > getListOfNodes()
void deleteNodeFromMap(String &nodeIP, String &setName)
void deleteSetFromNodeMap(String &setName)
void setItemKey(String &itemKeyIn)
void deleteNodeFromSingleSet(String &node, String &setName)
pdb::Handle< pdb::Vector< String > > getListOfTypes()
CatalogDatabaseMetadata(const Handle< CatalogDatabaseMetadata > &pdbDatabaseToCopy)
#define PDB_COUT
Definition: PDBDebug.h:31
pdb::Handle< pdb::Vector< String > > getListOfSets()
void addSetToMap(String &setName, String &nodeIP)
void setValues(String dbIdIn, pdb::String dbNameIn, pdb::String userCreatorIn, pdb::String createdOnIn, pdb::String lastModifiedIn)
char * c_str() const
Definition: PDBString.cc:184
Handle< Map< String, Vector< String > > > getNodesInDB()
void replaceListOfNodes(Handle< Vector< pdb::String >> &newList)
Handle< Map< String, Vector< String > > > nodesInDB
Handle< Map< String, Vector< String > > > setsInDB
pdb::Handle< pdb::Vector< String > > listOfSets
void replaceListOfSets(Handle< Vector< pdb::String >> &newList)