|
@@ -27,14 +27,17 @@ bool JSONDB::put(const std::string &n,const nlohmann::json &obj)
|
|
if (!_isValidObjectName(n))
|
|
if (!_isValidObjectName(n))
|
|
return false;
|
|
return false;
|
|
|
|
|
|
- std::string path(_genPath(n,true));
|
|
|
|
|
|
+ const std::string path(_genPath(n,true));
|
|
if (!path.length())
|
|
if (!path.length())
|
|
return false;
|
|
return false;
|
|
|
|
|
|
- std::string buf(obj.dump(2));
|
|
|
|
|
|
+ const std::string buf(obj.dump(2));
|
|
if (!OSUtils::writeFile(path.c_str(),buf))
|
|
if (!OSUtils::writeFile(path.c_str(),buf))
|
|
return false;
|
|
return false;
|
|
|
|
|
|
|
|
+ if (_feed)
|
|
|
|
+ fwrite(buf.c_str(),buf.length()+1,1,_feed);
|
|
|
|
+
|
|
_E &e = _db[n];
|
|
_E &e = _db[n];
|
|
e.obj = obj;
|
|
e.obj = obj;
|
|
e.lastModifiedOnDisk = OSUtils::getLastModified(path.c_str());
|
|
e.lastModifiedOnDisk = OSUtils::getLastModified(path.c_str());
|
|
@@ -55,7 +58,8 @@ const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceChe
|
|
if (e != _db.end()) {
|
|
if (e != _db.end()) {
|
|
if ((now - e->second.lastCheck) <= (uint64_t)maxSinceCheck)
|
|
if ((now - e->second.lastCheck) <= (uint64_t)maxSinceCheck)
|
|
return e->second.obj;
|
|
return e->second.obj;
|
|
- std::string path(_genPath(n,false));
|
|
|
|
|
|
+
|
|
|
|
+ const std::string path(_genPath(n,false));
|
|
if (!path.length()) // sanity check
|
|
if (!path.length()) // sanity check
|
|
return _EMPTY_JSON;
|
|
return _EMPTY_JSON;
|
|
|
|
|
|
@@ -68,13 +72,16 @@ const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceChe
|
|
e->second.obj = nlohmann::json::parse(buf);
|
|
e->second.obj = nlohmann::json::parse(buf);
|
|
e->second.lastModifiedOnDisk = lm; // don't update these if there is a parse error -- try again and again ASAP
|
|
e->second.lastModifiedOnDisk = lm; // don't update these if there is a parse error -- try again and again ASAP
|
|
e->second.lastCheck = now;
|
|
e->second.lastCheck = now;
|
|
|
|
+
|
|
|
|
+ if (_feed)
|
|
|
|
+ fwrite(buf.c_str(),buf.length()+1,1,_feed); // it changed, so send to feed (also sends all objects on startup, which we want for Central)
|
|
} catch ( ... ) {} // parse errors result in "holding pattern" behavior
|
|
} catch ( ... ) {} // parse errors result in "holding pattern" behavior
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
return e->second.obj;
|
|
return e->second.obj;
|
|
} else {
|
|
} else {
|
|
- std::string path(_genPath(n,false));
|
|
|
|
|
|
+ const std::string path(_genPath(n,false));
|
|
if (!path.length())
|
|
if (!path.length())
|
|
return _EMPTY_JSON;
|
|
return _EMPTY_JSON;
|
|
|
|
|
|
@@ -87,10 +94,14 @@ const nlohmann::json &JSONDB::get(const std::string &n,unsigned long maxSinceChe
|
|
e2.obj = nlohmann::json::parse(buf);
|
|
e2.obj = nlohmann::json::parse(buf);
|
|
} catch ( ... ) {
|
|
} catch ( ... ) {
|
|
e2.obj = _EMPTY_JSON;
|
|
e2.obj = _EMPTY_JSON;
|
|
|
|
+ buf = "{}";
|
|
}
|
|
}
|
|
e2.lastModifiedOnDisk = lm;
|
|
e2.lastModifiedOnDisk = lm;
|
|
e2.lastCheck = now;
|
|
e2.lastCheck = now;
|
|
|
|
|
|
|
|
+ if (_feed)
|
|
|
|
+ fwrite(buf.c_str(),buf.length()+1,1,_feed);
|
|
|
|
+
|
|
return e2.obj;
|
|
return e2.obj;
|
|
}
|
|
}
|
|
}
|
|
}
|