Search in sources :

Example 61 with ORecordId

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

the class OSBTreeBonsaiLocalTest method testKeyDelete.

public void testKeyDelete() throws Exception {
    for (int i = 0; i < KEYS_COUNT; i++) {
        sbTree.put(i, new ORecordId(i % 32000, i));
    }
    for (int i = 0; i < KEYS_COUNT; i++) {
        if (i % 3 == 0)
            Assert.assertEquals(sbTree.remove(i), new ORecordId(i % 32000, i));
    }
    Assert.assertEquals((int) sbTree.firstKey(), 1);
    Assert.assertEquals((int) sbTree.lastKey(), (KEYS_COUNT - 1) % 3 == 0 ? KEYS_COUNT - 2 : KEYS_COUNT - 1);
    for (int i = 0; i < KEYS_COUNT; i++) {
        if (i % 3 == 0)
            Assert.assertNull(sbTree.get(i));
        else
            Assert.assertEquals(sbTree.get(i), new ORecordId(i % 32000, i));
    }
}
Also used : ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 62 with ORecordId

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

the class OSBTreeBonsaiLocalTest method testKeyAddDelete.

public void testKeyAddDelete() throws Exception {
    for (int i = 0; i < KEYS_COUNT; i++) {
        sbTree.put(i, new ORecordId(i % 32000, i));
        Assert.assertEquals(sbTree.get(i), new ORecordId(i % 32000, i));
    }
    for (int i = 0; i < KEYS_COUNT; i++) {
        if (i % 3 == 0)
            Assert.assertEquals(sbTree.remove(i), new ORecordId(i % 32000, i));
        if (i % 2 == 0)
            sbTree.put(KEYS_COUNT + i, new ORecordId((KEYS_COUNT + i) % 32000, KEYS_COUNT + i));
    }
    Assert.assertEquals((int) sbTree.firstKey(), 1);
    Assert.assertEquals((int) sbTree.lastKey(), 2 * KEYS_COUNT - 2);
    for (int i = 0; i < KEYS_COUNT; i++) {
        if (i % 3 == 0)
            Assert.assertNull(sbTree.get(i));
        else
            Assert.assertEquals(sbTree.get(i), new ORecordId(i % 32000, i));
        if (i % 2 == 0)
            Assert.assertEquals(sbTree.get(KEYS_COUNT + i), new ORecordId((KEYS_COUNT + i) % 32000, KEYS_COUNT + i));
    }
}
Also used : ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 63 with ORecordId

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

the class ODistributedStorage method executeRecordOperationInLock.

private Object executeRecordOperationInLock(final boolean iUnlockAtTheEnd, final ORecordId rid, final OCallable<Object, OCallable<Void, ODistributedRequestId>> callback) throws Exception {
    final ORecordId rid2Lock;
    if (!rid.isPersistent())
        // CREATE A COPY TO MAINTAIN THE LOCK ON THE CLUSTER AVOIDING THE RID IS TRANSFORMED IN PERSISTENT. THIS ALLOWS TO HAVE
        // PARALLEL TX BECAUSE NEW RID LOCKS THE ENTIRE CLUSTER.
        rid2Lock = new ORecordId(rid.getClusterId(), -1l);
    else
        rid2Lock = rid;
    ODistributedRequestId requestId = null;
    final OLogSequenceNumber lastLSN = wrapped.getLSN();
    final AtomicBoolean lockReleased = new AtomicBoolean(false);
    try {
        requestId = acquireRecordLock(rid2Lock);
        final ODistributedRequestId finalReqId = requestId;
        final OCallable<Void, ODistributedRequestId> unlockCallback = new OCallable<Void, ODistributedRequestId>() {

            @Override
            public Void call(final ODistributedRequestId requestId) {
                // UNLOCK AS SOON AS THE REQUEST IS SENT
                if (lockReleased.compareAndSet(false, true)) {
                    releaseRecordLock(rid2Lock, finalReqId);
                    lockReleased.set(true);
                }
                return null;
            }
        };
        return OScenarioThreadLocal.executeAsDistributed(new Callable() {

            @Override
            public Object call() throws Exception {
                return callback.call(unlockCallback);
            }
        });
    } finally {
        if (iUnlockAtTheEnd) {
            if (lockReleased.compareAndSet(false, true)) {
                releaseRecordLock(rid2Lock, requestId);
            }
        }
        final OLogSequenceNumber currentLSN = wrapped.getLSN();
        if (!lastLSN.equals(currentLSN))
            // SAVE LAST LSN
            try {
                localDistributedDatabase.getSyncConfiguration().setLastLSN(getDistributedManager().getLocalNodeName(), ((OLocalPaginatedStorage) getUnderlying()).getLSN(), true);
            } catch (IOException e) {
                ODistributedServerLog.debug(this, dManager != null ? dManager.getLocalNodeName() : "?", null, ODistributedServerLog.DIRECTION.NONE, "Error on updating local LSN configuration for database '%s'", wrapped.getName());
            }
    }
}
Also used : OLocalPaginatedStorage(com.orientechnologies.orient.core.storage.impl.local.paginated.OLocalPaginatedStorage) OIOException(com.orientechnologies.common.io.OIOException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OCallable(com.orientechnologies.common.util.OCallable) Callable(java.util.concurrent.Callable) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) OException(com.orientechnologies.common.exception.OException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) HazelcastException(com.hazelcast.core.HazelcastException) ODistributedRedirectException(com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException) OIOException(com.orientechnologies.common.io.OIOException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OLogSequenceNumber(com.orientechnologies.orient.core.storage.impl.local.paginated.wal.OLogSequenceNumber) OCallable(com.orientechnologies.common.util.OCallable)

Example 64 with ORecordId

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

the class ODistributedTransactionManager method getInvolvedClusters.

protected Set<String> getInvolvedClusters(final List<ORecordOperation> uResult) {
    final Set<String> involvedClusters = new HashSet<String>();
    for (ORecordOperation op : uResult) {
        final ORecord record = op.getRecord();
        involvedClusters.add(storage.getClusterNameByRID((ORecordId) record.getIdentity()));
    }
    return involvedClusters;
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 65 with ORecordId

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

the class ODistributedTransactionManager method createUndoTasksFromTx.

/**
   * Create undo content for distributed 2-phase rollback. This list of undo tasks is sent to all the nodes to revert a transaction
   * and it's also applied locally.
   *
   * @param iTx Current transaction
   *
   * @return List of remote undo tasks
   */
protected List<OAbstractRemoteTask> createUndoTasksFromTx(final OTransaction iTx) {
    final List<OAbstractRemoteTask> undoTasks = new ArrayList<OAbstractRemoteTask>();
    for (ORecordOperation op : iTx.getAllRecordEntries()) {
        OAbstractRemoteTask undoTask = null;
        final ORecord record = op.getRecord();
        switch(op.type) {
            case ORecordOperation.CREATED:
                // CREATE UNDO TASK LATER ONCE THE RID HAS BEEN ASSIGNED
                break;
            case ORecordOperation.UPDATED:
            case ORecordOperation.DELETED:
                // CREATE UNDO TASK WITH THE PREVIOUS RECORD CONTENT/VERSION
                final ORecordId rid = (ORecordId) record.getIdentity();
                final AtomicReference<ORecord> previousRecord = new AtomicReference<ORecord>();
                OScenarioThreadLocal.executeAsDefault(new Callable<Object>() {

                    @Override
                    public Object call() throws Exception {
                        final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
                        final ORecordOperation txEntry = db.getTransaction().getRecordEntry(rid);
                        if (txEntry != null && txEntry.type == ORecordOperation.DELETED)
                            // GET DELETED RECORD FROM TX
                            previousRecord.set(txEntry.getRecord());
                        else {
                            final OStorageOperationResult<ORawBuffer> loadedBuffer = ODatabaseRecordThreadLocal.INSTANCE.get().getStorage().getUnderlying().readRecord(rid, null, true, false, null);
                            if (loadedBuffer != null) {
                                // LOAD THE RECORD FROM THE STORAGE AVOIDING USING THE DB TO GET THE TRANSACTIONAL CHANGES
                                final ORecord loaded = Orient.instance().getRecordFactoryManager().newInstance(loadedBuffer.getResult().recordType);
                                ORecordInternal.fill(loaded, rid, loadedBuffer.getResult().version, loadedBuffer.getResult().getBuffer(), false);
                                previousRecord.set(loaded);
                            } else
                                // RECORD NOT FOUND ON LOCAL STORAGE, ASK TO DB BECAUSE IT COULD BE SHARDED AND RESIDE ON ANOTHER SERVER
                                previousRecord.set(db.load(rid));
                        }
                        return null;
                    }
                });
                if (previousRecord.get() == null)
                    throw new ORecordNotFoundException(rid);
                if (op.type == ORecordOperation.UPDATED)
                    undoTask = new OFixUpdateRecordTask(previousRecord.get(), ORecordVersionHelper.clearRollbackMode(previousRecord.get().getVersion()));
                else
                    undoTask = new OResurrectRecordTask(previousRecord.get());
                break;
            default:
                continue;
        }
        if (undoTask != null)
            undoTasks.add(undoTask);
    }
    return undoTasks;
}
Also used : OStorageOperationResult(com.orientechnologies.orient.core.storage.OStorageOperationResult) AtomicReference(java.util.concurrent.atomic.AtomicReference) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) IOException(java.io.IOException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ORecord(com.orientechnologies.orient.core.record.ORecord)

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