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);
}
}
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);
}
}
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());
}
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();
}
}
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);
}
Aggregations