Search in sources :

Example 76 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId 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 77 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId 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 78 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId 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 79 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId 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 80 with ORecordId

use of com.orientechnologies.orient.core.id.ORecordId in project orientdb by orientechnologies.

the class CollectionIndexTest method testIndexCollectionUpdateRemoveItemInTx.

public void testIndexCollectionUpdateRemoveItemInTx() throws Exception {
    Collector collector = new Collector();
    collector.setStringCollection(new ArrayList<String>(Arrays.asList("spam", "eggs")));
    collector = database.save(collector);
    try {
        database.begin();
        Collector loadedCollector = (Collector) database.load(new ORecordId(collector.getId()));
        loadedCollector.getStringCollection().remove("spam");
        loadedCollector = database.save(loadedCollector);
        database.commit();
    } catch (Exception e) {
        database.rollback();
        throw e;
    }
    List<ODocument> result = database.command(new OCommandSQL("select key, rid from index:Collector.stringCollection")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(result.size(), 1);
    for (ODocument d : result) {
        Assert.assertTrue(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
        if (!d.field("key").equals("eggs")) {
            Assert.fail("Unknown key found: " + d.field("key"));
        }
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) Collector(com.orientechnologies.orient.test.domain.whiz.Collector) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecordId (com.orientechnologies.orient.core.id.ORecordId)431 Test (org.testng.annotations.Test)153 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)139 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)120 ORID (com.orientechnologies.orient.core.id.ORID)71 HashSet (java.util.HashSet)63 OIndexCursor (com.orientechnologies.orient.core.index.OIndexCursor)42 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)37 ORecord (com.orientechnologies.orient.core.record.ORecord)37 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)34 OIndexTxAwareMultiValue (com.orientechnologies.orient.core.index.OIndexTxAwareMultiValue)30 OIndexTxAwareOneValue (com.orientechnologies.orient.core.index.OIndexTxAwareOneValue)30 HashMap (java.util.HashMap)29 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)28 IOException (java.io.IOException)25 Child (com.orientechnologies.orient.test.domain.business.Child)24 OException (com.orientechnologies.common.exception.OException)23 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)23 Map (java.util.Map)22 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)21