Search in sources :

Example 6 with OImmutableClass

use of com.orientechnologies.orient.core.metadata.schema.OImmutableClass in project orientdb by orientechnologies.

the class OSQLFunctionMove method e2v.

protected Object e2v(final OrientBaseGraph graph, final OIdentifiable iRecord, final Direction iDirection, final String[] iLabels) {
    final ODocument rec = iRecord.getRecord();
    OImmutableClass clazz = ODocumentInternal.getImmutableSchemaClass(rec);
    if (clazz != null && clazz.isEdgeType()) {
        // EDGE
        final OrientEdge edge = graph.getEdge(rec);
        if (edge != null) {
            if (Direction.BOTH.equals(iDirection)) {
                Set<Vertex> result = new HashSet<Vertex>();
                result.add(edge.getVertex(Direction.OUT));
                result.add(edge.getVertex(Direction.IN));
                return result;
            } else {
                return edge.getVertex(iDirection);
            }
        }
    }
    return null;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) OrientEdge(com.tinkerpop.blueprints.impls.orient.OrientEdge) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) HashSet(java.util.HashSet)

Example 7 with OImmutableClass

use of com.orientechnologies.orient.core.metadata.schema.OImmutableClass 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)

Example 8 with OImmutableClass

use of com.orientechnologies.orient.core.metadata.schema.OImmutableClass in project orientdb by orientechnologies.

the class OrientElementIterator method next.

@SuppressWarnings("unchecked")
public T next() {
    OrientElement currentElement = null;
    if (!hasNext())
        throw new NoSuchElementException();
    Object current = itty.next();
    if (null == current)
        throw new NoSuchElementException();
    if (current instanceof OIdentifiable)
        current = ((OIdentifiable) current).getRecord();
    if (current instanceof ODocument) {
        final ODocument currentDocument = (ODocument) current;
        if (currentDocument.getInternalStatus() == ODocument.STATUS.NOT_LOADED)
            currentDocument.load();
        OImmutableClass immutableClass = ODocumentInternal.getImmutableSchemaClass(currentDocument);
        // clusterId -2 is a projection and is correct that doesn't have a class, we consider projection a vertex
        if (immutableClass == null && currentDocument.getIdentity().getClusterId() != -2)
            throw new IllegalArgumentException("Cannot determine the graph element type because the document class is null. Probably this is a projection, use the EXPAND() function");
        if (currentDocument.getIdentity().getClusterId() != -2 && immutableClass.isEdgeType())
            currentElement = graph.getEdge(currentDocument);
        else
            currentElement = graph.getVertex(currentDocument);
    }
    return (T) currentElement;
}
Also used : OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) NoSuchElementException(java.util.NoSuchElementException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 9 with OImmutableClass

use of com.orientechnologies.orient.core.metadata.schema.OImmutableClass in project orientdb by orientechnologies.

the class OrientVertexIterator method createGraphElement.

@Override
public Vertex createGraphElement(final Object iObject) {
    if (iObject instanceof OrientVertex)
        return (OrientVertex) iObject;
    if (iObject == null) {
        return null;
    }
    final ORecord rec = ((OIdentifiable) iObject).getRecord();
    if (rec == null || !(rec instanceof ODocument))
        return null;
    final ODocument value = (ODocument) rec;
    final OImmutableClass immutableClass = ODocumentInternal.getImmutableSchemaClass(value);
    return OGraphCommandExecutorSQLFactory.runWithAnyGraph(new OGraphCommandExecutorSQLFactory.GraphCallBack<Vertex>() {

        @Override
        public Vertex call(OrientBaseGraph graph) {
            final OrientVertex v;
            if (immutableClass.isVertexType()) {
                // DIRECT VERTEX
                v = graph.getVertex(value);
            } else if (immutableClass.isEdgeType()) {
                // EDGE
                if (vertex.settings.isUseVertexFieldsForEdgeLabels() || OrientEdge.isLabeled(OrientEdge.getRecordLabel(value), iLabels))
                    v = graph.getVertex(OrientEdge.getConnection(value, connection.getKey().opposite()));
                else
                    v = null;
            } else
                throw new IllegalStateException("Invalid content found between connections: " + value);
            return v;
        }
    });
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) ORecord(com.orientechnologies.orient.core.record.ORecord) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OGraphCommandExecutorSQLFactory(com.orientechnologies.orient.graph.sql.OGraphCommandExecutorSQLFactory) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 10 with OImmutableClass

use of com.orientechnologies.orient.core.metadata.schema.OImmutableClass in project orientdb by orientechnologies.

the class OAbstractPaginatedStorage method commit.

public List<ORecordOperation> commit(final OTransaction clientTx, Runnable callback) {
    checkOpeness();
    checkLowDiskSpaceFullCheckpointRequestsAndBackgroundDataFlushExceptions();
    txBegun.incrementAndGet();
    final ODatabaseDocumentInternal databaseRecord = (ODatabaseDocumentInternal) clientTx.getDatabase();
    final OIndexManagerProxy indexManager = databaseRecord.getMetadata().getIndexManager();
    final TreeMap<String, OTransactionIndexChanges> indexesToCommit = getSortedIndexEntries(clientTx);
    final Map<ORecordOperation, Integer> clusterOverrides = new IdentityHashMap<ORecordOperation, Integer>();
    databaseRecord.getMetadata().makeThreadLocalSchemaSnapshot();
    if (OLogManager.instance().isDebugEnabled())
        OLogManager.instance().debug(this, "%d Committing transaction %d on database '%s' (items=%d)...", Thread.currentThread().getId(), clientTx.getId(), databaseRecord.getName(), clientTx.getEntryCount());
    final Iterable<ORecordOperation> entries = (Iterable<ORecordOperation>) clientTx.getAllRecordEntries();
    final TreeMap<Integer, OCluster> clustersToLock = new TreeMap<Integer, OCluster>();
    final Set<ORecordOperation> newRecords = new TreeSet<ORecordOperation>(new Comparator<ORecordOperation>() {

        @Override
        public int compare(final ORecordOperation o1, final ORecordOperation o2) {
            return o1.getRecord().getIdentity().compareTo(o2.getRecord().getIdentity());
        }
    });
    for (ORecordOperation txEntry : entries) {
        if (txEntry.type == ORecordOperation.CREATED || txEntry.type == ORecordOperation.UPDATED) {
            final ORecord record = txEntry.getRecord();
            if (record instanceof ODocument)
                ((ODocument) record).validate();
        }
        if (txEntry.type == ORecordOperation.UPDATED || txEntry.type == ORecordOperation.DELETED) {
            final int clusterId = txEntry.getRecord().getIdentity().getClusterId();
            clustersToLock.put(clusterId, getClusterById(clusterId));
        } else if (txEntry.type == ORecordOperation.CREATED) {
            newRecords.add(txEntry);
            final ORecord record = txEntry.getRecord();
            final ORID rid = record.getIdentity();
            int clusterId = rid.getClusterId();
            if (record.isDirty() && clusterId == ORID.CLUSTER_ID_INVALID && record instanceof ODocument) {
                // TRY TO FIX CLUSTER ID TO THE DEFAULT CLUSTER ID DEFINED IN SCHEMA CLASS
                final OImmutableClass class_ = ODocumentInternal.getImmutableSchemaClass(((ODocument) record));
                if (class_ != null) {
                    clusterId = class_.getClusterForNewInstance((ODocument) record);
                    clusterOverrides.put(txEntry, clusterId);
                }
            }
            clustersToLock.put(clusterId, getClusterById(clusterId));
        }
    }
    final List<ORecordOperation> result = new ArrayList<ORecordOperation>();
    final List<Lock[]> indexKeyLockList = new ArrayList<Lock[]>(indexesToCommit.size());
    stateLock.acquireReadLock();
    try {
        try {
            try {
                checkOpeness();
                lockIndexKeys(indexManager, indexesToCommit, indexKeyLockList);
                makeStorageDirty();
                startStorageTx(clientTx);
                lockClusters(clustersToLock);
                lockRidBags(clustersToLock, indexesToCommit);
                lockIndexes(indexesToCommit);
                Map<ORecordOperation, OPhysicalPosition> positions = new IdentityHashMap<ORecordOperation, OPhysicalPosition>();
                for (ORecordOperation txEntry : newRecords) {
                    ORecord rec = txEntry.getRecord();
                    if (rec.isDirty()) {
                        ORecordId rid = (ORecordId) rec.getIdentity().copy();
                        ORecordId oldRID = rid.copy();
                        final Integer clusterOverride = clusterOverrides.get(txEntry);
                        final int clusterId = clusterOverride == null ? rid.getClusterId() : clusterOverride;
                        final OCluster cluster = getClusterById(clusterId);
                        OPhysicalPosition ppos = cluster.allocatePosition(ORecordInternal.getRecordType(rec));
                        rid.setClusterId(cluster.getId());
                        if (rid.getClusterPosition() > -1) {
                            // RECORD HAVING A HIGHER CLUSTER POSITION
                            while (rid.getClusterPosition() > ppos.clusterPosition) {
                                ppos = cluster.allocatePosition(ORecordInternal.getRecordType(rec));
                            }
                            if (rid.getClusterPosition() != ppos.clusterPosition)
                                throw new OConcurrentCreateException(rid, new ORecordId(rid.getClusterId(), ppos.clusterPosition));
                        }
                        positions.put(txEntry, ppos);
                        rid.setClusterPosition(ppos.clusterPosition);
                        clientTx.updateIdentityAfterCommit(oldRID, rid);
                    }
                }
                for (ORecordOperation txEntry : entries) {
                    commitEntry(txEntry, positions.get(txEntry));
                    result.add(txEntry);
                }
                commitIndexes(indexesToCommit);
                endStorageTx();
                OTransactionAbstract.updateCacheFromEntries(clientTx, entries, true);
                txCommit.incrementAndGet();
            } catch (IOException ioe) {
                makeRollback(clientTx, ioe);
            } catch (RuntimeException e) {
                makeRollback(clientTx, e);
            } finally {
                unlockIndexKeys(indexesToCommit, indexKeyLockList);
                transaction.set(null);
            }
        } finally {
            databaseRecord.getMetadata().clearThreadLocalSchemaSnapshot();
        }
    } finally {
        stateLock.releaseReadLock();
    }
    if (OLogManager.instance().isDebugEnabled())
        OLogManager.instance().debug(this, "%d Committed transaction %d on database '%s' (result=%s)", Thread.currentThread().getId(), clientTx.getId(), databaseRecord.getName(), result);
    return result;
}
Also used : ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Lock(java.util.concurrent.locks.Lock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ORecord(com.orientechnologies.orient.core.record.ORecord)

Aggregations

OImmutableClass (com.orientechnologies.orient.core.metadata.schema.OImmutableClass)16 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)14 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)8 ORecord (com.orientechnologies.orient.core.record.ORecord)6 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)4 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)2 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)2 ORecordId (com.orientechnologies.orient.core.id.ORecordId)2 Vertex (com.tinkerpop.blueprints.Vertex)2 OrientEdge (com.tinkerpop.blueprints.impls.orient.OrientEdge)2 OException (com.orientechnologies.common.exception.OException)1 OCommandScriptException (com.orientechnologies.orient.core.command.script.OCommandScriptException)1 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)1 ORID (com.orientechnologies.orient.core.id.ORID)1