diff --git a/CMakeLists.txt b/CMakeLists.txt index 4b61d43c5..568045d5d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,6 +24,7 @@ set(storagemanager_SRCS src/CloudStorage.cpp src/S3Storage.cpp src/LocalStorage.cpp + src/Cache.cpp ) option(TRACE "Enable some tracing output" OFF) diff --git a/src/Cache.cpp b/src/Cache.cpp new file mode 100644 index 000000000..991949164 --- /dev/null +++ b/src/Cache.cpp @@ -0,0 +1,79 @@ + +#include "Cache.h" +#include "Config.h" +#include +#include +#include + +using namespace std; +using namespace boost::filesystem; + +namespace storagemanager +{ + +Cache::Cache() +{ + Config *conf = Config::get(); + + string ssize = conf->getValue("Cache", "cache_size"); + if (ssize.empty()) + { + syslog(LOG_CRIT, "Cache/cache_size is not set"); + throw runtime_error("Please set Cache/cache_size in the storagemanager.cnf file"); + } + try + { + maxCacheSize = stol(ssize); + } + catch (invalid_argument &) + { + syslog(LOG_CRIT, "Cache/cache_size is not a number"); + throw runtime_error("Please set Cache/cache_size to a number"); + } + + prefix = conf->getValue("Cache", "path"); + if (prefix.empty()) + { + syslog(LOG_CRIT, "Cache/path is not set"); + throw runtime_error("Please set Cache/path in the storagemanager.cnf file"); + } + try + { + boost::filesystem::create_directories(prefix); + } + catch (exception &e) + { + syslog(LOG_CRIT, "Failed to create %s, got: %s", prefix.string().c_str(), e.what()); + throw e; + } +} + +Cache::~Cache() +{ +} + +void Cache::read(const vector &keys) +{ +} + +void Cache::exists(const vector &keys, vector *out) +{ +} + +void Cache::newObject(const string &key, size_t size) +{ +} + +void Cache::deletedObject(const string &key, size_t size) +{ +} + +void Cache::setCacheSize(size_t size) +{ +} + +void Cache::makeSpace(size_t size) +{ +} + +} diff --git a/src/Cache.h b/src/Cache.h new file mode 100644 index 000000000..a32cac474 --- /dev/null +++ b/src/Cache.h @@ -0,0 +1,34 @@ + +#ifndef CACHE_H_ +#define CACHE_H_ + +#include +#include +#include +#include + +namespace storagemanager +{ + +class Cache : public boost::noncopyable +{ + public: + Cache(); + virtual ~Cache(); + + void read(const std::vector &keys); + void exists(const std::vector &keys, std::vector *out); + void newObject(const std::string &key, size_t size); + void deletedObject(const std::string &key, size_t size); + void setCacheSize(size_t size); + void makeSpace(size_t size); + + private: + boost::filesystem::path prefix; + size_t maxCacheSize; + +}; + +} + +#endif diff --git a/src/Config.cpp b/src/Config.cpp index 7ba674370..3cac02ac7 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -63,12 +63,26 @@ Config::Config() boost::property_tree::ini_parser::read_ini(filename, contents); } -string use_envvar(boost::smatch envvar) +string use_envvar(const boost::smatch &envvar) { char *env = getenv(envvar[1].str().c_str()); return (env ? env : ""); } +string expand_numbers(const boost::smatch &match) +{ + long num = stol(match[1].str()); + char suffix = (char) ::tolower(match[2].str()[0]); + + if (suffix == 'g') + num <<= 30; + else if (suffix == 'm') + num <<= 20; + else if (suffix == 'k') + num <<= 10; + return ::to_string(num); +} + string Config::getValue(const string §ion, const string &key) const { // if we care, move this envvar substition stuff to where the file is loaded @@ -76,6 +90,13 @@ string Config::getValue(const string §ion, const string &key) const boost::regex re("\\$\\{(.+)\\}"); ret = boost::regex_replace(ret, re, use_envvar); + + // do the numeric substitutions. ex, the suffixes m, k, g + // ehhhhh. going to end up turning a string to a number, to a string, and then to a number again + // don't like that. OTOH who cares. + boost::regex num_re("^([[:digit:]]+)([mMkKgG])$", boost::regex::extended); + ret = boost::regex_replace(ret, num_re, expand_numbers); + return ret; } diff --git a/storagemanager.cnf b/storagemanager.cnf index 50059313b..212a6ae3d 100644 --- a/storagemanager.cnf +++ b/storagemanager.cnf @@ -12,5 +12,9 @@ region = us-west-1 # see https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html [LocalStorage] -path = ${HOME}/fake-cloud/ +path = ${HOME}/fake-cloud + +[Cache] +cache_size = 2g +path = ${HOME}/cache