Search in sources :

Example 26 with MessageDigest

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));
    }
}
Also used : Path(java.nio.file.Path) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Test(org.junit.Test)

Example 27 with MessageDigest

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);
    }
}
Also used : Path(java.nio.file.Path) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Test(org.junit.Test)

Example 28 with MessageDigest

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);
    }
}
Also used : Path(java.nio.file.Path) DigestInputStream(org.exist.util.crypto.digest.DigestInputStream) UnsynchronizedByteArrayInputStream(org.apache.commons.io.input.UnsynchronizedByteArrayInputStream) InputStream(java.io.InputStream) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Test(org.junit.Test)

Example 29 with MessageDigest

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));
        }
    }
}
Also used : Path(java.nio.file.Path) Tuple2(com.evolvedbinary.j8fu.tuple.Tuple2) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Test(org.junit.Test)

Example 30 with MessageDigest

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);
}
Also used : Path(java.nio.file.Path) MessageDigest(org.exist.util.crypto.digest.MessageDigest) Test(org.junit.Test)

Aggregations

MessageDigest (org.exist.util.crypto.digest.MessageDigest)30 Path (java.nio.file.Path)20 Test (org.junit.Test)19 InputStream (java.io.InputStream)6 UnsynchronizedByteArrayInputStream (org.apache.commons.io.input.UnsynchronizedByteArrayInputStream)5 DigestInputStream (org.exist.util.crypto.digest.DigestInputStream)5 StreamableDigest (org.exist.util.crypto.digest.StreamableDigest)4 Tuple2 (com.evolvedbinary.j8fu.tuple.Tuple2)3 IOException (java.io.IOException)3 URISyntaxException (java.net.URISyntaxException)3 DigestType (org.exist.util.crypto.digest.DigestType)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Nullable (javax.annotation.Nullable)2 EXistException (org.exist.EXistException)2 PermissionDeniedException (org.exist.security.PermissionDeniedException)2 Txn (org.exist.storage.txn.Txn)2 Try (com.evolvedbinary.j8fu.Try)1 TaggedTryUnchecked (com.evolvedbinary.j8fu.Try.TaggedTryUnchecked)1 Tuple (com.evolvedbinary.j8fu.tuple.Tuple.Tuple)1