Search in sources :

Example 16 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class Account method fromStream.

@OAfterDeserialization
public void fromStream(final ODocument iDocument) {
    initialized = true;
    if (iDocument.containsField("externalPhoto")) {
        // READ THE PHOTO FROM AN EXTERNAL RECORD AS PURE BINARY
        OBlob extRecord = iDocument.field("externalPhoto");
        photo = extRecord.toStream();
    }
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) OAfterDeserialization(com.orientechnologies.orient.core.annotation.OAfterDeserialization)

Example 17 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class ODatabaseDocumentTx method assignAndCheckCluster.

public int assignAndCheckCluster(ORecord record, String iClusterName) {
    ORecordId rid = (ORecordId) record.getIdentity();
    // if provided a cluster name use it.
    if (rid.getClusterId() <= ORID.CLUSTER_POS_INVALID && iClusterName != null) {
        rid.setClusterId(getClusterIdByName(iClusterName));
        if (rid.getClusterId() == -1)
            throw new IllegalArgumentException("Cluster name '" + iClusterName + "' is not configured");
    }
    OClass schemaClass = null;
    // if cluster id is not set yet try to find it out
    if (rid.getClusterId() <= ORID.CLUSTER_ID_INVALID && storage.isAssigningClusterIds()) {
        if (record instanceof ODocument) {
            schemaClass = ODocumentInternal.getImmutableSchemaClass(((ODocument) record));
            if (schemaClass != null) {
                if (schemaClass.isAbstract())
                    throw new OSchemaException("Document belongs to abstract class " + schemaClass.getName() + " and cannot be saved");
                rid.setClusterId(schemaClass.getClusterForNewInstance((ODocument) record));
            } else
                rid.setClusterId(getDefaultClusterId());
        } else {
            rid.setClusterId(getDefaultClusterId());
            if (record instanceof OBlob && rid.getClusterId() != ORID.CLUSTER_ID_INVALID) {
            // Set<Integer> blobClusters = getMetadata().getSchema().getBlobClusters();
            // if (!blobClusters.contains(rid.clusterId) && rid.clusterId != getDefaultClusterId() && rid.clusterId != 0) {
            // if (iClusterName == null)
            // iClusterName = getClusterNameById(rid.clusterId);
            // throw new IllegalArgumentException(
            // "Cluster name '" + iClusterName + "' (id=" + rid.clusterId + ") is not configured to store blobs, valid are "
            // + blobClusters.toString());
            // }
            }
        }
    } else if (record instanceof ODocument)
        schemaClass = ODocumentInternal.getImmutableSchemaClass(((ODocument) record));
    // If the cluster id was set check is validity
    if (rid.getClusterId() > ORID.CLUSTER_ID_INVALID) {
        if (schemaClass != null) {
            String messageClusterName = getClusterNameById(rid.getClusterId());
            checkRecordClass(schemaClass, messageClusterName, rid);
            if (!schemaClass.hasClusterId(rid.getClusterId())) {
                throw new IllegalArgumentException("Cluster name '" + messageClusterName + "' (id=" + rid.getClusterId() + ") is not configured to store the class '" + schemaClass.getName() + "', valid are " + Arrays.toString(schemaClass.getClusterIds()));
            }
        }
    }
    return rid.getClusterId();
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 18 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class OrientElement method getProperty.

/**
   * Returns a Property value.
   * 
   * @param key
   *          Property name
   * @return Property value if any, otherwise NULL.
   */
@Override
public <T> T getProperty(final String key) {
    if (key == null)
        return null;
    final OrientBaseGraph graph = getGraph();
    if (key.equals("_class"))
        return (T) ODocumentInternal.getImmutableSchemaClass(getRecord()).getName();
    else if (key.equals("_version"))
        return (T) new Integer(getRecord().getVersion());
    else if (key.equals("_rid"))
        return (T) rawElement.getIdentity().toString();
    final ODocument record = getRecord();
    if (record == null)
        // NO RECORD
        return null;
    final Object fieldValue = record.field(key);
    if (graph != null && fieldValue instanceof OIdentifiable && !(((OIdentifiable) fieldValue).getRecord() instanceof OBlob)) {
        ODocument fieldRecord = ((OIdentifiable) fieldValue).getRecord();
        if (fieldRecord != null) {
            final OClass schemaClass = fieldRecord.getSchemaClass();
            if (schemaClass != null && (schemaClass.isVertexType() || schemaClass.isEdgeType())) {
                // CONVERT IT TO VERTEX/EDGE
                return (T) graph.getElement(fieldValue);
            }
        }
        return (T) fieldValue;
    } else if (!(fieldValue instanceof Map) && OMultiValue.isMultiValue(fieldValue) && OMultiValue.getFirstValue(fieldValue) instanceof OIdentifiable) {
        final OIdentifiable firstValue = (OIdentifiable) OMultiValue.getFirstValue(fieldValue);
        if (firstValue instanceof ODocument) {
            final ODocument document = (ODocument) firstValue;
            /// clusterId -2 Is considered a projection so does not have a class but is a not embedded record
            if (document.getIdentity().getClusterId() != -2 && (document.isEmbedded() || ODocumentInternal.getImmutableSchemaClass(document) == null))
                return (T) fieldValue;
        }
        if (graph != null)
            // CONVERT IT TO ITERABLE<VERTEX/EDGE>
            return (T) new OrientElementIterable<OrientElement>(graph, OMultiValue.getMultiValueIterable(fieldValue));
    }
    return (T) fieldValue;
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 19 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class TransactionOptimisticTest method testTransactionOptimisticRollback.

@Test
public void testTransactionOptimisticRollback() throws IOException {
    if (database.getClusterIdByName("binary") == -1)
        database.addBlobCluster("binary");
    long rec = database.countClusterElements("binary");
    database.begin();
    OBlob recordBytes = new ORecordBytes("This is the first version".getBytes());
    recordBytes.save("binary");
    database.rollback();
    Assert.assertEquals(database.countClusterElements("binary"), rec);
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) Test(org.testng.annotations.Test)

Example 20 with OBlob

use of com.orientechnologies.orient.core.record.impl.OBlob in project orientdb by orientechnologies.

the class TransactionOptimisticTest method testTransactionOptimisticConcurrentException.

@Test(dependsOnMethods = "testTransactionOptimisticCommit")
public void testTransactionOptimisticConcurrentException() throws IOException {
    if (database.getClusterIdByName("binary") == -1)
        database.addBlobCluster("binary");
    ODatabaseDocumentTx db2 = new ODatabaseDocumentTx(database.getURL());
    db2.open("admin", "admin");
    database.activateOnCurrentThread();
    OBlob record1 = new ORecordBytes("This is the first version".getBytes());
    record1.save("binary");
    try {
        database.begin();
        // RE-READ THE RECORD
        record1.load();
        ODatabaseRecordThreadLocal.INSTANCE.set(db2);
        OBlob record2 = db2.load(record1.getIdentity());
        record2.setDirty();
        record2.fromStream("This is the second version".getBytes());
        record2.save();
        ODatabaseRecordThreadLocal.INSTANCE.set(database);
        record1.setDirty();
        record1.fromStream("This is the third version".getBytes());
        record1.save();
        database.commit();
        Assert.assertTrue(false);
    } catch (OConcurrentModificationException e) {
        Assert.assertTrue(true);
        database.rollback();
    } finally {
        database.close();
        db2.activateOnCurrentThread();
        db2.close();
    }
}
Also used : OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ORecordBytes(com.orientechnologies.orient.core.record.impl.ORecordBytes) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) Test(org.testng.annotations.Test)

Aggregations

OBlob (com.orientechnologies.orient.core.record.impl.OBlob)32 ORecordBytes (com.orientechnologies.orient.core.record.impl.ORecordBytes)20 Test (org.testng.annotations.Test)19 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)12 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)8 ORID (com.orientechnologies.orient.core.id.ORID)4 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)4 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)3 IOException (java.io.IOException)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 BufferedInputStream (java.io.BufferedInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 OAfterDeserialization (com.orientechnologies.orient.core.annotation.OAfterDeserialization)1 OBeforeSerialization (com.orientechnologies.orient.core.annotation.OBeforeSerialization)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)1