Search in sources :

Example 41 with DataIdentifier

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

the class AbstractSharedCachingDataStore method init.

public void init(String homeDir) throws DataStoreException {
    if (path == null) {
        path = homeDir + "/repository/datastore";
    }
    path = FilenameUtils.normalizeNoEndSeparator(new File(path).getAbsolutePath());
    checkArgument(stagingSplitPercentage >= 0 && stagingSplitPercentage <= 50, "Staging percentage cache should be between 0 and 50");
    this.rootDirectory = new File(path);
    this.tmp = new File(rootDirectory, "tmp");
    LOG.trace("Temporary file created [{}]", tmp.mkdirs());
    this.backend = createBackend();
    backend.init();
    String home = FilenameUtils.normalizeNoEndSeparator(new File(homeDir).getAbsolutePath());
    this.cache = new CompositeDataStoreCache(path, new File(home), cacheSize, stagingSplitPercentage, uploadThreads, new CacheLoader<String, InputStream>() {

        @Override
        public InputStream load(String key) throws Exception {
            return backend.read(new DataIdentifier(key));
        }
    }, new StagingUploader() {

        @Override
        public void write(String id, File file) throws DataStoreException {
            backend.write(new DataIdentifier(id), file);
        }
    }, statisticsProvider, listeningExecutor, schedulerExecutor, executor, stagingPurgeInterval, stagingRetryInterval);
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) CacheLoader(com.google.common.cache.CacheLoader) File(java.io.File)

Example 42 with DataIdentifier

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

the class AbstractSharedCachingDataStore method addRecord.

@Override
public DataRecord addRecord(InputStream inputStream, BlobOptions blobOptions) throws DataStoreException {
    Stopwatch watch = Stopwatch.createStarted();
    try {
        TransientFileFactory fileFactory = TransientFileFactory.getInstance();
        File tmpFile = fileFactory.createTransientFile("upload", null, tmp);
        // Copy the stream to the temporary file and calculate the
        // stream length and the message digest of the stream
        MessageDigest digest = MessageDigest.getInstance(DIGEST);
        OutputStream output = new DigestOutputStream(new FileOutputStream(tmpFile), digest);
        long length = 0;
        try {
            length = IOUtils.copyLarge(inputStream, output);
        } finally {
            output.close();
        }
        DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest()));
        LOG.debug("SHA-256 of [{}], length =[{}] took [{}] ms ", identifier, length, watch.elapsed(TimeUnit.MILLISECONDS));
        // otherwise add to backend
        if (blobOptions.getUpload() == SYNCHRONOUS || !cache.stage(identifier.toString(), tmpFile)) {
            backend.write(identifier, tmpFile);
            LOG.info("Added blob [{}] to backend", identifier);
            // offer to download cache
            cache.getDownloadCache().put(identifier.toString(), tmpFile);
        }
        return getRecordIfStored(identifier);
    } catch (Exception e) {
        LOG.error("Error in adding record");
        throw new DataStoreException("Error in adding record ", e);
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) Stopwatch(com.google.common.base.Stopwatch) MessageDigest(java.security.MessageDigest) File(java.io.File) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException)

Example 43 with DataIdentifier

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

the class S3Backend method getMetadataRecord.

@Override
public DataRecord getMetadataRecord(String name) {
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        ObjectMetadata meta = s3service.getObjectMetadata(bucket, addMetaKeyPrefix(name));
        return new S3DataRecord(this, s3service, bucket, new DataIdentifier(name), meta.getLastModified().getTime(), meta.getContentLength(), true);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 44 with DataIdentifier

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

the class S3Backend method getAllMetadataRecords.

@Override
public List<DataRecord> getAllMetadataRecords(String prefix) {
    List<DataRecord> metadataList = new ArrayList<DataRecord>();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(addMetaKeyPrefix(prefix));
        ObjectListing prevObjectListing = s3service.listObjects(listObjectsRequest);
        for (final S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
            metadataList.add(new S3DataRecord(this, s3service, bucket, new DataIdentifier(stripMetaKeyPrefix(s3ObjSumm.getKey())), s3ObjSumm.getLastModified().getTime(), s3ObjSumm.getSize(), true));
        }
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    return metadataList;
}
Also used : ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) 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) AbstractDataRecord(org.apache.jackrabbit.oak.spi.blob.AbstractDataRecord) DataRecord(org.apache.jackrabbit.core.data.DataRecord)

Example 45 with DataIdentifier

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

the class AbstractDataStoreTest method doDeleteAllOlderThan.

/**
     * Asserts that {@link DataStore#deleteAllOlderThan(long)} only deleted
     * records older than argument passed.
     */
protected void doDeleteAllOlderThan() throws Exception {
    Random random = randomGen;
    byte[] data = new byte[dataLength];
    random.nextBytes(data);
    DataRecord rec1 = ds.addRecord(new ByteArrayInputStream(data));
    data = new byte[dataLength];
    random.nextBytes(data);
    DataRecord rec2 = ds.addRecord(new ByteArrayInputStream(data));
    // sleep for some time to ensure that async upload completes in backend.
    sleep(10000);
    long updateTime = System.currentTimeMillis();
    ds.updateModifiedDateOnAccess(updateTime);
    // sleep to workaround System.currentTimeMillis granularity.
    sleep(3000);
    data = new byte[dataLength];
    random.nextBytes(data);
    DataRecord rec3 = ds.addRecord(new ByteArrayInputStream(data));
    data = new byte[dataLength];
    random.nextBytes(data);
    DataRecord rec4 = ds.addRecord(new ByteArrayInputStream(data));
    rec1 = ds.getRecord(rec1.getIdentifier());
    ds.clearInUse();
    Assert.assertEquals("only rec2 should be deleted", 1, ds.deleteAllOlderThan(updateTime));
    assertNull("rec2 should be null", ds.getRecordIfStored(rec2.getIdentifier()));
    Iterator<DataIdentifier> itr = ds.getAllIdentifiers();
    List<DataIdentifier> list = new ArrayList<DataIdentifier>();
    list.add(rec1.getIdentifier());
    list.add(rec3.getIdentifier());
    list.add(rec4.getIdentifier());
    while (itr.hasNext()) {
        assertTrue("record found on list", list.remove(itr.next()));
    }
    Assert.assertEquals("touched records found", 0, list.size());
    Assert.assertEquals("rec1 touched", true, rec1.getLastModified() > updateTime);
    Assert.assertEquals("rec3 touched", true, rec3.getLastModified() > updateTime);
    Assert.assertEquals("rec4 touched", true, rec4.getLastModified() > updateTime);
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) DataRecord(org.apache.jackrabbit.core.data.DataRecord)

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