Search in sources :

Example 41 with ORecord

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

the class OServerCommandPutIndex method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    final String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: index/<database>/<index-name>/<key>[/<value>]");
    iRequest.data.commandInfo = "Index put";
    ODatabaseDocument db = null;
    try {
        db = getProfiledDatabaseInstance(iRequest);
        final OIndex<?> index = db.getMetadata().getIndexManager().getIndex(urlParts[2]);
        if (index == null)
            throw new IllegalArgumentException("Index name '" + urlParts[2] + "' not found");
        final OIdentifiable record;
        if (urlParts.length > 4)
            // GET THE RECORD ID AS VALUE
            record = new ORecordId(urlParts[4]);
        else {
            // GET THE REQUEST CONTENT AS DOCUMENT
            if (iRequest.content == null || iRequest.content.length() == 0)
                throw new IllegalArgumentException("Index's entry value is null");
            record = new ODocument().fromJSON(iRequest.content);
        }
        final OIndexDefinition indexDefinition = index.getDefinition();
        final Object key;
        if (indexDefinition != null)
            key = indexDefinition.createValue(urlParts[3]);
        else
            key = urlParts[3];
        if (key == null)
            throw new IllegalArgumentException("Invalid key value : " + urlParts[3]);
        final boolean existent = record.getIdentity().isPersistent();
        if (existent && record instanceof ORecord)
            ((ORecord) record).save();
        index.put(key, record);
        if (existent)
            iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
        else
            iResponse.send(OHttpUtils.STATUS_CREATED_CODE, OHttpUtils.STATUS_CREATED_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
    } finally {
        if (db != null)
            db.close();
    }
    return false;
}
Also used : OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecord(com.orientechnologies.orient.core.record.ORecord) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 42 with ORecord

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

the class SQLInsertTest method getValidPositions.

private List<Long> getValidPositions(int clusterId) {
    final List<Long> positions = new ArrayList<Long>();
    final ORecordIteratorCluster<?> iteratorCluster = database.browseCluster(database.getClusterNameById(clusterId));
    for (int i = 0; i < 100; i++) {
        if (!iteratorCluster.hasNext())
            break;
        ORecord doc = iteratorCluster.next();
        positions.add(doc.getIdentity().getClusterPosition());
    }
    return positions;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 43 with ORecord

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

the class OTraverseRecordSetProcess method process.

@SuppressWarnings("unchecked")
public OIdentifiable process() {
    while (target.hasNext()) {
        record = target.next();
        index++;
        final ORecord rec = record.getRecord();
        if (rec instanceof ODocument) {
            ODocument doc = (ODocument) rec;
            if (doc.getIdentity().getClusterId() == -2 && doc.fields() == 1) {
                // projection
                // EXTRACT THE FIELD CONTEXT
                Object fieldvalue = doc.field(doc.fieldNames()[0]);
                if (fieldvalue instanceof Collection<?>) {
                    command.getContext().push(new OTraverseRecordSetProcess(command, ((Collection<OIdentifiable>) fieldvalue).iterator(), getPath()));
                } else if (fieldvalue instanceof ODocument) {
                    command.getContext().push(new OTraverseRecordProcess(command, (ODocument) fieldvalue, getPath()));
                }
            } else {
                command.getContext().push(new OTraverseRecordProcess(command, (ODocument) rec, getPath()));
            }
            return null;
        }
    }
    return pop();
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) Collection(java.util.Collection) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 44 with ORecord

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

the class ORecordSerializerCSVAbstract method embeddedMapToStream.

public void embeddedMapToStream(ODatabase<?> iDatabase, final OUserObject2RecordHandler iObjHandler, final StringBuilder iOutput, final OClass iLinkedClass, OType iLinkedType, final Object iValue, final boolean iSaveOnlyDirty) {
    iOutput.append(OStringSerializerHelper.MAP_BEGIN);
    if (iValue != null) {
        int items = 0;
        // EMBEDDED OBJECTS
        for (Entry<String, Object> o : ((Map<String, Object>) iValue).entrySet()) {
            if (items > 0)
                iOutput.append(OStringSerializerHelper.RECORD_SEPARATOR);
            if (o != null) {
                fieldTypeToString(iOutput, OType.STRING, o.getKey());
                iOutput.append(OStringSerializerHelper.ENTRY_SEPARATOR);
                if (o.getValue() instanceof ODocument && ((ODocument) o.getValue()).getIdentity().isValid()) {
                    fieldTypeToString(iOutput, OType.LINK, o.getValue());
                } else if (o.getValue() instanceof ORecord || o.getValue() instanceof ODocumentSerializable) {
                    final ODocument record;
                    if (o.getValue() instanceof ODocument)
                        record = (ODocument) o.getValue();
                    else if (o.getValue() instanceof ODocumentSerializable) {
                        record = ((ODocumentSerializable) o.getValue()).toDocument();
                        record.field(ODocumentSerializable.CLASS_NAME, o.getValue().getClass().getName());
                    } else {
                        if (iDatabase == null && ODatabaseRecordThreadLocal.INSTANCE.isDefined())
                            iDatabase = ODatabaseRecordThreadLocal.INSTANCE.get();
                        record = OObjectSerializerHelperManager.getInstance().toStream(o.getValue(), new ODocument(o.getValue().getClass().getSimpleName()), iDatabase instanceof ODatabaseObject ? ((ODatabaseObject) iDatabase).getEntityManager() : OEntityManagerInternal.INSTANCE, iLinkedClass, iObjHandler != null ? iObjHandler : new OUserObject2RecordHandler() {

                            public Object getUserObjectByRecord(OIdentifiable iRecord, final String iFetchPlan) {
                                return iRecord;
                            }

                            public ORecord getRecordByUserObject(Object iPojo, boolean iCreateIfNotAvailable) {
                                return new ODocument(iLinkedClass);
                            }

                            public boolean existsUserObjectByRID(ORID iRID) {
                                return false;
                            }

                            public void registerUserObject(Object iObject, ORecord iRecord) {
                            }

                            public void registerUserObjectAfterLinkSave(ORecord iRecord) {
                            }
                        }, null, iSaveOnlyDirty);
                    }
                    iOutput.append(OStringSerializerHelper.EMBEDDED_BEGIN);
                    toString(record, iOutput, null, iObjHandler, false, true);
                    iOutput.append(OStringSerializerHelper.EMBEDDED_END);
                } else if (o.getValue() instanceof Set<?>) {
                    // SUB SET
                    fieldTypeToString(iOutput, OType.EMBEDDEDSET, o.getValue());
                } else if (o.getValue() instanceof Collection<?>) {
                    // SUB LIST
                    fieldTypeToString(iOutput, OType.EMBEDDEDLIST, o.getValue());
                } else if (o.getValue() instanceof Map<?, ?>) {
                    // SUB MAP
                    fieldTypeToString(iOutput, OType.EMBEDDEDMAP, o.getValue());
                } else {
                    // EMBEDDED LITERALS
                    if (iLinkedType == null && o.getValue() != null) {
                        fieldTypeToString(iOutput, OType.getTypeByClass(o.getValue().getClass()), o.getValue());
                    } else {
                        fieldTypeToString(iOutput, iLinkedType, o.getValue());
                    }
                }
            }
            items++;
        }
    }
    iOutput.append(OStringSerializerHelper.MAP_END);
}
Also used : OTrackedSet(com.orientechnologies.orient.core.db.record.OTrackedSet) Set(java.util.Set) ORecordLazySet(com.orientechnologies.orient.core.db.record.ORecordLazySet) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ORecord(com.orientechnologies.orient.core.record.ORecord) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ODocumentSerializable(com.orientechnologies.orient.core.serialization.ODocumentSerializable) OUserObject2RecordHandler(com.orientechnologies.orient.core.db.OUserObject2RecordHandler) ORID(com.orientechnologies.orient.core.id.ORID) OTrackedMap(com.orientechnologies.orient.core.db.record.OTrackedMap) ORecordLazyMap(com.orientechnologies.orient.core.db.record.ORecordLazyMap) Map(java.util.Map) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 45 with ORecord

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

the class ORecordSerializerNetworkV0 method writeOptimizedLink.

private int writeOptimizedLink(final BytesContainer bytes, OIdentifiable link) {
    if (!link.getIdentity().isPersistent()) {
        final ORecord real = link.getRecord();
        if (real != null)
            link = real;
    }
    final int pos = OVarIntSerializer.write(bytes, link.getIdentity().getClusterId());
    OVarIntSerializer.write(bytes, link.getIdentity().getClusterPosition());
    return pos;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord)

Aggregations

ORecord (com.orientechnologies.orient.core.record.ORecord)194 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)91 ORecordId (com.orientechnologies.orient.core.id.ORecordId)40 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)36 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)27 ORID (com.orientechnologies.orient.core.id.ORID)27 IOException (java.io.IOException)21 OException (com.orientechnologies.common.exception.OException)17 Test (org.junit.Test)16 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)15 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)15 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)15 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)14 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)8 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)8 Test (org.testng.annotations.Test)8 OIOException (com.orientechnologies.common.io.OIOException)7