diff --git a/CHANGELOG.md b/CHANGELOG.md index 4aac20c..ef11029 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,7 @@ release. ### Added - Added relative kernel path support for furnishing [#100](https://github.com/DOI-USGS/SpiceQL/pull/100) +- Added get/set cache dir functions [#104](https://github.com/DOI-USGS/SpiceQL/pull/104) ## 1.2.4 diff --git a/SpiceQL/include/inventory.h b/SpiceQL/include/inventory.h index 57f77a7..7e7f44f 100644 --- a/SpiceQL/include/inventory.h +++ b/SpiceQL/include/inventory.h @@ -25,7 +25,8 @@ namespace SpiceQL { nlohmann::json search_for_kernelset_from_regex(std::vector list, bool full_kernel_path=false); std::string getDbFilePath(); - + void setDbFilePath(std::string db_file_path, bool override=false); + void create_database(std::vector mlist = {}); } } \ No newline at end of file diff --git a/SpiceQL/include/inventoryimpl.h b/SpiceQL/include/inventoryimpl.h index abdb606..96c0a0c 100644 --- a/SpiceQL/include/inventoryimpl.h +++ b/SpiceQL/include/inventoryimpl.h @@ -25,7 +25,8 @@ namespace SpiceQL { extern std::string DB_SS_TIME_INDICES_KEY; extern std::string DB_SPICE_ROOT_KEY; - std::string getCacheDir(); + std::string getCacheDir(); + void setCacheDir(std::string cache_dir, bool override=false); std::string getHdfFile(); class TimeIndexedKernels { diff --git a/SpiceQL/include/spiceql.h b/SpiceQL/include/spiceql.h index 864e337..9303457 100644 --- a/SpiceQL/include/spiceql.h +++ b/SpiceQL/include/spiceql.h @@ -12,3 +12,4 @@ #include "io.h" #include "query.h" #include "api.h" +#include "inventory.h" diff --git a/SpiceQL/src/inventory.cpp b/SpiceQL/src/inventory.cpp index d103a6d..608afbb 100644 --- a/SpiceQL/src/inventory.cpp +++ b/SpiceQL/src/inventory.cpp @@ -144,12 +144,29 @@ namespace SpiceQL { return kernels; } + + /** + * @brief Get the file path to the database file + * + * @return string + */ string getDbFilePath() { static string db_path = fs::path(getCacheDir()) / DB_HDF_FILE; SPDLOG_TRACE("db_path: {}", db_path); return db_path; } - + + + /** + * @brief Set the file path to the database file + * + * @param db_file_path The path to the database file + * @param override If true, the database file path will be set to the provided path regardless of the environment variable + */ + void setDbFilePath(string db_file_path, bool override) { + setCacheDir(db_file_path, override); + } + void create_database(vector mlist) { // force generate the database InventoryImpl db(true, mlist); diff --git a/SpiceQL/src/inventoryimpl.cpp b/SpiceQL/src/inventoryimpl.cpp index b0dae6b..9e43c23 100644 --- a/SpiceQL/src/inventoryimpl.cpp +++ b/SpiceQL/src/inventoryimpl.cpp @@ -17,7 +17,7 @@ #include #include -#include +#include #include "config.h" #include "inventoryimpl.h" @@ -47,23 +47,58 @@ namespace SpiceQL { string DB_STOP_TIME_KEY = "stoptime"; string DB_TIME_FILES_KEY = "path_index"; string DB_START_TIME_INDICES_KEY = "start_kindex"; - string DB_STOP_TIME_INDICES_KEY = "stop_kindex"; + string DB_STOP_TIME_INDICES_KEY = "stop_kindex"; + string CACHE_DIR_ENV_VAR = "SPICEQL_CACHE_DIR"; + static std::string CACHE_DIRECTORY = ""; + + + void setCacheDir(string cache_dir, bool override) { + + const char* cache_dir_char = getenv(CACHE_DIR_ENV_VAR.c_str()); + if (cache_dir_char == NULL || override) { + SPDLOG_DEBUG("Setting cache directory to: {}", cache_dir); + CACHE_DIRECTORY = cache_dir; + } + else if (cache_dir_char != NULL) { + SPDLOG_DEBUG("Cache directory set in environment variable " + CACHE_DIR_ENV_VAR + ": " + cache_dir_char); + CACHE_DIRECTORY = cache_dir_char; + } + else { + SPDLOG_DEBUG("Cache directory not set and not in environment variable " + CACHE_DIR_ENV_VAR + " and not overridden."); + std::string tempname = "spiceql-cache-" + gen_random(10); + CACHE_DIRECTORY = fs::temp_directory_path() / tempname / "spiceql_cache"; + } + + if (!fs::is_directory(CACHE_DIRECTORY)) { + SPDLOG_DEBUG("{} does not exist, attempting to create the directory", CACHE_DIRECTORY); + fs::create_directories(CACHE_DIRECTORY); + } + + SPDLOG_DEBUG("Setting cache directory to: {}", CACHE_DIRECTORY); + } string getCacheDir() { - static std::string CACHE_DIRECTORY = ""; if (CACHE_DIRECTORY == "") { - const char* cache_dir_char = getenv("SPICEQL_CACHE_DIR"); + const char* cache_dir_char = getenv(CACHE_DIR_ENV_VAR.c_str()); std::string cache_dir; - if (cache_dir_char == NULL) { - std::string tempname = "spiceql-cache-" + gen_random(10); - cache_dir = fs::temp_directory_path() / tempname / "spiceql_cache"; + if (cache_dir_char == NULL && (CACHE_DIRECTORY == "")) { + SPDLOG_DEBUG("Cache directory not set and not in environment variable " + CACHE_DIR_ENV_VAR + " and not overridden."); + throw runtime_error("Cache directory not set and not in environment variable " + CACHE_DIR_ENV_VAR + " and not overridden."); + } + else if (CACHE_DIRECTORY != "") { + SPDLOG_DEBUG("Cache directory set in CACHE_DIRECTORY: " + CACHE_DIRECTORY); + cache_dir = CACHE_DIRECTORY; + } + else if (cache_dir_char != NULL) { + SPDLOG_DEBUG("Cache directory set in environment variable " + CACHE_DIR_ENV_VAR + ": " + cache_dir_char); + cache_dir = cache_dir_char; } else { - cache_dir = cache_dir_char; + throw runtime_error("Cache directory not set and not in environment variable " + CACHE_DIR_ENV_VAR + " and not overridden."); } if (!fs::is_directory(cache_dir)) { diff --git a/SpiceQL/src/utils.cpp b/SpiceQL/src/utils.cpp index b4c61eb..0370184 100644 --- a/SpiceQL/src/utils.cpp +++ b/SpiceQL/src/utils.cpp @@ -1001,7 +1001,7 @@ namespace SpiceQL { SPDLOG_TRACE("SpiceQL DB Path: {}", dbPath.string()); if (!fs::is_directory(dbPath)) { - throw runtime_error("Config Directory Not Found."); + throw runtime_error("Config Directory " + dbPath.string() + " Not Found."); } return dbPath; diff --git a/SpiceQL/tests/InventoryTests.cpp b/SpiceQL/tests/InventoryTests.cpp index da9e63e..9b0a10e 100644 --- a/SpiceQL/tests/InventoryTests.cpp +++ b/SpiceQL/tests/InventoryTests.cpp @@ -13,6 +13,7 @@ #include #include + TEST_F(LroKernelSet, TestInventorySmithed) { Inventory::create_database(); nlohmann::json kernels = Inventory::search_for_kernelset("lroc", {"fk", "sclk", "spk", "ck"}, 110000000, 140000000, {"smithed", "reconstructed"}, {"smithed", "reconstructed"}, false); @@ -139,4 +140,21 @@ TEST_F(LroKernelSet, TestInventorySearchSetFromRegex) { EXPECT_EQ(kernels["spk"].size(), 3); EXPECT_EQ(kernels["spk_quality"].get(), "smithed"); EXPECT_EQ(fs::path(kernels["pck"][0].get()).filename(), "moon_080317.tf"); +} + +TEST_F(TempTestingFiles, TestInventorySetCacheDir) { + SpiceQL::Inventory::setDbFilePath(tempDir.string()); + const char* cache_dir = getenv("SPICEQL_CACHE_DIR"); + EXPECT_EQ(SpiceQL::Inventory::getDbFilePath(), fs::path(cache_dir)/"spiceqldb.hdf"); +} + +TEST_F(TempTestingFiles, TestInventorySetCacheDirOverride) { + fs::path new_path = fs::path(tempDir.string())/"new_path"; + SpiceQL::Inventory::setDbFilePath(new_path.string(), true); + EXPECT_EQ(SpiceQL::Inventory::getDbFilePath(), new_path/"spiceqldb.hdf"); +} + +TEST_F(TempTestingFiles, TestInventorySetCacheDirFail) { + unsetenv("SPICEQL_CACHE_DIR"); + EXPECT_THROW(SpiceQL::Inventory::getDbFilePath(), runtime_error); } \ No newline at end of file