summaryrefslogtreecommitdiff
path: root/controller/JSONDB.cpp
diff options
context:
space:
mode:
authorAdam Ierymenko <adam.ierymenko@gmail.com>2017-03-21 06:15:49 -0700
committerAdam Ierymenko <adam.ierymenko@gmail.com>2017-03-21 06:15:49 -0700
commitc62141fd9870eabf6f987da8e2a82ff5a999ddcf (patch)
treec9c50d6552d4c4895e0db2714939fd8ee9f613a0 /controller/JSONDB.cpp
parentae303ee90276f7cf85079c66d35716f80ea2321f (diff)
downloadinfinitytier-c62141fd9870eabf6f987da8e2a82ff5a999ddcf.tar.gz
infinitytier-c62141fd9870eabf6f987da8e2a82ff5a999ddcf.zip
Make controller do a simple write-through cache without revalidating. Means you must restart if files change on disk, but will decrease I/O considerably.
Diffstat (limited to 'controller/JSONDB.cpp')
-rw-r--r--controller/JSONDB.cpp63
1 files changed, 17 insertions, 46 deletions
diff --git a/controller/JSONDB.cpp b/controller/JSONDB.cpp
index 298ac721..9c28ddf1 100644
--- a/controller/JSONDB.cpp
+++ b/controller/JSONDB.cpp
@@ -53,8 +53,6 @@ bool JSONDB::put(const std::string &n,const nlohmann::json &obj)
_E &e = _db[n];
e.obj = obj;
- e.lastModifiedOnDisk = OSUtils::getLastModified(path.c_str());
- e.lastCheck = OSUtils::now();
return true;
}
@@ -64,53 +62,26 @@ const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceChe
if (!_isValidObjectName(n))
return _EMPTY_JSON;
- const uint64_t now = OSUtils::now();
- std::string buf;
std::map<std::string,_E>::iterator e(_db.find(n));
-
- if (e != _db.end()) {
- if ((now - e->second.lastCheck) <= (uint64_t)maxSinceCheck)
- return e->second.obj;
-
- const std::string path(_genPath(n,false));
- if (!path.length()) // sanity check
- return _EMPTY_JSON;
-
- // We are somewhat tolerant to momentary disk failures here. This may
- // occur over e.g. EC2's elastic filesystem (NFS).
- const uint64_t lm = OSUtils::getLastModified(path.c_str());
- if (e->second.lastModifiedOnDisk != lm) {
- if (OSUtils::readFile(path.c_str(),buf)) {
- try {
- e->second.obj = OSUtils::jsonParse(buf);
- e->second.lastModifiedOnDisk = lm; // don't update these if there is a parse error -- try again and again ASAP
- e->second.lastCheck = now;
- } catch ( ... ) {} // parse errors result in "holding pattern" behavior
- }
- }
-
+ if (e != _db.end())
return e->second.obj;
- } else {
- const std::string path(_genPath(n,false));
- if (!path.length())
- return _EMPTY_JSON;
-
- if (!OSUtils::readFile(path.c_str(),buf))
- return _EMPTY_JSON;
-
- const uint64_t lm = OSUtils::getLastModified(path.c_str());
- _E &e2 = _db[n];
- try {
- e2.obj = OSUtils::jsonParse(buf);
- } catch ( ... ) {
- e2.obj = _EMPTY_JSON;
- buf = "{}";
- }
- e2.lastModifiedOnDisk = lm;
- e2.lastCheck = now;
- return e2.obj;
+ const std::string path(_genPath(n,false));
+ if (!path.length())
+ return _EMPTY_JSON;
+ std::string buf;
+ if (!OSUtils::readFile(path.c_str(),buf))
+ return _EMPTY_JSON;
+
+ _E &e2 = _db[n];
+ try {
+ e2.obj = OSUtils::jsonParse(buf);
+ } catch ( ... ) {
+ e2.obj = _EMPTY_JSON;
+ buf = "{}";
}
+
+ return e2.obj;
}
void JSONDB::erase(const std::string &n)
@@ -131,7 +102,7 @@ void JSONDB::_reload(const std::string &p,const std::string &b)
std::vector<std::string> dl(OSUtils::listDirectory(p.c_str()));
for(std::vector<std::string>::const_iterator di(dl.begin());di!=dl.end();++di) {
if ((di->length() > 5)&&(di->substr(di->length() - 5) == ".json")) {
- this->get(b + di->substr(0,di->length() - 5),0);
+ this->get(b + di->substr(0,di->length() - 5));
} else {
this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR));
}