Search in sources :

Example 46 with ORidBag

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

the class OGraphRepair method isEdgeBroken.

private boolean isEdgeBroken(final OIdentifiable vertex, final String fieldName, final Direction direction, final OIdentifiable edgeRID, final ORepairStats stats, final boolean useVertexFieldsForEdgeLabels) {
    onScannedLink(stats, edgeRID);
    boolean broken = false;
    if (edgeRID == null)
        // RID NULL
        broken = true;
    else {
        ODocument record = null;
        try {
            record = edgeRID.getIdentity().getRecord();
        } catch (ORecordNotFoundException e) {
            broken = true;
        }
        if (record == null)
            // RECORD DELETED
            broken = true;
        else {
            final OImmutableClass immutableClass = ODocumentInternal.getImmutableSchemaClass(record);
            if (immutableClass == null || (!immutableClass.isVertexType() && !immutableClass.isEdgeType()))
                // INVALID RECORD TYPE: NULL OR NOT GRAPH TYPE
                broken = true;
            else {
                if (immutableClass.isVertexType()) {
                    // VERTEX -> LIGHTWEIGHT EDGE
                    final String inverseFieldName = OrientVertex.getInverseConnectionFieldName(fieldName, useVertexFieldsForEdgeLabels);
                    // CHECK THE VERTEX IS IN INVERSE EDGE CONTAINS
                    final Object inverseEdgeContainer = record.field(inverseFieldName);
                    if (inverseEdgeContainer == null)
                        // NULL CONTAINER
                        broken = true;
                    else {
                        if (inverseEdgeContainer instanceof OIdentifiable) {
                            if (!inverseEdgeContainer.equals(vertex))
                                // NOT THE SAME
                                broken = true;
                        } else if (inverseEdgeContainer instanceof Collection<?>) {
                            if (!((Collection) inverseEdgeContainer).contains(vertex))
                                // NOT IN COLLECTION
                                broken = true;
                        } else if (inverseEdgeContainer instanceof ORidBag) {
                            if (!((ORidBag) inverseEdgeContainer).contains(vertex))
                                // NOT IN RIDBAG
                                broken = true;
                        }
                    }
                } else {
                    // EDGE -> REGULAR EDGE, OK
                    final OIdentifiable backRID = OrientEdge.getConnection(record, direction);
                    if (backRID == null || !backRID.equals(vertex))
                        // BACK RID POINTS TO ANOTHER VERTEX
                        broken = true;
                }
            }
        }
    }
    if (broken) {
        onRemovedLink(stats, edgeRID);
        return true;
    }
    return false;
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OImmutableClass(com.orientechnologies.orient.core.metadata.schema.OImmutableClass) Collection(java.util.Collection) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 47 with ORidBag

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

the class OrientVertex method remove.

/**
   * Removes the current Vertex from the Graph. all the incoming and outgoing edges are automatically removed too.
   */
@Override
public void remove() {
    checkClass();
    final OrientBaseGraph graph = checkIfAttached();
    graph.setCurrentGraphInThreadLocal();
    graph.autoStartTransaction();
    final ODocument doc = getRecord();
    if (doc == null)
        throw ExceptionFactory.vertexWithIdDoesNotExist(this.getId());
    Map<String, List<ODocument>> treeRidbagEdgesToRemove = new HashMap<String, List<ODocument>>();
    if (!graph.getRawGraph().getTransaction().isActive()) {
        for (String fieldName : doc.fieldNames()) {
            final OPair<Direction, String> connection = getConnection(Direction.BOTH, fieldName);
            if (connection == null)
                // SKIP THIS FIELD
                continue;
            Object fv = doc.field(fieldName);
            if (fv instanceof ORidBag && !((ORidBag) fv).isEmbedded()) {
                List<ODocument> docs = new ArrayList<ODocument>();
                for (OIdentifiable id : (ORidBag) fv) docs.add(OrientBaseGraph.getDocument(id, true));
                treeRidbagEdgesToRemove.put(fieldName, docs);
            }
        }
    }
    // REMOVE THE VERTEX RECORD FIRST TO CATCH CME BEFORE EDGES ARE REMOVED
    super.removeRecord();
    // REMOVE THE VERTEX FROM MANUAL INDEXES
    final Iterator<Index<? extends Element>> it = graph.getIndices().iterator();
    if (it.hasNext()) {
        final Set<Edge> allEdges = new HashSet<Edge>();
        for (Edge e : getEdges(Direction.BOTH)) allEdges.add(e);
        while (it.hasNext()) {
            final Index<? extends Element> index = it.next();
            if (Vertex.class.isAssignableFrom(index.getIndexClass())) {
                OrientIndex<OrientVertex> idx = (OrientIndex<OrientVertex>) index;
                idx.removeElement(this);
            }
            if (Edge.class.isAssignableFrom(index.getIndexClass())) {
                OrientIndex<OrientEdge> idx = (OrientIndex<OrientEdge>) index;
                for (Edge e : allEdges) idx.removeElement((OrientEdge) e);
            }
        }
    }
    for (Map.Entry<String, List<ODocument>> entry : treeRidbagEdgesToRemove.entrySet()) {
        doc.removeField(entry.getKey());
        Iterator<ODocument> iter = entry.getValue().iterator();
        while (iter.hasNext()) {
            ODocument docEdge = iter.next();
            OrientBaseGraph.deleteEdgeIfAny(docEdge, false);
        }
    }
    graph.removeEdgesInternal(this, doc, null, true, settings.isUseVertexFieldsForEdgeLabels(), settings.isAutoScaleEdgeType());
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordLazyList(com.orientechnologies.orient.core.db.record.ORecordLazyList) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 48 with ORidBag

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

the class OrientEdge method dropEdgeFromVertex.

protected boolean dropEdgeFromVertex(final OIdentifiable iEdge, final ODocument iVertex, final String iFieldName, final Object iFieldValue) {
    if (iFieldValue == null) {
        // NO EDGE? WARN
        OLogManager.instance().debug(this, "Edge not found in vertex's property %s.%s while removing the edge %s", iVertex.getIdentity(), iFieldName, iEdge.getIdentity());
        return false;
    } else if (iFieldValue instanceof OIdentifiable) {
        if (iFieldValue.equals(iEdge))
            iVertex.removeField(iFieldName);
        else {
            // NO EDGE? WARN
            OLogManager.instance().warn(this, "Edge not found in vertex's property %s.%s link while removing the edge %s", iVertex.getIdentity(), iFieldName, iEdge.getIdentity());
            return false;
        }
    } else if (iFieldValue instanceof ORidBag) {
        // ALREADY A SET: JUST REMOVE THE NEW EDGE
        final ORidBag bag = (ORidBag) iFieldValue;
        bag.remove(iEdge);
    } else if (iFieldValue instanceof Collection<?>) {
        // CONVERT COLLECTION IN TREE-SET AND REMOVE THE EDGE
        final Collection<Object> coll = (Collection<Object>) iFieldValue;
        if (!coll.remove(iEdge)) {
            OLogManager.instance().warn(this, "Edge not found in vertex's property %s.%s set while removing the edge %s", iVertex.getIdentity(), iFieldName, iEdge.getIdentity());
            return false;
        }
        if (coll.size() == 1)
            iVertex.field(iFieldName, coll.iterator().next());
        else if (coll.size() == 0)
            iVertex.removeField(iFieldName);
    } else
        throw new IllegalStateException("Wrong type found in the field '" + iFieldName + "': " + iFieldValue.getClass());
    return true;
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 49 with ORidBag

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

the class OrientVertex method countEdges.

/**
   * (Blueprints Extension) Returns the number of edges connected to the current Vertex.
   *
   * @param iDirection The direction between OUT, IN or BOTH
   * @param iLabels    Optional labels as Strings to consider
   *
   * @return A long with the total edges found
   */
public long countEdges(final Direction iDirection, final String... iLabels) {
    checkIfAttached();
    long counter = 0;
    OrientBaseGraph.getEdgeClassNames(getGraph(), iLabels);
    OrientBaseGraph.encodeClassNames(iLabels);
    if (settings.isUseVertexFieldsForEdgeLabels() || iLabels == null || iLabels.length == 0) {
        // VERY FAST
        final ODocument doc = getRecord();
        for (String fieldName : doc.fieldNames()) {
            final OPair<Direction, String> connection = getConnection(iDirection, fieldName, iLabels);
            if (connection == null)
                // SKIP THIS FIELD
                continue;
            final Object fieldValue = doc.field(fieldName);
            if (fieldValue != null)
                if (fieldValue instanceof Collection<?>)
                    counter += ((Collection<?>) fieldValue).size();
                else if (fieldValue instanceof Map<?, ?>)
                    counter += ((Map<?, ?>) fieldValue).size();
                else if (fieldValue instanceof ORidBag) {
                    counter += ((ORidBag) fieldValue).size();
                } else {
                    counter++;
                }
        }
    } else {
        // SLOWER: BROWSE & FILTER
        for (Edge e : getEdges(iDirection, iLabels)) if (e != null)
            counter++;
    }
    return counter;
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 50 with ORidBag

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

the class LinkBagIndexTest method testIndexRidBagUpdateAddItemInTx.

public void testIndexRidBagUpdateAddItemInTx() throws Exception {
    final ODocument docOne = new ODocument();
    docOne.save();
    final ODocument docTwo = new ODocument();
    docTwo.save();
    final ODocument docThree = new ODocument();
    docThree.save();
    final ODocument document = new ODocument("RidBagIndexTestClass");
    final ORidBag ridBag = new ORidBag();
    ridBag.add(docOne);
    ridBag.add(docTwo);
    document.field("ridBag", ridBag);
    document.save();
    try {
        database.begin();
        ODocument loadedDocument = database.load(document.getIdentity());
        loadedDocument.<ORidBag>field("ridBag").add(docThree);
        document.save();
        database.commit();
    } catch (Exception e) {
        database.rollback();
        throw e;
    }
    List<ODocument> result = database.command(new OCommandSQL("select key, rid from index:ridBagIndex")).execute();
    Assert.assertNotNull(result);
    Assert.assertEquals(result.size(), 3);
    for (ODocument d : result) {
        Assert.assertTrue(d.containsField("key"));
        Assert.assertTrue(d.containsField("rid"));
        if (!d.field("key").equals(docOne.getIdentity()) && !d.field("key").equals(docTwo.getIdentity()) && !d.field("key").equals(docThree.getIdentity())) {
            Assert.fail("Unknown key found: " + d.field("key"));
        }
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)135 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)103 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)70 ORecordId (com.orientechnologies.orient.core.id.ORecordId)37 ArrayList (java.util.ArrayList)27 Test (org.testng.annotations.Test)24 ORID (com.orientechnologies.orient.core.id.ORID)21 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)15 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)13 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)11 OStorage (com.orientechnologies.orient.core.storage.OStorage)7 HashSet (java.util.HashSet)7 ORecordLazyList (com.orientechnologies.orient.core.db.record.ORecordLazyList)6 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)6 ORecordLazyMultiValue (com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue)4 ORecordLazySet (com.orientechnologies.orient.core.db.record.ORecordLazySet)4 Date (java.util.Date)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)3