if (script == null) throw new ScriptException("Script not found");
return script.<ODocument>field(ScriptsDao.LOCAL_STORAGE);
}
private static ODocument updateStorageLocked(String name,boolean before,JsonCallback updaterFn) throws ScriptException {
final ScriptsDao dao = ScriptsDao.getInstance();
ODocument script = null;
try {
script = dao.getByNameLocked(name);
if (script == null) throw new ScriptException("Script not found");
ODocument retScript = before ? script.copy() : script;
ODocument storage = script.<ODocument>field(ScriptsDao.LOCAL_STORAGE);
Optional<ODocument> storage1 = Optional.ofNullable(storage);
JsonNode current = storage1.map(ODocument::toJSON)
.map(Json.mapper()::readTreeOrMissing)
.orElse(NullNode.getInstance());
if (current.isMissingNode()) throw new ScriptEvalException("Error reading local storage as json");
JsonNode updated = updaterFn.call(current);
ODocument result = new ODocument().fromJSON(updated.toString());
script.field(ScriptsDao.LOCAL_STORAGE, result);
dao.save(script);
ODocument field = retScript.field(ScriptsDao.LOCAL_STORAGE);
return field;
} finally {
if (script != null) {
script.unlock();