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
StatisticsDB.cc
Go to the documentation of this file.
1 #ifndef STATISTICS_DB_CC
2 #define STATISTICS_DB_CC
3 
4 
5 #include "StatisticsDB.h"
6 
7 namespace pdb {
8 
9 
11 
12  this->conf = conf;
13  this->pathToDBFile = "file:" + this->conf->getStatisticsDB() + "/dbFile";
14 
15  //check whether pathToDBFile exists
16  //if it exists, we do not need create directories and tables
17  //otherwise, we need create directories and tables
18  if (createDir()){
19  createTables();
20  } else {
21  openDB();
22  }
23 
24  //initialize ids
25  dataId = getLatestId("DATA") + 1;
27 
28 }
29 
30 
32 
33  closeDB();
34 
35 }
36 
37 
39 
40  PDB_COUT << "to open DB " << this->pathToDBFile << std::endl;
41  if (sqlite3_open_v2(this->pathToDBFile.c_str(), &statisticsDBHandler,
42  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_URI, NULL) == SQLITE_OK) {
43 
44  sqlite3_exec(statisticsDBHandler, "PRAGMA journal_mode=WAL", NULL, NULL, NULL);
45  PDB_COUT << "self learning database open successfully" << std::endl;
46  return true;
47  } else {
48  PDB_COUT << "failure in opening self learning database" << std::endl;
49  return false;
50  }
51  return false;
52 
53 }
54 
56 
57  //TODO
58  return false;
59 }
60 
61 
62 
63 
65 
66  if (openDB() == false) {
67  return false;
68  }
69 
70  bool ret = execDB ("CREATE TABLE IF NOT EXISTS DATA (ID BIGINT PRIMARY KEY, "
71  "DATABASE_NAME VARCHAR(128), SET_NAME VARCHAR(128),"
72  "CREATED_JOBID VARCHAR(128), IS_REMOVED BOOLEAN,"
73  "SET_TYPE VARCHAR(32), CLASS_NAME VARCHAR(128),"
74  "TYPE_ID INT, SIZE BIGINT, PAGE_SIZE BIGINT,"
75  "MODIFICATION_TIME BIGINT) WITHOUT ROWID;");
76 
77  if (ret == true) {
78 
79  ret = execDB ("CREATE TABLE IF NOT EXISTS DATA_TRANSFORMATION (ID BIGINT PRIMARY KEY, "
80  "INPUT_DATA_ID BIGINT, OUTPUT_DATA_ID BIGINT, "
81  "NUM_PARTITIONS INT, NUM_NODES INT, "
82  "TRANSFORMATION_TYPE VARCHAR(32), TCAP TEXT, "
83  "COMPUTATIONS BLOB, "
84  "FOREIGN KEY (INPUT_DATA_ID) REFERENCES DATA(ID),"
85  "FOREIGN KEY (OUTPUT_DATA_ID) REFERENCES DATA(ID)) WITHOUT ROWID;");
86 
87  }
88 
89  return ret;
90 
91 }
92 
93 bool StatisticsDB::execDB (std::string cmdString) {
94 
95  PDB_COUT << "command: " << cmdString << std::endl;
96  sqlite3_stmt * cmdStatement;
97  if (sqlite3_prepare_v2 (statisticsDBHandler, cmdString.c_str(), -1, &cmdStatement,
98  NULL) == SQLITE_OK) {
99  sqlite3_step(cmdStatement);
100  sqlite3_finalize(cmdStatement);
101  return true;
102  } else {
103  std::string error = sqlite3_errmsg(statisticsDBHandler);
104  PDB_COUT << error << std::endl;
105  sqlite3_finalize(cmdStatement);
106  return false;
107  }
108 
109 }
110 
112 
113  if (mkdir(this->conf->getStatisticsDB().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)!= -1){
114  return true;
115  } else {
116  return false;
117  }
118 
119 }
120 
121 bool StatisticsDB::createData (std::string databaseName,
122  std::string setName,
123  std::string created_jobId,
124  std::string setType,
125  std::string className,
126  int typeId,
127  size_t pageSize,
128  long & id) {
129 
130  PDB_COUT << "to create data..." << std::endl;
131  id = dataId;
132  dataId ++;
133  string cmdString = "INSERT INTO DATA "
134  " (ID, DATABASE_NAME, SET_NAME, CREATED_JOBID, IS_REMOVED, SET_TYPE, "
135  "CLASS_NAME, TYPE_ID, SIZE, PAGE_SIZE, MODIFICATION_TIME) "
136  "VALUES(" + std::to_string(id) + ","
137  + quoteStr(databaseName) + ","
138  + quoteStr(setName) + ","
139  + quoteStr(created_jobId) + ","
140  + "0,"
141  + quoteStr(setType) + ","
142  + quoteStr(className) + ","
143  + std::to_string(typeId) + ","
144  + "0,"
145  + std::to_string(pageSize) + ","
146  + "strftime('%s', 'now', 'localtime'));";
147  PDB_COUT << "CreateData: " << cmdString << std::endl;
148  return execDB(cmdString);
149 
150 
151 }
152 
154  size_t size) {
155 
156  std::string cmdString = "UPDATE DATA set SIZE = " + std::to_string(size) +
157  ", MODIFICATION_TIME = strftime('%s', 'now', 'localtime') where ID = " +
158  std::to_string(id);
159  PDB_COUT << "UpdateDataForSize: " << cmdString << std::endl;
160  return execDB(cmdString);
161 
162 }
163 
165 
166  std::string cmdString = std::string("UPDATE DATA set IS_REMOVED = 1") +
167  std::string(", MODIFICATION_TIME = strftime('%s', 'now', 'localtime') where ID = ") +
168  std::to_string(id);
169  PDB_COUT << "UpdateDataForRemoval: " << cmdString << std::endl;
170  return execDB(cmdString);
171 
172 }
173 
174 bool StatisticsDB::createDataTransformation (long input_data_id,
175  long output_data_id,
176  int num_partitions,
177  int num_nodes,
178  std::string transformationType,
179  std::string tcap,
180  Handle<Vector<Handle<Computation>>> computations,
181  long& id) {
182 
183  PDB_COUT << "to create data transformation..." << std::endl;
184  id = transformationId;
185  sqlite3_stmt * statement;
186  transformationId ++;
187  replaceStr(tcap, "'", "''");
188  string cmdString = "INSERT INTO DATA_TRANSFORMATION "
189  " (ID, INPUT_DATA_ID, OUTPUT_DATA_ID, NUM_PARTITIONS, NUM_NODES, TRANSFORMATION_TYPE, TCAP, COMPUTATIONS) "
190  "VALUES(" + std::to_string(id) + ","
191  + std::to_string(input_data_id) + ","
192  + std::to_string(output_data_id) + ","
193  + std::to_string(num_partitions) + ","
194  + std::to_string(num_nodes) + ","
195  + quoteStr(transformationType) + ","
196  + quoteStr(tcap)
197  + ", ?);";
198  PDB_COUT << "CreateDataTransformation: " << cmdString << std::endl;
199  Handle<Vector<Handle<Computation>>> myComputations =
200  deepCopyToCurrentAllocationBlock<Vector<Handle<Computation>>>(computations);
201  if (sqlite3_prepare_v2(statisticsDBHandler, cmdString.c_str(), -1, &statement, NULL) == SQLITE_OK) {
202  //to get the bytes
204  getRecord<Vector<Handle<Computation>>>(myComputations);
205  sqlite3_bind_blob(statement, 1, record, record->numBytes(), SQLITE_STATIC);
206  sqlite3_step(statement);
207  sqlite3_finalize(statement);
208 
209  return true;
210  } else {
211  PDB_COUT << (std::string)(sqlite3_errmsg(statisticsDBHandler)) << std::endl;
212  sqlite3_finalize(statement);
213  return false;
214  }
215 
216 }
217 
218 std::vector<std::shared_ptr<TransformedSet>>
219 StatisticsDB::getTransformedSets(std::pair<std::string, std::string> databaseAndSetName) {
220 
221  std::vector<std::shared_ptr<TransformedSet>> ret;
222 
223  return ret;
224 
225 }
226 
227 
228 long StatisticsDB::getLatestId (std::string tableName) {
229 
230  long id;
231  sqlite3_stmt * statement;
232  std::string queryString = "SELECT MAX(ID) from " + quoteStr(tableName);
233  if (sqlite3_prepare_v2(statisticsDBHandler, queryString.c_str(), -1, &statement, NULL) == SQLITE_OK) {
234  int res = sqlite3_step(statement);
235  if (res == SQLITE_ROW) {
236  id = sqlite3_column_int(statement, 0);
237  } else {
238  id = -1;
239  }
240  } else {
241  PDB_COUT << (std::string)(sqlite3_errmsg(statisticsDBHandler)) << std::endl;
242  id = -1;
243  }
244  sqlite3_finalize(statement);
245  return id;
246 
247 }
248 
249 
250 long StatisticsDB::getLatestDataId (std::pair<std::string, std::string> databaseAndSetName) {
251  long id;
252  sqlite3_stmt * statement;
253  std::string queryString = "SELECT MAX(ID) from DATA where DATABASE_NAME=" + quoteStr(databaseAndSetName.first)
254  + " AND SET_NAME=" + quoteStr(databaseAndSetName.second);
255  PDB_COUT << "Get Latest Data Id: " << queryString << std::endl;
256  if (sqlite3_prepare_v2(statisticsDBHandler, queryString.c_str(), -1, &statement, NULL) == SQLITE_OK) {
257  int res = sqlite3_step(statement);
258  if (res == SQLITE_ROW) {
259  id = sqlite3_column_int(statement, 0);
260  } else {
261  id = -1;
262  }
263  } else {
264  PDB_COUT << (std::string)(sqlite3_errmsg(statisticsDBHandler)) << std::endl;
265  id = -1;
266  }
267  sqlite3_finalize(statement);
268  return id;
269 }
270 
271 
273  return getLatestId("DATA_TRANSFORMATION");
274 }
275 
276 
277 std::string StatisticsDB :: quoteStr(std::string& s) {
278 
279  return std::string("'") + s + std::string("'");
280 
281 }
282 
283 void StatisticsDB :: replaceStr (std::string& str,
284  const std::string& oldStr,
285  const std::string& newStr)
286 {
287  std::string::size_type pos = 0u;
288  while((pos = str.find(oldStr, pos)) != std::string::npos){
289  str.replace(pos, oldStr.length(), newStr);
290  pos += newStr.length();
291  }
292 }
293 
294 
295 }
296 
297 
298 #endif
bool createDataTransformation(long input_data_id, long output_data_id, int num_partitions, int num_nodes, std::string transformationType, std::string tcap, Handle< Vector< Handle< Computation >>> computations, long &id)
std::string pathToDBFile
Definition: StatisticsDB.h:174
long getLatestId(std::string tableName)
long getLatestTransformationId()
StatisticsDB(ConfigurationPtr conf)
Definition: StatisticsDB.cc:10
bool updateDataForSize(long id, size_t size)
ConfigurationPtr conf
Definition: StatisticsDB.h:180
size_t numBytes()
Definition: Record.cc:36
#define PDB_COUT
Definition: PDBDebug.h:31
shared_ptr< Configuration > ConfigurationPtr
Definition: Configuration.h:89
std::vector< std::shared_ptr< TransformedSet > > getTransformedSets(std::pair< std::string, std::string > databaseAndSetName)
sqlite3 * statisticsDBHandler
Definition: StatisticsDB.h:177
bool createData(std::string databaseName, std::string setName, std::string created_jobId, std::string setType, std::string className, int typeId, size_t pageSize, long &id)
long getLatestDataId(std::pair< std::string, std::string > databaseAndSetName)
std::string quoteStr(std::string &s)
bool updateDataForRemoval(long id)
void replaceStr(std::string &str, const std::string &oldStr, const std::string &newStr)
bool execDB(std::string cmdString)
Definition: StatisticsDB.cc:93