forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitdb.cpp
More file actions
166 lines (148 loc) · 5.68 KB
/
initdb.cpp
File metadata and controls
166 lines (148 loc) · 5.68 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
/*
* Copyright 2017 MapD Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <array>
#include <boost/filesystem.hpp>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include "Catalog/Catalog.h"
#include "Import/Importer.h"
#include "Shared/mapdpath.h"
#include "boost/program_options.hpp"
#define CALCITEPORT 39093
static const std::array<std::string, 3> SampleGeoFileNames{"us-states.json",
"us-counties.json",
"countries.json"};
static const std::array<std::string, 3> SampleGeoTableNames{"omnisci_states",
"omnisci_counties",
"omnisci_countries"};
int main(int argc, char* argv[]) {
std::string base_path;
bool force = false;
bool skip_geo = false;
namespace po = boost::program_options;
google::InitGoogleLogging(argv[0]);
po::options_description desc("Options");
desc.add_options()("help,h", "Print help messages ")(
"data",
po::value<std::string>(&base_path)->required(),
"Directory path to MapD catalogs")("force,f",
"Force overwriting of existing MapD instance")(
"skip-geo", "Skip inserting sample geo data");
po::positional_options_description positionalOptions;
positionalOptions.add("data", 1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv)
.options(desc)
.positional(positionalOptions)
.run(),
vm);
if (vm.count("help")) {
std::cout << "Usage: initdb [-f] <catalog path>\n";
return 0;
}
if (vm.count("force")) {
force = true;
}
if (vm.count("skip-geo")) {
skip_geo = true;
}
po::notify(vm);
} catch (boost::program_options::error& e) {
std::cerr << "Usage Error: " << e.what() << std::endl;
return 1;
}
if (!boost::filesystem::exists(base_path)) {
std::cerr << "Catalog basepath " + base_path + " does not exist.\n";
return 1;
}
std::string catalogs_path = base_path + "/mapd_catalogs";
if (boost::filesystem::exists(catalogs_path)) {
if (force) {
boost::filesystem::remove_all(catalogs_path);
} else {
std::cerr << "MapD catalogs already initialized at " + base_path +
". Use -f to force reinitialization.\n";
return 1;
}
}
std::string data_path = base_path + "/mapd_data";
if (boost::filesystem::exists(data_path)) {
if (force) {
boost::filesystem::remove_all(data_path);
} else {
std::cerr << "MapD data directory already exists at " + base_path +
". Use -f to force reinitialization.\n";
return 1;
}
}
std::string export_path = base_path + "/mapd_export";
if (boost::filesystem::exists(export_path)) {
if (force) {
boost::filesystem::remove_all(export_path);
} else {
std::cerr << "MapD export directory already exists at " + base_path +
". Use -f to force reinitialization.\n";
return 1;
}
}
if (!boost::filesystem::create_directory(catalogs_path)) {
std::cerr << "Cannot create mapd_catalogs subdirectory under " << base_path
<< std::endl;
}
if (!boost::filesystem::create_directory(export_path)) {
std::cerr << "Cannot create mapd_export subdirectory under " << base_path
<< std::endl;
}
try {
MapDParameters mapd_parms;
auto dummy =
std::make_shared<Data_Namespace::DataMgr>(data_path, mapd_parms, false, 0);
auto calcite = std::make_shared<Calcite>(-1, CALCITEPORT, base_path, 1024);
auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
sys_cat.init(base_path, dummy, {}, calcite, true, false);
if (!skip_geo) {
// Add geo samples to the system database using the root user
Catalog_Namespace::DBMetadata cur_db;
const std::string db_name(MAPD_SYSTEM_DB);
CHECK(sys_cat.getMetadataForDB(db_name, cur_db));
auto cat = std::make_shared<Catalog_Namespace::Catalog>(
base_path, cur_db, dummy, std::vector<LeafHostInfo>(), calcite);
Catalog_Namespace::Catalog::set(db_name, cat);
Catalog_Namespace::UserMetadata user;
CHECK(sys_cat.getMetadataForUser(MAPD_ROOT_USER, user));
Importer_NS::ImportDriver import_driver(cat, user);
const size_t num_samples = SampleGeoFileNames.size();
for (size_t i = 0; i < num_samples; i++) {
const std::string table_name = SampleGeoTableNames[i];
const std::string file_name = SampleGeoFileNames[i];
const auto file_path = boost::filesystem::path(
mapd_root_abs_path() + "/ThirdParty/geo_samples/" + file_name);
if (!boost::filesystem::exists(file_path)) {
throw std::runtime_error(
"Unable to populate geo sample data. File does not exist: " +
file_path.string());
}
import_driver.import_geo_table(file_path.string(), table_name);
}
}
} catch (std::exception& e) {
std::cerr << "Exception: " << e.what() << "\n";
}
return 0;
}