-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialLoad.hpp
More file actions
568 lines (486 loc) · 21.2 KB
/
Copy pathinitialLoad.hpp
File metadata and controls
568 lines (486 loc) · 21.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#ifndef __INITIAL_LOAD
#define __INITIAL_LOAD
#include <cstdint>
#include <unordered_map>
#include <filesystem>
#include <iostream>
#include <sstream>
#include <memory>
#include <stdexcept>
#include <climits>
#include <string>
#include <thread>
#include <format>
#include <chrono>
#include <atomic>
#include <utility>
#include <vector>
#include "SQL_PARSER.hpp"
#include "hft.hpp"
#include "global.hpp"
#include "utility.hpp"
#include "batchWriter.hpp"
namespace fs = std::filesystem;
std::string getCurrentDatabase(std::string &metaFile)
{
std::ifstream file(metaFile);
if (!file.is_open())
throw std::runtime_error("Unable to open meta file");
std::string line;
std::getline(file, line);
size_t keyPos = line.find("\"current_db\"");
if (keyPos == std::string::npos)
throw std::runtime_error("Key 'current_db' not found");
size_t colonPos = line.find(":", keyPos);
if (colonPos == std::string::npos)
throw std::runtime_error("Invalid format in meta file");
size_t firstQuote = line.find("\"", colonPos);
if (firstQuote == std::string::npos)
throw std::runtime_error("Invalid format in meta file");
size_t secondQuote = line.find("\"", firstQuote + 1);
if (secondQuote == std::string::npos)
throw std::runtime_error("Invalid format in meta file");
return line.substr(firstQuote + 1, secondQuote - firstQuote - 1);
}
void runVacuum() {
std::lock_guard<std::mutex> dbLock(dbMutex);
std::vector<std::pair<std::string, std::string>> tablesToVacuum;
for (auto& [dbName, tables] : globalTableCache) {
for (auto i=tables.begin();i!=tables.end();i++) {
tablesToVacuum.emplace_back(dbName, i->first);
}
}
for (auto& [db, table] : tablesToVacuum) {
DEBUG_LOG(table);
PagerHandler::vacuumTable(db, table);
}
}
void vacuumScheduler() {
while (!shuttingDown.load()) {
for (int i = 0; i < 360; i++) {
if (shuttingDown.load()) return;
std::this_thread::sleep_for(std::chrono::seconds(60));
}
if (shuttingDown.load()) return;
runVacuum();
}
}
void startVacuumThread() {
vacuumThread = std::thread(vacuumScheduler);
}
bool checkDBexist(const std::string &name)
{
std::stringstream file;
file << dbDirectoryPath;
file << name;
file << ".shivam.db";
return MyUtility::checkIfFileExist(file.str());
}
void initialDatabseLoad()
{
std::string currentDbMeta = currentDbPath;
if (!fs::is_directory(dbDirectoryPath)){
fs::create_directories(tableDirectory);
std::ofstream file(currentDbPath);
if (file.is_open()){
file << "{\"current_db\":\"\"}";
file.close();
}else{
DEBUG_LOG("Failed to create meta file");
}
}
std::string dbName = getCurrentDatabase(currentDbMeta);
if (dbName == "") return;
currentDatabase = dbName;
std::string filename = dbName + ".shivam.db";
std::string fullPath = dbDirectoryPath + "/" + filename;
if (!fs::exists(fullPath)) {
DEBUG_LOG("Database file does not exist: " << fullPath);
return;
}
std::string dbname = dbName;
std::shared_ptr<PythonLikeJSONParser> parser = std::make_shared<PythonLikeJSONParser>();
// Store parser in global cache
globalJsonCache[dbname] = parser;
DEBUG_LOG("dbname " << dbname);
DEBUG_LOG("FULL PATH " << fullPath);
if (!parser->loadFromFile(fullPath))
{
DEBUG_LOG("Failed to load file: " << fullPath);
std::stringstream err;
err << "Failed to load file: " << fullPath;
throw std::runtime_error(err.str());
}
try
{
JSONArrayWrapper tablesArray = (*parser)[0][std::string("tables")].asArray();
for (int64_t i = 0; i < tablesArray.size(); ++i)
{
std::string tableName = tablesArray[i][std::string("name")].getString();
std::string indexFileName = tableDirectory + "/" + dbname + "/" + tableName + ".data";
std::unique_ptr<IoUringQueue> io_queue = std::make_unique<IoUringQueue>(indexFileName);
std::string symbolString = std::to_string(tablesArray[i][std::string("symbol")].getInt());
std::string topString = std::to_string(tablesArray[i][std::string("top")].getInt());
DEBUG_LOG(symbolString << " " << topString);
bool isSymbol = false;
bool isTop = false;
HFT_DEBUG_FILE("error.txt", std::format("the tableName is {} and top string is {}",symbolString,topString));
if(!symbolString.empty() && symbolString != "-1") isSymbol = true;
if(topString.length() > 0 && topString[0]=='1') isTop = true;
JSONArrayWrapper columnsArray = tablesArray[i][std::string("columns")].asArray();
std::vector<std::shared_ptr<TableGlobalColumnNode>> columnNodes;
std::vector<int64_t> precisions;
for (int64_t j = 0; j < columnsArray.size(); ++j)
{
std::shared_ptr<TableGlobalColumnNode> node = std::make_shared<TableGlobalColumnNode>();
std::string columnDataName = columnsArray[j][std::string("name")].getString();
std::string columnDataType = columnsArray[j][std::string("type")].getString();
JSONArrayWrapper constraintArray = columnsArray[j][std::string("constraints")].asArray();
int64_t precision = columnsArray[j][std::string(std::string("precision"))].getInt();
precisions.push_back(precision);
DEBUG_LOG("the precision is " << precision);
int length = INT_MAX;
bool isUnique = false;
bool isPrimary = false;
bool autoIncrement = false;
bool createIndex = false;
for (int64_t k = 0; k < constraintArray.size(); ++k)
{
std::string constraint = constraintArray[k].getString();
if (constraint == "primary_key")
isPrimary = true;
if (constraint == "auto_increment")
autoIncrement = true;
if (constraint == "unique")
isUnique = true;
if (constraint == "create_index")
createIndex = true;
}
if (columnsArray[j].hasKey(std::string("length")))
{
length = columnsArray[j][std::string("length")];
}
node->constraint = constraintArray.toStringVector();
node->length = length;
node->precision = precision;
node->name = columnDataName;
node->type = columnDataType;
node->autoIncrement = autoIncrement;
node->isUnique = isUnique;
node->createIndex = createIndex;
node->isPrimary = isPrimary;
columnNodes.push_back(node);
if (isPrimary || isUnique)
{
TreeVariant tree = std::make_shared<BPlusTree<int64_t, IndexNode>>();
dbBtrees[dbname][tableName][columnDataName] = std::make_pair(tree, 0);
}
}
int ticks = -1;
if (tablesArray[i].hasKey(std::string("ticks"))) {
ticks = tablesArray[i][std::string("ticks")].getInt();
}
// Save table columns in globalTableCache
auto it=std::move(columnNodes);
globalTableCache[dbname][tableName] = {symbolString,it};
if(isSymbol){
int64_t symbol = static_cast<int64_t>(std::stoi(symbolString));
HFT_DEBUG_FILE("error.txt", std::format("the table name is {} symbol is {} isTop is {}",tableName,symbol,isTop));
HFT::symbolAccessArray[symbol].init(precisions,columnsArray.size(),isTop?1:0,symbol);
if (ticks > 0) {
HFT::symbolAccessArray[symbol].storageTicks = ticks;
}
auto it_f = batchWriterFileMap.find(symbol);
if (it_f == batchWriterFileMap.end() || !it_f->second) {
batchWriterFileMap[symbol] = std::move(io_queue);
} else {
// Reuse existing writer
DEBUG_LOG("[INITIAL_LOAD] Reusing existing writer for symbol " << symbol);
}
}
DEBUG_LOG("Loaded table: " << tableName << " from DB: " << dbname);
}
}
catch (const std::exception &e)
{
std::cerr << "Error accessing 'tables' in " << fullPath << ": " << e.what() << std::endl;
}
}
void loadAllNodesOfBtreeForPrimaryKey(TreeVariant &tree,std::string dbName, int64_t size, std::string tableName)
{
std::stringstream indexFileName;
indexFileName << tableDirectory << "/" << dbName << "/" << tableName << ".index";
DEBUG_LOG("INDEX FILE NAME " << indexFileName.str());
if (MyUtility::checkIfFileExist(indexFileName.str()))
{
std::fstream indexFile(indexFileName.str(), std::ios::in | std::ios::out | std::ios::binary);
if (indexFile.fail())
{
throw std::runtime_error("Failed to read primary key from index file");
}
int64_t indexFileSize = PagerHandler::getFileSize(indexFileName.str());
std::cout << "index file size is " << indexFileSize << "\n";
int64_t divider = 8 * (2 * size - 1);
int64_t getTotalNoOfRows = (int64_t)(indexFileSize / divider);
std::cout << "Total No Of rows " << getTotalNoOfRows << "\n";
while (getTotalNoOfRows--)
{
int64_t id, start, end;
std::streampos pos = indexFile.tellg();
if (pos != std::streampos(-1))
{
int64_t offset = static_cast<int64_t>(pos);
start = offset;
}
else
{
break;
}
indexFile.read(reinterpret_cast<char *>(&id), sizeof(int64_t));
// indexFile.seekg(sizeof(int64_t),std::ios::cur);
// indexFile.read(reinterpret_cast<char *>(&start), sizeof(int64_t));
indexFile.seekg((2 * (size - 1)) * sizeof(int64_t), std::ios::cur);
std::streampos endpos = indexFile.tellg();
if (endpos != std::streampos(-1))
{
int64_t offset = static_cast<int64_t>(endpos);
end = offset;
}
else
{
break;
}
IndexNode node{start, static_cast<int16_t>(end)};
DEBUG_LOG("id " << id << " start " << start << " end " << end);
std::visit([id, node](auto &treePtr)
{
using TreeType = std::decay_t<decltype(*treePtr)>;
if constexpr (std::is_same_v<TreeType, BPlusTree<int64_t, IndexNode>>) {
treePtr->insert(id, node);
} else if constexpr (std::is_same_v<TreeType, BPlusTree<std::string, IndexNode>>) {
treePtr->insert(std::to_string(id), node);
} }, tree);
}
}
else
{
throw std::runtime_error("the table does not exist");
}
}
void loadAllNodesOfBtreeForUniqueKey(TreeVariant &tree,std::string dbName, int64_t size, std::string tableName,std::string columnName, const std::vector<std::string> &sortedColumnsVector, const std::string &dataType)
{
std::stringstream indexFileName;
std::stringstream dataFileName;
indexFileName << tableDirectory << "/" << dbName << "/" << tableName << ".index";
dataFileName << tableDirectory << "/" << dbName << "/" << tableName << ".data";
int64_t columnNameIndexInSortedOrder = -1;
DEBUG_LOG("### --- startedSorted---");
for(auto ele:sortedColumnsVector){
DEBUG_LOG(ele);
}
for (int i = 0; i < sortedColumnsVector.size(); i++)
{
DEBUG_LOG(sortedColumnsVector[i] << " ");
if (columnName == sortedColumnsVector[i])
{
columnNameIndexInSortedOrder = i + 1;
break;
}
}
std::cout<<"\n";
if (columnNameIndexInSortedOrder == -1)
{
std::stringstream err;
err << "unique key " << columnName << " does not exist in table " << tableDirectory;
throw std::runtime_error(err.str());
}
DEBUG_LOG("INDEX FILE NAME " << indexFileName.str());
DEBUG_LOG("DATATYPE IS " << dataType);
if (MyUtility::checkIfFileExist(indexFileName.str()))
{
std::fstream indexFile(indexFileName.str(), std::ios::in | std::ios::out | std::ios::binary);
std::fstream dataFile(dataFileName.str(), std::ios::in | std::ios::out | std::ios::binary);
if (indexFile.fail())
{
throw std::runtime_error("Failed to read primary key from index file");
}
if (dataFile.fail())
{
throw std::runtime_error("Failed to read data from dataFile file");
}
int64_t indexFileSize = PagerHandler::getFileSize(indexFileName.str());
DEBUG_LOG("index file size is " << indexFileSize);
int64_t divider = 8 * (2 * size - 1);
int64_t getTotalNoOfRows = (int64_t)(indexFileSize / divider);
DEBUG_LOG("Total No Of rows " << getTotalNoOfRows);
DEBUG_LOG("columnNameIndexSortedOrder " << columnNameIndexInSortedOrder);
while (getTotalNoOfRows--)
{
std::variant<int64_t, std::string> id;
int64_t start, end;
std::streampos pos = indexFile.tellg();
if (pos != std::streampos(-1))
{
int64_t offset = static_cast<int64_t>(pos);
start = offset;
}
else
{
break;
}
indexFile.seekg(sizeof(int64_t), std::ios::cur);
indexFile.seekg(2 * (columnNameIndexInSortedOrder - 2) * sizeof(int64_t), std::ios::cur);
int64_t uniqueReadStartIndex, uniqueReadEndIndex;
indexFile.read(reinterpret_cast<char *>(&uniqueReadStartIndex), sizeof(int64_t));
indexFile.read(reinterpret_cast<char *>(&uniqueReadEndIndex), sizeof(int64_t));
indexFile.seekg(-2 * sizeof(int64_t), std::ios::cur);
indexFile.seekg(-2 * (columnNameIndexInSortedOrder - 2) * sizeof(int64_t), std::ios::cur);
indexFile.seekg(-1 * sizeof(int64_t), std::ios::cur);
dataFile.seekg(uniqueReadStartIndex, std::ios::beg);
try
{
if (dataType == "int")
{
// Data file stores values as ASCII strings, not binary int64_t.
// Read the string and convert to int64_t.
uint64_t strSize = uniqueReadEndIndex - uniqueReadStartIndex;
std::string strValue(strSize, '\0');
dataFile.read(strValue.data(), strSize);
DEBUG_LOG("VALUE READ (int) " << strValue);
int64_t value = std::stoll(strValue);
id = value;
}
else
{
uint64_t strSize = uniqueReadEndIndex - uniqueReadStartIndex;
DEBUG_LOG("start " << uniqueReadStartIndex << " and end " << uniqueReadEndIndex);
std::string value(strSize, '\0');
dataFile.read(value.data(), strSize);
DEBUG_LOG("VALUE READ " << value);
id = value;
}
}
catch(const std::exception& e)
{
std::cerr<<"erro at dataFileRead"<<"\n";
std::cerr << e.what() << '\n';
}
// indexFile.seekg(sizeof(int64_t),std::ios::cur);
// indexFile.read(reinterpret_cast<char *>(&start), sizeof(int64_t));
indexFile.seekg(sizeof(int64_t), std::ios::cur);
indexFile.seekg((2 * (size - 1)) * sizeof(int64_t), std::ios::cur);
std::streampos endpos = indexFile.tellg();
if (endpos != std::streampos(-1))
{
int64_t offset = static_cast<int64_t>(endpos);
end = offset;
}
else
{
break;
}
IndexNode node{start, static_cast<int16_t>(end)};
DEBUG_LOG("id ");
std::visit([](auto &&value)
{ DEBUG_LOG(value); }, id);
DEBUG_LOG(" start " << start << " end " << end);
std::visit([&](auto &treePtr, auto &&val)
{
using TreeType = std::decay_t<decltype(*treePtr)>;
using ValType = std::decay_t<decltype(val)>;
if constexpr (std::is_same_v<TreeType, BPlusTree<int64_t, IndexNode>>) {
if constexpr (std::is_same_v<ValType, int64_t>) {
treePtr->insert(val, node);
} else {
// convert string to int64_t
int64_t parsed = std::stoll(val);
treePtr->insert(parsed, node);
}
} else if constexpr (std::is_same_v<TreeType, BPlusTree<std::string, IndexNode>>) {
if constexpr (std::is_same_v<ValType, std::string>) {
treePtr->insert(val, node);
} else {
treePtr->insert(std::to_string(val), node);
}
} },
tree, id);
}
}
else
{
throw std::runtime_error("the table does not exist");
}
}
void initializePrimaryIndexBtrees(std::string tabName,bool first)
{
for (const auto &dbPair : globalTableCache)
{
const std::string &dbName = dbPair.first;
const auto &tables = dbPair.second;
for (const auto &tablePair : tables)
{
const std::string &tableName = tablePair.first;
if (!first && tabName!=tableName) continue;
if (tablePair.second.first!="-1") continue;
const auto &columns = tablePair.second.second;
int64_t noOfColumns = columns.size();
std::vector<std::string> sortedColumnVector;
for (const auto &columnPtr : columns)
{
sortedColumnVector.emplace_back(columnPtr->name);
}
sort(sortedColumnVector.begin(), sortedColumnVector.end());
for (const auto &columnPtr : columns)
{
DEBUG_LOG(columnPtr->name << " " << columnPtr->isPrimary << "hh");
if (columnPtr->isPrimary)
{
const std::string &columnName = columnPtr->name;
const std::string &type = columnPtr->type;
TreeVariant tree;
if (type == "int")
{
tree = std::make_shared<BPlusTree<int64_t, IndexNode>>();
}
else
{
std::cerr << "Unsupported primary key type: " << type
<< " for column: " << columnName << std::endl;
continue;
}
dbBtrees[dbName][tableName][columnName] = std::make_pair(std::move(tree), noOfColumns);
DEBUG_LOG("btreecalled");
loadAllNodesOfBtreeForPrimaryKey(dbBtrees[dbName][tableName][columnName].first,dbName, noOfColumns, tableName);
DEBUG_LOG("Initialized B+ Tree for " << dbName
<< "." << tableName << "." << columnName << " " << "and size is " << noOfColumns);
}
else if (columnPtr->isUnique)
{
std::cout << "for unique columns " << columnPtr->name << columnPtr->isUnique << "\n";
const std::string &columnName = columnPtr->name;
const std::string &type = columnPtr->type;
TreeVariant tree;
if (type == "int")
{
tree = std::make_shared<BPlusTree<int64_t, IndexNode>>();
}
else if (type == "varchar")
{
tree = std::make_shared<BPlusTree<std::string, IndexNode>>();
}
else
{
DEBUG_LOG("Unsupported Unique key type: " << type
<< " for column: " << columnName);
continue;
}
dbBtrees[dbName][tableName][columnName] = std::make_pair(std::move(tree), noOfColumns);
loadAllNodesOfBtreeForUniqueKey(dbBtrees[dbName][tableName][columnName].first,dbName, noOfColumns, tableName,columnName, sortedColumnVector, type);
DEBUG_LOG("Initialized B+ Tree for Unique Key" << dbName
<< "." << tableName << "." << columnName << " " << "and size is " << noOfColumns);
}
}
}
}
}
#endif // __INITIAL_LOAD