Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion SpiceQL/include/inventory.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ namespace SpiceQL {
nlohmann::json search_for_kernelset_from_regex(std::vector<std::string> list, bool full_kernel_path=false);

std::string getDbFilePath();

void setDbFilePath(std::string db_file_path, bool override=false);

void create_database(std::vector<std::string> mlist = {});
}
}
3 changes: 2 additions & 1 deletion SpiceQL/include/inventoryimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions SpiceQL/include/spiceql.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
#include "io.h"
#include "query.h"
#include "api.h"
#include "inventory.h"
19 changes: 18 additions & 1 deletion SpiceQL/src/inventory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> mlist) {
// force generate the database
InventoryImpl db(true, mlist);
Expand Down
51 changes: 43 additions & 8 deletions SpiceQL/src/inventoryimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <cereal/types/utility.hpp>

#include <highfive/H5Easy.hpp>
#include <highfive/highfive.hpp>
#include <highfive/highfive.hpp>

#include "config.h"
#include "inventoryimpl.h"
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion SpiceQL/src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions SpiceQL/tests/InventoryTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <spdlog/spdlog.h>
#include <highfive/highfive.hpp>


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);
Expand Down Expand Up @@ -139,4 +140,21 @@ TEST_F(LroKernelSet, TestInventorySearchSetFromRegex) {
EXPECT_EQ(kernels["spk"].size(), 3);
EXPECT_EQ(kernels["spk_quality"].get<string>(), "smithed");
EXPECT_EQ(fs::path(kernels["pck"][0].get<string>()).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);
}
Loading