long len = range.getEnd() - range.getStart();
log.info("{} {}", i, len);
}
log.info("Chose merge {}-{}", bestStart, bestEnd);
BlobStore blobStore = fs.getBlobStore(project);
List<FileRange> newRanges = Lists.newArrayList();
for (int i = 0; i < bestStart; i++) {
newRanges.add(file.getRanges(i));
}
try (TempFile tempFile = TempFile.create()) {
FileRange.Builder c = FileRange.newBuilder();
Hasher md5 = Hashing.md5().newHasher();
try (OutputStream fos = new HashingOutputStream(new FileOutputStream(tempFile.getFile()), md5)) {
for (int i = bestStart; i < bestEnd; i++) {
FileRange range = file.getRanges(i);
if (i == bestStart) {
c.setStart(range.getStart());
}
if (i == (bestEnd - 1)) {
c.setEnd(range.getEnd());
}
final BlobData blob = blobStore.find(range.getContentKey());
if (blob == null) {
throw new IOException("Unable to open storage for range: " + range);
}
blob.copyTo(fos);
}
}
if (!c.hasStart() || !c.hasEnd()) {
throw new IllegalStateException();
}
ByteString hash = ByteString.copyFrom(md5.hash().asBytes());
BlobData blobData = new BlobData(tempFile.getFile(), hash);
blobStore.put(blobData);
if (PARANOID) {
BlobData blob = blobStore.find(blobData.getHash());
if (blob == null) {
throw new IllegalStateException();
}
ByteString checkHash = ByteString.copyFrom(blob.hash(Hashing.md5()).asBytes());
if (!blobData.getHash().equals(checkHash)) {