|
17 | 17 | #include <cereal/types/utility.hpp> |
18 | 18 |
|
19 | 19 | #include <highfive/H5Easy.hpp> |
20 | | -#include <highfive/highfive.hpp> |
| 20 | +#include <highfive/highfive.hpp> |
21 | 21 |
|
22 | 22 | #include "config.h" |
23 | 23 | #include "inventoryimpl.h" |
@@ -47,23 +47,58 @@ namespace SpiceQL { |
47 | 47 | string DB_STOP_TIME_KEY = "stoptime"; |
48 | 48 | string DB_TIME_FILES_KEY = "path_index"; |
49 | 49 | 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 | + } |
51 | 79 |
|
52 | 80 |
|
53 | 81 | string getCacheDir() { |
54 | | - static std::string CACHE_DIRECTORY = ""; |
55 | 82 |
|
56 | 83 | 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()); |
58 | 85 |
|
59 | 86 | std::string cache_dir; |
60 | 87 |
|
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; |
64 | 99 | } |
65 | 100 | 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."); |
67 | 102 | } |
68 | 103 |
|
69 | 104 | if (!fs::is_directory(cache_dir)) { |
|
0 commit comments