/** * Builds a {@link WaveletDeltaRecord} and applies it to the wavelet container. * The delta must be non-empty. */ protected WaveletDeltaRecord applyDelta( ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDelta, WaveletDelta transformed) throws InvalidProtocolBufferException, OperationException { TransformedWaveletDelta transformedDelta = AppliedDeltaUtil.buildTransformedDelta(appliedDelta, transformed); WaveletDeltaRecord deltaRecord = new WaveletDeltaRecord(transformed.getTargetVersion(), appliedDelta, transformedDelta); waveletState.appendDelta(deltaRecord); return deltaRecord; }
/** * Calculates the hashed version after an applied delta is applied. */ public static HashedVersion calculateResultingHashedVersion( ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDelta) throws InvalidProtocolBufferException { return HASH_FACTORY.create( appliedDelta.getByteArray(), getHashedVersionAppliedAt(appliedDelta), appliedDelta.getMessage().getOperationsApplied()); }
/** * Build an applied delta message from a POJO delta. The delta is not signed. */ public static ByteStringMessage<ProtocolAppliedWaveletDelta> buildAppliedDelta(WaveletDelta delta, long applicationTimestamp) { ProtocolWaveletDelta protoDelta = CoreWaveletOperationSerializer.serialize(delta); ByteStringMessage<ProtocolWaveletDelta> deltaBytes = ByteStringMessage.serializeMessage(protoDelta); ProtocolSignedDelta signedDelta = ProtocolSignedDelta.newBuilder().setDelta(deltaBytes.getByteString()).build(); return AppliedDeltaUtil.buildAppliedDelta(signedDelta, delta.getTargetVersion(), delta.size(), applicationTimestamp); }
HashedVersion deltaEndVersion; try { deltaEndVersion = AppliedDeltaUtil.calculateResultingHashedVersion(appliedDelta); } catch (InvalidProtocolBufferException e) { LOG.warning("Skipping illformed applied delta " + appliedDelta, e);
AppliedDeltaUtil.buildAppliedDelta(signedDelta, transformed.getTargetVersion(), transformed.size(), applicationTimestamp);
/** * Build an applied delta message from a POJO delta. The delta is not signed. */ public static ByteStringMessage<ProtocolAppliedWaveletDelta> buildAppliedDelta(WaveletDelta delta, long applicationTimestamp) { ProtocolWaveletDelta protoDelta = CoreWaveletOperationSerializer.serialize(delta); ByteStringMessage<ProtocolWaveletDelta> deltaBytes = ByteStringMessage.serializeMessage(protoDelta); ProtocolSignedDelta signedDelta = ProtocolSignedDelta.newBuilder().setDelta(deltaBytes.getByteString()).build(); return AppliedDeltaUtil.buildAppliedDelta(signedDelta, delta.getTargetVersion(), delta.size(), applicationTimestamp); }
/** * Creates a delta of no-ops and builds the corresponding applied and * transformed delta objects. */ private static WaveletDeltaRecord makeDelta(HashedVersion appliedAtVersion, long timestamp, int numOps) throws InvalidProtocolBufferException { // Use no-op delta so the ops can actually apply. WaveletDelta delta = UTIL.makeNoOpDelta(appliedAtVersion, timestamp, numOps); ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDelta = WaveServerTestUtil.buildAppliedDelta(delta, timestamp); TransformedWaveletDelta transformedDelta = AppliedDeltaUtil.buildTransformedDelta(appliedDelta, delta); return new WaveletDeltaRecord(appliedAtVersion, appliedDelta, transformedDelta); } }
@Override public HashedVersion getAppliedAtVersion(long version) throws IOException { lifeCycle.enter(); try { checkIsOpen(); ByteStringMessage<ProtocolAppliedWaveletDelta> applied = getAppliedDelta(version); return (applied != null) ? AppliedDeltaUtil.getHashedVersionAppliedAt(applied) : null; } finally { lifeCycle.leave(); } }
/** * Creates a delta of no-ops and builds the corresponding applied and * transformed delta objects. */ private static WaveletDeltaRecord makeDelta(HashedVersion appliedAtVersion, long timestamp, int numOps) throws InvalidProtocolBufferException { // Use no-op delta so the ops can actually apply. WaveletDelta delta = UTIL.makeNoOpDelta(appliedAtVersion, timestamp, numOps); ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDelta = WaveServerTestUtil.buildAppliedDelta(delta, timestamp); TransformedWaveletDelta transformedDelta = AppliedDeltaUtil.buildTransformedDelta(appliedDelta, delta); return new WaveletDeltaRecord(appliedAtVersion, appliedDelta, transformedDelta); } }
/** * Read a record and return it. */ private WaveletDeltaRecord readRecord() throws IOException { DeltaHeader header = readDeltaHeader(); ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDelta = readAppliedDelta(header.appliedDeltaLength); TransformedWaveletDelta transformedDelta = readTransformedWaveletDelta( header.transformedDeltaLength); return new WaveletDeltaRecord(AppliedDeltaUtil.getHashedVersionAppliedAt(appliedDelta), appliedDelta, transformedDelta); }
/** * Builds a transformed delta from an applied delta and its transformed ops. */ public static TransformedWaveletDelta buildTransformedDelta( ByteStringMessage<ProtocolAppliedWaveletDelta> appliedDeltaBytes, WaveletDelta transformed) throws InvalidProtocolBufferException { ProtocolAppliedWaveletDelta appliedDelta = appliedDeltaBytes.getMessage(); Preconditions.checkArgument( getHashedVersionAppliedAt(appliedDeltaBytes).equals(transformed.getTargetVersion())); Preconditions.checkArgument(appliedDelta.getOperationsApplied() == transformed.size()); HashedVersion resultingVersion = HASH_FACTORY.create(appliedDeltaBytes.getByteArray(), transformed.getTargetVersion(), appliedDelta.getOperationsApplied()); return TransformedWaveletDelta.cloneOperations(resultingVersion, appliedDelta.getApplicationTimestamp(), transformed); }
appliedAt = AppliedDeltaUtil.getHashedVersionAppliedAt(appliedDelta); } catch (InvalidProtocolBufferException e) { markStateCorrupted();
HashedVersion hashedVersion = AppliedDeltaUtil.getHashedVersionAppliedAt(appliedDelta); Preconditions.checkState(hashedVersion.equals(getCurrentVersion()), "Applied delta must apply to current version");