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.
This commit is contained in:
parent
ae303ee902
commit
c62141fd98
4 changed files with 40 additions and 83 deletions
|
@ -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 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;
|
||||
|
||||
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;
|
||||
_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));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue