use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OrientVertex method createLink.
/**
* (Internal only) Creates a link between a vertices and a Graph Element.
*/
public static Object createLink(final OrientBaseGraph iGraph, 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 (iGraph.isAutoScaleEdgeType() && (prop == null || propType == OType.LINK || "true".equalsIgnoreCase(prop.getCustom(OrientVertexType.OrientVertexProperty.ORDERED)))) {
// CREATE ONLY ONE LINK
out = iTo;
outType = OType.LINK;
} else if (propType == OType.LINKLIST || (prop != null && "true".equalsIgnoreCase(prop.getCustom(OrientVertexType.OrientVertexProperty.ORDERED)))) {
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
throw new IllegalStateException("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 IllegalStateException("Type of field provided in schema '" + prop.getType() + "' cannot be used for creation to hold several links.");
if (prop != null && "true".equalsIgnoreCase(prop.getCustom(OrientVertexType.OrientVertexProperty.ORDERED))) {
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);
} else if (found instanceof Collection<?>) {
// USE THE FOUND COLLECTION
out = null;
((Collection<Object>) found).add(iTo);
} else
throw new IllegalStateException("Relationship content is invalid on field " + iFieldName + ". Found: " + found);
if (out != null)
// OVERWRITE IT
iFromVertex.field(iFieldName, out, outType);
return out;
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OrientVertex method getEdges.
/**
* (Blueprints Extension) Returns all the edges from the current Vertex to another one.
*
* @param iDestination The target vertex
* @param iDirection The direction between OUT, IN or BOTH
* @param iLabels Optional labels as Strings to consider
*
* @return
*/
public Iterable<Edge> getEdges(final OrientVertex iDestination, final Direction iDirection, final String... iLabels) {
setCurrentGraphInThreadLocal();
final ODocument doc = getRecord();
OrientBaseGraph.getEdgeClassNames(getGraph(), iLabels);
OrientBaseGraph.encodeClassNames(iLabels);
final OMultiCollectionIterator<Edge> iterable = new OMultiCollectionIterator<Edge>().setEmbedded(true);
String[] fieldNames = null;
if (iLabels != null && iLabels.length > 0) {
// EDGE LABELS: CREATE FIELD NAME TABLE (FASTER THAN EXTRACT FIELD NAMES FROM THE DOCUMENT)
fieldNames = getFieldNames(iDirection, iLabels);
if (fieldNames != null)
// EARLY FETCH ALL THE FIELDS THAT MATTERS
doc.deserializeFields(fieldNames);
}
if (fieldNames == null)
fieldNames = doc.fieldNames();
for (String fieldName : fieldNames) {
final OPair<Direction, String> connection = getConnection(iDirection, fieldName, iLabels);
if (connection == null)
// SKIP THIS FIELD
continue;
final Object fieldValue = doc.rawField(fieldName);
if (fieldValue != null) {
final OIdentifiable destinationVId = iDestination != null ? (OIdentifiable) iDestination.getId() : null;
if (fieldValue instanceof OIdentifiable) {
addSingleEdge(doc, iterable, fieldName, connection, fieldValue, destinationVId, iLabels);
} else if (fieldValue instanceof Collection<?>) {
Collection<?> coll = (Collection<?>) fieldValue;
if (coll.size() == 1) {
// SINGLE ITEM: AVOID CALLING ITERATOR
if (coll instanceof ORecordLazyMultiValue)
addSingleEdge(doc, iterable, fieldName, connection, ((ORecordLazyMultiValue) coll).rawIterator().next(), destinationVId, iLabels);
else if (coll instanceof List<?>)
addSingleEdge(doc, iterable, fieldName, connection, ((List<?>) coll).get(0), destinationVId, iLabels);
else
addSingleEdge(doc, iterable, fieldName, connection, coll.iterator().next(), destinationVId, iLabels);
} else {
// CREATE LAZY Iterable AGAINST COLLECTION FIELD
if (coll instanceof ORecordLazyMultiValue) {
iterable.add(new OrientEdgeIterator(this, iDestination, coll, ((ORecordLazyMultiValue) coll).rawIterator(), connection, iLabels, coll.size()));
} else
iterable.add(new OrientEdgeIterator(this, iDestination, coll, coll.iterator(), connection, iLabels, -1));
}
} else if (fieldValue instanceof ORidBag) {
iterable.add(new OrientEdgeIterator(this, iDestination, fieldValue, ((ORidBag) fieldValue).rawIterator(), connection, iLabels, ((ORidBag) fieldValue).size()));
}
}
}
return iterable;
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OrientVertex method copyRidBags.
private void copyRidBags(ORecord oldRecord, ODocument newDoc) {
ODocument oldDoc = (ODocument) oldRecord;
for (String field : oldDoc.fieldNames()) {
if (field.equalsIgnoreCase("out") || field.equalsIgnoreCase("in") || field.startsWith("out_") || field.startsWith("in_") || field.startsWith("OUT_") || field.startsWith("IN_")) {
Object val = oldDoc.rawField(field);
if (val instanceof ORidBag) {
ORidBag bag = (ORidBag) val;
if (!bag.isEmbedded()) {
ORidBag newBag = new ORidBag();
Iterator<OIdentifiable> rawIter = bag.rawIterator();
while (rawIter.hasNext()) {
newBag.add(rawIter.next());
}
newDoc.field(field, newBag);
}
}
}
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OGraphRepair method repairVertices.
protected void repairVertices(OrientBaseGraph graph, ORepairStats stats, OCommandOutputListener outputListener, Map<String, List<String>> options) {
final ODatabaseDocumentTx db = graph.getRawGraph();
final OMetadata metadata = db.getMetadata();
final OSchema schema = metadata.getSchema();
final OClass vertexClass = schema.getClass(OrientVertexType.CLASS_NAME);
if (vertexClass != null) {
final long countVertices = db.countClass(vertexClass.getName());
long skipVertices = 0l;
if (options != null && options.get("-skipVertices") != null) {
skipVertices = Long.parseLong(options.get("-skipVertices").get(0));
}
message(outputListener, "Scanning " + countVertices + " vertices...\n");
long parsedVertices = 0l;
final long beginTime = System.currentTimeMillis();
for (ODocument vertex : db.browseClass(vertexClass.getName())) {
parsedVertices++;
if (skipVertices > 0 && parsedVertices <= skipVertices)
continue;
stats.scannedVertices++;
if (eventListener != null)
eventListener.onScannedVertex(vertex);
if (outputListener != null && stats.scannedVertices % 100000 == 0) {
long speedPerSecond = (long) (parsedVertices / ((System.currentTimeMillis() - beginTime) / 1000.0));
if (speedPerSecond < 1)
speedPerSecond = 1;
final long remaining = (countVertices - parsedVertices) / speedPerSecond;
message(outputListener, "+ vertices: scanned " + stats.scannedVertices + ", repaired " + stats.repairedVertices + " (estimated remaining time " + remaining + " secs)\n");
}
final OrientVertex v = graph.getVertex(vertex);
boolean modifiedVertex = false;
for (String fieldName : vertex.fieldNames()) {
final OPair<Direction, String> connection = v.getConnection(Direction.BOTH, fieldName, null);
if (connection == null)
// SKIP THIS FIELD
continue;
final Object fieldValue = vertex.rawField(fieldName);
if (fieldValue != null) {
if (fieldValue instanceof OIdentifiable) {
if (isEdgeBroken(vertex, fieldName, connection.getKey(), (OIdentifiable) fieldValue, stats, graph.settings.isUseVertexFieldsForEdgeLabels())) {
modifiedVertex = true;
vertex.field(fieldName, (Object) null);
}
} else if (fieldValue instanceof Collection<?>) {
final Collection<?> coll = ((Collection<?>) fieldValue);
for (Iterator<?> it = coll.iterator(); it.hasNext(); ) {
final Object o = it.next();
if (isEdgeBroken(vertex, fieldName, connection.getKey(), (OIdentifiable) o, stats, graph.settings.isUseVertexFieldsForEdgeLabels())) {
modifiedVertex = true;
it.remove();
}
}
} else if (fieldValue instanceof ORidBag) {
final ORidBag ridbag = ((ORidBag) fieldValue);
for (Iterator<?> it = ridbag.rawIterator(); it.hasNext(); ) {
final Object o = it.next();
if (isEdgeBroken(vertex, fieldName, connection.getKey(), (OIdentifiable) o, stats, graph.settings.isUseVertexFieldsForEdgeLabels())) {
modifiedVertex = true;
it.remove();
}
}
}
}
}
if (modifiedVertex) {
stats.repairedVertices++;
if (eventListener != null)
eventListener.onRepairedVertex(vertex);
message(outputListener, "+ repaired corrupted vertex " + vertex + "\n");
vertex.save();
}
}
message(outputListener, "Scanning vertices completed\n");
}
}
use of com.orientechnologies.orient.core.db.record.ridbag.ORidBag in project orientdb by orientechnologies.
the class OGraphRepair method repairEdges.
protected void repairEdges(OrientBaseGraph graph, ORepairStats stats, OCommandOutputListener outputListener, Map<String, List<String>> options) {
final ODatabaseDocumentTx db = graph.getRawGraph();
final OMetadata metadata = db.getMetadata();
final OSchema schema = metadata.getSchema();
final OrientConfigurableGraph.Settings settings = graph.settings;
final boolean useVertexFieldsForEdgeLabels = settings.isUseVertexFieldsForEdgeLabels();
final OClass edgeClass = schema.getClass(OrientEdgeType.CLASS_NAME);
if (edgeClass != null) {
final long countEdges = db.countClass(edgeClass.getName());
long skipEdges = 0l;
if (options != null && options.get("-skipEdges") != null) {
skipEdges = Long.parseLong(options.get("-skipEdges").get(0));
}
message(outputListener, "Scanning " + countEdges + " edges (skipEdges=" + skipEdges + ")...\n");
long parsedEdges = 0l;
final long beginTime = System.currentTimeMillis();
for (ODocument edge : db.browseClass(edgeClass.getName())) {
final ORID edgeId = edge.getIdentity();
parsedEdges++;
if (skipEdges > 0 && parsedEdges <= skipEdges)
continue;
stats.scannedEdges++;
if (eventListener != null)
eventListener.onScannedEdge(edge);
if (outputListener != null && stats.scannedEdges % 100000 == 0) {
long speedPerSecond = (long) (parsedEdges / ((System.currentTimeMillis() - beginTime) / 1000.0));
if (speedPerSecond < 1)
speedPerSecond = 1;
final long remaining = (countEdges - parsedEdges) / speedPerSecond;
message(outputListener, "+ edges: scanned " + stats.scannedEdges + ", removed " + stats.removedEdges + " (estimated remaining time " + remaining + " secs)\n");
}
boolean outVertexMissing = false;
String removalReason = "";
final OIdentifiable out = OrientEdge.getConnection(edge, Direction.OUT);
if (out == null)
outVertexMissing = true;
else {
ODocument outVertex;
try {
outVertex = out.getRecord();
} catch (ORecordNotFoundException e) {
outVertex = null;
}
if (outVertex == null)
outVertexMissing = true;
else {
final String outFieldName = OrientVertex.getConnectionFieldName(Direction.OUT, edge.getClassName(), useVertexFieldsForEdgeLabels);
final Object outEdges = outVertex.field(outFieldName);
if (outEdges == null)
outVertexMissing = true;
else if (outEdges instanceof ORidBag) {
if (!((ORidBag) outEdges).contains(edgeId))
outVertexMissing = true;
} else if (outEdges instanceof Collection) {
if (!((Collection) outEdges).contains(edgeId))
outVertexMissing = true;
} else if (outEdges instanceof OIdentifiable) {
if (((OIdentifiable) outEdges).getIdentity().equals(edgeId))
outVertexMissing = true;
}
}
}
if (outVertexMissing)
removalReason = "missing outgoing vertex (" + out + ")";
boolean inVertexMissing = false;
final OIdentifiable in = OrientEdge.getConnection(edge, Direction.IN);
if (in == null)
inVertexMissing = true;
else {
ODocument inVertex;
try {
inVertex = in.getRecord();
} catch (ORecordNotFoundException e) {
inVertex = null;
}
if (inVertex == null)
inVertexMissing = true;
else {
final String inFieldName = OrientVertex.getConnectionFieldName(Direction.IN, edge.getClassName(), useVertexFieldsForEdgeLabels);
final Object inEdges = inVertex.field(inFieldName);
if (inEdges == null)
inVertexMissing = true;
else if (inEdges instanceof ORidBag) {
if (!((ORidBag) inEdges).contains(edgeId))
inVertexMissing = true;
} else if (inEdges instanceof Collection) {
if (!((Collection) inEdges).contains(edgeId))
inVertexMissing = true;
} else if (inEdges instanceof OIdentifiable) {
if (((OIdentifiable) inEdges).getIdentity().equals(edgeId))
inVertexMissing = true;
}
}
}
if (inVertexMissing) {
if (!removalReason.isEmpty())
removalReason += ", ";
removalReason += "missing incoming vertex (" + in + ")";
}
if (outVertexMissing || inVertexMissing) {
try {
message(outputListener, "+ deleting corrupted edge " + edge + " because " + removalReason + "\n");
edge.delete();
stats.removedEdges++;
if (eventListener != null)
eventListener.onRemovedEdge(edge);
} catch (Exception e) {
message(outputListener, "Error on deleting edge " + edge.getIdentity() + " (" + e.getMessage() + ")");
}
}
}
message(outputListener, "Scanning edges completed\n");
}
}
Aggregations