Skip to content

Commit d2f9ee4

Browse files
authored
added cache dir set and get (#104)
* added cache dir set and get * added changelog * added inventory * oops
1 parent 2c0bf64 commit d2f9ee4

File tree

8 files changed

+86
-12
lines changed

8 files changed

+86
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ release.
4444

4545
### Added
4646
- Added relative kernel path support for furnishing [#100](https://github.com/DOI-USGS/SpiceQL/pull/100)
47+
- Added get/set cache dir functions [#104](https://github.com/DOI-USGS/SpiceQL/pull/104)
4748

4849
## 1.2.4
4950

SpiceQL/include/inventory.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ namespace SpiceQL {
2525
nlohmann::json search_for_kernelset_from_regex(std::vector<std::string> list, bool full_kernel_path=false);
2626

2727
std::string getDbFilePath();
28-
28+
void setDbFilePath(std::string db_file_path, bool override=false);
29+
2930
void create_database(std::vector<std::string> mlist = {});
3031
}
3132
}

SpiceQL/include/inventoryimpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ namespace SpiceQL {
2525
extern std::string DB_SS_TIME_INDICES_KEY;
2626
extern std::string DB_SPICE_ROOT_KEY;
2727

28-
std::string getCacheDir();
28+
std::string getCacheDir();
29+
void setCacheDir(std::string cache_dir, bool override=false);
2930
std::string getHdfFile();
3031

3132
class TimeIndexedKernels {

SpiceQL/include/spiceql.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
#include "io.h"
1313
#include "query.h"
1414
#include "api.h"
15+
#include "inventory.h"

SpiceQL/src/inventory.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,29 @@ namespace SpiceQL {
144144
return kernels;
145145
}
146146

147+
148+
/**
149+
* @brief Get the file path to the database file
150+
*
151+
* @return string
152+
*/
147153
string getDbFilePath() {
148154
static string db_path = fs::path(getCacheDir()) / DB_HDF_FILE;
149155
SPDLOG_TRACE("db_path: {}", db_path);
150156
return db_path;
151157
}
152-
158+
159+
160+
/**
161+
* @brief Set the file path to the database file
162+
*
163+
* @param db_file_path The path to the database file
164+
* @param override If true, the database file path will be set to the provided path regardless of the environment variable
165+
*/
166+
void setDbFilePath(string db_file_path, bool override) {
167+
setCacheDir(db_file_path, override);
168+
}
169+
153170
void create_database(vector<string> mlist) {
154171
// force generate the database
155172
InventoryImpl db(true, mlist);

SpiceQL/src/inventoryimpl.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <cereal/types/utility.hpp>
1818

1919
#include <highfive/H5Easy.hpp>
20-
#include <highfive/highfive.hpp>
20+
#include <highfive/highfive.hpp>
2121

2222
#include "config.h"
2323
#include "inventoryimpl.h"
@@ -47,23 +47,58 @@ namespace SpiceQL {
4747
string DB_STOP_TIME_KEY = "stoptime";
4848
string DB_TIME_FILES_KEY = "path_index";
4949
string DB_START_TIME_INDICES_KEY = "start_kindex";
50-
string DB_STOP_TIME_INDICES_KEY = "stop_kindex";
50+
string DB_STOP_TIME_INDICES_KEY = "stop_kindex";
51+
string CACHE_DIR_ENV_VAR = "SPICEQL_CACHE_DIR";
52+
static std::string CACHE_DIRECTORY = "";
53+
54+
55+
void setCacheDir(string cache_dir, bool override) {
56+
57+
const char* cache_dir_char = getenv(CACHE_DIR_ENV_VAR.c_str());
58+
if (cache_dir_char == NULL || override) {
59+
SPDLOG_DEBUG("Setting cache directory to: {}", cache_dir);
60+
CACHE_DIRECTORY = cache_dir;
61+
}
62+
else if (cache_dir_char != NULL) {
63+
SPDLOG_DEBUG("Cache directory set in environment variable " + CACHE_DIR_ENV_VAR + ": " + cache_dir_char);
64+
CACHE_DIRECTORY = cache_dir_char;
65+
}
66+
else {
67+
SPDLOG_DEBUG("Cache directory not set and not in environment variable " + CACHE_DIR_ENV_VAR + " and not overridden.");
68+
std::string tempname = "spiceql-cache-" + gen_random(10);
69+
CACHE_DIRECTORY = fs::temp_directory_path() / tempname / "spiceql_cache";
70+
}
71+
72+
if (!fs::is_directory(CACHE_DIRECTORY)) {
73+
SPDLOG_DEBUG("{} does not exist, attempting to create the directory", CACHE_DIRECTORY);
74+
fs::create_directories(CACHE_DIRECTORY);
75+
}
76+
77+
SPDLOG_DEBUG("Setting cache directory to: {}", CACHE_DIRECTORY);
78+
}
5179

5280

5381
string getCacheDir() {
54-
static std::string CACHE_DIRECTORY = "";
5582

5683
if (CACHE_DIRECTORY == "") {
57-
const char* cache_dir_char = getenv("SPICEQL_CACHE_DIR");
84+
const char* cache_dir_char = getenv(CACHE_DIR_ENV_VAR.c_str());
5885

5986
std::string cache_dir;
6087

61-
if (cache_dir_char == NULL) {
62-
std::string tempname = "spiceql-cache-" + gen_random(10);
63-
cache_dir = fs::temp_directory_path() / tempname / "spiceql_cache";
88+
if (cache_dir_char == NULL && (CACHE_DIRECTORY == "")) {
89+
SPDLOG_DEBUG("Cache directory not set and not in environment variable " + CACHE_DIR_ENV_VAR + " and not overridden.");
90+
throw runtime_error("Cache directory not set and not in environment variable " + CACHE_DIR_ENV_VAR + " and not overridden.");
91+
}
92+
else if (CACHE_DIRECTORY != "") {
93+
SPDLOG_DEBUG("Cache directory set in CACHE_DIRECTORY: " + CACHE_DIRECTORY);
94+
cache_dir = CACHE_DIRECTORY;
95+
}
96+
else if (cache_dir_char != NULL) {
97+
SPDLOG_DEBUG("Cache directory set in environment variable " + CACHE_DIR_ENV_VAR + ": " + cache_dir_char);
98+
cache_dir = cache_dir_char;
6499
}
65100
else {
66-
cache_dir = cache_dir_char;
101+
throw runtime_error("Cache directory not set and not in environment variable " + CACHE_DIR_ENV_VAR + " and not overridden.");
67102
}
68103

69104
if (!fs::is_directory(cache_dir)) {

SpiceQL/src/utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,7 @@ namespace SpiceQL {
10011001
SPDLOG_TRACE("SpiceQL DB Path: {}", dbPath.string());
10021002

10031003
if (!fs::is_directory(dbPath)) {
1004-
throw runtime_error("Config Directory Not Found.");
1004+
throw runtime_error("Config Directory " + dbPath.string() + " Not Found.");
10051005
}
10061006

10071007
return dbPath;

SpiceQL/tests/InventoryTests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <spdlog/spdlog.h>
1414
#include <highfive/highfive.hpp>
1515

16+
1617
TEST_F(LroKernelSet, TestInventorySmithed) {
1718
Inventory::create_database();
1819
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) {
139140
EXPECT_EQ(kernels["spk"].size(), 3);
140141
EXPECT_EQ(kernels["spk_quality"].get<string>(), "smithed");
141142
EXPECT_EQ(fs::path(kernels["pck"][0].get<string>()).filename(), "moon_080317.tf");
143+
}
144+
145+
TEST_F(TempTestingFiles, TestInventorySetCacheDir) {
146+
SpiceQL::Inventory::setDbFilePath(tempDir.string());
147+
const char* cache_dir = getenv("SPICEQL_CACHE_DIR");
148+
EXPECT_EQ(SpiceQL::Inventory::getDbFilePath(), fs::path(cache_dir)/"spiceqldb.hdf");
149+
}
150+
151+
TEST_F(TempTestingFiles, TestInventorySetCacheDirOverride) {
152+
fs::path new_path = fs::path(tempDir.string())/"new_path";
153+
SpiceQL::Inventory::setDbFilePath(new_path.string(), true);
154+
EXPECT_EQ(SpiceQL::Inventory::getDbFilePath(), new_path/"spiceqldb.hdf");
155+
}
156+
157+
TEST_F(TempTestingFiles, TestInventorySetCacheDirFail) {
158+
unsetenv("SPICEQL_CACHE_DIR");
159+
EXPECT_THROW(SpiceQL::Inventory::getDbFilePath(), runtime_error);
142160
}

0 commit comments

Comments
 (0)