ConvMetadataGsonImpl convMetadata = new ConvMetadataGsonImpl();
convMetadata.setImportMetadata(importMetadata);
final SlobId newId;
if (!preserveHistory) {
List<WaveletOperation> history = Lists.newArrayList();
WaveletHistoryConverter converter = new WaveletHistoryConverter(
getConvNindoConverter(attachmentMapping));
for (WaveletOperation op :
new HistorySynthesizer().synthesizeHistory(wavelet, snapshot.getSecond())) {
history.add(converter.convertAndApply(convertGooglewaveToGmail(op)));
}
history.addAll(participantFixup);
newId = waveletCreator.newConvWithGeneratedId(
ImmutableList.<WaveletOperation>of(), convMetadata, true);
ConvHistoryWriter historyWriter = new ConvHistoryWriter(newId);
try {
for (WaveletOperation op : history) {
historyWriter.append(op);
}
historyWriter.finish();
} catch (ChangeRejected e) {
log.warning("Synthesized history rejected: " + history);
throw new RuntimeException("Synthesized history rejected", e);
}
} else {
long version = 0;
newId = waveletCreator.newConvWithGeneratedId(
ImmutableList.<WaveletOperation>of(), convMetadata, true);
ConvHistoryWriter historyWriter = new ConvHistoryWriter(newId);
WaveletHistoryConverter converter =
new WaveletHistoryConverter(getConvNindoConverter(attachmentMapping));
try {
// NOTE(ohler): We have to stop at snapshot.getFirst().getVersion() even if
// getRawDeltas gives us more, since otherwise, participantFixup may be out-of-date.
while (version < snapshot.getFirst().getVersion()) {
log.info("converter state: " + converter);
List<ProtocolAppliedWaveletDelta> rawDeltas =
robotApi.getRawDeltas(waveletName, version);
for (ProtocolAppliedWaveletDelta rawDelta : rawDeltas) {
WaveletDelta delta = CoreWaveletOperationSerializer.deserialize(
ProtocolWaveletDelta.parseFrom(rawDelta.getSignedOriginalDelta().getDelta()));
for (WaveletOperation badOp : delta) {
Preconditions.checkState(badOp.getContext().getTimestamp() == -1,
"Unexpected timestamp: %s in delta %s", badOp, delta);
// TODO(ohler): Rename
// CoreWaveletOperationSerializer.deserialize() to
// deserializeWithNoTimestamp() or something.
WaveletOperation withTimestamp = WaveletOperation.cloneOp(badOp,
new WaveletOperationContext(badOp.getContext().getCreator(),
rawDelta.getApplicationTimestamp(),
badOp.getContext().getVersionIncrement()));
WaveletOperation converted =
converter.convertAndApply(convertGooglewaveToGmail(withTimestamp));
//log.info(version + ": " + op + " -> " + converted);
historyWriter.append(converted);
version++;
}
}