/** Convenience operation to make a displayable string from a Node, that has been used for an Id. */ public static String str(Node node) { if ( node == null ) return "<null>"; return fromNode(node).toString(); }
/** Update the version of the local data store */ private void setLocalState(Version version, Node patchId) { setLocalState(version, (patchId == null) ? null : Id.fromNode(patchId)); }
public Id getIdAsId() { return Id.fromNode(getId()) ; }
public Id getPreviousIdAsId() { return Id.fromNode(getPrevious()) ; }
/** Retrieve a patch by version. */ @Override public RDFPatch fetch(Id dsRef, Version version) { checkLink(); DataSource source = getDataSourceOrNull(dsRef); if ( source == null ) return null; RDFPatch patch = source.getPatchLog().fetch(version); if ( LOG.isInfoEnabled() ) { if ( patch == null ) { FmtLog.info(LOG, "fetch: Dest=%s, %s, Not found", source, version); } else { Id id = Id.fromNode(patch.getId()); FmtLog.info(LOG, "fetch: Dest=%s, %s, Patch=%s", source, version, id); } } return patch; }
/** Validate a patch for this {@code PatchLog} */ public static boolean validate(RDFPatch patch, PatchLog log) { Id previousId = Id.fromNode(patch.getPrevious()); Id patchId = Id.fromNode(patch.getId()); if ( patchId == null ) { FmtLog.warn(LOG, "No patch id"); return false; } if ( previousId == null ) { if ( !log.isEmpty() ) { FmtLog.warn(LOG, "No previous for patch when PatchLog is not empty: patch=%s", patchId); return false; } } else { if ( Objects.equals(patchId, previousId) ) { FmtLog.warn(LOG, "Patch ihas itself as the previous patch: patch=%s : previous=%s", patchId, previousId); return false ; } if ( log.isEmpty() ) { FmtLog.warn(LOG, "Previous reference for patch but PatchLog is empty: patch=%s : previous=%s", patchId, previousId); return false ; } } try { // XXX Why separate? validate(log, patch.header(), patchId, previousId, PatchValidation::badPatchEx); return true ; } catch (DeltaException ex) { return false; } }
@Override final public Version append(RDFPatch patch) { // System.err.println(">>append"); // RDFPatchOps.write(System.err, patch); // System.err.println("<<append"); return patchLogLockRtn(()->{ Id thisId = Id.fromNode(patch.getId()); Id prevId = Id.fromNode(patch.getPrevious()); // Is it a reply of the last patch? if ( ! isEmpty() && getLatestId().equals(thisId) ) { if ( Objects.equals(prevId, logIndex.getPreviousId()) ) FmtLog.warn(LOG, "Patch id matches log head, but patch previous does not match log previous id"); return getLatestVersion(); } PatchValidation.validateNewPatch(this, thisId, prevId, PatchValidation::badPatchEx); patchStorage.store(thisId, patch); Version version = logIndex.nextVersion(); // This is the commit point. Indeside the log lock. logIndex.save(version, thisId, prevId); return version; }); }
/** 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())); }
@Test public void id_fromNode() { Id id1 = Id.create(); assertEquals(36, id1.asPlainString().length()); Id id2 = Id.fromNode(id1.asNode()); assertEquals(id1, id2); }