Search in sources :

Example 1 with ORemoteFetchContext

use of com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext in project orientdb by orientechnologies.

the class ONetworkProtocolBinary method readRecord.

protected void readRecord(final OClientConnection connection) throws IOException {
    setDataCommandInfo(connection, "Load record");
    if (!isConnectionAlive(connection))
        return;
    final ORecordId rid = channel.readRID();
    final String fetchPlanString = channel.readString();
    boolean ignoreCache = false;
    ignoreCache = channel.readByte() == 1;
    boolean loadTombstones = false;
    loadTombstones = channel.readByte() > 0;
    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().load(rid, fetchPlanString, ignoreCache, loadTombstones, OStorage.LOCKING_STRATEGY.NONE);
        beginResponse();
        try {
            sendOk(connection, clientTxId);
            if (record != null) {
                // HAS RECORD
                channel.writeByte((byte) 1);
                byte[] bytes = getRecordBytes(connection, record);
                int length = trimCsvSerializedContent(connection, bytes);
                if (connection.getData().protocolVersion <= OChannelBinaryProtocol.PROTOCOL_VERSION_27) {
                    channel.writeBytes(bytes, length);
                    channel.writeVersion(record.getVersion());
                    channel.writeByte(ORecordInternal.getRecordType(record));
                } else {
                    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 2 with ORemoteFetchContext

use of com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext in project orientdb by orientechnologies.

the class OAbstractCommandResultListener method fetchRecord.

protected void fetchRecord(final Object iRecord, final OFetchListener iFetchListener) {
    if (fetchPlan != null && fetchPlan != OFetchHelper.DEFAULT_FETCHPLAN && iRecord instanceof ORecord) {
        final ORecord record = (ORecord) iRecord;
        final OFetchContext context = new ORemoteFetchContext();
        OFetchHelper.fetch(record, record, fetchPlan, iFetchListener, context, "");
    }
}
Also used : OFetchContext(com.orientechnologies.orient.core.fetch.OFetchContext) ORemoteFetchContext(com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext) ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 3 with ORemoteFetchContext

use of com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext 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 4 with ORemoteFetchContext

use of com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext 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 5 with ORemoteFetchContext

use of com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext in project orientdb by orientechnologies.

the class DepthFetchPlanTest method testFullDepthFetchPlan.

@Test
public void testFullDepthFetchPlan() {
    ODatabaseDocument database = new ODatabaseDocumentTx("memory:" + DepthFetchPlanTest.class.getSimpleName());
    database.create();
    try {
        database.getMetadata().getSchema().createClass("Test");
        ODocument doc = new ODocument("Test");
        ODocument doc1 = new ODocument("Test");
        ODocument doc2 = new ODocument("Test");
        ODocument doc3 = new ODocument("Test");
        doc.field("name", "name");
        database.save(doc);
        doc1.field("name", "name1");
        doc1.field("ref", doc);
        database.save(doc1);
        doc2.field("name", "name2");
        doc2.field("ref", doc1);
        database.save(doc2);
        doc3.field("name", "name2");
        doc3.field("ref", doc2);
        database.save(doc3);
        OFetchContext context = new ORemoteFetchContext();
        CountFetchListener listener = new CountFetchListener();
        OFetchHelper.fetch(doc3, doc3, OFetchHelper.buildFetchPlan("[*]ref:-1"), listener, context, "");
        assertEquals(3, listener.count);
    } finally {
        database.drop();
    }
}
Also used : OFetchContext(com.orientechnologies.orient.core.fetch.OFetchContext) ORemoteFetchContext(com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

OFetchContext (com.orientechnologies.orient.core.fetch.OFetchContext)7 ORemoteFetchContext (com.orientechnologies.orient.core.fetch.remote.ORemoteFetchContext)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 ORecord (com.orientechnologies.orient.core.record.ORecord)5 ORemoteFetchListener (com.orientechnologies.orient.core.fetch.remote.ORemoteFetchListener)4 ORecordId (com.orientechnologies.orient.core.id.ORecordId)3 IOException (java.io.IOException)3 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)2 OLockException (com.orientechnologies.common.concur.lock.OLockException)2 OException (com.orientechnologies.common.exception.OException)2 OIOException (com.orientechnologies.common.io.OIOException)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 OFetchListener (com.orientechnologies.orient.core.fetch.OFetchListener)2 OFetchPlan (com.orientechnologies.orient.core.fetch.OFetchPlan)2 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)2 SocketException (java.net.SocketException)2 Test (org.testng.annotations.Test)2