Search in sources :

Example 6 with Direction

use of com.tinkerpop.blueprints.Direction in project titan by thinkaurelius.

the class RelationComparator method compare.

@Override
public int compare(final InternalRelation r1, final InternalRelation r2) {
    if (r1.equals(r2))
        return 0;
    // 0) RelationType
    int reltypecompare = (r1.isProperty() ? 1 : 2) - (r2.isProperty() ? 1 : 2);
    if (reltypecompare != 0)
        return reltypecompare;
    // 1) TitanType
    InternalType t1 = (InternalType) r1.getType(), t2 = (InternalType) r2.getType();
    int typecompare = t1.compareTo(t2);
    if (typecompare != 0)
        return typecompare;
    assert t1.equals(t2);
    // 2) Direction
    Direction dir1 = null, dir2 = null;
    for (int i = 0; i < r1.getLen(); i++) if (r1.getVertex(i).equals(vertex)) {
        dir1 = EdgeDirection.fromPosition(i);
        break;
    }
    for (int i = 0; i < r2.getLen(); i++) if (r2.getVertex(i).equals(vertex)) {
        dir2 = EdgeDirection.fromPosition(i);
        break;
    }
    // ("Either relation is not incident on vertex [%s]", vertex);
    assert dir1 != null && dir2 != null;
    int dirCompare = EdgeDirection.position(dir1) - EdgeDirection.position(dir2);
    if (dirCompare != 0)
        return dirCompare;
    // Breakout: If type&direction are the same and the type is unique in the direction it follows that the relations are the same
    if (t1.isUnique(dir1))
        return 0;
    // 3) Compare sort key values
    for (long typeid : t1.getSortKey()) {
        int keycompare = compareOnKey(r1, r2, typeid, t1.getSortOrder());
        if (keycompare != 0)
            return keycompare;
    }
    // 4) Compare property objects or other vertices
    if (r1.isProperty()) {
        Object o1 = ((TitanProperty) r1).getValue();
        Object o2 = ((TitanProperty) r2).getValue();
        Preconditions.checkArgument(o1 != null && o2 != null);
        if (!o1.equals(o2)) {
            int objectcompare = 0;
            if (Comparable.class.isAssignableFrom(((TitanKey) t1).getDataType())) {
                objectcompare = ((Comparable) o1).compareTo(o2);
            } else {
                objectcompare = System.identityHashCode(o1) - System.identityHashCode(o2);
            }
            if (objectcompare != 0)
                return objectcompare;
        }
    } else {
        Preconditions.checkArgument(r1.isEdge() && r2.isEdge());
        int vertexcompare = r1.getVertex(EdgeDirection.position(dir1.opposite())).compareTo(r2.getVertex(EdgeDirection.position(dir1.opposite())));
        if (vertexcompare != 0)
            return vertexcompare;
    }
    // 5)compare relation ids
    return r1.compareTo(r2);
}
Also used : InternalType(com.thinkaurelius.titan.graphdb.internal.InternalType) Direction(com.tinkerpop.blueprints.Direction)

Example 7 with Direction

use of com.tinkerpop.blueprints.Direction in project titan by thinkaurelius.

the class TitanGraphTest method testQuery.

@Test
public void testQuery() {
    TitanKey name = tx.makeKey("name").dataType(String.class).single().indexed(Vertex.class).unique().make();
    TitanKey time = tx.makeKey("time").dataType(Integer.class).single().make();
    TitanKey weight = tx.makeKey("weight").dataType(Double.class).single().make();
    TitanLabel author = tx.makeLabel("author").manyToOne().unidirected().make();
    TitanLabel connect = tx.makeLabel("connect").sortKey(time).make();
    TitanLabel connectDesc = tx.makeLabel("connectDesc").sortKey(time).sortOrder(Order.DESC).make();
    TitanLabel friend = tx.makeLabel("friend").sortKey(weight, time).sortOrder(Order.ASC).signature(author).make();
    TitanLabel friendDesc = tx.makeLabel("friendDesc").sortKey(weight, time).sortOrder(Order.DESC).signature(author).make();
    TitanLabel knows = tx.makeLabel("knows").sortKey(author, weight).make();
    TitanLabel follows = tx.makeLabel("follows").make();
    TitanVertex v = tx.addVertex();
    v.addProperty(name, "v");
    TitanVertex u = tx.addVertex();
    u.addProperty(name, "u");
    int noVertices = 10000;
    assertEquals(0, (noVertices - 1) % 3);
    TitanVertex[] vs = new TitanVertex[noVertices];
    for (int i = 1; i < noVertices; i++) {
        vs[i] = tx.addVertex();
        vs[i].addProperty(name, "v" + i);
    }
    TitanLabel[] labelsV = { connect, friend, knows };
    TitanLabel[] labelsU = { connectDesc, friendDesc, knows };
    for (int i = 1; i < noVertices; i++) {
        for (TitanVertex vertex : new TitanVertex[] { v, u }) {
            for (Direction d : new Direction[] { OUT, IN }) {
                TitanLabel label = vertex == v ? labelsV[i % 3] : labelsU[i % 3];
                TitanEdge e = d == OUT ? vertex.addEdge(label, vs[i]) : vs[i].addEdge(label, vertex);
                e.setProperty("time", i);
                e.setProperty("weight", i % 4 + 0.5);
                e.setProperty("name", "e" + i);
                e.setProperty(author, i % 5 == 0 ? v : vs[i % 5]);
            }
        }
    }
    int edgesPerLabel = noVertices / 3;
    VertexList vl;
    Map<TitanVertex, Iterable<TitanEdge>> results;
    Map<TitanVertex, Iterable<TitanProperty>> results2;
    TitanVertex[] qvs;
    int lastTime;
    Iterator<Edge> outer;
    clopen();
    long[] vidsubset = new long[31 - 3];
    for (int i = 0; i < vidsubset.length; i++) vidsubset[i] = vs[i + 3].getID();
    Arrays.sort(vidsubset);
    // ##################################################
    // Queries from Cache
    // ##################################################
    clopen();
    for (int i = 1; i < noVertices; i++) vs[i] = tx.getVertex(vs[i].getID());
    v = tx.getVertex(v.getID());
    u = tx.getVertex(u.getID());
    qvs = new TitanVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
    // To trigger queries from cache (don't copy!!!)
    assertEquals(2 * (noVertices - 1), Iterables.size(v.getEdges()));
    assertEquals(10, Iterables.size(v.query().labels("connect").limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").limit(10).vertices()));
    assertEquals(10, Iterables.size(v.query().labels("connect").has("time", Compare.GREATER_THAN, 30).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").has("time", Compare.GREATER_THAN, 30).limit(10).vertices()));
    lastTime = 0;
    for (Edge e : v.query().labels("connect").direction(OUT).limit(20).edges()) {
        int nowTime = e.getProperty("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime <= nowTime);
        lastTime = nowTime;
    }
    lastTime = Integer.MAX_VALUE;
    for (Edge e : u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
        int nowTime = e.getProperty("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime >= nowTime);
        lastTime = nowTime;
    }
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).has("time", Compare.GREATER_THAN, 60).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").direction(OUT).has("time", Compare.GREATER_THAN, 60).limit(10).vertices()));
    outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
    for (Edge e : v.query().labels("connect").direction(OUT).limit(10).edges()) {
        assertEquals(e, outer.next());
    }
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).count());
    assertEquals(10, u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31).count());
    assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).count());
    assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).count());
    assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).count());
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertexIds().size());
    assertEquals(edgesPerLabel - 10, v.query().labels("connect").direction(OUT).has("time", Compare.GREATER_THAN, 31).count());
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).count());
    assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).count());
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Compare.LESS_THAN_EQUAL, 10).count());
    assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Compare.GREATER_THAN, 10).count());
    assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).count());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("author", v).count());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 0.0, 4.0).count());
    assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 0.0, 2.0).count());
    assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 2.1, 4.0).count());
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertexIds()));
    assertEquals(30, v.query().labels("friend", "connect", "knows").direction(OUT).interval("time", 3, 33).count());
    assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).count());
    assertEquals(1, v.query().labels("connect").direction(OUT).adjacentVertex(vs[6]).has("time", 6).count());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacentVertex(vs[11]).count());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacentVertex(vs[11]).has("weight", 3.5).count());
    assertEquals(2, v.query().labels("connect").adjacentVertex(vs[6]).has("time", 6).count());
    assertEquals(0, v.query().labels("connect").adjacentVertex(vs[8]).has("time", 8).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).count());
    assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).count());
    assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), Iterables.size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
    assertEquals(1, v.query().direction(IN).has("time", 1).count());
    assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).count());
    assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(OUT).vertices()));
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(IN).vertices()));
    for (Direction dir : new Direction[] { IN, OUT }) {
        vl = v.query().labels().direction(dir).interval("time", 3, 31).vertexIds();
        vl.sort();
        for (int i = 0; i < vl.size(); i++) assertEquals(vidsubset[i], vl.getID(i));
    }
    assertEquals(2 * (noVertices - 1), Iterables.size(v.getEdges()));
    // Property queries
    assertEquals(1, Iterables.size(v.query().properties()));
    assertEquals(1, Iterables.size(v.query().keys("name").properties()));
    // MultiQueries
    results = tx.multiQuery(qvs).direction(IN).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(1, Iterables.size(result));
    results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(2, Iterables.size(result));
    results = tx.multiQuery(qvs).labels("knows").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(0, Iterables.size(result));
    results = tx.multiQuery(qvs).titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(4, Iterables.size(result));
    results2 = tx.multiQuery(qvs).properties();
    for (Iterable<TitanProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    results2 = tx.multiQuery(qvs).keys("name").properties();
    for (Iterable<TitanProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    // ##################################################
    // Same queries as above but without memory loading (i.e. omitting the first query)
    // ##################################################
    clopen();
    for (int i = 1; i < noVertices; i++) vs[i] = tx.getVertex(vs[i].getID());
    v = tx.getVertex(v.getID());
    u = tx.getVertex(u.getID());
    qvs = new TitanVertex[] { vs[6], vs[9], vs[12], vs[15], vs[60] };
    assertEquals(10, Iterables.size(v.query().labels("connect").limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").limit(10).vertices()));
    assertEquals(10, Iterables.size(v.query().labels("connect").has("time", Compare.GREATER_THAN, 30).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").has("time", Compare.GREATER_THAN, 30).limit(10).vertices()));
    lastTime = 0;
    for (Edge e : v.query().labels("connect").direction(OUT).limit(20).edges()) {
        int nowTime = e.getProperty("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime <= nowTime);
        lastTime = nowTime;
    }
    lastTime = Integer.MAX_VALUE;
    for (Edge e : u.query().labels("connectDesc").direction(OUT).limit(20).edges()) {
        int nowTime = e.getProperty("time");
        assertTrue(lastTime + " vs. " + nowTime, lastTime >= nowTime);
        lastTime = nowTime;
    }
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).has("time", Compare.GREATER_THAN, 60).limit(10).vertices()));
    assertEquals(10, Iterables.size(u.query().labels("connectDesc").direction(OUT).has("time", Compare.GREATER_THAN, 60).limit(10).vertices()));
    outer = v.query().labels("connect").direction(OUT).limit(20).edges().iterator();
    for (Edge e : v.query().labels("connect").direction(OUT).limit(10).edges()) {
        assertEquals(e, outer.next());
    }
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).count());
    assertEquals(10, u.query().labels("connectDesc").direction(OUT).interval("time", 3, 31).count());
    assertEquals(10, v.query().labels("connect").direction(IN).interval("time", 3, 31).count());
    assertEquals(10, u.query().labels("connectDesc").direction(IN).interval("time", 3, 31).count());
    assertEquals(0, v.query().labels("connect").direction(OUT).has("time", null).count());
    assertEquals(10, v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertexIds().size());
    assertEquals(edgesPerLabel - 10, v.query().labels("connect").direction(OUT).has("time", Compare.GREATER_THAN, 31).count());
    assertEquals(10, Iterables.size(v.query().labels("connect").direction(OUT).interval("time", 3, 31).vertices()));
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(3, u.query().labels("friendDesc").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(1, v.query().labels("friend").direction(OUT).has("weight", 0.5).interval("time", 4, 10).count());
    assertEquals(1, u.query().labels("friendDesc").direction(OUT).has("weight", 0.5).interval("time", 4, 10).count());
    assertEquals(3, v.query().labels("friend").direction(OUT).interval("time", 3, 33).has("weight", 0.5).count());
    assertEquals(4, v.query().labels("friend").direction(OUT).has("time", Compare.LESS_THAN_EQUAL, 10).count());
    assertEquals(edgesPerLabel - 4, v.query().labels("friend").direction(OUT).has("time", Compare.GREATER_THAN, 10).count());
    assertEquals(20, v.query().labels("friend", "connect").direction(OUT).interval("time", 3, 33).count());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("author", v).count());
    assertEquals((int) Math.ceil(edgesPerLabel / 5.0), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 0.0, 4.0).count());
    assertEquals((int) Math.ceil(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 0.0, 2.0).count());
    assertEquals((int) Math.floor(edgesPerLabel / (5.0 * 2)), v.query().labels("knows").direction(OUT).has("author", v).interval("weight", 2.1, 4.0).count());
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertices()));
    assertEquals(20, Iterables.size(v.query().labels("connect", "friend").direction(OUT).interval("time", 3, 33).vertexIds()));
    assertEquals(30, v.query().labels("friend", "connect", "knows").direction(OUT).interval("time", 3, 33).count());
    assertEquals(noVertices - 2, v.query().labels("friend", "connect", "knows").direction(OUT).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(0, v.query().has("age", null).labels("undefined").direction(OUT).count());
    assertEquals(1, v.query().labels("connect").direction(OUT).adjacentVertex(vs[6]).has("time", 6).count());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacentVertex(vs[11]).count());
    assertEquals(1, v.query().labels("knows").direction(OUT).adjacentVertex(vs[11]).has("weight", 3.5).count());
    assertEquals(2, v.query().labels("connect").adjacentVertex(vs[6]).has("time", 6).count());
    assertEquals(0, v.query().labels("connect").adjacentVertex(vs[8]).has("time", 8).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(OUT).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").direction(IN).count());
    assertEquals(2 * edgesPerLabel, v.query().labels("connect").direction(BOTH).count());
    assertEquals(edgesPerLabel, v.query().labels("connect").has("undefined", null).direction(OUT).count());
    assertEquals(2 * (int) Math.ceil((noVertices - 1) / 4.0), Iterables.size(v.query().labels("connect", "friend", "knows").has("weight", 1.5).vertexIds()));
    assertEquals(1, v.query().direction(IN).has("time", 1).count());
    assertEquals(10, v.query().direction(OUT).interval("time", 4, 14).count());
    assertEquals(9, v.query().direction(IN).interval("time", 4, 14).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(9, v.query().direction(OUT).interval("time", 4, 14).has("time", Compare.NOT_EQUAL, 10).count());
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(OUT).vertices()));
    assertEquals(noVertices - 1, Iterables.size(v.query().direction(IN).vertices()));
    for (Direction dir : new Direction[] { IN, OUT }) {
        vl = v.query().labels().direction(dir).interval("time", 3, 31).vertexIds();
        vl.sort();
        for (int i = 0; i < vl.size(); i++) assertEquals(vidsubset[i], vl.getID(i));
    }
    assertEquals(2 * (noVertices - 1), Iterables.size(v.getEdges()));
    // Property queries
    assertEquals(1, Iterables.size(v.query().properties()));
    assertEquals(1, Iterables.size(v.query().keys("name").properties()));
    // MultiQueries
    results = tx.multiQuery(qvs).direction(IN).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(1, Iterables.size(result));
    results = tx.multiQuery(Sets.newHashSet(qvs)).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(2, Iterables.size(result));
    results = tx.multiQuery(qvs).labels("knows").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(0, Iterables.size(result));
    results = tx.multiQuery(qvs).titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(4, Iterables.size(result));
    results2 = tx.multiQuery(qvs).properties();
    for (Iterable<TitanProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    results2 = tx.multiQuery(qvs).keys("name").properties();
    for (Iterable<TitanProperty> result : results2.values()) assertEquals(1, Iterables.size(result));
    // ##################################################
    // End copied queries
    // ##################################################
    newTx();
    v = (TitanVertex) tx.getVertices("name", "v").iterator().next();
    assertNotNull(v);
    assertEquals(2, v.query().has("weight", 1.5).interval("time", 10, 30).limit(2).vertexIds().size());
    assertEquals(10, v.query().has("weight", 1.5).interval("time", 10, 30).vertexIds().size());
    newTx();
    v = (TitanVertex) tx.getVertices("name", "v").iterator().next();
    assertNotNull(v);
    assertEquals(2, v.query().has("weight", 1.5).interval("time", 10, 30).limit(2).count());
    assertEquals(10, v.query().has("weight", 1.5).interval("time", 10, 30).count());
    newTx();
    // Test partially new vertex queries
    TitanVertex[] qvs2 = new TitanVertex[qvs.length + 2];
    qvs2[0] = tx.addVertex();
    for (int i = 0; i < qvs.length; i++) qvs2[i + 1] = tx.getVertex(qvs[i].getID());
    qvs2[qvs2.length - 1] = tx.addVertex();
    qvs2[0].addEdge("connect", qvs2[qvs2.length - 1]);
    qvs2[qvs2.length - 1].addEdge("connect", qvs2[0]);
    results = tx.multiQuery(qvs2).direction(IN).labels("connect").titanEdges();
    for (Iterable<TitanEdge> result : results.values()) assertEquals(1, Iterables.size(result));
}
Also used : Direction(com.tinkerpop.blueprints.Direction) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Example 8 with Direction

use of com.tinkerpop.blueprints.Direction 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 9 with Direction

use of com.tinkerpop.blueprints.Direction in project pentaho-metaverse by pentaho.

the class BlueprintsGraphMetaverseReader method traceVertices.

/**
 * Traces all of the
 *
 * @param vertex    The source vertex to traverse from
 * @param clone     The clone of the source vertex in the sub-graph
 * @param direction The direction to traverse the graph in
 * @param graph1    The source graph
 * @param graph2    The sub-graph being built
 * @param edgeTypes The types of edges to include in the traversal. Can be null (all edges).
 */
private void traceVertices(Vertex vertex, Vertex clone, Direction direction, Graph graph1, Graph graph2, Set<String> edgeTypes) {
    Direction opDirection = direction == Direction.IN ? Direction.OUT : Direction.IN;
    Iterator<Edge> edges = vertex.getEdges(direction).iterator();
    while (edges.hasNext()) {
        Edge edge = edges.next();
        if (edgeTypes != null && !edgeTypes.contains(edge.getLabel())) {
            continue;
        }
        Vertex nextVertex = edge.getVertex(opDirection);
        Vertex target = GraphUtil.cloneVertexIntoGraph(nextVertex, graph2);
        Vertex node1 = direction == Direction.IN ? target : clone;
        Vertex node2 = direction == Direction.IN ? clone : target;
        String edgeId = (String) node1.getId() + ">" + (String) node2.getId();
        if (graph2.getEdge(edgeId) == null) {
            if (direction == Direction.IN) {
                graph2.addEdge(edgeId, target, clone, edge.getLabel());
            } else {
                graph2.addEdge(edgeId, clone, target, edge.getLabel());
            }
        }
        traceVertices(nextVertex, target, direction, graph1, graph2, edgeTypes);
        if (direction == Direction.OUT) {
            traceVertices(nextVertex, target, Direction.IN, graph1, graph2, DictionaryHelper.STRUCTURAL_LINK_TYPES);
        }
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Direction(com.tinkerpop.blueprints.Direction) Edge(com.tinkerpop.blueprints.Edge)

Example 10 with Direction

use of com.tinkerpop.blueprints.Direction in project titan by thinkaurelius.

the class AbstractVertexCentricQueryBuilder method constructQuery.

protected BaseVertexCentricQuery constructQuery(RelationType returnType) {
    assert returnType != null;
    if (limit == 0)
        return BaseVertexCentricQuery.emptyQuery();
    // Prepare direction
    if (returnType == RelationType.PROPERTY) {
        if (dir == Direction.IN)
            return BaseVertexCentricQuery.emptyQuery();
        dir = Direction.OUT;
    }
    assert getVertexConstraint() == null || returnType == RelationType.EDGE;
    // Prepare constraints
    And<TitanRelation> conditions = QueryUtil.constraints2QNF(tx, constraints);
    if (conditions == null)
        return BaseVertexCentricQuery.emptyQuery();
    assert limit > 0;
    // Don't be smart with query limit adjustments - it just messes up the caching layer and penalizes when appropriate limits are set by the user!
    int sliceLimit = limit;
    // Construct (optimal) SliceQueries
    EdgeSerializer serializer = tx.getEdgeSerializer();
    List<BackendQueryHolder<SliceQuery>> queries;
    if (!hasTypes()) {
        BackendQueryHolder<SliceQuery> query = new BackendQueryHolder<SliceQuery>(serializer.getQuery(returnType), ((dir == Direction.BOTH || (returnType == RelationType.PROPERTY && dir == Direction.OUT)) && !conditions.hasChildren() && includeHidden), true, null);
        if (sliceLimit != Query.NO_LIMIT && sliceLimit < Integer.MAX_VALUE / 3) {
            // If only one direction is queried, ask for twice the limit from backend since approximately half will be filtered
            if (dir != Direction.BOTH && (returnType == RelationType.EDGE || returnType == RelationType.RELATION))
                sliceLimit *= 2;
            // on properties, add some for the hidden properties on a vertex
            if (!includeHidden && (returnType == RelationType.PROPERTY || returnType == RelationType.RELATION))
                sliceLimit += 3;
        }
        query.getBackendQuery().setLimit(computeLimit(conditions, sliceLimit));
        queries = ImmutableList.of(query);
        // Add remaining conditions that only apply if no type is defined
        if (!includeHidden)
            conditions.add(new HiddenFilterCondition<TitanRelation>());
        conditions.add(returnType);
    } else {
        Set<TitanType> ts = new HashSet<TitanType>(types.length);
        queries = new ArrayList<BackendQueryHolder<SliceQuery>>(types.length + 4);
        for (String typeName : types) {
            InternalType type = getType(typeName);
            if (type != null && (includeHidden || !type.isHidden())) {
                ts.add(type);
                if (type.isPropertyKey()) {
                    if (returnType == RelationType.EDGE)
                        throw new IllegalArgumentException("Querying for edges but including a property key: " + type.getName());
                    returnType = RelationType.PROPERTY;
                }
                if (type.isEdgeLabel()) {
                    if (returnType == RelationType.PROPERTY)
                        throw new IllegalArgumentException("Querying for properties but including an edge label: " + type.getName());
                    returnType = RelationType.EDGE;
                }
                // Construct sort key constraints (if any, and if not direction==Both)
                EdgeSerializer.TypedInterval[] sortKeyConstraints = new EdgeSerializer.TypedInterval[type.getSortKey().length];
                And<TitanRelation> remainingConditions = conditions;
                boolean vertexConstraintApplies = type.getSortKey().length == 0 || conditions.hasChildren();
                if (type.getSortKey().length > 0 && conditions.hasChildren()) {
                    remainingConditions = conditions.clone();
                    sortKeyConstraints = compileSortKeyConstraints(type, tx, remainingConditions);
                    // Constraints cannot be matched
                    if (sortKeyConstraints == null)
                        continue;
                    Interval interval;
                    if (sortKeyConstraints[sortKeyConstraints.length - 1] == null || (interval = sortKeyConstraints[sortKeyConstraints.length - 1].interval) == null || !interval.isPoint()) {
                        vertexConstraintApplies = false;
                    }
                }
                Direction[] dirs = { dir };
                EdgeSerializer.VertexConstraint vertexConstraint = getVertexConstraint();
                if (dir == Direction.BOTH && (hasSortKeyConstraints(sortKeyConstraints) || (vertexConstraintApplies && vertexConstraint != null))) {
                    // Split on direction in the presence of effective sort key constraints
                    dirs = new Direction[] { Direction.OUT, Direction.IN };
                }
                for (Direction dir : dirs) {
                    EdgeSerializer.VertexConstraint vertexCon = vertexConstraint;
                    if (vertexCon == null || !vertexConstraintApplies || type.isUnique(dir))
                        vertexCon = null;
                    EdgeSerializer.TypedInterval[] sortConstraints = sortKeyConstraints;
                    if (hasSortKeyConstraints(sortKeyConstraints) && type.isUnique(dir)) {
                        sortConstraints = new EdgeSerializer.TypedInterval[type.getSortKey().length];
                    }
                    boolean isFitted = !remainingConditions.hasChildren() && vertexConstraint == vertexCon && sortConstraints == sortKeyConstraints;
                    SliceQuery q = serializer.getQuery(type, dir, sortConstraints, vertexCon);
                    q.setLimit(computeLimit(remainingConditions, sliceLimit));
                    queries.add(new BackendQueryHolder<SliceQuery>(q, isFitted, true, null));
                }
            }
        }
        if (queries.isEmpty())
            return BaseVertexCentricQuery.emptyQuery();
        conditions.add(getTypeCondition(ts));
    }
    return new BaseVertexCentricQuery(QueryUtil.simplifyQNF(conditions), dir, queries, limit);
}
Also used : Direction(com.tinkerpop.blueprints.Direction) SliceQuery(com.thinkaurelius.titan.diskstorage.keycolumnvalue.SliceQuery) InternalType(com.thinkaurelius.titan.graphdb.internal.InternalType) EdgeSerializer(com.thinkaurelius.titan.graphdb.database.EdgeSerializer) PointInterval(com.thinkaurelius.titan.util.datastructures.PointInterval) Interval(com.thinkaurelius.titan.util.datastructures.Interval) ProperInterval(com.thinkaurelius.titan.util.datastructures.ProperInterval)

Aggregations

Direction (com.tinkerpop.blueprints.Direction)11 InternalType (com.thinkaurelius.titan.graphdb.internal.InternalType)5 EdgeDirection (com.thinkaurelius.titan.graphdb.relations.EdgeDirection)3 Edge (com.tinkerpop.blueprints.Edge)3 LongArrayList (com.carrotsearch.hppc.LongArrayList)2 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)2 Vertex (com.tinkerpop.blueprints.Vertex)2 LongObjectOpenHashMap (com.carrotsearch.hppc.LongObjectOpenHashMap)1 LongOpenHashSet (com.carrotsearch.hppc.LongOpenHashSet)1 LongSet (com.carrotsearch.hppc.LongSet)1 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)1 OException (com.orientechnologies.common.exception.OException)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)1 OMetadata (com.orientechnologies.orient.core.metadata.OMetadata)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OSchema (com.orientechnologies.orient.core.metadata.schema.OSchema)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 EdgeLabel (com.thinkaurelius.titan.core.EdgeLabel)1