Search in sources :

Example 1 with DataIdentifier

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

the class BLOBInDataStore method getInstance.

static BLOBInDataStore getInstance(DataStore store, InputStream in) throws DataStoreException {
    DataRecord rec = store.addRecord(in);
    DataIdentifier identifier = rec.getIdentifier();
    if (auditLogger.isDebugEnabled()) {
        auditLogger.debug("{} ({})", identifier, rec.getLength());
    }
    return new BLOBInDataStore(store, identifier);
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) DataRecord(org.apache.jackrabbit.core.data.DataRecord)

Example 2 with DataIdentifier

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

the class InternalValue method create.

/**
     * Create a new internal value from the given JCR value.
     * If the data store is enabled, large binary values are stored in the data store.
     *
     * @param value the JCR value
     * @param resolver
     * @param store the data store
     * @return the created internal value
     * @throws RepositoryException
     * @throws ValueFormatException
     */
public static InternalValue create(Value value, NamePathResolver resolver, DataStore store) throws ValueFormatException, RepositoryException {
    switch(value.getType()) {
        case PropertyType.BINARY:
            BLOBFileValue blob = null;
            if (value instanceof BinaryValueImpl) {
                BinaryValueImpl bin = (BinaryValueImpl) value;
                DataIdentifier identifier = bin.getDataIdentifier();
                if (identifier != null) {
                    if (bin.usesDataStore(store)) {
                        // access the record to ensure it is not garbage collected
                        store.getRecord(identifier);
                        blob = BLOBInDataStore.getInstance(store, identifier);
                    } else {
                        if (store.getRecordIfStored(identifier) != null) {
                            // it exists - so we don't need to stream it again
                            // but we need to create a new object because the original
                            // one might be in a different data store (repository)
                            blob = BLOBInDataStore.getInstance(store, identifier);
                        }
                    }
                }
            }
            if (blob == null) {
                Binary b = value.getBinary();
                boolean dispose = false;
                try {
                    if (b instanceof BLOBFileValue) {
                        // use as is
                        blob = (BLOBFileValue) b;
                    } else {
                        // create a copy from the stream
                        dispose = true;
                        blob = getBLOBFileValue(store, b.getStream(), true);
                    }
                } finally {
                    if (dispose) {
                        b.dispose();
                    }
                }
            }
            return new InternalValue(blob);
        case PropertyType.BOOLEAN:
            return create(value.getBoolean());
        case PropertyType.DATE:
            return create(value.getDate());
        case PropertyType.DOUBLE:
            return create(value.getDouble());
        case PropertyType.DECIMAL:
            return create(value.getDecimal());
        case PropertyType.LONG:
            return create(value.getLong());
        case PropertyType.REFERENCE:
            return create(new NodeId(value.getString()));
        case PropertyType.WEAKREFERENCE:
            return create(new NodeId(value.getString()), true);
        case PropertyType.URI:
            try {
                return create(new URI(value.getString()));
            } catch (URISyntaxException e) {
                throw new ValueFormatException(e.getMessage());
            }
        case PropertyType.NAME:
            try {
                if (value instanceof QValueValue) {
                    QValue qv = ((QValueValue) value).getQValue();
                    if (qv instanceof InternalValue) {
                        return (InternalValue) qv;
                    } else {
                        return create(qv.getName());
                    }
                } else {
                    return create(resolver.getQName(value.getString()));
                }
            } catch (NameException e) {
                throw new ValueFormatException(e.getMessage());
            }
        case PropertyType.PATH:
            try {
                if (value instanceof QValueValue) {
                    QValue qv = ((QValueValue) value).getQValue();
                    if (qv instanceof InternalValue) {
                        return (InternalValue) qv;
                    } else {
                        return create(qv.getPath());
                    }
                } else {
                    return create(resolver.getQPath(value.getString(), false));
                }
            } catch (MalformedPathException mpe) {
                throw new ValueFormatException(mpe.getMessage());
            }
        case PropertyType.STRING:
            return create(value.getString());
        default:
            throw new IllegalArgumentException("illegal value");
    }
}
Also used : QValueValue(org.apache.jackrabbit.spi.commons.value.QValueValue) AbstractQValue(org.apache.jackrabbit.spi.commons.value.AbstractQValue) QValue(org.apache.jackrabbit.spi.QValue) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeId(org.apache.jackrabbit.core.id.NodeId) ValueFormatException(javax.jcr.ValueFormatException) Binary(javax.jcr.Binary)

Example 3 with DataIdentifier

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

the class ValueFactoryImpl method createValue.

@Override
public Value createValue(Binary binary) {
    try {
        if (binary instanceof BLOBInDataStore) {
            BLOBInDataStore blob = (BLOBInDataStore) binary;
            DataIdentifier identifier = blob.getDataIdentifier();
            InternalValue value;
            if (blob.usesDataStore(store)) {
                value = InternalValue.getInternalValue(identifier, store, false);
            } else {
                value = InternalValue.getInternalValue(identifier, store, true);
            }
            if (value != null) {
                // if the value is already in this data store
                return new BinaryValueImpl(value.getBLOBFileValue());
            }
        } else if (binary instanceof BLOBFileValue) {
            return new BinaryValueImpl(((BLOBFileValue) binary).copy());
        } else if (binary instanceof ReferenceBinary) {
            String reference = ((ReferenceBinary) binary).getReference();
            DataRecord record = store.getRecordFromReference(reference);
            if (record != null) {
                return new BinaryValueImpl(BLOBInDataStore.getInstance(store, record.getIdentifier()));
            }
        }
        return createValue(binary.getStream());
    } catch (RepositoryException e) {
        log.error(e.getMessage(), e);
    // ignore - the super method may be smarter
    }
    return super.createValue(binary);
}
Also used : DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) RepositoryException(javax.jcr.RepositoryException) DataRecord(org.apache.jackrabbit.core.data.DataRecord) ReferenceBinary(org.apache.jackrabbit.api.ReferenceBinary)

Example 4 with DataIdentifier

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

the class DataStoreBlobStore 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) DataRecord(org.apache.jackrabbit.core.data.DataRecord)

Example 5 with DataIdentifier

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

the class OakFileDataStore method getAllRecords.

@Override
public Iterator<DataRecord> getAllRecords() {
    final String path = normalizeNoEndSeparator(new File(getPath()).getAbsolutePath());
    final OakFileDataStore store = this;
    return Files.fileTreeTraverser().postOrderTraversal(new File(path)).filter(new Predicate<File>() {

        @Override
        public boolean apply(File input) {
            return input.isFile() && !input.getParent().equals(path);
        }
    }).transform(new Function<File, DataRecord>() {

        @Override
        public DataRecord apply(File input) {
            return new FileDataRecord(store, new DataIdentifier(input.getName()), input);
        }
    }).iterator();
}
Also used : Function(com.google.common.base.Function) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) FileDataRecord(org.apache.jackrabbit.core.data.FileDataRecord) File(java.io.File)

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