Search in sources :

Example 1 with OSBTreeCollectionManager

use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager in project orientdb by orientechnologies.

the class ORidBag method toStream.

public int toStream(BytesContainer bytesContainer) throws OSerializationException {
    final ORecordSerializationContext context = ORecordSerializationContext.getContext();
    if (context != null) {
        if (isEmbedded() && ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager() != null && delegate.size() >= topThreshold) {
            ORidBagDelegate oldDelegate = delegate;
            delegate = new OSBTreeRidBag();
            boolean oldAutoConvert = oldDelegate.isAutoConvertToRecord();
            oldDelegate.setAutoConvertToRecord(false);
            for (OIdentifiable identifiable : oldDelegate) delegate.add(identifiable);
            final ORecord owner = oldDelegate.getOwner();
            delegate.setOwner(owner);
            for (OMultiValueChangeListener<OIdentifiable, OIdentifiable> listener : oldDelegate.getChangeListeners()) delegate.addChangeListener(listener);
            owner.setDirty();
            oldDelegate.setAutoConvertToRecord(oldAutoConvert);
            oldDelegate.requestDelete();
        } else if (bottomThreshold >= 0 && !isEmbedded() && delegate.size() <= bottomThreshold) {
            ORidBagDelegate oldDelegate = delegate;
            boolean oldAutoConvert = oldDelegate.isAutoConvertToRecord();
            oldDelegate.setAutoConvertToRecord(false);
            delegate = new OEmbeddedRidBag();
            for (OIdentifiable identifiable : oldDelegate) delegate.add(identifiable);
            final ORecord owner = oldDelegate.getOwner();
            delegate.setOwner(owner);
            for (OMultiValueChangeListener<OIdentifiable, OIdentifiable> listener : oldDelegate.getChangeListeners()) delegate.addChangeListener(listener);
            owner.setDirty();
            oldDelegate.setAutoConvertToRecord(oldAutoConvert);
            oldDelegate.requestDelete();
        }
    }
    final UUID oldUuid = uuid;
    final OSBTreeCollectionManager sbTreeCollectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
    if (sbTreeCollectionManager != null)
        uuid = sbTreeCollectionManager.listenForChanges(this);
    else
        uuid = null;
    boolean hasUuid = uuid != null;
    final int serializedSize = OByteSerializer.BYTE_SIZE + delegate.getSerializedSize() + ((hasUuid) ? OUUIDSerializer.UUID_SIZE : 0);
    int pointer = bytesContainer.alloc(serializedSize);
    int offset = pointer;
    final byte[] stream = bytesContainer.bytes;
    byte configByte = 0;
    if (isEmbedded())
        configByte |= 1;
    if (hasUuid)
        configByte |= 2;
    stream[offset++] = configByte;
    if (hasUuid) {
        OUUIDSerializer.INSTANCE.serialize(uuid, stream, offset);
        offset += OUUIDSerializer.UUID_SIZE;
    }
    delegate.serialize(stream, offset, oldUuid);
    return pointer;
}
Also used : OEmbeddedRidBag(com.orientechnologies.orient.core.db.record.ridbag.embedded.OEmbeddedRidBag) ORecordSerializationContext(com.orientechnologies.orient.core.storage.impl.local.paginated.ORecordSerializationContext) OSBTreeRidBag(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager) ORecord(com.orientechnologies.orient.core.record.ORecord) UUID(java.util.UUID)

Example 2 with OSBTreeCollectionManager

use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager 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 3 with OSBTreeCollectionManager

use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager in project orientdb by orientechnologies.

the class OStorageRemote method updateRecord.

public OStorageOperationResult<Integer> updateRecord(final ORecordId iRid, final boolean updateContent, final byte[] iContent, final int iVersion, final byte iRecordType, final int iMode, final ORecordCallback<Integer> iCallback) {
    final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
    Integer resVersion = asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(final OChannelBinaryAsynchClient network, final OStorageRemoteSession session, int mode) throws IOException {
            try {
                beginRequest(network, OChannelBinaryProtocol.REQUEST_RECORD_UPDATE, session);
                network.writeRID(iRid);
                network.writeBoolean(updateContent);
                network.writeBytes(iContent);
                network.writeVersion(iVersion);
                network.writeByte(iRecordType);
                network.writeByte((byte) mode);
            } finally {
                endRequest(network);
            }
        }
    }, new OStorageRemoteOperationRead<Integer>() {

        @Override
        public Integer execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            try {
                beginResponse(network, session);
                final Integer r = network.readVersion();
                final Map<OBonsaiCollectionPointer, OPair<Long, Long>> collectionChanges = readCollectionChanges(network);
                updateCollection(collectionChanges, collectionManager);
                return r;
            } finally {
                endResponse(network);
            }
        }
    }, iMode, iRid, iCallback, "Error on update record " + iRid);
    if (resVersion == null)
        // Returning given version in case of no answer from server
        resVersion = iVersion;
    return new OStorageOperationResult<Integer>(resVersion);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)

Example 4 with OSBTreeCollectionManager

use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager in project orientdb by orientechnologies.

the class ConcurrencySBTreeBonsaiLocalTest method testName.

@Test
public void testName() throws Exception {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx("memory:" + ConcurrencySBTreeBonsaiLocalTest.class.getName());
    db.create();
    ExecutorService exec = Executors.newCachedThreadPool();
    try {
        OSBTreeCollectionManager coll = db.getSbTreeCollectionManager();
        OBonsaiCollectionPointer treePointer = coll.createSBTree(3, null);
        OSBTreeBonsaiLocal<OIdentifiable, Integer> tree = (OSBTreeBonsaiLocal<OIdentifiable, Integer>) coll.loadSBTree(treePointer);
        OBonsaiCollectionPointer treePointer1 = coll.createSBTree(3, null);
        final OSBTreeBonsaiLocal<OIdentifiable, Integer> tree1 = (OSBTreeBonsaiLocal<OIdentifiable, Integer>) coll.loadSBTree(treePointer1);
        final OAtomicOperationsManager atomManager = ((OAbstractPaginatedStorage) db.getStorage()).getAtomicOperationsManager();
        atomManager.startAtomicOperation(tree, false);
        for (int i = 1000; i < 2000; i++) tree.put(new ORecordId(10, i), 1);
        Future<?> ex = null;
        try {
            ex = exec.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        atomManager.startAtomicOperation(tree1, false);
                        for (int i = 2000; i < 3000; i++) tree1.put(new ORecordId(10, i), 1);
                        atomManager.endAtomicOperation(false, null, tree1);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            ex.get(10, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
        // Is supposed to go in deadlock correct that goes in timeout
        }
        atomManager.endAtomicOperation(false, null, tree);
        ex.get();
        OSBTreeRidBag bag = new OSBTreeRidBag();
        bag.setCollectionPointer(tree.getCollectionPointer());
        bag.setAutoConvertToRecord(false);
        Assert.assertEquals(tree.size(), 1000);
        for (OIdentifiable id : bag) {
            if (id.getIdentity().getClusterPosition() > 2000)
                Assert.fail("found a wrong rid in the ridbag");
        }
        OSBTreeRidBag secondBag = new OSBTreeRidBag();
        secondBag.setAutoConvertToRecord(false);
        secondBag.setCollectionPointer(tree1.getCollectionPointer());
        Assert.assertEquals(tree1.size(), 1000);
        for (OIdentifiable id : secondBag) {
            if (id.getIdentity().getClusterPosition() < 2000)
                Assert.fail("found a wrong rid in the ridbag");
        }
    } finally {
        exec.shutdown();
        db.drop();
    }
}
Also used : OBonsaiCollectionPointer(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OBonsaiCollectionPointer) OSBTreeRidBag(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId) TimeoutException(java.util.concurrent.TimeoutException) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager) OSBTreeBonsaiLocal(com.orientechnologies.orient.core.index.sbtreebonsai.local.OSBTreeBonsaiLocal) ExecutorService(java.util.concurrent.ExecutorService) OAtomicOperationsManager(com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperationsManager) OAbstractPaginatedStorage(com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 5 with OSBTreeCollectionManager

use of com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager in project orientdb by orientechnologies.

the class ONetworkProtocolBinary method sendCollectionChanges.

private void sendCollectionChanges(OClientConnection connection) throws IOException {
    OSBTreeCollectionManager collectionManager = connection.getDatabase().getSbTreeCollectionManager();
    if (collectionManager != null) {
        Map<UUID, OBonsaiCollectionPointer> changedIds = collectionManager.changedIds();
        channel.writeInt(changedIds.size());
        for (Entry<UUID, OBonsaiCollectionPointer> entry : changedIds.entrySet()) {
            UUID id = entry.getKey();
            channel.writeLong(id.getMostSignificantBits());
            channel.writeLong(id.getLeastSignificantBits());
            OCollectionNetworkSerializer.INSTANCE.writeCollectionPointer(channel, entry.getValue());
        }
        collectionManager.clearChangedIds();
    }
}
Also used : OBonsaiCollectionPointer(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OBonsaiCollectionPointer) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)

Aggregations

OSBTreeCollectionManager (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)10 OBonsaiCollectionPointer (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OBonsaiCollectionPointer)6 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)5 OIOException (com.orientechnologies.common.io.OIOException)3 ORecordId (com.orientechnologies.orient.core.id.ORecordId)3 IOException (java.io.IOException)3 OChannelBinaryAsynchClient (com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)2 OSBTreeRidBag (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeRidBag)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)1 OLockException (com.orientechnologies.common.concur.lock.OLockException)1 OException (com.orientechnologies.common.exception.OException)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OEmbeddedRidBag (com.orientechnologies.orient.core.db.record.ridbag.embedded.OEmbeddedRidBag)1 OTreeInternal (com.orientechnologies.orient.core.index.sbtree.OTreeInternal)1 OSBTreeBonsaiLocal (com.orientechnologies.orient.core.index.sbtreebonsai.local.OSBTreeBonsaiLocal)1 OAbstractPaginatedStorage (com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage)1 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)1 ORecordSerializationContext (com.orientechnologies.orient.core.storage.impl.local.paginated.ORecordSerializationContext)1 OAtomicOperationsManager (com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperationsManager)1