Search in sources :

Example 71 with ORidBag

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

the class OrientBaseGraph method removeEdges.

/**
   * (Internal only)
   */
protected static void removeEdges(final OrientBaseGraph graph, final ODocument iVertex, final String iFieldName, final OIdentifiable iVertexToRemove, final boolean iAlsoInverse, final boolean useVertexFieldsForEdgeLabels, final boolean autoScaleEdgeType, final boolean forceReload) {
    if (iVertex == null)
        return;
    final Object fieldValue = iVertexToRemove != null ? iVertex.field(iFieldName) : iVertex.removeField(iFieldName);
    if (fieldValue == null)
        return;
    if (fieldValue instanceof OIdentifiable) {
        if (iVertexToRemove != null) {
            if (!fieldValue.equals(iVertexToRemove))
                // NOT FOUND
                return;
            iVertex.removeField(iFieldName);
            deleteEdgeIfAny(iVertexToRemove, forceReload);
        }
        if (iAlsoInverse)
            removeInverseEdge(graph, iVertex, iFieldName, iVertexToRemove, (OIdentifiable) fieldValue, useVertexFieldsForEdgeLabels, autoScaleEdgeType, forceReload);
    } else if (fieldValue instanceof ORidBag) {
        // COLLECTION OF RECORDS: REMOVE THE ENTRY
        final ORidBag bag = (ORidBag) fieldValue;
        if (iVertexToRemove != null) {
            // SEARCH SEQUENTIALLY (SLOWER)
            for (Iterator<OIdentifiable> it = bag.rawIterator(); it.hasNext(); ) {
                final ODocument curr = getDocument(it.next(), forceReload);
                if (curr == null)
                    // EDGE REMOVED
                    continue;
                if (curr == null)
                    // ALREADY DELETED (BYPASSING GRAPH API?), JUST REMOVE THE REFERENCE FROM BAG
                    it.remove();
                else if (iVertexToRemove.equals(curr)) {
                    // FOUND AS VERTEX
                    it.remove();
                    if (iAlsoInverse)
                        removeInverseEdge(graph, iVertex, iFieldName, iVertexToRemove, curr, useVertexFieldsForEdgeLabels, autoScaleEdgeType, forceReload);
                    break;
                } else if (ODocumentInternal.getImmutableSchemaClass(curr).isEdgeType()) {
                    final Direction direction = OrientVertex.getConnectionDirection(iFieldName, useVertexFieldsForEdgeLabels);
                    // EDGE, REMOVE THE EDGE
                    if (iVertexToRemove.equals(OrientEdge.getConnection(curr, direction.opposite()))) {
                        it.remove();
                        if (iAlsoInverse)
                            removeInverseEdge(graph, iVertex, iFieldName, iVertexToRemove, curr, useVertexFieldsForEdgeLabels, autoScaleEdgeType, forceReload);
                        break;
                    }
                }
            }
            deleteEdgeIfAny(iVertexToRemove, forceReload);
        } else {
            // DELETE ALL THE EDGES
            for (Iterator<OIdentifiable> it = bag.rawIterator(); it.hasNext(); ) {
                OIdentifiable edge = it.next();
                if (iAlsoInverse)
                    removeInverseEdge(graph, iVertex, iFieldName, null, edge, useVertexFieldsForEdgeLabels, autoScaleEdgeType, forceReload);
                deleteEdgeIfAny(edge, forceReload);
            }
        }
        if (autoScaleEdgeType && bag.isEmpty())
            // FORCE REMOVAL OF ENTIRE FIELD
            iVertex.removeField(iFieldName);
    } else if (fieldValue instanceof Collection) {
        final Collection col = (Collection) fieldValue;
        if (iVertexToRemove != null) {
            // SEARCH SEQUENTIALLY (SLOWER)
            for (Iterator<OIdentifiable> it = col.iterator(); it.hasNext(); ) {
                final ODocument curr = getDocument(it.next(), forceReload);
                if (curr == null)
                    // EDGE REMOVED
                    continue;
                if (iVertexToRemove.equals(curr)) {
                    // FOUND AS VERTEX
                    it.remove();
                    if (iAlsoInverse)
                        removeInverseEdge(graph, iVertex, iFieldName, iVertexToRemove, curr, useVertexFieldsForEdgeLabels, autoScaleEdgeType, forceReload);
                    break;
                } else if (ODocumentInternal.getImmutableSchemaClass(curr).isVertexType()) {
                    final Direction direction = OrientVertex.getConnectionDirection(iFieldName, useVertexFieldsForEdgeLabels);
                    // EDGE, REMOVE THE EDGE
                    if (iVertexToRemove.equals(OrientEdge.getConnection(curr, direction.opposite()))) {
                        it.remove();
                        if (iAlsoInverse)
                            removeInverseEdge(graph, iVertex, iFieldName, iVertexToRemove, curr, useVertexFieldsForEdgeLabels, autoScaleEdgeType, forceReload);
                        break;
                    }
                }
            }
            deleteEdgeIfAny(iVertexToRemove, forceReload);
        } else {
            // DELETE ALL THE EDGES
            for (OIdentifiable edge : (Iterable<OIdentifiable>) col) {
                if (iAlsoInverse)
                    removeInverseEdge(graph, iVertex, iFieldName, null, edge, useVertexFieldsForEdgeLabels, autoScaleEdgeType, forceReload);
                deleteEdgeIfAny(edge, forceReload);
            }
        }
        if (autoScaleEdgeType && col.isEmpty())
            // FORCE REMOVAL OF ENTIRE FIELD
            iVertex.removeField(iFieldName);
    }
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 72 with ORidBag

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

the class ORidBagTest method testContentChange.

public void testContentChange() {
    ODocument document = new ODocument();
    final ORidBag ridBag = new ORidBag();
    document.field("ridBag", ridBag);
    document.save();
    ridBag.add(new ORecordId("#77:10"));
    Assert.assertTrue(document.isDirty());
    boolean expectCME = false;
    if (ORecordInternal.isContentChanged(document)) {
        assertEmbedded(true);
        expectCME = true;
    } else {
        assertEmbedded(false);
    }
    document.save();
    ODocument copy = new ODocument();
    copy.fromStream(document.toStream());
    ORecordInternal.setIdentity(copy, new ORecordId(document.getIdentity()));
    ORecordInternal.setVersion(copy, document.getVersion());
    ORidBag copyRidBag = copy.field("ridBag");
    Assert.assertNotSame(copyRidBag, ridBag);
    copyRidBag.add(new ORecordId("#77:11"));
    Assert.assertTrue(copy.isDirty());
    Assert.assertTrue(!document.isDirty());
    ridBag.add(new ORecordId("#77:12"));
    Assert.assertTrue(document.isDirty());
    document.save();
    try {
        copy.save();
        Assert.assertTrue(!expectCME);
    } catch (OConcurrentModificationException cme) {
        Assert.assertTrue(expectCME);
    }
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 73 with ORidBag

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

the class ORidBagTest method testAdd2.

public void testAdd2() throws Exception {
    ORidBag bag = new ORidBag();
    bag.setAutoConvertToRecord(false);
    bag.add(new ORecordId("#77:2"));
    bag.add(new ORecordId("#77:2"));
    Assert.assertTrue(bag.contains(new ORecordId("#77:2")));
    Assert.assertTrue(!bag.contains(new ORecordId("#77:3")));
    assertEquals(bag.size(), 2);
    assertEmbedded(bag.isEmbedded());
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 74 with ORidBag

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

the class ORidBagTest method testEmptyIterator.

public void testEmptyIterator() throws Exception {
    ORidBag bag = new ORidBag();
    bag.setAutoConvertToRecord(false);
    assertEmbedded(bag.isEmbedded());
    assertEquals(bag.size(), 0);
    for (OIdentifiable id : bag) {
        Assert.fail();
    }
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable)

Example 75 with ORidBag

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

the class ORidBagTest method testAddAllAndIterator.

public void testAddAllAndIterator() throws Exception {
    final Set<OIdentifiable> expected = new HashSet<OIdentifiable>(8);
    expected.add(new ORecordId("#77:12"));
    expected.add(new ORecordId("#77:13"));
    expected.add(new ORecordId("#77:14"));
    expected.add(new ORecordId("#77:15"));
    expected.add(new ORecordId("#77:16"));
    ORidBag bag = new ORidBag();
    bag.setAutoConvertToRecord(false);
    bag.addAll(expected);
    assertEmbedded(bag.isEmbedded());
    assertEquals(bag.size(), 5);
    Set<OIdentifiable> actual = new HashSet<OIdentifiable>(8);
    for (OIdentifiable id : bag) {
        actual.add(id);
    }
    assertEquals(actual, expected);
}
Also used : ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

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