Search in sources :

Example 31 with DataIdentifier

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

the class CachingDataStoreTest method lazyLoadStream.

/**
     * Add in datastore, invalidate from cache and lazy load record stream.
     */
@Test
public void lazyLoadStream() throws Exception {
    LOG.info("Starting lazyLoadStream");
    File f = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
    String id = getIdForInputStream(f);
    FileInputStream fin = new FileInputStream(f);
    closer.register(fin);
    DataRecord rec = dataStore.addRecord(fin);
    assertEquals(id, rec.getIdentifier().toString());
    //start & finish
    taskLatch.countDown();
    callbackLatch.countDown();
    waitFinish();
    // Invalidate from the local cache
    dataStore.getCache().invalidate(id);
    // retrieve record from the datastore
    rec = dataStore.getRecordIfStored(new DataIdentifier(id));
    assertNotNull(rec);
    assertEquals(id, rec.getIdentifier().toString());
    // the file should not be in cache
    File cached = dataStore.getCache().getIfPresent(id);
    assertNull(cached);
    // assert stream
    assertFile(rec.getStream(), f, folder);
    // Now should be available in the cache
    cached = dataStore.getCache().getIfPresent(id);
    assertNotNull(cached);
    assertTrue(Files.equal(f, cached));
    dataStore.deleteRecord(new DataIdentifier(id));
    rec = dataStore.getRecordIfStored(new DataIdentifier(id));
    assertNull(rec);
    LOG.info("Finished lazyLoadStream");
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) Hex.encodeHexString(org.apache.commons.codec.binary.Hex.encodeHexString) DataRecord(org.apache.jackrabbit.core.data.DataRecord) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 32 with DataIdentifier

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

the class CachingDataStoreTest method zeroCacheAddGetDelete.

/**
     * Add, get, delete when zero cache size.
     * @throws Exception
     */
@Test
public void zeroCacheAddGetDelete() throws Exception {
    LOG.info("Starting zeroCacheAddGetDelete");
    dataStore.close();
    init(1, 0, 0);
    File f = copyToFile(randomStream(0, 4 * 1024), folder.newFile());
    String id = getIdForInputStream(f);
    FileInputStream fin = new FileInputStream(f);
    closer.register(fin);
    DataRecord rec = dataStore.addRecord(fin);
    assertEquals(id, rec.getIdentifier().toString());
    assertFile(rec.getStream(), f, folder);
    rec = dataStore.getRecordIfStored(new DataIdentifier(id));
    assertEquals(id, rec.getIdentifier().toString());
    assertFile(rec.getStream(), f, folder);
    assertEquals(1, Iterators.size(dataStore.getAllIdentifiers()));
    dataStore.deleteRecord(new DataIdentifier(id));
    rec = dataStore.getRecordIfStored(new DataIdentifier(id));
    assertNull(rec);
    LOG.info("Finished zeroCacheAddGetDelete");
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) Hex.encodeHexString(org.apache.commons.codec.binary.Hex.encodeHexString) DataRecord(org.apache.jackrabbit.core.data.DataRecord) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 33 with DataIdentifier

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

the class SafeDataStoreBlobStore method getReference.

@Override
public String getReference(@Nonnull String encodedBlobId) {
    checkNotNull(encodedBlobId);
    String blobId = extractBlobId(encodedBlobId);
    //Reference are not created for in memory record
    if (InMemoryDataRecord.isInstance(blobId)) {
        return null;
    }
    DataRecord record;
    try {
        record = delegate.getRecordIfStored(new DataIdentifier(blobId));
        if (record != null) {
            return record.getReference();
        } else {
            log.debug("No blob found for id [{}]", blobId);
        }
    } catch (DataStoreException e) {
        log.warn("Unable to access the blobId for  [{}]", blobId, e);
    }
    return null;
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) InMemoryDataRecord(org.apache.jackrabbit.oak.plugins.blob.datastore.InMemoryDataRecord) DataRecord(org.apache.jackrabbit.core.data.DataRecord)

Example 34 with DataIdentifier

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

the class S3Backend method getAllIdentifiers.

@Override
public Iterator<DataIdentifier> getAllIdentifiers() throws DataStoreException {
    long start = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        Set<DataIdentifier> ids = new HashSet<DataIdentifier>();
        ObjectListing prevObjectListing = s3service.listObjects(bucket);
        while (true) {
            for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
                String id = getIdentifierName(s3ObjSumm.getKey());
                if (id != null) {
                    ids.add(new DataIdentifier(id));
                }
            }
            if (!prevObjectListing.isTruncated())
                break;
            prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing);
        }
        LOG.debug("getAllIdentifiers returned size [{}] took [{}] ms.", ids.size(), (System.currentTimeMillis() - start));
        return ids.iterator();
    } catch (AmazonServiceException e) {
        throw new DataStoreException("Could not list objects", 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) AmazonServiceException(com.amazonaws.AmazonServiceException) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) HashSet(java.util.HashSet)

Example 35 with DataIdentifier

use of org.apache.jackrabbit.core.data.DataIdentifier in project jackrabbit 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()) {
                DataIdentifier identifier = new DataIdentifier(getIdentifierName(s3ObjSumm.getKey()));
                long lastModified = s3ObjSumm.getLastModified().getTime();
                LOG.debug("Identifier [{}]'s lastModified = [{}]", identifier, lastModified);
                if (lastModified < min && getDataStore().confirmDelete(identifier) && //  order is important here
                s3service.getObjectMetadata(bucket, s3ObjSumm.getKey()).getLastModified().getTime() < min) {
                    getDataStore().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)

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