LOG.error("checkDirect failed {}", future.failedReason());
return;
}
Message responseMessage = future.responseMessage();
DataMap dataMap = responseMessage.dataMap(0);
if (dataMap == null) {
LOG.error("nothing received, something is wrong");
futureSync.failed("nothing received, something is wrong");
return;
}
Map<Number640, Data> retVal = new HashMap<Number640, Data>();
boolean syncMessageRequired = false;
int dataCopy = 0;
int dataOrig = 0;
//int dataCopyCount = 0;
//int diffCount = 0;
//int dataNotCopied = 0;
for (Map.Entry<Number640, Data> entry : dataMap.dataMap().entrySet()) {
Data data = entry.getValue();
if(data.length() == 0) {
if(data.isFlag1()) {
LOG.debug("no sync required");
syncMessageRequired = false;
} else if(data.isFlag2()) {
LOG.debug("copy required for key {}",entry.getKey());
syncMessageRequired = true;
Data data2 = peer.storageLayer().get(entry.getKey());
dataOrig += data2.length();
//copy
retVal.put(entry.getKey(), data2);
dataCopy += data2.length();
}
} else {
LOG.debug("sync required");
syncMessageRequired = true;
Data data2 = peer.storageLayer().get(entry.getKey());
dataOrig += data2.length();
final ByteBuf buffer = data.buffer();
Number160 versionKey = SyncUtils.decodeHeader(buffer);
Number160 hash = SyncUtils.decodeHeader(buffer);
List<Checksum> checksums = SyncUtils.decodeChecksums(buffer);
// TODO: don't copy data, toBytes does a copy!
List<Instruction> instructions = RSync.instructions(
data2.toBytes(), checksums, blockSize);
AlternativeCompositeByteBuf abuf = AlternativeCompositeByteBuf.compBuffer();
dataCopy += SyncUtils.encodeInstructions(instructions, versionKey, hash, abuf);
DataBuffer dataBuffer = new DataBuffer(abuf);
//diff
Data data1 = new Data(dataBuffer).flag1();
retVal.put(entry.getKey(), data1);
}
}
final SyncStat syncStat = new SyncStat(peer.peerAddress().peerId(), other.peerId(), dataCopy, dataOrig);
if (syncMessageRequired) {
SyncBuilder.this.dataMap(new DataMap(retVal));
FutureResponse fr = peerSync.syncRPC().syncMessage(other,
SyncBuilder.this, future2.channelCreator());
fr.addListener(new BaseFutureAdapter<FutureResponse>() {
@Override
public void operationComplete(FutureResponse future) throws Exception {