Skip to content

Commit

Permalink
refactor: moved some logic from old graph delegate class to document …
Browse files Browse the repository at this point in the history
…based one
  • Loading branch information
tglman committed Oct 3, 2023
1 parent feeb43e commit b624555
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1161,8 +1161,8 @@ private OEdge addEdgeInternal(
&& (fields == null || fields.length == 0)
&& !forceRegular) {
edge = newLightweightEdge(iClassName, from, to);
OVertexDelegate.createLink(from.getRecord(), to.getRecord(), outFieldName);
OVertexDelegate.createLink(to.getRecord(), from.getRecord(), inFieldName);
OVertexDocument.createLink(from.getRecord(), to.getRecord(), outFieldName);
OVertexDocument.createLink(to.getRecord(), from.getRecord(), inFieldName);
} else {
edge = newEdgeInternal(iClassName);
edge.setProperty("out", currentVertex.getRecord());
Expand All @@ -1181,11 +1181,11 @@ private OEdge addEdgeInternal(

if (!outDocumentModified) {
// OUT-VERTEX ---> IN-VERTEX/EDGE
OVertexDelegate.createLink(outDocument, edge.getRecord(), outFieldName);
OVertexDocument.createLink(outDocument, edge.getRecord(), outFieldName);
}

// IN-VERTEX ---> OUT-VERTEX/EDGE
OVertexDelegate.createLink(inDocument, edge.getRecord(), inFieldName);
OVertexDocument.createLink(inDocument, edge.getRecord(), inFieldName);
}

// OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ private void promoteToRegularEdge() {
ODatabaseDocument db = getDatabase();
OVertex from = getFrom();
OVertex to = getTo();
OVertexDelegate.detachOutgointEdge(from, this);
OVertexDelegate.detachIncomingEdge(to, this);
OVertexDocument.detachOutgointEdge(from, this);
OVertexDocument.detachIncomingEdge(to, this);
this.element =
((ODatabaseDocumentInternal) db)
.newRegularEdge(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public OEdgeDocument copy() {
public static void deleteLinks(OEdge delegate) {
OVertex from = delegate.getFrom();
if (from != null) {
OVertexDelegate.detachOutgointEdge(from, delegate);
OVertexDocument.detachOutgointEdge(from, delegate);
}
OVertex to = delegate.getTo();
if (to != null) {
OVertexDelegate.detachIncomingEdge(to, delegate);
OVertexDocument.detachIncomingEdge(to, delegate);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,19 @@
package com.orientechnologies.orient.core.record.impl;

import com.orientechnologies.common.collection.OMultiCollectionIterator;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.common.util.OPair;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordElement;
import com.orientechnologies.orient.core.db.record.ORecordLazyList;
import com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.exception.ODatabaseException;
import com.orientechnologies.orient.core.exception.ORecordNotFoundException;
import com.orientechnologies.orient.core.exception.OSerializationException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.ODirection;
Expand Down Expand Up @@ -217,55 +213,6 @@ public static void deleteLinks(OVertex delegate) {
}
}

protected static void detachOutgointEdge(OVertex vertex, OEdge edge) {
detachEdge(vertex, edge, "out_");
}

protected static void detachIncomingEdge(OVertex vertex, OEdge edge) {
detachEdge(vertex, edge, "in_");
}

protected static void detachEdge(OVertex vertex, OEdge edge, String fieldPrefix) {
String className = edge.getSchemaType().get().getName();

if (className.equalsIgnoreCase("e")) {
className = "";
}
String edgeField = fieldPrefix + className;
Object edgeProp = vertex.getProperty(edgeField);
OIdentifiable edgeId = null;

edgeId = ((OIdentifiable) edge).getIdentity();
if (edgeId == null) {
// lightweight edge

OVertex out = edge.getFrom();
OVertex in = edge.getTo();
if (out != null && vertex.getIdentity().equals(out.getIdentity())) {
edgeId = (OIdentifiable) in;
} else {
edgeId = (OIdentifiable) out;
}
}

if (edgeProp instanceof Collection) {
((Collection) edgeProp).remove(edgeId);
} else if (edgeProp instanceof ORidBag) {
((ORidBag) edgeProp).remove(edgeId);
} else if (edgeProp instanceof OIdentifiable
&& ((OIdentifiable) edgeProp).getIdentity() != null
&& ((OIdentifiable) edgeProp).getIdentity().equals(edgeId)
|| edge.isLightweight()) {
vertex.removeProperty(edgeField);
} else {
OLogManager.instance()
.warn(
vertex,
"Error detaching edge: the vertex collection field is of type "
+ (edgeProp == null ? "null" : edgeProp.getClass()));
}
}

@Override
public OEdge addEdge(OVertex to, String type) {
ODatabaseDocument db = getDatabase();
Expand Down Expand Up @@ -498,84 +445,6 @@ protected Set<String> getEdgeFieldNames(final ODirection iDirection, String... i
return result;
}

/** (Internal only) Creates a link between a vertices and a Graph Element. */
public static Object createLink(
final ODocument iFromVertex, final OIdentifiable iTo, final String iFieldName) {
final Object out;
OType outType = iFromVertex.fieldType(iFieldName);
Object found = iFromVertex.field(iFieldName);

final OClass linkClass = ODocumentInternal.getImmutableSchemaClass(iFromVertex);
if (linkClass == null)
throw new IllegalArgumentException("Class not found in source vertex: " + iFromVertex);

final OProperty prop = linkClass.getProperty(iFieldName);
final OType propType = prop != null && prop.getType() != OType.ANY ? prop.getType() : null;

if (found == null) {
if (propType == OType.LINKLIST
|| (prop != null
&& "true".equalsIgnoreCase(prop.getCustom("ordered")))) { // TODO constant
final Collection coll = new ORecordLazyList(iFromVertex);
coll.add(iTo);
out = coll;
outType = OType.LINKLIST;
} else if (propType == null || propType == OType.LINKBAG) {
final ORidBag bag = new ORidBag();
bag.add(iTo);
out = bag;
outType = OType.LINKBAG;
} else if (propType == OType.LINK) {
out = iTo;
outType = OType.LINK;
} else
throw new ODatabaseException(
"Type of field provided in schema '"
+ prop.getType()
+ "' cannot be used for link creation.");

} else if (found instanceof OIdentifiable) {
if (prop != null && propType == OType.LINK)
throw new ODatabaseException(
"Type of field provided in schema '"
+ prop.getType()
+ "' cannot be used for creation to hold several links.");

if (prop != null && "true".equalsIgnoreCase(prop.getCustom("ordered"))) { // TODO constant
final Collection coll = new ORecordLazyList(iFromVertex);
coll.add(found);
coll.add(iTo);
out = coll;
outType = OType.LINKLIST;
} else {
final ORidBag bag = new ORidBag();
bag.add((OIdentifiable) found);
bag.add(iTo);
out = bag;
outType = OType.LINKBAG;
}
} else if (found instanceof ORidBag) {
// ADD THE LINK TO THE COLLECTION
out = null;

((ORidBag) found).add(iTo.getRecord());

} else if (found instanceof Collection<?>) {
// USE THE FOUND COLLECTION
out = null;
((Collection<Object>) found).add(iTo);

} else
throw new ODatabaseException(
"Relationship content is invalid on field " + iFieldName + ". Found: " + found);

if (out != null)
// OVERWRITE IT
iFromVertex.setProperty(iFieldName, out, outType);

return out;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof OIdentifiable)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.orientechnologies.orient.core.record.impl;

import com.orientechnologies.common.collection.OMultiCollectionIterator;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.common.util.OPair;
import com.orientechnologies.orient.core.db.ODatabaseDocumentInternal;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.ODatabaseSession;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordLazyList;
import com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue;
import com.orientechnologies.orient.core.db.record.ORecordOperation;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.exception.ODatabaseException;
import com.orientechnologies.orient.core.exception.ORecordNotFoundException;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OSchema;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.ODirection;
import com.orientechnologies.orient.core.record.OEdge;
import com.orientechnologies.orient.core.record.ORecord;
Expand Down Expand Up @@ -605,4 +610,131 @@ private String getConnectionClass(final ODirection iDirection, final String iFie
public OVertexDocument copy() {
return (OVertexDocument) copyTo(new OVertexDocument());
}

protected static void detachEdge(OVertex vertex, OEdge edge, String fieldPrefix) {
String className = edge.getSchemaType().get().getName();

if (className.equalsIgnoreCase("e")) {
className = "";
}
String edgeField = fieldPrefix + className;
Object edgeProp = vertex.getProperty(edgeField);
OIdentifiable edgeId = null;

edgeId = ((OIdentifiable) edge).getIdentity();
if (edgeId == null) {
// lightweight edge

OVertex out = edge.getFrom();
OVertex in = edge.getTo();
if (out != null && vertex.getIdentity().equals(out.getIdentity())) {
edgeId = (OIdentifiable) in;
} else {
edgeId = (OIdentifiable) out;
}
}

if (edgeProp instanceof Collection) {
((Collection) edgeProp).remove(edgeId);
} else if (edgeProp instanceof ORidBag) {
((ORidBag) edgeProp).remove(edgeId);
} else if (edgeProp instanceof OIdentifiable
&& ((OIdentifiable) edgeProp).getIdentity() != null
&& ((OIdentifiable) edgeProp).getIdentity().equals(edgeId)
|| edge.isLightweight()) {
vertex.removeProperty(edgeField);
} else {
OLogManager.instance()
.warn(
vertex,
"Error detaching edge: the vertex collection field is of type "
+ (edgeProp == null ? "null" : edgeProp.getClass()));
}
}

protected static void detachIncomingEdge(OVertex vertex, OEdge edge) {
OVertexDocument.detachEdge(vertex, edge, "in_");
}

protected static void detachOutgointEdge(OVertex vertex, OEdge edge) {
OVertexDocument.detachEdge(vertex, edge, "out_");
}

/** (Internal only) Creates a link between a vertices and a Graph Element. */
public static Object createLink(
final ODocument iFromVertex, final OIdentifiable iTo, final String iFieldName) {
final Object out;
OType outType = iFromVertex.fieldType(iFieldName);
Object found = iFromVertex.field(iFieldName);

final OClass linkClass = ODocumentInternal.getImmutableSchemaClass(iFromVertex);
if (linkClass == null)
throw new IllegalArgumentException("Class not found in source vertex: " + iFromVertex);

final OProperty prop = linkClass.getProperty(iFieldName);
final OType propType = prop != null && prop.getType() != OType.ANY ? prop.getType() : null;

if (found == null) {
if (propType == OType.LINKLIST
|| (prop != null
&& "true".equalsIgnoreCase(prop.getCustom("ordered")))) { // TODO constant
final Collection coll = new ORecordLazyList(iFromVertex);
coll.add(iTo);
out = coll;
outType = OType.LINKLIST;
} else if (propType == null || propType == OType.LINKBAG) {
final ORidBag bag = new ORidBag();
bag.add(iTo);
out = bag;
outType = OType.LINKBAG;
} else if (propType == OType.LINK) {
out = iTo;
outType = OType.LINK;
} else
throw new ODatabaseException(
"Type of field provided in schema '"
+ prop.getType()
+ "' cannot be used for link creation.");

} else if (found instanceof OIdentifiable) {
if (prop != null && propType == OType.LINK)
throw new ODatabaseException(
"Type of field provided in schema '"
+ prop.getType()
+ "' cannot be used for creation to hold several links.");

if (prop != null && "true".equalsIgnoreCase(prop.getCustom("ordered"))) { // TODO constant
final Collection coll = new ORecordLazyList(iFromVertex);
coll.add(found);
coll.add(iTo);
out = coll;
outType = OType.LINKLIST;
} else {
final ORidBag bag = new ORidBag();
bag.add((OIdentifiable) found);
bag.add(iTo);
out = bag;
outType = OType.LINKBAG;
}
} else if (found instanceof ORidBag) {
// ADD THE LINK TO THE COLLECTION
out = null;

((ORidBag) found).add(iTo.getRecord());

} else if (found instanceof Collection<?>) {
// USE THE FOUND COLLECTION
out = null;
((Collection<Object>) found).add(iTo);

} else
throw new ODatabaseException(
"Relationship content is invalid on field " + iFieldName + ". Found: " + found);

if (out != null)
// OVERWRITE IT
iFromVertex.setProperty(iFieldName, out, outType);

return out;
}
}

0 comments on commit b624555

Please sign in to comment.