summaryrefslogtreecommitdiff
path: root/zto/controller/JSONDB.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'zto/controller/JSONDB.cpp')
-rw-r--r--zto/controller/JSONDB.cpp85
1 files changed, 24 insertions, 61 deletions
diff --git a/zto/controller/JSONDB.cpp b/zto/controller/JSONDB.cpp
index 1277aab..8b6de9b 100644
--- a/zto/controller/JSONDB.cpp
+++ b/zto/controller/JSONDB.cpp
@@ -53,64 +53,35 @@ 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;
}
-const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceCheck)
+const nlohmann::json &JSONDB::get(const std::string &n)
{
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)
@@ -126,22 +97,14 @@ void JSONDB::erase(const std::string &n)
_db.erase(n);
}
-void JSONDB::_reload(const std::string &p)
+void JSONDB::_reload(const std::string &p,const std::string &b)
{
- std::map<std::string,char> l(OSUtils::listDirectoryFull(p.c_str()));
- for(std::map<std::string,char>::iterator li(l.begin());li!=l.end();++li) {
- if (li->second == 'f') {
- // assume p starts with _basePath, which it always does -- will throw otherwise
- std::string n(p.substr(_basePath.length()));
- while ((n.length() > 0)&&(n[0] == ZT_PATH_SEPARATOR)) n = n.substr(1);
- if (ZT_PATH_SEPARATOR != '/') std::replace(n.begin(),n.end(),ZT_PATH_SEPARATOR,'/');
- if ((n.length() > 0)&&(n[n.length() - 1] != '/')) n.push_back('/');
- n.append(li->first);
- if ((n.length() > 5)&&(n.substr(n.length() - 5) == ".json")) {
- this->get(n.substr(0,n.length() - 5),0); // causes load and cache or update
- }
- } else if (li->second == 'd') {
- this->_reload(p + ZT_PATH_SEPARATOR + li->first);
+ 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));
+ } else {
+ this->_reload((p + ZT_PATH_SEPARATOR + *di),(b + *di + ZT_PATH_SEPARATOR));
}
}
}