DeltaFriendlyOldBlobSizeLimiter implements RecommendationModifier { @Override public List<QualifiedRecommendation> getModifiedRecommendations( File oldFile, File newFile, List<QualifiedRecommendation> originalRecommendations) { List<QualifiedRecommendation> sorted = sortRecommendations(originalRecommendations); List<QualifiedRecommendation> result = new ArrayList<>(sorted.size()); long bytesRemaining = maxSizeBytes - oldFile.length(); for (QualifiedRecommendation originalRecommendation : sorted) { if (!originalRecommendation.getRecommendation().uncompressOldEntry) { result.add(originalRecommendation); } else { long extraBytesConsumed = originalRecommendation.getOldEntry().getUncompressedSize() - originalRecommendation.getOldEntry().getCompressedSize(); if (bytesRemaining - extraBytesConsumed >= 0) { result.add(originalRecommendation); bytesRemaining -= extraBytesConsumed; } else { result.add( new QualifiedRecommendation( originalRecommendation.getOldEntry(), originalRecommendation.getNewEntry(), Recommendation.UNCOMPRESS_NEITHER, RecommendationReason.RESOURCE_CONSTRAINED)); } } } return result; } DeltaFriendlyOldBlobSizeLimiter(long maxSizeBytes); @Override List<QualifiedRecommendation> getModifiedRecommendations(
      File oldFile, File newFile, List<QualifiedRecommendation> originalRecommendations);  }
@Test public void testZeroLimit() { DeltaFriendlyOldBlobSizeLimiter limiter = new DeltaFriendlyOldBlobSizeLimiter(0); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_C_300K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(tempFile, tempFile, ALL_RECS)); }
@Test public void testMaxLimit() { DeltaFriendlyOldBlobSizeLimiter limiter = new DeltaFriendlyOldBlobSizeLimiter(Long.MAX_VALUE); assertEquivalence(ALL_RECS, limiter.getModifiedRecommendations(tempFile, tempFile, ALL_RECS)); }
@Test public void testLimit_ExactlySmallest() { long limit = REC_A_100K.getOldEntry().getUncompressedSize() - REC_A_100K.getOldEntry().getCompressedSize(); DeltaFriendlyOldBlobSizeLimiter limiter = new DeltaFriendlyOldBlobSizeLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_A_100K); expected.addAll(suppressed(REC_B_200K, REC_C_300K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(tempFile, tempFile, ALL_RECS)); }
@Test public void testLimit_EdgeUnderSmallest() { long limit = REC_A_100K.getOldEntry().getUncompressedSize() - REC_A_100K.getOldEntry().getCompressedSize() - 1; DeltaFriendlyOldBlobSizeLimiter limiter = new DeltaFriendlyOldBlobSizeLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_C_300K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(tempFile, tempFile, ALL_RECS)); }
@Test public void testLimit_EdgeOverSmallest() { long limit = REC_A_100K.getOldEntry().getUncompressedSize() - REC_A_100K.getOldEntry().getCompressedSize() + 1; DeltaFriendlyOldBlobSizeLimiter limiter = new DeltaFriendlyOldBlobSizeLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_A_100K); expected.addAll(suppressed(REC_B_200K, REC_C_300K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(tempFile, tempFile, ALL_RECS)); }
@Test public void testLimit_ExactlyLargest() { long limit = REC_D_400K.getOldEntry().getUncompressedSize() - REC_D_400K.getOldEntry().getCompressedSize(); DeltaFriendlyOldBlobSizeLimiter limiter = new DeltaFriendlyOldBlobSizeLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_D_400K); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_C_300K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(tempFile, tempFile, ALL_RECS)); }
@Test public void testLimit_EdgeUnderLargest() { long limit = REC_D_400K.getOldEntry().getUncompressedSize() - REC_D_400K.getOldEntry().getCompressedSize() - 1; DeltaFriendlyOldBlobSizeLimiter limiter = new DeltaFriendlyOldBlobSizeLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_C_300K); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_D_400K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(tempFile, tempFile, ALL_RECS)); }
@Test public void testLimit_EdgeOverLargest() { long limit = REC_D_400K.getOldEntry().getUncompressedSize() - REC_D_400K.getOldEntry().getCompressedSize() + 1; DeltaFriendlyOldBlobSizeLimiter limiter = new DeltaFriendlyOldBlobSizeLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_D_400K); expected.addAll(suppressed(REC_A_100K, REC_B_200K, REC_C_300K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(tempFile, tempFile, ALL_RECS)); }
@Test public void testLimit_Complex() { long limit = (REC_D_400K.getOldEntry().getUncompressedSize() - REC_D_400K.getOldEntry().getCompressedSize()) + (REC_B_200K.getOldEntry().getUncompressedSize() - REC_B_200K.getOldEntry().getCompressedSize()); DeltaFriendlyOldBlobSizeLimiter limiter = new DeltaFriendlyOldBlobSizeLimiter(limit); List<QualifiedRecommendation> expected = new ArrayList<QualifiedRecommendation>(); expected.add(REC_B_200K); expected.add(REC_D_400K); expected.addAll(suppressed(REC_A_100K, REC_C_300K)); expected.addAll(ALL_IGNORED_RECS); assertEquivalence(expected, limiter.getModifiedRecommendations(tempFile, tempFile, ALL_RECS)); }