@Override public void onSuccess(int operationsApplied, ProtocolHashedVersion hashedVersionAfterApplication, long applicationTimestamp) { listener.onSuccess(operationsApplied, CoreWaveletOperationSerializer.deserialize(hashedVersionAfterApplication), applicationTimestamp); } });
private static DocOp deserializeDocOp(DBObject dbObject) throws PersistenceException { try { return CoreWaveletOperationSerializer.deserialize(ProtocolDocumentOperation .parseFrom(((byte[]) dbObject.get(FIELD_BYTES)))); } catch (InvalidProtocolBufferException e) { throw new PersistenceException(e); } } }
/** * Deserializes a {@link ProtocolWaveletDelta} as a transformed wavelet delta. */ public static TransformedWaveletDelta deserialize(ProtocolWaveletDelta delta, HashedVersion resultingVersion, long applicationTimestamp) { ParticipantId author = ParticipantId.ofUnsafe(delta.getAuthor()); int count = delta.getOperationCount(); Preconditions.checkArgument(count > 0, "Cannot deserialize an empty delta"); List<WaveletOperation> ops = Lists.newArrayListWithCapacity(count); if (count > 1) { WaveletOperationContext context = new WaveletOperationContext(author, applicationTimestamp, 1); for (int i = 0; i < count - 1; i++) { ProtocolWaveletOperation op = delta.getOperation(i); ops.add(deserialize(op, context)); } } WaveletOperationContext context = new WaveletOperationContext(author, applicationTimestamp, 1, resultingVersion); ops.add(deserialize(delta.getOperation(count - 1), context)); return new TransformedWaveletDelta(author, resultingVersion, applicationTimestamp, ops); }
/** * Deserializes a {@link ProtocolWaveletDelta} as an untransformed wavelet * delta. */ public static WaveletDelta deserialize(ProtocolWaveletDelta delta) { List<WaveletOperation> ops = Lists.newArrayList(); for (ProtocolWaveletOperation op : delta.getOperationList()) { WaveletOperationContext context = new WaveletOperationContext( ParticipantId.ofUnsafe(delta.getAuthor()), Constants.NO_TIMESTAMP, 1); ops.add(deserialize(op, context)); } HashedVersion hashedVersion = deserialize(delta.getHashedVersion()); return new WaveletDelta(new ParticipantId(delta.getAuthor()), hashedVersion, ops); }
private HashedVersion getVersionParameter(OperationRequest operation, ParamsProperty parameter) throws InvalidRequestException { byte[] bytes = OperationUtil.<byte[]>getRequiredParameter( operation, parameter); ProtocolHashedVersion protoVersion; try { protoVersion = ProtocolHashedVersion.parseFrom(bytes); } catch (InvalidProtocolBufferException ex) { throw new InvalidRequestException("Invalid version " + parameter.key(), operation, ex); } return CoreWaveletOperationSerializer.deserialize(protoVersion); } }
/** * Creates the non-existent remote wavelet container at this server and commits it. * Calling commit at this known version, forces the history to be fetched up to this point. * TODO (alown): Possible race condition here with update? (Though I don't think it would result in * anything more serious than repeated history fetches.) */ private void createAndCommitRemoteWavelet(WaveletName waveletName, ProtocolHashedVersion committedVersion) { RemoteWaveletContainer wavelet = getOrCreateRemoteWavelet(waveletName); HashedVersion v = CoreWaveletOperationSerializer.deserialize(committedVersion); wavelet.commit(v); LOG.info("Passed commit message for version " + v.getVersion() + " to RemoteWavelet"); }
/** * Deserialize a {@link ProtoTransformedWaveletDelta} into a {@link TransformedWaveletDelta} */ public static TransformedWaveletDelta deserialize(ProtoTransformedWaveletDelta delta) { long applicationTimestamp = delta.getApplicationTimestamp(); HashedVersion resultingVersion = CoreWaveletOperationSerializer.deserialize(delta.getResultingVersion()); ParticipantId author = ParticipantId.ofUnsafe(delta.getAuthor()); ImmutableList.Builder<WaveletOperation> operations = ImmutableList.builder(); int numOperations = delta.getOperationCount(); for (int i = 0; i < numOperations; i++) { WaveletOperationContext context; if (i == numOperations - 1) { context = new WaveletOperationContext(author, applicationTimestamp, 1, resultingVersion); } else { context = new WaveletOperationContext(author, applicationTimestamp, 1); } operations.add(CoreWaveletOperationSerializer.deserialize(delta.getOperation(i), context)); } return new TransformedWaveletDelta(author, resultingVersion, applicationTimestamp, operations.build()); } }
/** * Deserialize a {@link WaveletSnapshot} into a list of * {@link WaveletOperation}s. * * @param snapshot snapshot protocol buffer to deserialize * @return a list of operations */ public static List<CoreWaveletOperation> deserialize(WaveletSnapshot snapshot) { List<CoreWaveletOperation> ops = Lists.newArrayList(); for (String participant : snapshot.getParticipantIdList()) { CoreAddParticipant addOp = new CoreAddParticipant(new ParticipantId(participant)); ops.add(addOp); } for (DocumentSnapshot document : snapshot.getDocumentList()) { CoreWaveletDocumentOperation docOp = new CoreWaveletDocumentOperation( document.getDocumentId(), deserialize(document.getDocumentOperation())); ops.add(docOp); } return ops; }
@Override public void run(ProtocolWaveletUpdate update) { ++counter; assertEquals(WAVELET_NAME, getWaveletName(update.getWaveletName())); assertTrue(update.hasCommitNotice()); assertEquals(BEGIN_VERSION, CoreWaveletOperationSerializer.deserialize(update.getCommitNotice())); } });
@Override public void run(ProtocolWaveletUpdate update) { ++counter; assertEquals(WAVELET_NAME, getWaveletName(update.getWaveletName())); assertTrue(update.hasCommitNotice()); assertEquals(BEGIN_VERSION, CoreWaveletOperationSerializer.deserialize(update.getCommitNotice())); } });
public void testOnAddParticipantEvent() { DeltaSequence POJO_DELTAS = DeltaSequence.of(CoreWaveletOperationSerializer .deserialize(ADD_DELTA, END_VERSION, 0L)); long dummyCreationTime = System.currentTimeMillis(); WaveletData wavelet = WaveletDataUtil.createEmptyWavelet(WAVELET_NAME, PARTICIPANT, BEGIN_VERSION, dummyCreationTime); dispatcher.waveletUpdate(wavelet, POJO_DELTAS); verify(listener).onParticipantAdded(WAVELET_NAME, PARTICIPANT); }
public void testOnRemoveParticipantEvent() { DeltaSequence POJO_DELTAS = DeltaSequence.of(CoreWaveletOperationSerializer .deserialize(REMOVE_DELTA, END_VERSION, 0L)); long dummyCreationTime = System.currentTimeMillis(); WaveletData wavelet = WaveletDataUtil.createEmptyWavelet(WAVELET_NAME, PARTICIPANT, BEGIN_VERSION, dummyCreationTime); dispatcher.waveletUpdate(wavelet, POJO_DELTAS); verify(listener).onParticipantRemoved(WAVELET_NAME, PARTICIPANT); } }
public void testOnAddParticipantEvent() { DeltaSequence POJO_DELTAS = DeltaSequence.of(CoreWaveletOperationSerializer .deserialize(ADD_DELTA, END_VERSION, 0L)); long dummyCreationTime = System.currentTimeMillis(); WaveletData wavelet = WaveletDataUtil.createEmptyWavelet(WAVELET_NAME, PARTICIPANT, BEGIN_VERSION, dummyCreationTime); dispatcher.waveletUpdate(wavelet, POJO_DELTAS); verify(listener).onParticipantAdded(WAVELET_NAME, PARTICIPANT); }
public void testOnRemoveParticipantEvent() { DeltaSequence POJO_DELTAS = DeltaSequence.of(CoreWaveletOperationSerializer .deserialize(REMOVE_DELTA, END_VERSION, 0L)); long dummyCreationTime = System.currentTimeMillis(); WaveletData wavelet = WaveletDataUtil.createEmptyWavelet(WAVELET_NAME, PARTICIPANT, BEGIN_VERSION, dummyCreationTime); dispatcher.waveletUpdate(wavelet, POJO_DELTAS); verify(listener).onParticipantRemoved(WAVELET_NAME, PARTICIPANT); } }
private static void addDocumentSnapshotToWavelet( DocumentSnapshot snapshot, WaveletData container) throws InvalidParticipantAddress { DocOp op = CoreWaveletOperationSerializer.deserialize(snapshot.getDocumentOperation()); DocInitialization docInit = DocOpUtil.asInitialization(op); Collection<ParticipantId> contributors = CollectionUtils.newArrayList(); for (String p : snapshot.getContributorList()) { contributors.add(getParticipantId(p)); } container.createDocument( snapshot.getDocumentId(), getParticipantId(snapshot.getAuthor()), contributors, docInit, snapshot.getLastModifiedTime(), snapshot.getLastModifiedVersion()); } }
@Override public void waveletCommitUpdate(WaveletName waveletName, ProtocolHashedVersion committedVersion, WaveletUpdateCallback callback) { Preconditions.checkNotNull(committedVersion); if (isLocalWavelet(waveletName)) { LOG.warning("Got commit update for local wavelet " + waveletName); callback.onFailure(FederationErrors.badRequest("Received commit update to local wavelet")); return; } RemoteWaveletContainer wavelet; try { wavelet = getRemoteWavelet(waveletName); } catch (WaveletStateException e) { LOG.warning("Failed to access wavelet " + waveletName + " for commit update", e); callback.onFailure(FederationErrors.internalServerError("Storage access failure")); return; } if (wavelet != null) { wavelet.commit(CoreWaveletOperationSerializer.deserialize(committedVersion)); } else { if(LOG.isInfoLoggable()) { LOG.info("Got commit update for missing wavelet " + waveletName); } createAndCommitRemoteWavelet(waveletName, committedVersion); } callback.onSuccess(); } };
@Override public void getDeltaSignerInfo(ByteString signerId, WaveletName waveletName, ProtocolHashedVersion deltaEndVersion, DeltaSignerInfoResponseListener listener) { LocalWaveletContainer wavelet = loadLocalWavelet(waveletName, listener); if (wavelet != null) { HashedVersion endVersion = CoreWaveletOperationSerializer.deserialize(deltaEndVersion); if (wavelet.isDeltaSigner(endVersion, signerId)) { ProtocolSignerInfo signerInfo = certificateManager.retrieveSignerInfo(signerId); if (signerInfo == null) { // Oh no! We are supposed to store it, and we already know they did sign this delta. LOG.severe("No stored signer info for valid getDeltaSignerInfo on " + waveletName); listener.onFailure(FederationErrors.badRequest("Unknown signer info")); } else { listener.onSuccess(signerInfo); } } else { LOG.info("getDeltaSignerInfo was not authrorised for wavelet " + waveletName + ", end version " + deltaEndVersion); listener.onFailure(FederationErrors.badRequest("Not authorised to get signer info")); } } }
/** * Assert that an operation is unchanged when serialised then deserialised. * * @param op operation to check */ private static void assertReversible(WaveletOperation op) { // Test both (de)serialising a single operation... assertEquals(op, CoreWaveletOperationSerializer.deserialize( CoreWaveletOperationSerializer.serialize(op), OP_CONTEXT)); List<WaveletOperation> ops = ImmutableList.of(op, op, op); ParticipantId author = new ParticipantId("kalman@google.com"); HashedVersion hashedVersion = HashedVersion.unsigned(0); WaveletDelta delta = new WaveletDelta(author, hashedVersion, ops); ProtocolWaveletDelta serialized = CoreWaveletOperationSerializer.serialize(delta); WaveletDelta deserialized = CoreWaveletOperationSerializer.deserialize(serialized); assertEquals(hashedVersion.getVersion(), serialized.getHashedVersion().getVersion()); assertTrue(Arrays.equals(hashedVersion.getHistoryHash(), serialized.getHashedVersion().getHistoryHash().toByteArray())); assertDeepEquals(delta, deserialized); }
/** * Assert that an operation is unchanged when serialised then deserialised. * * @param op operation to check */ private static void assertReversible(WaveletOperation op) { // Test both (de)serialising a single operation... assertEquals(op, CoreWaveletOperationSerializer.deserialize( CoreWaveletOperationSerializer.serialize(op), OP_CONTEXT)); List<WaveletOperation> ops = ImmutableList.of(op, op, op); ParticipantId author = new ParticipantId("kalman@google.com"); HashedVersion hashedVersion = HashedVersion.unsigned(0); WaveletDelta delta = new WaveletDelta(author, hashedVersion, ops); ProtocolWaveletDelta serialized = CoreWaveletOperationSerializer.serialize(delta); WaveletDelta deserialized = CoreWaveletOperationSerializer.deserialize(serialized); assertEquals(hashedVersion.getVersion(), serialized.getHashedVersion().getVersion()); assertTrue(Arrays.equals(hashedVersion.getHistoryHash(), serialized.getHashedVersion().getHistoryHash().toByteArray())); assertDeepEquals(delta, deserialized); }
/** * Deserialize a {@link ProtocolWaveletOperation} as a {@link WaveletOperation}. * * @param protobufOp protocol buffer wavelet operation to deserialize * @return deserialized wavelet operation */ public static WaveletOperation deserialize(ProtocolWaveletOperation protobufOp, WaveletOperationContext context) { if (protobufOp.hasNoOp()) { return new NoOp(context); } else if (protobufOp.hasAddParticipant()) { return new AddParticipant(context, new ParticipantId(protobufOp.getAddParticipant())); } else if (protobufOp.hasRemoveParticipant()) { return new RemoveParticipant(context, new ParticipantId(protobufOp.getRemoveParticipant())); } else if (protobufOp.hasMutateDocument()) { return new WaveletBlipOperation(protobufOp.getMutateDocument().getDocumentId(), new BlipContentOperation(context, deserialize(protobufOp.getMutateDocument().getDocumentOperation()))); } else { throw new IllegalArgumentException("Unsupported operation: " + protobufOp); } }