Search in sources :

Example 76 with ORecord

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

the class OServerCommandGetDictionary method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    iRequest.data.commandInfo = "Dictionary lookup";
    String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: dictionary/<database>/<key>");
    ODatabaseDocument db = null;
    try {
        db = getProfiledDatabaseInstance(iRequest);
        final ORecord record = db.getDictionary().get(urlParts[2]);
        if (record == null)
            throw new ORecordNotFoundException(null, "Key '" + urlParts[2] + "' was not found in the database dictionary");
        iResponse.writeRecord(record);
    } 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) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException)

Example 77 with ORecord

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

the class OServerCommandGetDocument method execute.

@Override
public boolean execute(final OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    ODatabaseDocument db = null;
    final String[] urlParts = checkSyntax(iRequest.url, 3, "Syntax error: document/<database>/<record-id>[/fetchPlan]");
    final String fetchPlan = urlParts.length > 3 ? urlParts[3] : null;
    iRequest.data.commandInfo = "Load document";
    final ORecord rec;
    final int parametersPos = urlParts[2].indexOf('?');
    final String rid = parametersPos > -1 ? urlParts[2].substring(0, parametersPos) : urlParts[2];
    try {
        db = getProfiledDatabaseInstance(iRequest);
        rec = db.load(new ORecordId(rid), fetchPlan);
        if (rec == null)
            iResponse.send(OHttpUtils.STATUS_NOTFOUND_CODE, "Not Found", OHttpUtils.CONTENT_JSON, "Record with id '" + urlParts[2] + "' was not found.", null);
        else if (iRequest.httpMethod.equals("HEAD"))
            // JUST SEND HTTP CODE 200
            iResponse.send(OHttpUtils.STATUS_OK_CODE, OHttpUtils.STATUS_OK_DESCRIPTION, null, null, OHttpUtils.HEADER_ETAG + rec.getVersion());
        else {
            final String ifNoneMatch = iRequest.getHeader("If-None-Match");
            if (ifNoneMatch != null && Integer.toString(rec.getVersion()).equals(ifNoneMatch)) {
                // SAME CONTENT, DON'T SEND BACK RECORD
                iResponse.send(OHttpUtils.STATUS_OK_NOMODIFIED_CODE, OHttpUtils.STATUS_OK_NOMODIFIED_DESCRIPTION, null, null, OHttpUtils.HEADER_ETAG + rec.getVersion());
            }
            // SEND THE DOCUMENT BACK
            iResponse.writeRecord(rec, fetchPlan, 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) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 78 with ORecord

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

the class ONetworkProtocolBinary method createRecord.

protected void createRecord(OClientConnection connection) throws IOException {
    setDataCommandInfo(connection, "Create record");
    if (!isConnectionAlive(connection))
        return;
    final int dataSegmentId = connection.getData().protocolVersion < 24 ? channel.readInt() : 0;
    final ORecordId rid = new ORecordId(channel.readShort(), ORID.CLUSTER_POS_INVALID);
    final byte[] buffer = channel.readBytes();
    final byte recordType = channel.readByte();
    final byte mode = channel.readByte();
    final ORecord record = createRecord(connection, rid, buffer, recordType);
    if (mode < 2) {
        beginResponse();
        try {
            sendOk(connection, clientTxId);
            if (connection.getData().protocolVersion > OChannelBinaryProtocol.PROTOCOL_VERSION_25)
                channel.writeShort((short) record.getIdentity().getClusterId());
            channel.writeLong(record.getIdentity().getClusterPosition());
            channel.writeVersion(record.getVersion());
            if (connection.getData().protocolVersion >= 20)
                sendCollectionChanges(connection);
        } finally {
            endResponse(connection);
        }
    }
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 79 with ORecord

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

the class ONetworkProtocolBinary method readRecordIfVersionIsNotLatest.

protected void readRecordIfVersionIsNotLatest(final OClientConnection connection) throws IOException {
    setDataCommandInfo(connection, "Load record if version is not latest");
    if (!isConnectionAlive(connection))
        return;
    final ORecordId rid = channel.readRID();
    final int recordVersion = channel.readVersion();
    final String fetchPlanString = channel.readString();
    boolean ignoreCache = channel.readByte() == 1;
    if (rid.getClusterId() == 0 && rid.getClusterPosition() == 0) {
        // @COMPATIBILITY 0.9.25
        // SEND THE DB CONFIGURATION INSTEAD SINCE IT WAS ON RECORD 0:0
        OFetchHelper.checkFetchPlanValid(fetchPlanString);
        beginResponse();
        try {
            sendOk(connection, clientTxId);
            channel.writeByte((byte) 1);
            final byte[] storageStream = connection.getDatabase().getStorage().callInLock(new Callable<byte[]>() {

                @Override
                public byte[] call() throws Exception {
                    return connection.getDatabase().getStorage().getConfiguration().toStream(connection.getData().protocolVersion);
                }
            }, false);
            if (connection.getData().protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_27) {
                channel.writeBytes(storageStream);
                channel.writeVersion(0);
                channel.writeByte(OBlob.RECORD_TYPE);
            } else {
                channel.writeByte(OBlob.RECORD_TYPE);
                channel.writeVersion(0);
                channel.writeBytes(storageStream);
            }
            // NO MORE RECORDS
            channel.writeByte((byte) 0);
        } finally {
            endResponse(connection);
        }
    } else {
        final ORecord record = connection.getDatabase().loadIfVersionIsNotLatest(rid, recordVersion, fetchPlanString, ignoreCache);
        beginResponse();
        try {
            sendOk(connection, clientTxId);
            if (record != null) {
                // HAS RECORD
                channel.writeByte((byte) 1);
                byte[] bytes = getRecordBytes(connection, record);
                int length = trimCsvSerializedContent(connection, bytes);
                channel.writeByte(ORecordInternal.getRecordType(record));
                channel.writeVersion(record.getVersion());
                channel.writeBytes(bytes, length);
                if (fetchPlanString.length() > 0) {
                    // PLAN
                    if (record instanceof ODocument) {
                        final OFetchPlan fetchPlan = OFetchHelper.buildFetchPlan(fetchPlanString);
                        final Set<ORecord> recordsToSend = new HashSet<ORecord>();
                        final ODocument doc = (ODocument) record;
                        final OFetchListener listener = new ORemoteFetchListener() {

                            @Override
                            protected void sendRecord(ORecord iLinked) {
                                recordsToSend.add(iLinked);
                            }
                        };
                        final OFetchContext context = new ORemoteFetchContext();
                        OFetchHelper.fetch(doc, doc, fetchPlan, listener, context, "");
                        // SEND RECORDS TO LOAD IN CLIENT CACHE
                        for (ORecord d : recordsToSend) {
                            if (d.getIdentity().isValid()) {
                                // CLIENT CACHE
                                channel.writeByte((byte) 2);
                                // RECORD. IT ISN'T PART OF THE RESULT SET
                                writeIdentifiable(connection, d);
                            }
                        }
                    }
                }
            }
            // NO MORE RECORDS
            channel.writeByte((byte) 0);
        } finally {
            endResponse(connection);
        }
    }
}
Also used : OFetchContext(com.orientechnologies.orient.core.fetch.OFetchContext) OFetchPlan(com.orientechnologies.orient.core.fetch.OFetchPlan) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OLockException(com.orientechnologies.common.concur.lock.OLockException) OException(com.orientechnologies.common.exception.OException) SocketException(java.net.SocketException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) OIOException(com.orientechnologies.common.io.OIOException) OOfflineClusterException(com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException) IOException(java.io.IOException) ORemoteFetchListener(com.orientechnologies.orient.core.fetch.remote.ORemoteFetchListener) ORemoteFetchContext(com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext) ORecord(com.orientechnologies.orient.core.record.ORecord) OFetchListener(com.orientechnologies.orient.core.fetch.OFetchListener) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 80 with ORecord

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

the class OMatchStatement method addResult.

private boolean addResult(MatchContext matchContext, OSQLAsynchQuery<ODocument> request, OCommandContext ctx) {
    ODocument doc = null;
    if (returnsElements()) {
        for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
            if (isExplicitAlias(entry.getKey()) && entry.getValue() != null) {
                ORecord record = entry.getValue().getRecord();
                if (request.getResultListener() != null && record != null) {
                    if (!addSingleResult(request, (OBasicCommandContext) ctx, record))
                        return false;
                }
            }
        }
    } else if (returnsPathElements()) {
        for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
            if (entry.getValue() != null) {
                ORecord record = entry.getValue().getRecord();
                if (request.getResultListener() != null && record != null) {
                    if (!addSingleResult(request, (OBasicCommandContext) ctx, record))
                        return false;
                }
            }
        }
    } else if (returnsPatterns()) {
        doc = getDatabase().newInstance();
        doc.setTrackingChanges(false);
        for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
            if (isExplicitAlias(entry.getKey())) {
                doc.field(entry.getKey(), entry.getValue());
            }
        }
    } else if (returnsPaths()) {
        doc = getDatabase().newInstance();
        doc.setTrackingChanges(false);
        for (Map.Entry<String, OIdentifiable> entry : matchContext.matched.entrySet()) {
            doc.field(entry.getKey(), entry.getValue());
        }
    } else if (returnsJson()) {
        doc = jsonToDoc(matchContext, ctx);
    } else {
        doc = getDatabase().newInstance();
        doc.setTrackingChanges(false);
        int i = 0;
        ODocument mapDoc = new ODocument();
        mapDoc.setTrackingChanges(false);
        mapDoc.fromMap((Map) matchContext.matched);
        ctx.setVariable("$current", mapDoc);
        for (OExpression item : returnItems) {
            OIdentifier returnAliasIdentifier = returnAliases.get(i);
            OIdentifier returnAlias;
            if (returnAliasIdentifier == null) {
                returnAlias = item.getDefaultAlias();
            } else {
                returnAlias = returnAliasIdentifier;
            }
            doc.field(returnAlias.getStringValue(), item.execute(mapDoc, ctx));
            i++;
        }
        doc.setTrackingChanges(true);
    }
    if (request.getResultListener() != null && doc != null) {
        if (!addSingleResult(request, (OBasicCommandContext) ctx, doc))
            return false;
    }
    return true;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecord (com.orientechnologies.orient.core.record.ORecord)177 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)80 ORecordId (com.orientechnologies.orient.core.id.ORecordId)37 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)35 ORID (com.orientechnologies.orient.core.id.ORID)24 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)19 IOException (java.io.IOException)18 Test (org.junit.Test)15 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)14 OException (com.orientechnologies.common.exception.OException)13 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)13 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)12 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)11 ArrayList (java.util.ArrayList)10 Test (org.testng.annotations.Test)8 OIOException (com.orientechnologies.common.io.OIOException)7 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)7 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)7 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)6 ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)6