@Override public Version getCurrentVersion() { return Version.create(version); }
/** Sync until some version */ private void syncToVersion(Version version) { //long remoteVer = getRemoteVersionLatestOrDefault(VERSION_UNSET); if ( ! Version.isValid(version) ) { FmtLog.debug(LOG, "Sync: Asked for no patches to sync"); return; } Version localVer = getLocalVersion(); // // -1 ==> no entries, uninitialized. // if ( DeltaConst.versionUninitialized(localVer) ) { // FmtLog.info(LOG, "Sync: No log entries"); // localVer = DeltaConst.VERSION_INIT; // setLocalState(localVer, (Node)null); // return; // } if ( localVer.value() > version.value() ) FmtLog.info(LOG, "[%s] Local version ahead of remote : [local=%d, remote=%d]", datasourceId, getLocalVersion(), getRemoteVersionCached()); if ( localVer.value() >= version.value() ) return; // bring up-to-date. FmtLog.info(LOG, "Sync: Versions [%s, %s]", localVer, version); playPatches(localVer.value()+1, version.value()) ; //FmtLog.info(LOG, "Now: Versions [%d, %d]", getLocalVersion(), remoteVer); }
/** Is this version a possible version? (i.e. not a marker) */ public static boolean isValid(Version version) { if ( version == null ) return false; return version.isValid(); }
private void detailsByDSD(DataSourceDescription dsd) { PatchLogInfo logInfo = dLink.getPatchLogInfo(dsd.getId()); if ( logInfo == null ) { // Some thing bad somewhere. System.out.printf("[%s %s <%s> [no info] %s]\n", dsd.getId(), dsd.getName(), dsd.getUri()); return; } if ( Version.INIT.equals(logInfo.getMinVersion()) && Version.INIT.equals(logInfo.getMaxVersion()) ) { if ( logInfo.getLatestPatch() != null ) // Should not happen. System.out.printf("[%s %s <%s> [empty] %s]\n", dsd.getId(), dsd.getName(), dsd.getUri(), logInfo.getLatestPatch().toString()); else System.out.printf("[%s %s <%s> [empty]]\n", dsd.getId(), dsd.getName(), dsd.getUri()); return; } if ( logInfo.getMinVersion().isValid() ) { System.out.printf("[%s %s <%s> [%s,%s] %s]\n", dsd.getId(), dsd.getName(), dsd.getUri(), logInfo.getMinVersion(), logInfo.getMaxVersion(), (logInfo.getLatestPatch()==null)?"<no patches>":logInfo.getLatestPatch().toString() ); } }
/** Add fields to current JsonBuilder object */ public void addJsonFields(JsonBuilder b) { dsd.addJsonFields(b); b.key(F_MINVER).value(minVersion.asJson()); b.key(F_MAXVER).value(maxVersion.asJson()); if ( latestPatch != null ) b.key(F_LATEST).value(latestPatch.asString()); else b.key(F_LATEST).value(""); }
public static PatchLogInfo fromJson(JsonObject obj) { DataSourceDescription dsd = DataSourceDescription.fromJson(obj); Version minVer = Version.fromJson(obj, F_MINVER, Version.UNSET) ; Version maxVer = Version.fromJson(obj, F_MAXVER, Version.UNSET) ; String latestPatchStr = JSONX.getStrOrNull(obj, F_LATEST); Id latestPatch = null; if ( latestPatchStr != null && !latestPatchStr.isEmpty() ) latestPatch = Id.fromString(latestPatchStr); return new PatchLogInfo(dsd, minVer, maxVer, latestPatch); }
@Override protected void execCmd() { if ( getPositional().isEmpty() ) { Id dsRef = getDescription().getId(); PatchLogInfo logInfo = dLink.getPatchLogInfo(dsRef); if ( ! logInfo.getMaxVersion().isValid()) { throw new CmdException(getCommandName()+" : Empty log"); } exec1(logInfo.getMaxVersion()); return ; } getPositional().forEach(v->{ long patchVersion; try { patchVersion = Integer.parseInt(v); } catch (NumberFormatException ex) { throw new CmdException(getCommandName()+" : Invalid version"); } exec1(Version.create(patchVersion)); }); }
@Override public Stream<RDFPatch> range(Version start, Version finish) { if ( ! Version.isValid(start) ) throw new DeltaException("Bad start version : "+start); if ( ! Version.isValid(finish) ) throw new DeltaException("Bad finish version : "+finish); // Isolation not necessary. Patch files are immutable once written. return range$(start, finish); }
private static JsonObject stateToJson(Id datasource, String name, String uri, LocalStorageType storage, Version version, Id patchId) { String x = ""; if ( patchId != null ) x = patchId.asPlainString(); String patchStr = x; return JSONX.buildObject(builder->{ builder .pair(F_VERSION, version.asJson()) .pair(F_ID, patchStr) .pair(F_NAME, name) .pair(F_DATASOURCE, datasource.asPlainString()); if ( storage != null ) builder.pair(F_STORAGE, storage.typeName()); if ( uri != null ) builder.pair(F_URI, uri); }); }
@Override public Version append(Id dsRef, RDFPatch patch) { checkLink(); long t1 = System.currentTimeMillis(); String str = retry(()->{ RDFChangesHTTP remote = createRDFChanges(dsRef); // [NET] Network point // If not re-applyable, we need a copy. patch.apply(remote); return remote.getResponse(); }, ()->patch.repeatable(), ()->"Retry append patch.", ()->"Failed to append patch : "+dsRef); long t2 = System.currentTimeMillis(); long elapsed_ms = (t2-t1); if ( str != null ) { try { JsonObject obj = JSON.parse(str); Version version = Version.fromJson(obj, DeltaConst.F_VERSION); return version; } catch (Exception ex) { FmtLog.warn(this.getClass(), "[%s] Error in response body : %s", dsRef, ex.getMessage()); } } else { FmtLog.warn(this.getClass(), "[%s] No response body", dsRef); } // No response body or syntax error. return Version.UNSET; }
@Override public boolean equals(Object obj) { if ( this == obj ) return true; if ( obj == null ) return false; if ( getClass() != obj.getClass() ) return false; PatchInfo other = (PatchInfo)obj; if ( patch == null ) { if ( other.patch != null ) return false; } else if ( !patch.equals(other.patch) ) return false; if ( previous == null ) { if ( other.previous != null ) return false; } else if ( !previous.equals(other.previous) ) return false; if ( version == null ) { if ( other.version != null ) return false; } else if ( !version.equals(other.version) ) return false; return true; }
@Override public void save(Version version, Id patch, Id prev) { // Should always be called inside the patch lock. save(version.value(), patch, prev); }
/** Send a patch to log server. */ public synchronized void append(RDFPatch patch) { checkDeltaConnection(); Version ver = dLink.append(datasourceId, patch); if ( ! Version.isValid(ver) ) // Didn't happen. return ; Version ver0 = state.version(); if ( ver0.value() >= ver.value() ) FmtLog.warn(LOG, "[%s] Version did not advance: %d -> %d", datasourceId.toString(), ver0 , ver); state.updateState(ver, Id.fromNode(patch.getId())); }