-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
324 lines (292 loc) · 8.58 KB
/
Copy pathmain.cpp
File metadata and controls
324 lines (292 loc) · 8.58 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
#include "Btrees_testing.hpp"
#include "IndicatorHandler.hpp"
#include "SQL_LEXER.hpp"
#include "SQL_PARSER.hpp"
#include "UDPReceiver.hpp"
#include "hft.hpp"
#include "initialLoad.hpp"
#include "logging.hpp"
#include "strategyHandler.hpp"
#include "tradePackets.hpp"
#include "utility.hpp"
#include "utils/cpu_affinity.hpp"
#include "web_socks_og.hpp"
#include <iostream>
#include <string>
#include <termios.h>
#include <thread>
#include <unistd.h>
#include <vector>
using namespace std;
std::string typeToString(TokenType TYPE);
static std::vector<std::jthread> worker_threads;
void setup() {
worker_threads.emplace_back(NetFeed::run_receiver, 0);
worker_threads.emplace_back(NetFeed::run_packet_parser, 1);
worker_threads.emplace_back(NetFeed::run_strategy_parser, 2);
worker_threads.emplace_back(init_web_sockets, 3);
worker_threads.emplace_back(tradeHandler::run_trade_handler, 4);
}
std::string sha256(const string& input) {
unsigned char hash[256];
SHA256(reinterpret_cast<const unsigned char*>(input.c_str()),input.size(),hash);
stringstream ss;
for (int i=0;i<256;i++) ss<<hex<<setw(2)<<setfill('0')<<(int)hash[i];
return ss.str();
}
std::string get_id(){
std::ifstream f("/sys/class/dmi/id/product_uuid");
std::string uuid;
f>>uuid;
uuid=sha256(uuid);
return uuid;
}
string readLineWithHistory(vector<string> &history, int &historyIndex) {
termios oldt, newt;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
string input;
vector<char> buffer;
while (true) {
char c = getchar();
if (c == '\n') {
cout << "\n";
break;
}
if (c == 27) {
char c1 = getchar();
char c2 = getchar();
if (c1 == '[') {
if (c2 == 'A') {
if (!history.empty() && historyIndex > 0) {
historyIndex--;
cout << "\33[2K\rnanoVaultDb> ";
input = history[historyIndex];
cout << input;
}
} else if (c2 == 'B') {
if (!history.empty() && historyIndex < (int)history.size() - 1) {
historyIndex++;
cout << "\33[2K\rnanoVaultDb> ";
input = history[historyIndex];
cout << input;
} else {
historyIndex = history.size();
cout << "\33[2K\rnanoVaultDb> ";
input.clear();
}
}
}
continue;
}
if (c == 127 || c == 8) {
if (!input.empty()) {
input.pop_back();
cout << "\b \b";
}
continue;
}
input.push_back(c);
cout << c;
}
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return input;
}
int main(int argc, char const *argv[]) {
std::string db_path = "";
for (int i = 1; i < argc; ++i) {
std::string arg = argv[i];
if (arg == "--db-path" || arg == "-d") {
if (i + 1 < argc) {
db_path = argv[++i];
} else {
std::cerr << "Error: --db-path requires a path argument." << std::endl;
return 1;
}
} else if (arg.rfind("--", 0) != 0 && arg.rfind("-", 0) != 0) {
db_path = arg;
}
}
if (!db_path.empty()) {
if (db_path.back() == '/' || db_path.back() == '\\') {
db_path.pop_back();
}
dbDirectoryPath = db_path;
currentDbPath = dbDirectoryPath + "/current_db.meta";
tableDirectory = dbDirectoryPath + "/tables";
std::cout << "Using database directory: " << dbDirectoryPath << std::endl;
}
// std::string msg=get_id();
// cout<<msg<<endl;
// if (msg!=get_msg_from_server) throw runtime_error("Firstly run download.exe file with sudo permissions");
ExchangeHelper::load_api_keys();
initialDatabseLoad();
HFT::InitalStorage::initialIndicatorLoad();
HFT::InitalStorage::initialStrategyLoad();
// runVacuum();
initializePrimaryIndexBtrees("abcd", true);
cout << "\n";
test_b_trees();
std::cout << "Finished b+\n";
setup();
std::vector<std::string> testSQLs = {
// R"(
// CREATE DATABASE test10;
// )",
// R"(
// CREATE TABLE StudentRolls (
// id INT PRIMARY KEY AUTO_INCREMENT,
// roll_no INT NOT NULL UNIQUE
// );
// // // // // // // // // // // )",
// R"(
// INSERT INTO StudentRolls (roll_no)
// VALUES (10);
// )",
// R"(
// INSERT INTO StudentRolls (roll_no)
// VALUES (15);
// )",
// R"(
// UPDATE StudentRolls SET roll_no="WH" WHERE roll_no="Hey";
// )",
// R"(
// DELETE FROM StudentRolls WHERE roll_no=12;
// )",
// R"(
// INSERT INTO StudentRolls (roll_no)
// VALUES ("Woho");
// )",
// R"(
// SELECT * FROM StudentRolls;
// )",
// R"(
// STATISTICS MEAN FROM StudentRolls ON roll_no WHERE roll_no="Woho";
// )",
// R"(
// STATISTICS COUNT FROM StudentRolls ON roll_no WHERE roll_no="W";
// )" ,
R"(
CREATE HFT TABLE testing_hft_table (
timestamp DOUBLE PRECISION 0,
price DOUBLE PRECISION 10,
volume DOUBLE PRECISION 2,
side DOUBLE PRECISION 0
) SYMBOL 6 TOP;
)",
// R"(
// ADD INDICATOR "sma" ( "10" ) ON SYMBOL 6 COLUMN_NO 2 ticks 100;
// )",
// R"(
// ENABLE STRATEGY "again" ("10") on symbol 6 column_no 0 ticks 10;
// )",
// R"(
// ENABLE BATCH WRITING ON TABLE "testing_hft_table" TICKS 100;
// )",
// R"(
// DISABLE BATCH WRITING ON TABLE "testing_hft_table";
// )",
// R"(
// DROP TABLE StudentRolls;
// )",
// R"(
// DROP DATABASE test;
// )",
// R"(
// USE school;
// )",
// R"(
// MEMORY KEY=a VALUES=123 TTL=5;
// )",
// R"(
// MEMORY GET KEY=a;
// )",
};
// for (int i = 1; i <= 10; i++)g++ -std=c++20 -fsanitize=address -g -O0 -Wall
// -Wextra main.cpp -o main
// {
// std::string insertSQL =
// "INSERT INTO testing (rollno, name, age) VALUES (" +
// std::to_string(i) + ", \"Student" +
// std::to_string(i) + "\", " +
// std::to_string(18 + i) + ");";
// testSQLs.push_back(insertSQL);
// }
// initialDatabseLoad();
IndicatorHandler::registerAllIndicators(IndicatorHandler::indicatorRegistry);
StrategyHandler::registerAllStrategy(StrategyHandler::strategyRegistry);
for (const auto &sql : testSQLs) {
cout << "\n=============================\n";
cout << "SQL:\n" << sql << endl;
cout << "=============================\n";
try {
Lexer lexer(sql);
vector<Token *> tokens = lexer.tokenize();
// Debug: Print tokens
cout << "Tokens:\n";
for (Token *token : tokens) {
cout << typeToString(token->TYPE) << " : " << token->VALUE << endl;
}
Parser parser(tokens);
parser.parse(); // Par/se the SQL
} catch (const std::exception &e) {
cerr << "Error: " << e.what() << endl;
}
cout << "\n";
}
// NetFeed::run_receiver();
// --- Start Shell REPL below ---
vector<string> history;
vector<string> rem_sqls = exec_rem_sqls();
for (string s : rem_sqls) {
try {
Lexer lexer(s);
vector<Token *> tokens = lexer.tokenize();
for (Token *token : tokens) {
cout << typeToString(token->TYPE) << " : " << token->VALUE << endl;
}
Parser parser(tokens);
std::string output = parser.parse();
std::cout << output << "\n";
} catch (const std::exception &e) {
cerr << "Warning: Failed to execute recovered SQL -> " << e.what()<< endl;
}
}
int historyIndex = 0;
while (true) {
cout << "nanoVaultDb> ";
string sql = readLineWithHistory(history, historyIndex);
if (sql == "exit" || sql == "quit")
break;
if (!sql.empty()) {
history.push_back(sql);
historyIndex = history.size();
}
while (sql.find(';') == string::npos) {
cout << " ...> ";
string more = readLineWithHistory(history, historyIndex);
if (!more.empty()) {
history.push_back(more);
historyIndex = history.size();
}
sql += "\n" + more;
}
try {
// logging(sql);
Lexer lexer(sql);
vector<Token *> tokens = lexer.tokenize();
for (Token *token : tokens) {
cout << typeToString(token->TYPE) << " : " << token->VALUE << endl;
}
Parser parser(tokens);
std::string output = parser.parse();
std::cout << output << "\n";
clear_log();
} catch (const std::exception &e) {
cerr << "Error: " << e.what() << endl;
}
}
return 0;
}