Search in sources :

Example 46 with ORecord

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

the class OTransactionOptimistic method rollback.

@Override
public void rollback(boolean force, int commitLevelDiff) {
    if (txStartCounter < 0)
        throw new OStorageException("Invalid value of TX counter");
    checkTransaction();
    txStartCounter += commitLevelDiff;
    status = TXSTATUS.ROLLBACKING;
    if (!force && txStartCounter > 0) {
        OLogManager.instance().debug(this, "Nested transaction was closed but transaction itself was scheduled for rollback.");
        return;
    }
    if (txStartCounter < 0)
        throw new OTransactionException("Transaction was rolled back more times than it was started.");
    database.getStorage().callInLock(new Callable<Void>() {

        public Void call() throws Exception {
            database.getStorage().rollback(OTransactionOptimistic.this);
            return null;
        }
    }, true);
    // CLEAR THE CACHE
    database.getLocalCache().clear();
    // REMOVE ALL THE DIRTY ENTRIES AND UNDO ANY DIRTY DOCUMENT IF POSSIBLE.
    for (ORecordOperation v : allEntries.values()) {
        final ORecord rec = v.getRecord();
        if (rec.isDirty())
            if (rec instanceof ODocument && ((ODocument) rec).isTrackingChanges())
                ((ODocument) rec).undo();
            else
                rec.unload();
    }
    close();
    status = TXSTATUS.ROLLED_BACK;
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecord(com.orientechnologies.orient.core.record.ORecord) OException(com.orientechnologies.common.exception.OException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 47 with ORecord

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

the class OTransactionOptimistic method saveRecord.

public ORecord saveRecord(final ORecord iRecord, final String iClusterName, final OPERATION_MODE iMode, final boolean iForceCreate, final ORecordCallback<? extends Number> iRecordCreatedCallback, final ORecordCallback<Integer> iRecordUpdatedCallback) {
    this.recordCreatedCallback = (ORecordCallback<Long>) iRecordCreatedCallback;
    this.recordUpdatedCallback = iRecordUpdatedCallback;
    if (iRecord == null)
        return null;
    boolean originalSaved = false;
    final ODirtyManager dirtyManager = ORecordInternal.getDirtyManager(iRecord);
    do {
        Set<ORecord> newRecord = dirtyManager.getNewRecords();
        Set<ORecord> updatedRecord = dirtyManager.getUpdateRecords();
        dirtyManager.clear();
        if (newRecord != null) {
            for (ORecord rec : newRecord) {
                if (rec instanceof ODocument)
                    ODocumentInternal.convertAllMultiValuesToTrackedVersions((ODocument) rec);
                if (rec == iRecord) {
                    addRecord(rec, ORecordOperation.CREATED, iClusterName);
                    originalSaved = true;
                } else
                    addRecord(rec, ORecordOperation.CREATED, getClusterName(rec));
            }
        }
        if (updatedRecord != null) {
            for (ORecord rec : updatedRecord) {
                if (rec instanceof ODocument)
                    ODocumentInternal.convertAllMultiValuesToTrackedVersions((ODocument) rec);
                if (rec == iRecord) {
                    final byte operation = iForceCreate ? ORecordOperation.CREATED : iRecord.getIdentity().isValid() ? ORecordOperation.UPDATED : ORecordOperation.CREATED;
                    addRecord(rec, operation, iClusterName);
                    originalSaved = true;
                } else
                    addRecord(rec, ORecordOperation.UPDATED, getClusterName(rec));
            }
        }
    } while (dirtyManager.getNewRecords() != null || dirtyManager.getUpdateRecords() != null);
    if (!originalSaved && iRecord.isDirty()) {
        final byte operation = iForceCreate ? ORecordOperation.CREATED : iRecord.getIdentity().isValid() ? ORecordOperation.UPDATED : ORecordOperation.CREATED;
        addRecord(iRecord, operation, iClusterName);
    }
    return iRecord;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODirtyManager(com.orientechnologies.orient.core.record.impl.ODirtyManager) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 48 with ORecord

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

the class OTransactionOptimistic method invokeCallbacks.

private void invokeCallbacks() {
    if (recordCreatedCallback != null || recordUpdatedCallback != null) {
        for (ORecordOperation operation : allEntries.values()) {
            final ORecord record = operation.getRecord();
            final ORID identity = record.getIdentity();
            if (operation.type == ORecordOperation.CREATED && recordCreatedCallback != null)
                recordCreatedCallback.call(new ORecordId(identity), identity.getClusterPosition());
            else if (operation.type == ORecordOperation.UPDATED && recordUpdatedCallback != null)
                recordUpdatedCallback.call(new ORecordId(identity), record.getVersion());
        }
    }
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ORecord(com.orientechnologies.orient.core.record.ORecord) ORID(com.orientechnologies.orient.core.id.ORID) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 49 with ORecord

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

the class OCommandExecutorSQLSelect method executeSearchRecord.

protected boolean executeSearchRecord(final OIdentifiable id, final OCommandContext iContext, boolean callHooks) {
    if (id == null)
        return false;
    final ORID identity = id.getIdentity();
    if (uniqueResult != null) {
        if (uniqueResult.containsKey(identity))
            return true;
        if (identity.isValid())
            uniqueResult.put(identity, identity);
    }
    if (!checkInterruption())
        return false;
    final LOCKING_STRATEGY contextLockingStrategy = iContext.getVariable("$locking") != null ? (LOCKING_STRATEGY) iContext.getVariable("$locking") : null;
    final LOCKING_STRATEGY localLockingStrategy = contextLockingStrategy != null ? contextLockingStrategy : lockingStrategy;
    if (localLockingStrategy != null && !(localLockingStrategy == LOCKING_STRATEGY.DEFAULT || localLockingStrategy == LOCKING_STRATEGY.NONE || localLockingStrategy == LOCKING_STRATEGY.EXCLUSIVE_LOCK || localLockingStrategy == LOCKING_STRATEGY.SHARED_LOCK))
        throw new IllegalStateException("Unsupported locking strategy " + localLockingStrategy);
    if (localLockingStrategy == LOCKING_STRATEGY.SHARED_LOCK) {
        id.lock(false);
        if (id instanceof ORecord) {
            final ORecord record = (ORecord) id;
            record.reload(null, true, false);
        }
    } else if (localLockingStrategy == LOCKING_STRATEGY.EXCLUSIVE_LOCK) {
        id.lock(true);
        if (id instanceof ORecord) {
            final ORecord record = (ORecord) id;
            record.reload(null, true, false);
        }
    }
    ORecord record = null;
    try {
        if (!(id instanceof ORecord)) {
            record = getDatabase().load(id.getIdentity(), null, !isUseCache());
            if (id instanceof OContextualRecordId && ((OContextualRecordId) id).getContext() != null) {
                Map<String, Object> ridContext = ((OContextualRecordId) id).getContext();
                for (String key : ridContext.keySet()) {
                    context.setVariable(key, ridContext.get(key));
                }
            }
        } else {
            record = (ORecord) id;
        }
        iContext.updateMetric("recordReads", +1);
        if (record == null)
            // SKIP IT
            return true;
        if (ORecordInternal.getRecordType(record) != ODocument.RECORD_TYPE && checkSkipBlob())
            // SKIP binary records in case of projection.
            return true;
        iContext.updateMetric("documentReads", +1);
        iContext.setVariable("current", record);
        if (filter(record, iContext)) {
            if (callHooks) {
                ((ODatabaseDocumentInternal) getDatabase()).callbackHooks(ORecordHook.TYPE.BEFORE_READ, record);
                ((ODatabaseDocumentInternal) getDatabase()).callbackHooks(ORecordHook.TYPE.AFTER_READ, record);
            }
            if (parallel) {
                try {
                    applyGroupBy(record, iContext);
                    resultQueue.put(new AsyncResult(record, iContext));
                } catch (InterruptedException e) {
                    Thread.interrupted();
                    return false;
                }
                tmpQueueOffer.incrementAndGet();
            } else {
                applyGroupBy(record, iContext);
                if (!handleResult(record, iContext)) {
                    // LIMIT REACHED
                    return false;
                }
            }
        }
    } finally {
        if (localLockingStrategy != null && record != null && record.isLocked()) {
            // CONTEXT LOCK: lock must be released (no matter if filtered or not)
            if (localLockingStrategy == LOCKING_STRATEGY.EXCLUSIVE_LOCK || localLockingStrategy == LOCKING_STRATEGY.SHARED_LOCK) {
                record.unlock();
            }
        }
    }
    return true;
}
Also used : OContextualRecordId(com.orientechnologies.orient.core.id.OContextualRecordId) ORecord(com.orientechnologies.orient.core.record.ORecord) LOCKING_STRATEGY(com.orientechnologies.orient.core.storage.OStorage.LOCKING_STRATEGY) ORID(com.orientechnologies.orient.core.id.ORID) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 50 with ORecord

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

the class OrientEdgeIterator method createGraphElement.

@Override
public OrientEdge createGraphElement(final Object iObject) {
    if (iObject instanceof OrientEdge)
        return (OrientEdge) iObject;
    final OIdentifiable rec = (OIdentifiable) iObject;
    if (rec == null) {
        // SKIP IT
        OLogManager.instance().warn(this, "Record (%s) is null", iObject);
        return null;
    }
    final ORecord record = rec.getRecord();
    if (record == null) {
        // SKIP IT
        OLogManager.instance().warn(this, "Record (%s) is null", rec);
        return null;
    }
    if (!(record instanceof ODocument)) {
        // SKIP IT
        OLogManager.instance().warn(this, "Found a record (%s) that is not an edge. Source vertex : %s, Target vertex : %s, Database : %s", rec, sourceVertex != null ? sourceVertex.getIdentity() : null, targetVertex != null ? targetVertex.getIdentity() : null, record.getDatabase().getURL());
        return null;
    }
    final ODocument value = rec.getRecord();
    if (value == null) {
        return null;
    }
    OImmutableClass immutableSchema = ODocumentInternal.getImmutableSchemaClass(value);
    if (immutableSchema == null) {
        ODatabaseDocument db = value.getDatabaseIfDefined();
        if (db == null) {
            return null;
        }
        db.getMetadata().reload();
        immutableSchema = ODocumentInternal.getImmutableSchemaClass(value);
        if (immutableSchema == null) {
            return null;
        }
    }
    final OrientEdge edge;
    if (immutableSchema.isVertexType()) {
        // DIRECT VERTEX, CREATE DUMMY EDGE
        OrientBaseGraph graph = this.sourceVertex.getGraph();
        boolean newGraph = false;
        if (graph == null) {
            newGraph = true;
            ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
            if (db != null) {
                graph = new OrientGraphNoTx((ODatabaseDocumentTx) db);
            }
        }
        if (connection.getKey() == Direction.OUT) {
            edge = graph.getEdgeInstance(this.sourceVertex.getIdentity(), rec.getIdentity(), connection.getValue());
        } else {
            edge = graph.getEdgeInstance(rec.getIdentity(), this.sourceVertex.getIdentity(), connection.getValue());
        }
        if (newGraph) {
            graph.shutdown(false, false);
        }
    } else if (immutableSchema.isEdgeType()) {
        // EDGE
        edge = new OrientEdge(this.sourceVertex.getGraph(), rec.getIdentity(), connection.getValue());
    } else
        throw new IllegalStateException("Invalid content found while iterating edges, value '" + value + "' is not an edge");
    if (this.sourceVertex.settings.isUseVertexFieldsForEdgeLabels() || edge.isLabeled(labels))
        return edge;
    return null;
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ORecord(com.orientechnologies.orient.core.record.ORecord) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecord (com.orientechnologies.orient.core.record.ORecord)177 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)80 ORecordId (com.orientechnologies.orient.core.id.ORecordId)37 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)35 ORID (com.orientechnologies.orient.core.id.ORID)24 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)19 IOException (java.io.IOException)18 Test (org.junit.Test)15 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)14 OException (com.orientechnologies.common.exception.OException)13 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)13 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)12 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)11 ArrayList (java.util.ArrayList)10 Test (org.testng.annotations.Test)8 OIOException (com.orientechnologies.common.io.OIOException)7 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)7 OOfflineClusterException (com.orientechnologies.orient.core.storage.impl.local.paginated.OOfflineClusterException)7 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)6 ORecordHook (com.orientechnologies.orient.core.hook.ORecordHook)6