public JsonObject asJson() { return JSONX.buildObject(b->addJsonFields(b)); }
/** Access a field of a JSON object : return as {@code Optional<String>} */ public static Optional<String> getStr(JsonObject obj, String field) { return Optional.ofNullable(getStrOrNull(obj, field)); }
/** Access a field of a JSON object, return an {@code int} or a default value. */ public static int getInt(JsonObject obj, String field, int dftValue) { JsonValue jv = obj.get(field); if ( jv == null ) return dftValue; return getInt(jv, dftValue); }
/** Parse a configuration file. */ public Builder parse(String configFile) { Path path = Paths.get(configFile); if ( ! Files.exists(path) ) throw new DeltaConfigException("File not found: "+configFile); // -- version JsonObject obj = JSON.read(configFile); int version = JSONX.getInt(obj, F_VERSION, -99); if ( version == -99 ) { LOG.warn("No version number for the configuration file : assuming 'current'"); version = DeltaConst.SYSTEM_VERSION; } if ( version != SYSTEM_VERSION ) throw new DeltaConfigException("Version number for LocalServer must be "+DeltaConst.SYSTEM_VERSION+"."); this.configFile = configFile; // -- log provider logProvider = DPS.PatchStoreFileProvider; String logTypeName = JSONX.getStrOrNull(obj, F_LOG_TYPE); if ( logTypeName != null ) { String providerName = PatchStoreMgr.canonical(logTypeName); if ( providerName == null ) providerName = DeltaConst.LOG_FILE; logProvider = providerName; } setProperty(DeltaConst.pDeltaFile, path.getParent().toString()); return this; }
/** Access a field of a JSON object : return a {@code long}, or the default value. */ public static long getLong(JsonObject obj, String field, long dftValue) { JsonValue jv = obj.get(field); if ( jv == null ) return dftValue; return getLong(jv, dftValue); }
/** Set an existing zNode to the the bytes for the JSON object */ public static void zkSetJson(CuratorFramework client, String statePath, JsonObject x) { byte[] bytes = JSONX.asBytes(x); zkSet(client, statePath, bytes); }
/** Create a safe copy of a {@link JsonValue}. * <p> * If the JsonValue is a structure (object or array), copy the structure recursively. * <p> * If the JsonValue is a primitive (string, number, boolean or null), * it is immutable so return the same object. */ public static JsonValue copy(JsonValue arg) { JsonBuilder builder = builder(arg) ; return builder==null ? arg : builder.build() ; }
DeltaServerConfig conf = new DeltaServerConfig(); if ( obj.hasKey(fPort) ) { int x = JSONX.getInt(obj, fPort, -1); if ( x >= 0 ) conf.serverPort = x; conf.jettyConf = JSONX.getStrOrNull(obj, fJetty); conf.zkConnectionString = JSONX.getStrOrNull(obj, fZkConnectionString); conf.zkConf = JSONX.getStrOrNull(obj, fZkConfig); int x = JSONX.getInt(obj, fZkPort, -1); if ( x >= 0 ) conf.zkPort = x; conf.zkData = JSONX.getStrOrNull(obj, fZkData); conf.fileBase = JSONX.getStrOrNull(obj, fFileDirData); conf.provider = Provider.create(JSONX.getStrOrNull(obj, fProvider)); conf.s3BucketName = JSONX.getStrOrNull(obj, fS3BucketName); if ( obj.hasKey(fS3Region) ) conf.s3Region = JSONX.getStrOrNull(obj, fS3Region); if ( obj.hasKey(fS3CredsFile) ) conf.s3CredentialsFile = JSONX.getStrOrNull(obj, fS3CredsFile); if ( obj.hasKey(fS3CredsProfile) ) conf.s3CredentialsProfile = JSONX.getStrOrNull(obj, fS3CredsProfile);
public static Version fromJson(JsonObject obj, String field, Version dftValue) { long ver = JSONX.getLong(obj, field, dftValue.version) ; return create(ver); }
/** Create and set a new zNode: the zNode must not exist before this operation. */ public static void zkCreateSetJson(CuratorFramework client, String statePath, JsonObject x) { byte[] bytes = JSONX.asBytes(x); zkCreateSet(client, statePath, bytes); }
public JsonObject asJson() { return JSONX.buildObject(b->addJsonFields(b)); }
private Id getIdOrNull(JsonObject obj, String field) { String s = JSONX.getStrOrNull(obj, field); if ( s == null ) return null; return Id.fromString(s); }
long ver = JSONX.getLong(obj, fVersion, -99); if ( patchId == null ) { FmtLog.error(LOG, "Null patch id (%s, %s)", patchId, prevId);
private void save(long version, Id patch, Id prev) { newState(version, patch, prev); JsonObject x = stateToJson(version, patch, prev); byte[] bytes = JSONX.asBytes(x); if ( patch != null ) { // [META] // Record the basic header - (version, id, prev) - for validation. if ( keepHeaderInfo ) Zk.zkCreateSet(client, headerPath(patch), bytes); // Write version->id mapping. Zk.zkCreateSet(client, versionPath(version), patch.asBytes()); } Zk.zkSet(client, statePath, bytes); }
/** The initial state object of a patch log */ /*package*/ static JsonObject initialStateJson() { FmtLog.debug(LOG, "initialStateJson"); return JSONX.buildObject(b->{ b.pair(fVersion, DeltaConst.VERSION_INIT); }); }
public static DataSourceDescription fromJson(JsonObject obj) { String idStr = JSONX.getStrOrNull(obj, F_ID); if ( idStr == null ) throw new DeltaException("Missing \"id:\" in DataSourceDescription JSON"); String name = JSONX.getStrOrNull(obj, F_NAME); if ( name == null ) { @SuppressWarnings("deprecation") String n = JSONX.getStrOrNull(obj, F_BASE); // Compatibility. Log.warn(DataSourceDescription.class, "Deprecated: Use of field name \"base\" - change to \"name\""); name = n; } if ( name == null ) throw new DeltaException("Missing \"name:\" in DataSourceDescription JSON"); String uri = JSONX.getStrOrNull(obj, F_URI); return new DataSourceDescription(Id.fromString(idStr), name, uri); }
public static Version fromJson(JsonObject obj, String field) { long ver = JSONX.getLong(obj, field, -99) ; if ( ver < -1 ) throw new DeltaException("Bad version number: '"+JSON.toStringFlat(obj.get(field))+"'"); return create(ver); }
/** Send a JSON argument to a URL+name by POST and received a JSON object in return. */ public static JsonValue rpc(String url, String opName, JsonValue arg) { JsonObject a = JSONX.buildObject((b)->{ // if ( token != null ) // b.key(F_TOKEN).value(token.asString()); b.pair(F_OP, opName); b.pair(F_OP_ID, Long.toString(counter.incrementAndGet())); b.pair(F_ARG, arg); }) ; return rpc(url, a) ; }
String patchStr = JSONX.getStrOrNull(sourceObj, F_ID); if ( patchStr == null || patchStr.isEmpty() ) { dataState.patchId = null; String dsStr = JSONX.getStrOrNull(sourceObj, F_DATASOURCE); if ( dsStr != null ) dataState.datasource = Id.fromString(dsStr); String name = JSONX.getStrOrNull(sourceObj, F_NAME); if ( name != null ) dataState.name = name; String uri = JSONX.getStrOrNull(sourceObj, F_URI); if ( uri != null ) dataState.uri = uri; String typeName = JSONX.getStrOrNull(sourceObj, F_STORAGE); LocalStorageType storage = LocalStorageType.fromString(typeName);
@Override public PatchInfo getPatchInfo(Id id) { String p = headerPath(id); JsonObject obj = Zk.zkFetchJson(client, p); Id patchId = getIdOrNull(obj, fId); if ( ! Objects.equals(id, patchId) ) { /*msg*/ } Id prevId = getIdOrNull(obj, fPrevious); long ver = JSONX.getLong(obj, fVersion, -99); Version version = ver < 0 ? Version.UNSET : Version.create(ver); return new PatchInfo(patchId, Version.UNSET, prevId); }