Search in sources :

Example 21 with DataIdentifier

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

the class AzureBlobStoreBackend method getRecord.

@Override
public DataRecord getRecord(DataIdentifier identifier) throws DataStoreException {
    if (null == identifier) {
        throw new NullPointerException("identifier");
    }
    String key = getKeyName(identifier);
    long start = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        CloudBlockBlob blob = getAzureContainer().getBlockBlobReference(key);
        if (blob.exists()) {
            blob.downloadAttributes();
            AzureBlobStoreDataRecord record = new AzureBlobStoreDataRecord(this, connectionString, containerName, new DataIdentifier(getIdentifierName(blob.getName())), blob.getProperties().getLastModified().getTime(), blob.getProperties().getLength());
            LOG.debug("Data record read for blob. identifier={} duration={} record={}", key, (System.currentTimeMillis() - start), record);
            return record;
        } else {
            LOG.debug("Blob not found. identifier={} duration={}", key, (System.currentTimeMillis() - start));
            throw new DataStoreException(String.format("Cannot find blob. identifier=%s", key));
        }
    } catch (StorageException e) {
        LOG.info("Error getting data record for blob. identifier={}", key, e);
        throw new DataStoreException(String.format("Cannot retrieve blob. identifier=%s", key), e);
    } catch (URISyntaxException e) {
        LOG.debug("Error getting data record for blob. identifier={}", key, e);
        throw new DataStoreException(String.format("Cannot retrieve blob. identifier=%s", key), e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) URISyntaxException(java.net.URISyntaxException) CloudBlockBlob(com.microsoft.azure.storage.blob.CloudBlockBlob) StorageException(com.microsoft.azure.storage.StorageException)

Example 22 with DataIdentifier

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

the class AzureBlobStoreBackend method getAllMetadataRecords.

@Override
public List<DataRecord> getAllMetadataRecords(String prefix) {
    if (null == prefix) {
        throw new NullPointerException("prefix");
    }
    long start = System.currentTimeMillis();
    final List<DataRecord> records = Lists.newArrayList();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        CloudBlobDirectory metaDir = getAzureContainer().getDirectoryReference(META_DIR_NAME);
        for (ListBlobItem item : metaDir.listBlobs(prefix)) {
            if (item instanceof CloudBlob) {
                CloudBlob blob = (CloudBlob) item;
                records.add(new AzureBlobStoreDataRecord(this, connectionString, containerName, new DataIdentifier(stripMetaKeyPrefix(blob.getName())), blob.getProperties().getLastModified().getTime(), blob.getProperties().getLength(), true));
            }
        }
        LOG.debug("Metadata records read. recordsRead={} metadataFolder={} duration={}", records.size(), prefix, (System.currentTimeMillis() - start));
    } catch (StorageException e) {
        LOG.info("Error reading all metadata records. metadataFolder={}", prefix, e);
    } catch (DataStoreException | URISyntaxException e) {
        LOG.debug("Error reading all metadata records. metadataFolder={}", prefix, e);
    } finally {
        if (null != contextClassLoader) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    return records;
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) ListBlobItem(com.microsoft.azure.storage.blob.ListBlobItem) CloudBlobDirectory(com.microsoft.azure.storage.blob.CloudBlobDirectory) URISyntaxException(java.net.URISyntaxException) CloudBlob(com.microsoft.azure.storage.blob.CloudBlob) AbstractDataRecord(org.apache.jackrabbit.oak.spi.blob.AbstractDataRecord) DataRecord(org.apache.jackrabbit.core.data.DataRecord) StorageException(com.microsoft.azure.storage.StorageException)

Example 23 with DataIdentifier

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

the class S3Backend method deleteAllOlderThan.

@Override
public Set<DataIdentifier> deleteAllOlderThan(long min) throws DataStoreException {
    long start = System.currentTimeMillis();
    // S3 stores lastModified to lower boundary of timestamp in ms.
    // and hence min is reduced by 1000ms.
    min = min - 1000;
    Set<DataIdentifier> deleteIdSet = new HashSet<DataIdentifier>(30);
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        ObjectListing prevObjectListing = s3service.listObjects(bucket);
        while (true) {
            List<DeleteObjectsRequest.KeyVersion> deleteList = new ArrayList<DeleteObjectsRequest.KeyVersion>();
            for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
                if (!s3ObjSumm.getKey().startsWith(META_KEY_PREFIX)) {
                    DataIdentifier identifier = new DataIdentifier(getIdentifierName(s3ObjSumm.getKey()));
                    long lastModified = s3ObjSumm.getLastModified().getTime();
                    LOG.debug("Identifier [{}]'s lastModified = [{}]", identifier, lastModified);
                    if (lastModified < min && store.confirmDelete(identifier) && //  order is important here
                    s3service.getObjectMetadata(bucket, s3ObjSumm.getKey()).getLastModified().getTime() < min) {
                        store.deleteFromCache(identifier);
                        LOG.debug("add id [{}] to delete lists", s3ObjSumm.getKey());
                        deleteList.add(new DeleteObjectsRequest.KeyVersion(s3ObjSumm.getKey()));
                        deleteIdSet.add(identifier);
                    }
                }
            }
            if (deleteList.size() > 0) {
                DeleteObjectsRequest delObjsReq = new DeleteObjectsRequest(bucket);
                delObjsReq.setKeys(deleteList);
                DeleteObjectsResult dobjs = s3service.deleteObjects(delObjsReq);
                if (dobjs.getDeletedObjects().size() != deleteList.size()) {
                    throw new DataStoreException("Incomplete delete object request. only  " + dobjs.getDeletedObjects().size() + " out of " + deleteList.size() + " are deleted");
                } else {
                    LOG.debug("[{}] records deleted from datastore", deleteList);
                }
            }
            if (!prevObjectListing.isTruncated()) {
                break;
            }
            prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing);
        }
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    LOG.info("deleteAllOlderThan: min=[{}] exit. Deleted[{}] records. Number of records deleted [{}] took [{}]ms", new Object[] { min, deleteIdSet, deleteIdSet.size(), (System.currentTimeMillis() - start) });
    return deleteIdSet;
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) ArrayList(java.util.ArrayList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) DeleteObjectsResult(com.amazonaws.services.s3.model.DeleteObjectsResult) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest) HashSet(java.util.HashSet)

Example 24 with DataIdentifier

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

the class AzureDataStoreTest method testCreateAndDeleteBlobHappyPath.

@Test
public void testCreateAndDeleteBlobHappyPath() throws DataStoreException, IOException {
    final DataRecord uploadedRecord = ds.addRecord(new ByteArrayInputStream(testBuffer));
    DataIdentifier identifier = uploadedRecord.getIdentifier();
    assertTrue(backend.exists(identifier));
    assertTrue(0 != uploadedRecord.getLastModified());
    assertEquals(testBuffer.length, uploadedRecord.getLength());
    final DataRecord retrievedRecord = ds.getRecord(identifier);
    validateRecord(retrievedRecord, new String(testBuffer), uploadedRecord);
    ds.deleteRecord(identifier);
    assertFalse(backend.exists(uploadedRecord.getIdentifier()));
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) ByteArrayInputStream(java.io.ByteArrayInputStream) DataRecord(org.apache.jackrabbit.core.data.DataRecord) Hex.encodeHexString(org.apache.commons.codec.binary.Hex.encodeHexString) Test(org.junit.Test)

Example 25 with DataIdentifier

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

the class AzureDataStoreTest method testBackendDeleteRecordNullIdentifier.

// Delete (Backend)
@Test
public void testBackendDeleteRecordNullIdentifier() throws DataStoreException {
    DataIdentifier identifier = null;
    try {
        backend.deleteRecord(identifier);
        fail();
    } catch (NullPointerException e) {
        assert ("identifier".equals(e.getMessage()));
    }
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) 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