use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.
the class BlobStoreImplTest method removeDuplicates.
@Test
public void removeDuplicates() throws IOException {
final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
final Path blobDir = temporaryFolder.newFolder("blob").toPath();
final Tuple2<byte[], MessageDigest> testFile1 = generateTestFile();
final Tuple2<byte[], MessageDigest> testFile2 = generateTestFile();
try (final BlobStore blobStore = newBlobStore(blobDbx, blobDir)) {
blobStore.open();
BlobId testFile1Id = addAndVerify(blobStore, testFile1);
BlobId testFile2Id = addAndVerify(blobStore, testFile2);
// add again to increase reference count
testFile1Id = addAndVerify(blobStore, testFile1);
testFile2Id = addAndVerify(blobStore, testFile2);
// remove second reference
blobStore.remove(null, testFile1Id);
blobStore.remove(null, testFile2Id);
// should still exist with one more reference
getAndVerify(blobStore, testFile1Id, testFile1);
getAndVerify(blobStore, testFile2Id, testFile2);
// remove first reference
blobStore.remove(null, testFile1Id);
blobStore.remove(null, testFile2Id);
// should no longer exist as all references were removed
assertNull(blobStore.get(null, testFile1Id));
assertNull(blobStore.get(null, testFile2Id));
}
}
use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.
the class BlobStoreImplTest method get.
@Test
public void get() throws IOException {
final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
final Path blobDir = temporaryFolder.newFolder("blob").toPath();
final Tuple2<byte[], MessageDigest> testFile1 = generateTestFile();
final Tuple2<byte[], MessageDigest> testFile2 = generateTestFile();
try (final BlobStore blobStore = newBlobStore(blobDbx, blobDir)) {
blobStore.open();
final BlobId testFileId1 = addAndVerify(blobStore, testFile1);
final BlobId testFileId2 = addAndVerify(blobStore, testFile2);
getAndVerify(blobStore, testFileId1, testFile1);
getAndVerify(blobStore, testFileId2, testFile2);
}
}
use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.
the class BlobStoreImplTest method copy.
@Test
public void copy() throws IOException {
final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
final Path blobDir = temporaryFolder.newFolder("blob").toPath();
final Tuple2<byte[], MessageDigest> testFile1 = generateTestFile();
try (final BlobStore blobStore = newBlobStore(blobDbx, blobDir)) {
blobStore.open();
// store
final BlobId testFile1Id = addAndVerify(blobStore, testFile1);
// copy
final BlobId copyBlobId = blobStore.copy(null, testFile1Id);
// check the two copies are identical
final Tuple2<byte[], MessageDigest> testFile1Data;
try (final InputStream testFile1Is = blobStore.get(null, testFile1Id)) {
testFile1Data = readAll(testFile1Is);
}
final Tuple2<byte[], MessageDigest> copyBlobData;
try (final InputStream copyBlobIs = blobStore.get(null, copyBlobId)) {
copyBlobData = readAll(copyBlobIs);
}
assertEquals(testFile1Data._2, copyBlobData._2);
assertArrayEquals(testFile1Data._1, copyBlobData._1);
}
}
use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.
the class BlobStoreImplTest method removeUnique.
@Test
public void removeUnique() throws IOException {
final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
final Path blobDir = temporaryFolder.newFolder("blob").toPath();
final List<Tuple2<byte[], MessageDigest>> testFiles = Arrays.asList(generateTestFile(), generateTestFile(), generateTestFile(), generateTestFile(), generateTestFile());
try (final BlobStore blobStore = newBlobStore(blobDbx, blobDir)) {
blobStore.open();
final List<BlobId> addedBlobIds = new ArrayList<>();
for (final Tuple2<byte[], MessageDigest> testFile : testFiles) {
addedBlobIds.add(addAndVerify(blobStore, testFile));
}
// remove each added blob
for (final BlobId addedBlobId : addedBlobIds) {
blobStore.remove(null, addedBlobId);
}
// check that each blob was removed
for (final BlobId addedBlobId : addedBlobIds) {
assertNull(blobStore.get(null, addedBlobId));
}
}
}
use of org.exist.util.crypto.digest.MessageDigest in project exist by eXist-db.
the class BlobStoreImplTest method compactPersistentReferences.
@Test
public void compactPersistentReferences() throws IOException {
final Path blobDbx = temporaryFolder.getRoot().toPath().resolve("blob.dbx");
final Path blobDir = temporaryFolder.newFolder("blob").toPath();
final Tuple2<byte[], MessageDigest> testFile1 = generateTestFile();
final Tuple2<byte[], MessageDigest> testFile2 = generateTestFile();
final Tuple2<byte[], MessageDigest> testFile3 = generateTestFile();
try (final BlobStore blobStore = newBlobStore(blobDbx, blobDir)) {
blobStore.open();
final BlobId testFile1Id = addAndVerify(blobStore, testFile1);
final BlobId testFile2Id = addAndVerify(blobStore, testFile2);
BlobId testFile3Id = addAndVerify(blobStore, testFile3);
// add a second reference for testFile3
testFile3Id = addAndVerify(blobStore, testFile3);
// remove testFile2
blobStore.remove(null, testFile2Id);
// remove one of the two references to testFile3
blobStore.remove(null, testFile3Id);
}
// should be 1 entry per unique test file in the blob.dbx, each entry is the digest and then the reference count
// i.e. only 3 entries!
long expectedBlobDbxLen = calculateBlobStoreSize(3);
long actualBlobDbxLen = Files.size(blobDbx);
assertEquals(expectedBlobDbxLen, actualBlobDbxLen);
// reopen and close the blob store, this should call {@link BlobStoreImpl#compactPersistentReferences}
try (final BlobStore blobStore = newBlobStore(blobDbx, blobDir)) {
blobStore.open();
}
// after compaction, should only be 2 entries, because testFile2 was removed. testFile3 is still present as it has one reference remaining
expectedBlobDbxLen = calculateBlobStoreSize(2);
actualBlobDbxLen = Files.size(blobDbx);
assertEquals(expectedBlobDbxLen, actualBlobDbxLen);
}
Aggregations