Search in sources :

Example 91 with OIdentifiable

use of com.orientechnologies.orient.core.db.record.OIdentifiable 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 92 with OIdentifiable

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

the class OAsyncCommandResultListener method linkdedBySimpleValue.

@Override
public void linkdedBySimpleValue(ODocument doc) {
    ORemoteFetchListener listener = new ORemoteFetchListener() {

        @Override
        protected void sendRecord(ORecord iLinked) {
            if (!alreadySent.contains(iLinked.getIdentity())) {
                alreadySent.add(iLinked.getIdentity());
                try {
                    // CACHE IT ON THE CLIENT
                    protocol.channel.writeByte((byte) 2);
                    protocol.writeIdentifiable(connection, iLinked);
                } catch (IOException e) {
                    OLogManager.instance().error(this, "Cannot write against channel", e);
                }
            }
        }

        @Override
        public void parseLinked(ODocument iRootRecord, OIdentifiable iLinked, Object iUserObject, String iFieldName, OFetchContext iContext) throws OFetchException {
            if (iLinked instanceof ORecord)
                sendRecord((ORecord) iLinked);
        }

        @Override
        public void parseLinkedCollectionValue(ODocument iRootRecord, OIdentifiable iLinked, Object iUserObject, String iFieldName, OFetchContext iContext) throws OFetchException {
            if (iLinked instanceof ORecord)
                sendRecord((ORecord) iLinked);
        }
    };
    final OFetchContext context = new ORemoteFetchContext();
    OFetchHelper.fetch(doc, doc, OFetchHelper.buildFetchPlan(""), listener, context, "");
}
Also used : OFetchContext(com.orientechnologies.orient.core.fetch.OFetchContext) ORemoteFetchListener(com.orientechnologies.orient.core.fetch.remote.ORemoteFetchListener) ORemoteFetchContext(com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext) ORecord(com.orientechnologies.orient.core.record.ORecord) IOException(java.io.IOException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 93 with OIdentifiable

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

the class OServerCommandGetCluster method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: cluster/<database>/<cluster-name>[/<limit>]<br>Limit is optional and is setted to 20 by default. Set expressely to 0 to have no limits.");
    iRequest.data.commandInfo = "Browse cluster";
    iRequest.data.commandDetail = urlParts[2];
    ODatabaseDocument db = null;
    try {
        db = getProfiledDatabaseInstance(iRequest);
        if (db.getClusterIdByName(urlParts[2]) > -1) {
            final int limit = urlParts.length > 3 ? Integer.parseInt(urlParts[3]) : 20;
            final List<OIdentifiable> response = new ArrayList<OIdentifiable>();
            for (ORecord rec : db.browseCluster(urlParts[2])) {
                if (limit > 0 && response.size() >= limit)
                    break;
                response.add(rec);
            }
            iResponse.writeRecords(response);
        } else
            iResponse.send(OHttpUtils.STATUS_NOTFOUND_CODE, null, null, null, null);
    } finally {
        if (db != null)
            db.close();
    }
    return false;
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecord(com.orientechnologies.orient.core.record.ORecord) ArrayList(java.util.ArrayList) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 94 with OIdentifiable

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

the class OServerCommandGetIndex method execute.

@SuppressWarnings("unchecked")
@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>");
    iRequest.data.commandInfo = "Index get";
    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 Object content = index.get(urlParts[3]);
        if (content == null)
            iResponse.send(OHttpUtils.STATUS_NOTFOUND_CODE, OHttpUtils.STATUS_NOTFOUND_DESCRIPTION, OHttpUtils.CONTENT_TEXT_PLAIN, null, null);
        else {
            final StringBuilder buffer = new StringBuilder(128);
            buffer.append('[');
            if (content instanceof Collection<?>) {
                Collection<OIdentifiable> collection = (Collection<OIdentifiable>) content;
                int count = 0;
                for (OIdentifiable item : collection) {
                    if (count > 0) {
                        buffer.append(", ");
                    }
                    buffer.append(item.getRecord().toJSON());
                    count++;
                }
            } else
                buffer.append(((OIdentifiable) content).getRecord().toJSON());
            buffer.append(']');
            if (isJsonResponse(iResponse)) {
                iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_JSON, buffer.toString(), null);
            } else {
                iResponse.send(OHttpUtils.STATUS_OK_CODE, "OK", OHttpUtils.CONTENT_TEXT_PLAIN, buffer.toString(), null);
            }
        }
    } finally {
        if (db != null)
            db.close();
    }
    return false;
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) Collection(java.util.Collection) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 95 with OIdentifiable

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

the class SQLDeleteEdgeTest method testFromInString.

public void testFromInString() {
    database.command(new OCommandSQL("CREATE CLASS FromInStringE extends E")).execute();
    database.command(new OCommandSQL("CREATE CLASS FromInStringV extends V")).execute();
    OIdentifiable v1 = database.command(new OCommandSQL("create vertex FromInStringV set name = ' from '")).execute();
    OIdentifiable v2 = database.command(new OCommandSQL("create vertex FromInStringV set name = ' FROM '")).execute();
    OIdentifiable v3 = database.command(new OCommandSQL("create vertex FromInStringV set name = ' TO '")).execute();
    database.command(new OCommandSQL("create edge FromInStringE from " + v1.getIdentity() + " to " + v2.getIdentity())).execute();
    database.command(new OCommandSQL("create edge FromInStringE from " + v1.getIdentity() + " to " + v3.getIdentity())).execute();
    List<OIdentifiable> result = database.query(new OSQLSynchQuery<ODocument>("SELECT expand(out()[name = ' FROM ']) FROM FromInStringV"));
    Assert.assertEquals(result.size(), 1);
    result = database.query(new OSQLSynchQuery<ODocument>("SELECT expand(in()[name = ' from ']) FROM FromInStringV"));
    Assert.assertEquals(result.size(), 2);
    result = database.query(new OSQLSynchQuery<ODocument>("SELECT expand(out()[name = ' TO ']) FROM FromInStringV"));
    Assert.assertEquals(result.size(), 1);
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)536 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)278 ORecordId (com.orientechnologies.orient.core.id.ORecordId)120 Test (org.testng.annotations.Test)104 HashSet (java.util.HashSet)89 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)79 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)70 ORID (com.orientechnologies.orient.core.id.ORID)56 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)47 Test (org.junit.Test)43 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)42 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)41 ArrayList (java.util.ArrayList)39 ORecord (com.orientechnologies.orient.core.record.ORecord)35 Map (java.util.Map)31 ByteBuffer (java.nio.ByteBuffer)28 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)26 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)22 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)22 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)21