Search in sources :

Example 6 with OMetadata

use of com.orientechnologies.orient.core.metadata.OMetadata 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");
    }
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Direction(com.tinkerpop.blueprints.Direction) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OMetadata(com.orientechnologies.orient.core.metadata.OMetadata) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) Iterator(java.util.Iterator) Collection(java.util.Collection) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 7 with OMetadata

use of com.orientechnologies.orient.core.metadata.OMetadata 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");
    }
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OMetadata(com.orientechnologies.orient.core.metadata.OMetadata) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) Collection(java.util.Collection) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OMetadata (com.orientechnologies.orient.core.metadata.OMetadata)7 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)4 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)3 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)3 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)2 ORID (com.orientechnologies.orient.core.id.ORID)2 Collection (java.util.Collection)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)1 OIndex (com.orientechnologies.orient.core.index.OIndex)1 Direction (com.tinkerpop.blueprints.Direction)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1