Search in sources :

Example 1 with ORecord

use of com.orientechnologies.orient.core.record.ORecord 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)

Example 2 with ORecord

use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.

the class ODistributedTransactionManager method createTxTask.

protected OTxTask createTxTask(final List<ORecordOperation> uResult) {
    final OTxTask txTask = new OTxTask();
    for (ORecordOperation op : uResult) {
        final ORecord record = op.getRecord();
        final OAbstractRecordReplicatedTask task;
        switch(op.type) {
            case ORecordOperation.CREATED:
                task = new OCreateRecordTask(record);
                break;
            case ORecordOperation.UPDATED:
                task = new OUpdateRecordTask(record);
                break;
            case ORecordOperation.DELETED:
                task = new ODeleteRecordTask(record);
                break;
            default:
                continue;
        }
        txTask.add(task);
    }
    return txTask;
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 3 with ORecord

use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.

the class OCreateRecordTask method forceUpdate.

protected ORecord forceUpdate(final ODistributedServerManager manager, final ODatabaseDocumentInternal database, final ODistributedRequestId requestId, final ORawBuffer loadedRecord) {
    // LOAD IT AS RECORD
    final ORecord loadedRecordInstance = Orient.instance().getRecordFactoryManager().newInstance(loadedRecord.recordType);
    ORecordInternal.fill(loadedRecordInstance, rid, loadedRecord.version, loadedRecord.getBuffer(), false);
    // RECORD HAS BEEN ALREADY CREATED (PROBABLY DURING DATABASE SYNC) CHECKING COHERENCY
    if (Arrays.equals(loadedRecord.getBuffer(), content))
        // SAME CONTENT
        return loadedRecordInstance;
    ODistributedServerLog.info(this, manager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Error on creating record in an existent position. toStore=%s stored=%s reqId=%s", getRecord(), loadedRecordInstance, requestId);
    throw new ODistributedOperationException("Cannot create the record " + rid + " in an already existent position");
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODistributedOperationException(com.orientechnologies.orient.server.distributed.task.ODistributedOperationException)

Example 4 with ORecord

use of com.orientechnologies.orient.core.record.ORecord in project orientdb by orientechnologies.

the class OResurrectRecordTask method executeRecordTask.

@Override
public Object executeRecordTask(ODistributedRequestId requestId, final OServer iServer, ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
    ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "Resurrecting deleted record %s/%s v.%d reqId=%s", database.getName(), rid.toString(), version, requestId);
    try {
        database.getStorage().recyclePosition(rid, new byte[] {}, version, recordType);
        // CREATE A RECORD TO CALL ALL THE HOOKS (LIKE INDEXES FOR UNIQUE CONSTRAINTS)
        final ORecord loadedRecordInstance = Orient.instance().getRecordFactoryManager().newInstance(recordType);
        ORecordInternal.fill(loadedRecordInstance, rid, version, content, true);
        loadedRecordInstance.save();
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> resurrected deleted record");
        return Boolean.TRUE;
    } catch (OPaginatedClusterException e) {
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> no resurrection, because the record was not deleted");
        return Boolean.TRUE;
    } catch (Exception e) {
        ODistributedServerLog.error(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.IN, "+-> error on resurrecting deleted record: the record is already deleted");
    }
    return Boolean.FALSE;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException) OPaginatedClusterException(com.orientechnologies.orient.core.exception.OPaginatedClusterException)

Example 5 with ORecord

use of com.orientechnologies.orient.core.record.ORecord in project YCSB by brianfrankcooper.

the class OrientDBClient method delete.

@Override
public Status delete(String table, String key) {
    while (true) {
        try (ODatabaseDocumentTx db = databasePool.acquire()) {
            final ODictionary<ORecord> dictionary = db.getMetadata().getIndexManager().getDictionary();
            dictionary.remove(key);
            return Status.OK;
        } catch (OConcurrentModificationException cme) {
            continue;
        } catch (Exception e) {
            e.printStackTrace();
            return Status.ERROR;
        }
    }
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException)

Aggregations

ORecord (com.orientechnologies.orient.core.record.ORecord)194 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)91 ORecordId (com.orientechnologies.orient.core.id.ORecordId)40 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)36 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)27 ORID (com.orientechnologies.orient.core.id.ORID)27 IOException (java.io.IOException)21 OException (com.orientechnologies.common.exception.OException)17 Test (org.junit.Test)16 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)15 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)15 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)15 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)14 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)10 ArrayList (java.util.ArrayList)9 Map (java.util.Map)9 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)8 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)8 Test (org.testng.annotations.Test)8 OIOException (com.orientechnologies.common.io.OIOException)7