-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTinyDB.cpp
83 lines (78 loc) · 2.82 KB
/
TinyDB.cpp
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
#include ".\include\REPL.hpp"
REPL repl;
Runtime runtime;
int main()
{
repl.welcome();
while (true)
{
repl.start();
std::string now_input = repl.get_now_input();
if (now_input.empty())
{
std::cout << std::endl;
continue;
}
auto timer_begin = std::chrono::high_resolution_clock::now();
if (now_input[0] == '.')
{
if (!repl.parse_meta_cmd(now_input))
break;
if (now_input == ".load")
{
std::string filepath;
std::cin >> filepath;
std::ifstream infile(filepath);
std::string line;
int all = 0, success = 0;
std::vector<std::string> result;
while (std::getline(infile, line))
{
if (line.empty())
break;
all++;
Runtime::Statement statement = REPL::parse_statement(line);
if (!Runtime::valid_statement(statement))
continue;
result = runtime.run_statement(statement);
success++;
if (success % 10000 == 0)
std::cout << "Loading... " << success << " of " << all << " lines." << std::endl;
}
infile.close();
std::thread worker(&Runtime::scheduler, &runtime);
worker.detach();
std::cout << "-------------" << std::endl;
std::cout << "Task finished" << std::endl;
std::cout << "Load " << success << " of " << all << " lines." << std::endl;
for (auto &x : result)
std::cout << x << std::endl;
}
}
else
{
Runtime::Statement statement = REPL::parse_statement(now_input);
if (!Runtime::valid_statement(statement))
continue;
std::vector<std::string> result = runtime.run_statement(statement);
for (auto &x : result)
std::cout << x << std::endl;
if (statement.opt != Runtime::Operation::SELECT)
{
std::thread worker(&Runtime::scheduler, &runtime);
worker.detach();
}
}
#ifdef DEBUG
assert(runtime.check());
#endif
auto timer_end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(timer_end - timer_begin);
std::cout << "Time measured: " << duration.count() * 1e-3 << " ms." << std::endl;
std::cout << "-------------" << std::endl;
}
if (!runtime.is_finish())
runtime.scheduler();
std::cout << "Goodbye TinyDB!" << std::endl;
return 0;
}