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();
}
}
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();
}
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;
}
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);
}
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();
}
}
Aggregations