Refactored the database init code.
Project: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/commit/f673f945 Tree: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/tree/f673f945 Diff: http://git-wip-us.apache.org/repos/asf/incubator-quickstep/diff/f673f945 Branch: refs/heads/exact-filter Commit: f673f94561e104c62d0441f1ded04737e23b0ba0 Parents: 54e0654 Author: Zuyu Zhang <zu...@apache.org> Authored: Sun Nov 20 23:55:22 2016 -0800 Committer: Zuyu Zhang <zu...@apache.org> Committed: Sun Nov 20 23:55:22 2016 -0800 ---------------------------------------------------------------------- cli/CMakeLists.txt | 8 +++- cli/DefaultsConfigurator.cpp | 77 +++++++++++++++++++++++++++++++++++++++ cli/DefaultsConfigurator.hpp | 10 +++++ cli/QuickstepCli.cpp | 36 +----------------- 4 files changed, 94 insertions(+), 37 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f673f945/cli/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 9847787..9b62af9 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -75,7 +75,7 @@ else() quickstep_utility_Macros) endif() -add_library(quickstep_cli_DefaultsConfigurator ../empty_src.cpp DefaultsConfigurator.hpp) +add_library(quickstep_cli_DefaultsConfigurator DefaultsConfigurator.cpp DefaultsConfigurator.hpp) add_library(quickstep_cli_InputParserUtil InputParserUtil.cpp InputParserUtil.hpp) add_library(quickstep_cli_PrintToScreen PrintToScreen.cpp PrintToScreen.hpp) @@ -106,10 +106,14 @@ target_link_libraries(quickstep_cli_CommandExecutor quickstep_utility_SqlError) target_link_libraries(quickstep_cli_DefaultsConfigurator + glog + quickstep_catalog_Catalog + quickstep_catalog_Catalog_proto + quickstep_catalog_CatalogDatabase quickstep_utility_Macros) if(QUICKSTEP_HAVE_LIBNUMA) target_link_libraries(quickstep_cli_DefaultsConfigurator - ${LIBNUMA_LIBRARY}) + ${LIBNUMA_LIBRARY}) endif() target_link_libraries(quickstep_cli_Flags quickstep_cli_DefaultsConfigurator http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f673f945/cli/DefaultsConfigurator.cpp ---------------------------------------------------------------------- diff --git a/cli/DefaultsConfigurator.cpp b/cli/DefaultsConfigurator.cpp new file mode 100644 index 0000000..355031c --- /dev/null +++ b/cli/DefaultsConfigurator.cpp @@ -0,0 +1,77 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 "cli/DefaultsConfigurator.hpp" + +#include "cli/CliConfig.h" // For QUICKSTEP_OS_WINDOWS. + +#ifdef QUICKSTEP_OS_WINDOWS +#include <experimental/filesystem> +#else +#include <cstdlib> +#endif // QUICKSTEP_OS_WINDOWS + +#include <fstream> +#include <string> + +#include "catalog/Catalog.hpp" +#include "catalog/Catalog.pb.h" +#include "catalog/CatalogDatabase.hpp" + +#include "glog/logging.h" + +using std::string; + +namespace quickstep { + +void DefaultsConfigurator::InitializeDefaultDatabase(const string &storage_path, const string &catalog_path) { + // TODO(jmp): Refactor the code in this file! + LOG(INFO) << "Initializing the database, creating a new catalog file and storage directory"; + + // Create the directory + // TODO(jmp): At some point, likely in C++-17, we will just have the + // filesystem path, and we can clean this up +#ifdef QUICKSTEP_OS_WINDOWS + CHECK(std::experimental::filesystem::create_directories(storage_path)) + << "Failed when attempting to create the directory: " << storage_path + << "\nCheck if the directory already exists. If so, delete it or move it before initializing"; +#else + { + const string path_name = "mkdir " + storage_path; + CHECK(std::system(path_name.c_str())) + << "Failed when attempting to create the directory: " << storage_path; + } +#endif // QUICKSTEP_OS_WINDOWS + + // Create the default catalog file. + std::ofstream catalog_file(catalog_path.c_str()); + CHECK(catalog_file.good()) + << "ERROR: Unable to open " << catalog_path << " for writing."; + + Catalog catalog; + catalog.addDatabase(new CatalogDatabase(nullptr, "default")); + + CHECK(catalog.getProto().SerializeToOstream(&catalog_file)) + << "ERROR: Unable to serialize catalog proto to file " << catalog_path; + + // Close the catalog file - it will be reopened below by the QueryProcessor. + catalog_file.close(); +} + +} // namespace quickstep http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f673f945/cli/DefaultsConfigurator.hpp ---------------------------------------------------------------------- diff --git a/cli/DefaultsConfigurator.hpp b/cli/DefaultsConfigurator.hpp index 4fd62ac..4b534d6 100644 --- a/cli/DefaultsConfigurator.hpp +++ b/cli/DefaultsConfigurator.hpp @@ -27,6 +27,7 @@ #endif // QUICKSTEP_HAVE_LIBNUMA #include <cstddef> +#include <string> #include <thread> // NOLINT(build/c++11) #ifdef QUICKSTEP_HAVE_LIBNUMA @@ -106,6 +107,15 @@ class DefaultsConfigurator { return 1; } + /** + * @brief Initialize the default database with no relations. + * + * @param storage_path The filesystem directory to store catalog. + * @param catalog_path The full path of the catalog file. + **/ + static void InitializeDefaultDatabase(const std::string &storage_path, + const std::string &catalog_path); + private: /** * @brief Private constructor to disable instantiation of the class. http://git-wip-us.apache.org/repos/asf/incubator-quickstep/blob/f673f945/cli/QuickstepCli.cpp ---------------------------------------------------------------------- diff --git a/cli/QuickstepCli.cpp b/cli/QuickstepCli.cpp index 6a7c500..465793c 100644 --- a/cli/QuickstepCli.cpp +++ b/cli/QuickstepCli.cpp @@ -216,41 +216,7 @@ int main(int argc, char* argv[]) { string catalog_path(FLAGS_storage_path); catalog_path.append(kCatalogFilename); if (quickstep::FLAGS_initialize_db) { // Initialize the database - // TODO(jmp): Refactor the code in this file! - LOG(INFO) << "Initializing the database, creating a new catalog file and storage directory\n"; - - // Create the directory - // TODO(jmp): At some point, likely in C++-17, we will just have the - // filesystem path, and we can clean this up -#ifdef QUICKSTEP_OS_WINDOWS - std::filesystem::create_directories(FLAGS_storage_path); - LOG(FATAL) << "Failed when attempting to create the directory: " << FLAGS_storage_path << "\n"; - LOG(FATAL) << "Check if the directory already exists. If so, delete it or move it before initializing \n"; -#else - { - string path_name = "mkdir " + FLAGS_storage_path; - if (std::system(path_name.c_str())) { - LOG(FATAL) << "Failed when attempting to create the directory: " << FLAGS_storage_path << "\n"; - } - } -#endif - - // Create the default catalog file. - std::ofstream catalog_file(catalog_path); - if (!catalog_file.good()) { - LOG(FATAL) << "ERROR: Unable to open " << kCatalogFilename << " for writing.\n"; - } - - quickstep::Catalog catalog; - catalog.addDatabase(new quickstep::CatalogDatabase(nullptr, "default")); - - if (!catalog.getProto().SerializeToOstream(&catalog_file)) { - LOG(FATAL) << "ERROR: Unable to serialize catalog proto to file " << kCatalogFilename; - return 1; - } - - // Close the catalog file - it will be reopened below by the QueryProcessor. - catalog_file.close(); + DefaultsConfigurator::InitializeDefaultDatabase(FLAGS_storage_path, catalog_path); } // Setup QueryProcessor, including CatalogDatabase.