Search in sources :

Example 1 with ORecordAbstract

use of com.orientechnologies.orient.core.record.ORecordAbstract in project jnosql-diana-driver by eclipse.

the class DefaultOrientDBDocumentCollectionManagerAsync method delete.

@Override
public void delete(DocumentDeleteQuery query, Consumer<Void> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException {
    requireNonNull(query, "query is required");
    requireNonNull(callBack, "callBack is required");
    ODatabaseDocumentTx tx = pool.acquire();
    DocumentQuery selectQuery = new OrientDBDocumentQuery(query);
    QueryOSQLFactory.QueryResult orientQuery = toAsync(selectQuery, l -> {
        l.forEach(ORecordAbstract::delete);
        callBack.accept(null);
    });
    tx.command(orientQuery.getQuery()).execute(orientQuery.getParams());
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)

Example 2 with ORecordAbstract

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

the class OServerCommandGetFileDownload method execute.

@Override
public boolean execute(OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: fileDownload/<database>/rid/[/<fileName>][/<fileType>].");
    final String fileName = urlParts.length > 3 ? encodeResponseText(urlParts[3]) : "unknown";
    final String fileType = urlParts.length > 5 ? encodeResponseText(urlParts[4]) + '/' + encodeResponseText(urlParts[5]) : (urlParts.length > 4 ? encodeResponseText(urlParts[4]) : "");
    final String rid = urlParts[2];
    iRequest.data.commandInfo = "Download";
    iRequest.data.commandDetail = rid;
    final ORecordAbstract response;
    ODatabaseDocument db = getProfiledDatabaseInstance(iRequest);
    try {
        response = db.load(new ORecordId(rid));
        if (response != null) {
            if (response instanceof OBlob) {
                sendORecordBinaryFileContent(iRequest, iResponse, OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, fileType, (OBlob) response, fileName);
            } else if (response instanceof ODocument) {
                for (OProperty prop : ODocumentInternal.getImmutableSchemaClass(((ODocument) response)).properties()) {
                    if (prop.getType().equals(OType.BINARY))
                        sendBinaryFieldFileContent(iRequest, iResponse, OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, fileType, (byte[]) ((ODocument) response).field(prop.getName()), fileName);
                }
            } else {
                iResponse.send(OHttpUtils.STATUS_INVALIDMETHOD_CODE, "Record requested is not a file nor has a readable schema", OHttpUtils.CONTENT_TEXT_PLAIN, "Record requested is not a file nor has a readable schema", null);
            }
        } else {
            iResponse.send(OHttpUtils.STATUS_INVALIDMETHOD_CODE, "Record requested not exists", OHttpUtils.CONTENT_TEXT_PLAIN, "Record requestes not exists", null);
        }
    } catch (Exception e) {
        iResponse.send(OHttpUtils.STATUS_INTERNALERROR_CODE, OHttpUtils.STATUS_INTERNALERROR_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, e.getMessage(), null);
    } finally {
        if (db != null)
            db.close();
    }
    return false;
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OBlob(com.orientechnologies.orient.core.record.impl.OBlob) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecordId(com.orientechnologies.orient.core.id.ORecordId) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 3 with ORecordAbstract

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

the class OContentRecordConflictStrategy method onUpdate.

@Override
public byte[] onUpdate(OStorage storage, final byte iRecordType, final ORecordId rid, final int iRecordVersion, final byte[] iRecordContent, final AtomicInteger iDatabaseVersion) {
    final boolean hasSameContent;
    if (iRecordType == ODocument.RECORD_TYPE) {
        // No need lock, is already inside a lock.
        OStorageOperationResult<ORawBuffer> res = storage.readRecord(rid, null, false, false, null);
        final ODocument storedRecord = new ODocument(rid).fromStream(res.getResult().getBuffer());
        final ODocument newRecord = new ODocument().fromStream(iRecordContent);
        final ODatabaseDocumentInternal currentDb = ODatabaseRecordThreadLocal.INSTANCE.get();
        hasSameContent = ODocumentHelper.hasSameContentOf(storedRecord, currentDb, newRecord, currentDb, null, false);
    } else {
        // CHECK BYTE PER BYTE
        final ORecordAbstract storedRecord = rid.getRecord();
        hasSameContent = Arrays.equals(storedRecord.toStream(), iRecordContent);
    }
    if (hasSameContent)
        // OK
        iDatabaseVersion.set(Math.max(iDatabaseVersion.get(), iRecordVersion));
    else
        // NO DOCUMENT, CANNOT MERGE SO RELY TO THE VERSION CHECK
        checkVersions(rid, iRecordVersion, iDatabaseVersion.get());
    return null;
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 4 with ORecordAbstract

use of com.orientechnologies.orient.core.record.ORecordAbstract in project hale by halestudio.

the class OGroup method configureDocument.

private void configureDocument(ORecordAbstract<?> document, ODatabaseRecord db, DefinitionGroup definition) {
    // document.setDatabase(db);
    if (document instanceof ODocument) {
        // reset class name
        ODocument doc = (ODocument) document;
        /*
			 * Attention: Two long class names cause problems as file names will
			 * be based on them.
			 */
        String className = null;
        if (definition != null) {
            className = ONamespaceMap.encode(determineName(definition));
        } else if (doc.containsField(OSerializationHelper.BINARY_WRAPPER_FIELD) || doc.containsField(OSerializationHelper.FIELD_SERIALIZATION_TYPE)) {
            className = OSerializationHelper.BINARY_WRAPPER_CLASSNAME;
        }
        if (className != null) {
            OSchema schema = db.getMetadata().getSchema();
            if (!schema.existsClass(className)) {
                // if the class doesn't exist yet, create a physical cluster
                // manually for it
                int cluster = db.addCluster(className, CLUSTER_TYPE.PHYSICAL);
                schema.createClass(className, cluster);
            }
            doc.setClassName(className);
        }
        // configure children
        for (Entry<String, Object> field : doc) {
            List<ODocument> docs = new ArrayList<ODocument>();
            List<ORecordAbstract<?>> recs = new ArrayList<ORecordAbstract<?>>();
            if (field.getValue() instanceof Collection<?>) {
                for (Object value : (Collection<?>) field.getValue()) {
                    if (value instanceof ODocument && !getSpecialFieldNames().contains(field.getKey())) {
                        docs.add((ODocument) value);
                    } else if (value instanceof ORecordAbstract<?>) {
                        recs.add((ORecordAbstract<?>) value);
                    }
                }
            } else if (field.getValue() instanceof ODocument && !getSpecialFieldNames().contains(field.getKey())) {
                docs.add((ODocument) field.getValue());
            } else if (field.getValue() instanceof ORecordAbstract<?>) {
                recs.add((ORecordAbstract<?>) field.getValue());
            }
            if (definition != null) {
                for (ODocument valueDoc : docs) {
                    ChildDefinition<?> child = definition.getChild(decodeProperty(field.getKey()));
                    DefinitionGroup childGroup;
                    if (child.asProperty() != null) {
                        childGroup = child.asProperty().getPropertyType();
                    } else if (child.asGroup() != null) {
                        childGroup = child.asGroup();
                    } else {
                        throw new IllegalStateException("Document is associated neither with a property nor a property group.");
                    }
                    configureDocument(valueDoc, db, childGroup);
                }
            }
            for (ORecordAbstract<?> fieldRec : recs) {
                configureDocument(fieldRec, db, null);
            }
        }
    }
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ArrayList(java.util.ArrayList) DefinitionGroup(eu.esdihumboldt.hale.common.schema.model.DefinitionGroup) Collection(java.util.Collection) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 5 with ORecordAbstract

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

the class OObjectProxyMethodHandler method lazyLoadField.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected Object lazyLoadField(final Object self, final String fieldName, Object docValue, final Object currentValue) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    boolean customSerialization = false;
    final Field f = OObjectEntitySerializer.getField(fieldName, self.getClass());
    if (f == null)
        return currentValue;
    if (OObjectEntitySerializer.isSerializedType(f)) {
        customSerialization = true;
    }
    if (docValue instanceof OIdentifiable) {
        if (OIdentifiable.class.isAssignableFrom(f.getType())) {
            if (ORecordAbstract.class.isAssignableFrom(f.getType())) {
                ORecordAbstract record = ((OIdentifiable) docValue).getRecord();
                OObjectEntitySerializer.setFieldValue(f, self, record);
                return record;
            } else {
                OObjectEntitySerializer.setFieldValue(f, self, docValue);
                return docValue;
            }
        } else {
            docValue = convertDocumentToObject((ODocument) ((OIdentifiable) docValue).getRecord(), self);
        }
    } else if (docValue instanceof Collection<?>) {
        docValue = manageCollectionLoad(f, self, docValue, customSerialization);
    } else if (docValue instanceof Map<?, ?>) {
        docValue = manageMapLoad(f, self, docValue, customSerialization);
    } else if (docValue.getClass().isArray() && !docValue.getClass().getComponentType().isPrimitive()) {
        docValue = manageArrayLoad(docValue, f);
    } else if (customSerialization) {
        docValue = OObjectEntitySerializer.deserializeFieldValue(OObjectEntitySerializer.getField(fieldName, self.getClass()).getType(), docValue);
    } else {
        if (f.getType().isEnum()) {
            if (docValue instanceof Number)
                docValue = ((Class<Enum>) f.getType()).getEnumConstants()[((Number) docValue).intValue()];
            else
                docValue = Enum.valueOf((Class<Enum>) f.getType(), docValue.toString());
        }
    }
    OObjectEntitySerializer.setFieldValue(f, self, docValue);
    return docValue;
}
Also used : ORecordAbstract(com.orientechnologies.orient.core.record.ORecordAbstract) Field(java.lang.reflect.Field) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) OObjectCustomSerializerMap(com.orientechnologies.orient.object.serialization.OObjectCustomSerializerMap) HashMap(java.util.HashMap) OObjectEnumLazyMap(com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) OObjectLazyMap(com.orientechnologies.orient.object.db.OObjectLazyMap) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecordAbstract (com.orientechnologies.orient.core.record.ORecordAbstract)8 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)7 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 HashSet (java.util.HashSet)2 Test (org.testng.annotations.Test)2 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 ORecordLazyMap (com.orientechnologies.orient.core.db.record.ORecordLazyMap)1 OTrackedMap (com.orientechnologies.orient.core.db.record.OTrackedMap)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)1 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)1 OBlob (com.orientechnologies.orient.core.record.impl.OBlob)1 ORawBuffer (com.orientechnologies.orient.core.storage.ORawBuffer)1 OObjectLazyMap (com.orientechnologies.orient.object.db.OObjectLazyMap)1 OObjectEnumLazyMap (com.orientechnologies.orient.object.enumerations.OObjectEnumLazyMap)1 OObjectCustomSerializerMap (com.orientechnologies.orient.object.serialization.OObjectCustomSerializerMap)1