Search in sources :

Example 46 with DataIdentifier

use of org.apache.jackrabbit.core.data.DataIdentifier in project jackrabbit-oak by apache.

the class DataStoreBlobStoreTest method testGetAllChunks.

@Test
public void testGetAllChunks() throws Exception {
    DataIdentifier d10 = new DataIdentifier("d-10");
    DataIdentifier d20 = new DataIdentifier("d-20");
    DataIdentifier d30 = new DataIdentifier("d-30");
    List<DataIdentifier> dis = ImmutableList.of(d10, d20, d30);
    List<DataRecord> recs = Lists.newArrayList(Iterables.transform(dis, new Function<DataIdentifier, DataRecord>() {

        @Nullable
        @Override
        public DataRecord apply(@Nullable DataIdentifier input) {
            return new TimeDataRecord(input);
        }
    }));
    OakFileDataStore mockedDS = mock(OakFileDataStore.class);
    when(mockedDS.getAllRecords()).thenReturn(recs.iterator());
    when(mockedDS.getRecord(new DataIdentifier("d-10"))).thenReturn(new TimeDataRecord(d10));
    when(mockedDS.getRecord(new DataIdentifier("d-20"))).thenReturn(new TimeDataRecord(d20));
    when(mockedDS.getRecord(new DataIdentifier("d-30"))).thenReturn(new TimeDataRecord(d30));
    DataStoreBlobStore ds = new DataStoreBlobStore(mockedDS);
    Iterator<String> chunks = ds.getAllChunkIds(25);
    Set<String> expected = Sets.newHashSet("d-10", "d-20");
    assertEquals(expected, Sets.newHashSet(chunks));
}
Also used : Function(com.google.common.base.Function) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) DataRecord(org.apache.jackrabbit.core.data.DataRecord) Nullable(javax.annotation.Nullable) AbstractBlobStoreTest(org.apache.jackrabbit.oak.spi.blob.AbstractBlobStoreTest) Test(org.junit.Test)

Example 47 with DataIdentifier

use of org.apache.jackrabbit.core.data.DataIdentifier in project jackrabbit-oak by apache.

the class DataStoreBlobStoreTest method testExternalBinary.

@Test
public void testExternalBinary() throws DataStoreException, IOException {
    int maxInlineSize = 300;
    int actualSize = maxInlineSize + 10;
    byte[] data = new byte[actualSize];
    new Random().nextBytes(data);
    DataIdentifier testDI = new DataIdentifier("test");
    DataRecord testDR = new ByteArrayDataRecord(data, testDI, "testReference");
    DataStore mockedDS = mock(DataStore.class);
    when(mockedDS.getMinRecordLength()).thenReturn(maxInlineSize);
    when(mockedDS.getRecord(testDI)).thenReturn(testDR);
    when(mockedDS.getRecordIfStored(testDI)).thenReturn(testDR);
    when(mockedDS.addRecord(any(InputStream.class))).thenReturn(testDR);
    DataStoreBlobStore ds = new DataStoreBlobStore(mockedDS);
    DataRecord dr = ds.addRecord(new ByteArrayInputStream(data));
    assertFalse(InMemoryDataRecord.isInstance(dr.getIdentifier().toString()));
    assertEquals(testDI, dr.getIdentifier());
    assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(data), dr.getStream()));
    assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(data), new BlobStoreInputStream(ds, dr.getIdentifier().toString(), 0)));
    assertEquals(dr, ds.getRecordIfStored(dr.getIdentifier()));
    assertEquals(dr, ds.getRecord(dr.getIdentifier()));
    //        assertTrue(ds.getInputStream(dr.getIdentifier().toString()) instanceof BufferedInputStream);
    assertEquals(actualSize, ds.getBlobLength(dr.getIdentifier().toString()));
    assertEquals(testDI.toString(), BlobId.of(ds.writeBlob(new ByteArrayInputStream(data))).blobId);
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) BlobStoreInputStream(org.apache.jackrabbit.oak.spi.blob.BlobStoreInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DataStore(org.apache.jackrabbit.core.data.DataStore) BlobStoreInputStream(org.apache.jackrabbit.oak.spi.blob.BlobStoreInputStream) DataRecord(org.apache.jackrabbit.core.data.DataRecord) AbstractBlobStoreTest(org.apache.jackrabbit.oak.spi.blob.AbstractBlobStoreTest) Test(org.junit.Test)

Example 48 with DataIdentifier

use of org.apache.jackrabbit.core.data.DataIdentifier in project jackrabbit by apache.

the class VFSBackend method deleteOlderRecursive.

/**
     * Deletes any descendant record files under {@code folderObject} if the record files are older than {@code timestamp},
     * and push all the deleted record identifiers into {@code deleteIdSet}.
     * @param deleteIdSet set to store all the deleted record identifiers
     * @param folderObject folder object to start with
     * @param timestamp timestamp
     * @throws FileSystemException if any file system exception occurs
     * @throws DataStoreException if any file system exception occurs
     */
private void deleteOlderRecursive(Set<DataIdentifier> deleteIdSet, FileObject folderObject, long timestamp) throws FileSystemException, DataStoreException {
    FileType type;
    DataIdentifier identifier;
    for (FileObject fileObject : VFSUtils.getChildFileOrFolders(folderObject)) {
        type = fileObject.getType();
        if (type == FileType.FOLDER) {
            deleteOlderRecursive(deleteIdSet, fileObject, timestamp);
            synchronized (this) {
                if (!VFSUtils.hasAnyChildFileOrFolder(fileObject)) {
                    fileObject.delete();
                }
            }
        } else if (type == FileType.FILE) {
            long lastModified = getLastModifiedTime(fileObject);
            if (lastModified < timestamp) {
                identifier = new DataIdentifier(fileObject.getName().getBaseName());
                if (getDataStore().confirmDelete(identifier)) {
                    getDataStore().deleteFromCache(identifier);
                    if (LOG.isInfoEnabled()) {
                        LOG.info("Deleting old file " + fileObject.getName().getFriendlyURI() + " modified: " + new Timestamp(lastModified).toString() + " length: " + fileObject.getContent().getSize());
                    }
                    if (deleteRecordFileObject(fileObject)) {
                        deleteIdSet.add(identifier);
                    } else {
                        LOG.warn("Failed to delete old file " + fileObject.getName().getFriendlyURI());
                    }
                }
            }
        }
    }
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) FileType(org.apache.commons.vfs2.FileType) FileObject(org.apache.commons.vfs2.FileObject) Timestamp(java.sql.Timestamp)

Example 49 with DataIdentifier

use of org.apache.jackrabbit.core.data.DataIdentifier in project jackrabbit-oak by apache.

the class AzureDataStoreTest method testBackendGetRecordInvalidIdentifierThrowsDataStoreException.

@Test
public void testBackendGetRecordInvalidIdentifierThrowsDataStoreException() {
    try {
        backend.getRecord(new DataIdentifier("invalid"));
        fail();
    } catch (DataStoreException e) {
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) Test(org.junit.Test)

Example 50 with DataIdentifier

use of org.apache.jackrabbit.core.data.DataIdentifier in project jackrabbit-oak by apache.

the class AzureDataStoreTest method testBackendGetAllRecordsReturnsAll.

// GetAllRecords (Backend)
@Test
public void testBackendGetAllRecordsReturnsAll() throws DataStoreException, IOException {
    for (int recCount : Lists.newArrayList(0, 1, 2, 5)) {
        Map<DataIdentifier, String> addedRecords = Maps.newHashMap();
        if (0 < recCount) {
            for (int i = 0; i < recCount; i++) {
                String data = String.format("testData%d", i);
                DataRecord record = ds.addRecord(new ByteArrayInputStream(data.getBytes()));
                addedRecords.put(record.getIdentifier(), data);
            }
        }
        Iterator<DataRecord> iter = backend.getAllRecords();
        List<DataIdentifier> identifiers = Lists.newArrayList();
        int actualCount = 0;
        while (iter.hasNext()) {
            DataRecord record = iter.next();
            identifiers.add(record.getIdentifier());
            assertTrue(addedRecords.containsKey(record.getIdentifier()));
            StringWriter writer = new StringWriter();
            IOUtils.copy(record.getStream(), writer);
            assertTrue(writer.toString().equals(addedRecords.get(record.getIdentifier())));
            actualCount++;
        }
        for (DataIdentifier identifier : identifiers) {
            ds.deleteRecord(identifier);
        }
        assertEquals(recCount, actualCount);
    }
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) Hex.encodeHexString(org.apache.commons.codec.binary.Hex.encodeHexString) DataRecord(org.apache.jackrabbit.core.data.DataRecord) Test(org.junit.Test)

Aggregations

DataIdentifier (org.apache.jackrabbit.core.data.DataIdentifier)60 Test (org.junit.Test)31 DataRecord (org.apache.jackrabbit.core.data.DataRecord)30 File (java.io.File)22 DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)16 Hex.encodeHexString (org.apache.commons.codec.binary.Hex.encodeHexString)13 FileInputStream (java.io.FileInputStream)12 ByteArrayInputStream (java.io.ByteArrayInputStream)8 ArrayList (java.util.ArrayList)8 FileUtils.copyInputStreamToFile (org.apache.commons.io.FileUtils.copyInputStreamToFile)5 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)4 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)4 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 RepositoryException (javax.jcr.RepositoryException)4 Function (com.google.common.base.Function)3 StorageException (com.microsoft.azure.storage.StorageException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 SQLException (java.sql.SQLException)3 HashSet (java.util.HashSet)3