forked from PUC-IIC3413/2022-1-MilleniumDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_db.cc
99 lines (83 loc) · 3.36 KB
/
create_db.cc
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
#include <chrono>
#include <climits>
#include <experimental/filesystem>
#include <iostream>
#include <boost/program_options.hpp>
#include "base/binding/binding_id.h"
#include "base/ids/var_id.h"
#include "execution/binding_id_iter/index_nested_loop_join.h"
#include "execution/binding_id_iter/index_scan.h"
#include "import/quad_model/bulk_import.h"
#include "query_optimizer/quad_model/quad_model.h"
#include "storage/buffer_manager.h"
#include "storage/file_manager.h"
using namespace std;
namespace po = boost::program_options;
int main(int argc, char **argv) {
string input_filename;
string db_folder;
int buffer_size;
try {
// Parse arguments
po::options_description desc("Allowed options");
desc.add_options()
("help,h", "show this help message")
("db-folder,d", po::value<string>(&db_folder)->required(), "set database folder path")
("buffer-size,b", po::value<int>(&buffer_size)->default_value(BufferManager::DEFAULT_SHARED_BUFFER_POOL_SIZE),
"set buffer pool size")
("filename,f", po::value<string>(&input_filename)->required(), "import filename")
;
po::positional_options_description p;
p.add("filename", 1);
p.add("db-folder", 1);
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm);
if (vm.count("help")) {
cout << "Usage: create_db ./path/to/import_file.txt ./path/to/new_db [OPTIONS]\n";
cout << desc << "\n";
return 0;
}
po::notify(vm);
// Validate params
if (buffer_size < 0) {
cerr << "Buffer size cannot be a negative number.\n";
return 1;
}
{ // check db_folder is empty or does not exists
namespace fs = std::experimental::filesystem;
if (fs::exists(db_folder) && !fs::is_empty(db_folder)) {
cout << "Database folder already exists and it's not empty\n";
return EXIT_FAILURE;
}
}
cout << "Creating new database\n";
cout << " input file: " << input_filename << "\n";
cout << " db folder: " << db_folder << "\n";
cout << " buffer size: " << buffer_size << "\n\n";
auto start = chrono::system_clock::now();
cout << "Initializing system...\n";
{
// will initialize the model. When it comes out of scope the model is destroyed.
auto model_destroyer = QuadModel::init(db_folder, buffer_size, 0, 0);
// to measure time initializing the model
auto end_model = chrono::system_clock::now();
chrono::duration<float, milli> model_duration = end_model - start;
cout << " done in " << model_duration.count() << " ms\n\n";
// start the import
auto import = BulkImport(input_filename);
import.start_import();
}
auto end = chrono::system_clock::now();
chrono::duration<float, milli> duration = end - start;
cout << "Total duration: " << duration.count() << " ms\n";
return EXIT_SUCCESS;
}
catch (exception& e) {
cerr << "Exception: " << e.what() << "\n";
return EXIT_FAILURE;
}
catch (...) {
cerr << "Exception of unknown type!\n";
return EXIT_FAILURE;
}
}