Search in sources :

Example 86 with ORecordId

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

the class OStorageRemote method createRecord.

public OStorageOperationResult<OPhysicalPosition> createRecord(final ORecordId iRid, final byte[] iContent, final int iRecordVersion, final byte iRecordType, final int iMode, final ORecordCallback<Long> iCallback) {
    final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
    ORecordCallback<OPhysicalPosition> realCallback = null;
    if (iCallback != null) {
        realCallback = new ORecordCallback<OPhysicalPosition>() {

            @Override
            public void call(ORecordId iRID, OPhysicalPosition iParameter) {
                iCallback.call(iRID, iParameter.clusterPosition);
            }
        };
    }
    final ORecordId idCopy = iRid.copy();
    // The Upper layer require to return this also if it not really receive response from the network
    final OPhysicalPosition ppos = new OPhysicalPosition(iRecordType);
    asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(final OChannelBinaryAsynchClient network, final OStorageRemoteSession session, int mode) throws IOException {
            try {
                beginRequest(network, OChannelBinaryProtocol.REQUEST_RECORD_CREATE, session);
                network.writeShort((short) iRid.getClusterId());
                network.writeBytes(iContent);
                network.writeByte(iRecordType);
                network.writeByte((byte) mode);
            } finally {
                endRequest(network);
            }
        }
    }, new OStorageRemoteOperationRead<OPhysicalPosition>() {

        @Override
        public OPhysicalPosition execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            // SYNCHRONOUS
            try {
                beginResponse(network, session);
                // FIRST READ THE ENTIRE RESPONSE
                short clusterId = network.readShort();
                final long clPos = network.readLong();
                final int recVer = network.readVersion();
                final Map<OBonsaiCollectionPointer, OPair<Long, Long>> collectionChanges = readCollectionChanges(network);
                // APPLY CHANGES
                ppos.clusterPosition = clPos;
                ppos.recordVersion = recVer;
                // THIS IS A COMPATIBILITY FIX TO AVOID TO FILL THE CLUSTER ID IN CASE OF ASYNC
                if (iMode == 0) {
                    iRid.setClusterId(clusterId);
                    iRid.setClusterPosition(ppos.clusterPosition);
                }
                idCopy.setClusterId(clusterId);
                idCopy.setClusterPosition(ppos.clusterPosition);
                updateCollection(collectionChanges, collectionManager);
                return ppos;
            } finally {
                endResponse(network);
            }
        }
    }, iMode, idCopy, realCallback, "Error on create record in cluster " + iRid.getClusterId());
    return new OStorageOperationResult<OPhysicalPosition>(ppos);
}
Also used : OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)

Example 87 with ORecordId

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

the class OStorageRemoteAsynchEventListener method onRequest.

public void onRequest(final byte iRequestCode, final Object obj) {
    //Using get status to avoid to check the session.
    if (storage.getStatus() == STATUS.CLOSED)
        return;
    if (iRequestCode == OChannelBinaryProtocol.REQUEST_PUSH_DISTRIB_CONFIG) {
        storage.updateClusterConfiguration(null, (byte[]) obj);
        if (OLogManager.instance().isDebugEnabled()) {
            synchronized (storage.getClusterConfiguration()) {
                OLogManager.instance().debug(this, "Received new cluster configuration: %s", storage.getClusterConfiguration().toJSON("prettyPrint"));
            }
        }
    } else if (iRequestCode == OChannelBinaryProtocol.REQUEST_PUSH_LIVE_QUERY) {
        byte[] bytes = (byte[]) obj;
        DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes));
        Integer id = null;
        try {
            byte what = dis.readByte();
            if (what == 'r') {
                byte op = dis.readByte();
                id = dis.readInt();
                final ORecord record = Orient.instance().getRecordFactoryManager().newInstance(dis.readByte());
                final int version = readVersion(dis);
                final ORecordId rid = readRID(dis);
                final byte[] content = readBytes(dis);
                ORecordInternal.fill(record, rid, version, content, false);
                OLiveResultListener listener = liveQueryListeners.get(id);
                if (listener != null) {
                    listener.onLiveResult(id, new ORecordOperation(record, op));
                } else {
                    OLogManager.instance().warn(this, "Receiving invalid LiveQuery token: " + id);
                }
            } else if (what == 'u') {
                id = dis.readInt();
                OLiveResultListener listener = liveQueryListeners.get(id);
                listener.onUnsubscribe(id);
            }
        } catch (IOException e) {
            if (id != null) {
                OLiveResultListener listener = liveQueryListeners.get(id);
                if (listener != null) {
                    listener.onError(id);
                }
            }
            e.printStackTrace();
        }
    }
    byte op;
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ByteArrayInputStream(java.io.ByteArrayInputStream) ORecord(com.orientechnologies.orient.core.record.ORecord) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) OLiveResultListener(com.orientechnologies.orient.core.sql.query.OLiveResultListener) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 88 with ORecordId

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

the class OStorageRemoteAsyncOperationTest method testSyncCall.

@Test
public void testSyncCall() {
    final CallStatus status = new CallStatus();
    storage.asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session, int mode) throws IOException {
            assertNull(status.status);
            status.status = "write";
        }
    }, new OStorageRemoteOperationRead<Object>() {

        @Override
        public Object execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            assertEquals(status.status, "write");
            status.status = "read";
            return null;
        }
    }, 0, new ORecordId(-1, -1), null, "");
    assertEquals(status.status, "read");
}
Also used : IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.junit.Test)

Example 89 with ORecordId

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

the class OStorageRemoteAsyncOperationTest method testAsyncRead.

@Test
public void testAsyncRead() throws InterruptedException {
    final CallStatus status = new CallStatus();
    final CountDownLatch callBackWait = new CountDownLatch(1);
    final CountDownLatch readDone = new CountDownLatch(1);
    final CountDownLatch callBackDone = new CountDownLatch(1);
    final Object res = new Object();
    storage.asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session, int mode) throws IOException {
            assertNull(status.status);
            status.status = "write";
        }
    }, new OStorageRemoteOperationRead<Object>() {

        @Override
        public Object execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            try {
                if (callBackWait.await(10, TimeUnit.MILLISECONDS))
                    readDone.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return res;
        }
    }, 1, new ORecordId(-1, -1), new ORecordCallback<Object>() {

        @Override
        public void call(ORecordId iRID, Object iParameter) {
            callBackDone.countDown();
        }
    }, "");
    // SBLCK THE CALLBAC THAT SHOULD BE IN ANOTHER THREAD
    callBackWait.countDown();
    boolean called = readDone.await(10, TimeUnit.MILLISECONDS);
    if (!called)
        fail("Read not called");
    called = callBackDone.await(10, TimeUnit.MILLISECONDS);
    if (!called)
        fail("Callback not called");
}
Also used : IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.junit.Test)

Example 90 with ORecordId

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

the class OCommandExecutorSQLSelect method unwind.

private Collection<OIdentifiable> unwind(final OIdentifiable iRecord, final List<String> unwindFields, final OCommandContext iContext) {
    final List<OIdentifiable> result = new ArrayList<OIdentifiable>();
    ODocument doc;
    if (iRecord instanceof ODocument) {
        doc = (ODocument) iRecord;
    } else {
        doc = iRecord.getRecord();
    }
    if (unwindFields.size() == 0) {
        ORecordInternal.setIdentity(doc, new ORecordId(-2, getTemporaryRIDCounter(iContext)));
        result.add(doc);
    } else {
        String firstField = unwindFields.get(0);
        final List<String> nextFields = unwindFields.subList(1, unwindFields.size());
        Object fieldValue = doc.field(firstField);
        if (fieldValue == null || !(fieldValue instanceof Iterable) || fieldValue instanceof ODocument) {
            result.addAll(unwind(doc, nextFields, iContext));
        } else {
            Iterator iterator = ((Iterable) fieldValue).iterator();
            if (!iterator.hasNext()) {
                ODocument unwindedDoc = new ODocument();
                doc.copyTo(unwindedDoc);
                unwindedDoc.field(firstField, (Object) null);
                result.addAll(unwind(unwindedDoc, nextFields, iContext));
            } else {
                do {
                    Object o = iterator.next();
                    ODocument unwindedDoc = new ODocument();
                    doc.copyTo(unwindedDoc);
                    unwindedDoc.field(firstField, o);
                    result.addAll(unwind(unwindedDoc, nextFields, iContext));
                } while (iterator.hasNext());
            }
        }
    }
    return result;
}
Also used : OIdentifiableIterator(com.orientechnologies.orient.core.iterator.OIdentifiableIterator) OMultiCollectionIterator(com.orientechnologies.common.collection.OMultiCollectionIterator) OSortedMultiIterator(com.orientechnologies.common.collection.OSortedMultiIterator) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) 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