OmniSciDB  8a228a1076
/home/jenkins-slave/workspace/core-os-doxygen/initdb.cpp File Reference
#include <thrift/Thrift.h>
#include <array>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include "Catalog/Catalog.h"
#include "ImportExport/Importer.h"
#include "Logger/Logger.h"
#include "QueryRunner/QueryRunner.h"
#include "Shared/mapdpath.h"
+ Include dependency graph for initdb.cpp:

Go to the source code of this file.

Macros

#define CALCITEPORT   3279
 

Functions

int main (int argc, char *argv[])
 

Variables

static const std::array< std::string, 3 > SampleGeoFileNames
 
static const std::array< std::string, 3 > SampleGeoTableNames
 
bool g_enable_thrift_logs {false}
 

Macro Definition Documentation

◆ CALCITEPORT

#define CALCITEPORT   3279

Definition at line 32 of file initdb.cpp.

Referenced by EmbeddedDatabase::DBEngineImpl::DBEngineImpl(), and main().

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 43 of file initdb.cpp.

References CALCITEPORT, cat(), CHECK, g_enable_thrift_logs, Catalog_Namespace::Catalog::get(), logger::LogOptions::get_options(), logger::init(), Catalog_Namespace::SysCatalog::instance(), mapd_root_abs_path(), OMNISCI_DEFAULT_DB, OMNISCI_ROOT_USER, run, SampleGeoFileNames, SampleGeoTableNames, and logger::LogOptions::set_base_path().

43  {
44  std::string base_path;
45  bool force = false;
46  bool skip_geo = false;
47  namespace po = boost::program_options;
48 
49  po::options_description desc("Options");
50  desc.add_options()("help,h", "Print help messages ")(
51  "data",
52  po::value<std::string>(&base_path)->required(),
53  "Directory path to OmniSci catalogs")(
54  "force,f", "Force overwriting of existing OmniSci instance")(
55  "skip-geo", "Skip inserting sample geo data");
56 
57  desc.add_options()("enable-thrift-logs",
58  po::value<bool>(&g_enable_thrift_logs)
59  ->default_value(g_enable_thrift_logs)
60  ->implicit_value(true),
61  "Enable writing messages directly from thrift to stdout/stderr.");
62 
63  logger::LogOptions log_options(argv[0]);
64  desc.add(log_options.get_options());
65 
66  po::positional_options_description positionalOptions;
67  positionalOptions.add("data", 1);
68 
69  po::variables_map vm;
70 
71  try {
72  po::store(po::command_line_parser(argc, argv)
73  .options(desc)
74  .positional(positionalOptions)
75  .run(),
76  vm);
77  if (vm.count("help")) {
78  std::cout << desc;
79  return 0;
80  }
81  if (vm.count("force")) {
82  force = true;
83  }
84  if (vm.count("skip-geo")) {
85  skip_geo = true;
86  }
87  po::notify(vm);
88  } catch (boost::program_options::error& e) {
89  std::cerr << "Usage Error: " << e.what() << std::endl;
90  return 1;
91  }
92 
93  if (!g_enable_thrift_logs) {
94  apache::thrift::GlobalOutput.setOutputFunction([](const char* msg) {});
95  }
96 
97  if (!boost::filesystem::exists(base_path)) {
98  std::cerr << "Catalog basepath " + base_path + " does not exist.\n";
99  return 1;
100  }
101  std::string catalogs_path = base_path + "/mapd_catalogs";
102  if (boost::filesystem::exists(catalogs_path)) {
103  if (force) {
104  boost::filesystem::remove_all(catalogs_path);
105  } else {
106  std::cerr << "OmniSci catalogs already initialized at " + base_path +
107  ". Use -f to force reinitialization.\n";
108  return 1;
109  }
110  }
111  std::string data_path = base_path + "/mapd_data";
112  if (boost::filesystem::exists(data_path)) {
113  if (force) {
114  boost::filesystem::remove_all(data_path);
115  } else {
116  std::cerr << "OmniSci data directory already exists at " + base_path +
117  ". Use -f to force reinitialization.\n";
118  return 1;
119  }
120  }
121  std::string export_path = base_path + "/mapd_export";
122  if (boost::filesystem::exists(export_path)) {
123  if (force) {
124  boost::filesystem::remove_all(export_path);
125  } else {
126  std::cerr << "OmniSci export directory already exists at " + base_path +
127  ". Use -f to force reinitialization.\n";
128  return 1;
129  }
130  }
131  if (!boost::filesystem::create_directory(catalogs_path)) {
132  std::cerr << "Cannot create mapd_catalogs subdirectory under " << base_path
133  << std::endl;
134  }
135  if (!boost::filesystem::create_directory(export_path)) {
136  std::cerr << "Cannot create mapd_export subdirectory under " << base_path
137  << std::endl;
138  }
139 
140  log_options.set_base_path(base_path);
141  logger::init(log_options);
142 
143  try {
144  SystemParameters sys_parms;
145  auto dummy =
146  std::make_shared<Data_Namespace::DataMgr>(data_path, sys_parms, false, 0);
147  auto calcite =
148  std::make_shared<Calcite>(-1, CALCITEPORT, base_path, 1024, 5000, true, "");
149  auto& sys_cat = Catalog_Namespace::SysCatalog::instance();
150  sys_cat.init(base_path, dummy, {}, calcite, true, false, {});
151 
152  if (!skip_geo) {
153  // Add geo samples to the system database using the root user
155  const std::string db_name(OMNISCI_DEFAULT_DB);
156  CHECK(sys_cat.getMetadataForDB(db_name, cur_db));
158  base_path, cur_db, dummy, std::vector<LeafHostInfo>(), calcite, false);
160  CHECK(sys_cat.getMetadataForUser(OMNISCI_ROOT_USER, user));
161 
162  QueryRunner::ImportDriver import_driver(cat, user);
163 
164  const size_t num_samples = SampleGeoFileNames.size();
165  for (size_t i = 0; i < num_samples; i++) {
166  const std::string table_name = SampleGeoTableNames[i];
167  const std::string file_name = SampleGeoFileNames[i];
168 
169  const auto file_path = boost::filesystem::path(
170  mapd_root_abs_path() + "/ThirdParty/geo_samples/" + file_name);
171  if (!boost::filesystem::exists(file_path)) {
172  throw std::runtime_error(
173  "Unable to populate geo sample data. File does not exist: " +
174  file_path.string());
175  }
176 
177  import_driver.importGeoTable(file_path.string(), table_name, true, true, false);
178  }
179  }
180 
181  } catch (std::exception& e) {
182  std::cerr << "Exception: " << e.what() << "\n";
183  }
184  return 0;
185 }
static std::shared_ptr< Catalog > get(const std::string &dbName)
Definition: Catalog.cpp:3671
std::string mapd_root_abs_path()
Definition: mapdpath.h:30
static const std::array< std::string, 3 > SampleGeoFileNames
Definition: initdb.cpp:34
static SysCatalog & instance()
Definition: SysCatalog.h:288
std::string cat(Ts &&... args)
void init(LogOptions const &log_opts)
Definition: Logger.cpp:280
const std::string OMNISCI_DEFAULT_DB
Definition: SysCatalog.h:58
static const std::array< std::string, 3 > SampleGeoTableNames
Definition: initdb.cpp:37
bool g_enable_thrift_logs
Definition: initdb.cpp:41
const std::string OMNISCI_ROOT_USER
Definition: SysCatalog.h:59
#define CALCITEPORT
Definition: initdb.cpp:32
#define CHECK(condition)
Definition: Logger.h:197
static bool run
+ Here is the call graph for this function:

Variable Documentation

◆ g_enable_thrift_logs

bool g_enable_thrift_logs {false}

Definition at line 41 of file initdb.cpp.

Referenced by main().

◆ SampleGeoFileNames

const std::array<std::string, 3> SampleGeoFileNames
static
Initial value:
{"us-states.json",
"us-counties.json",
"countries.json"}

Definition at line 34 of file initdb.cpp.

Referenced by main().

◆ SampleGeoTableNames

const std::array<std::string, 3> SampleGeoTableNames
static
Initial value:
{"omnisci_states",
"omnisci_counties",
"omnisci_countries"}

Definition at line 37 of file initdb.cpp.

Referenced by main().