org.apache.tinkerpop.gremlin.process.traversal.Order Java Examples

The following examples show how to use org.apache.tinkerpop.gremlin.process.traversal.Order. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TraversalUtil.java    From hugegraph with Apache License 2.0 6 votes vote down vote up
public static void extractOrder(Step<?, ?> newStep,
                                Traversal.Admin<?, ?> traversal) {
    Step<?, ?> step = newStep;
    do {
        step = step.getNextStep();
        if (step instanceof OrderGlobalStep) {
            QueryHolder holder = (QueryHolder) newStep;
            @SuppressWarnings("resource")
            OrderGlobalStep<?, ?> orderStep = (OrderGlobalStep<?, ?>) step;
            orderStep.getComparators().forEach(comp -> {
                ElementValueComparator<?> comparator =
                        (ElementValueComparator<?>) comp.getValue1();
                holder.orderBy(comparator.getPropertyKey(),
                               (Order) comparator.getValueComparator());
            });
            TraversalHelper.copyLabels(step, newStep, false);
            traversal.removeStep(step);
        }
        step = step.getNextStep();
    } while (step instanceof OrderGlobalStep ||
             step instanceof IdentityStep);
}
 
Example #2
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 6 votes vote down vote up
private void testOrderBy_assert(SqlgGraph sqlgGraph, Vertex a1, Vertex a2, Vertex a3, Vertex b1, Vertex b2, Vertex b3) {
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) sqlgGraph.traversal()
            .V().hasLabel("A")
            .order()
            .by("name", Order.incr).by("surname", Order.decr);
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> result = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(6, result.size());
    Assert.assertEquals(a3, result.get(0));
    Assert.assertEquals(a2, result.get(1));
    Assert.assertEquals(a1, result.get(2));
    Assert.assertEquals(b3, result.get(3));
    Assert.assertEquals(b2, result.get(4));
    Assert.assertEquals(b1, result.get(5));
}
 
Example #3
Source File: HasStepFolder.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
static boolean validJanusGraphOrder(OrderGlobalStep orderGlobalStep, Traversal rootTraversal, boolean isVertexOrder) {
    List<Pair<Traversal.Admin, Object>> comparators = orderGlobalStep.getComparators();
    for (Pair<Traversal.Admin, Object> comp : comparators) {
        String key;
        if (comp.getValue0() instanceof ElementValueTraversal &&
                comp.getValue1() instanceof Order) {
            key = ((ElementValueTraversal) comp.getValue0()).getPropertyKey();
        } else if (comp.getValue1() instanceof ElementValueComparator) {
            ElementValueComparator evc = (ElementValueComparator) comp.getValue1();
            if (!(evc.getValueComparator() instanceof Order)) return false;
            key = evc.getPropertyKey();
        } else {
            // do not fold comparators that include nested traversals that are not simple ElementValues
            return false;
        }
        JanusGraphTransaction tx = JanusGraphTraversalUtil.getTx(rootTraversal.asAdmin());
        PropertyKey pKey = tx.getPropertyKey(key);
        if (pKey == null
                || !(Comparable.class.isAssignableFrom(pKey.dataType()))
                || (isVertexOrder && pKey.cardinality() != Cardinality.SINGLE)) {
            return false;
        }
    }
    return true;
}
 
Example #4
Source File: SparqlToGremlinCompiler.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
/**
 * Extracts any {@code SortCondition} instances from the SPARQL query and holds them in an index of their keys
 * where the value is that keys sorting direction.
 */
private static Map<String, Order> createOrderIndexFromQuery(final Query query) {
    final Map<String, Order> orderingIndex = new HashMap<>();
    if (query.hasOrderBy()) {
        final List<SortCondition> sortingConditions = query.getOrderBy();

        for (SortCondition sortCondition : sortingConditions) {
            final Expr expr = sortCondition.getExpression();

            // by default, the sort will be ascending. getDirection() returns -2 if the DESC/ASC isn't
            // supplied - weird
            orderingIndex.put(expr.getVarName(), sortCondition.getDirection() == -1 ? Order.desc : Order.asc);
        }
    }

    return orderingIndex;
}
 
Example #5
Source File: TestReplacedStepEmitComparator.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testComparatorViolations() {
    loadGratefulDead();
    final Traversal<Vertex, Vertex> traversal = this.sqlgGraph.traversal().V().has("song", "name", "OH BOY").out("followedBy").out("followedBy").order().by("performances").by("songType", Order.decr);
    printTraversalForm(traversal);
    int counter = 0;
    String lastSongType = "a";
    int lastPerformances = Integer.MIN_VALUE;
    while (traversal.hasNext()) {
        final Vertex vertex = traversal.next();
        final String currentSongType = vertex.value("songType");
        final int currentPerformances = vertex.value("performances");
        Assert.assertTrue(currentPerformances == lastPerformances || currentPerformances > lastPerformances);
        if (currentPerformances == lastPerformances)
            Assert.assertTrue(currentSongType.equals(lastSongType) || currentSongType.compareTo(lastSongType) < 0);
        lastSongType = currentSongType;
        lastPerformances = currentPerformances;
        counter++;
    }
    Assert.assertEquals(144, counter);
}
 
Example #6
Source File: SortFieldDescriptionTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void getTraversalAddsATraversalWithADefaultValueForTheVerticesWithoutTheProperty() {
  GraphTraversal<Vertex, Vertex> traversal = newGraph()
    .withVertex(v -> v.withTimId("id1").withProperty(PROPERTY, "123"))
    .withVertex(v -> v.withTimId("id2").withProperty(PROPERTY, "1234"))
    .withVertex(v -> v.withTimId("id3"))
    .build()
    .traversal()
    .V();
  SortFieldDescription instance = newSortFieldDescription()
    .withName("name")
    .withDefaultValue("")
    .withProperty(localProperty().withName(PROPERTY))
    .build();

  GraphTraversal<?, ?> orderTraversal = instance.getTraversal().get(0);

  List<Vertex> vertices = traversal.order().by(orderTraversal, Order.incr).toList();
  assertThat(vertices, contains(
    likeVertex().withTimId("id3"),
    likeVertex().withTimId("id1"),
    likeVertex().withTimId("id2")));
}
 
Example #7
Source File: SortFieldDescriptionTest.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void getTraversalReturnsTheTraversalToOrderThePropertyValues() {
  GraphTraversal<Vertex, Vertex> traversal = newGraph()
    .withVertex(v -> v.withTimId("id1").withProperty(PROPERTY, "123"))
    .withVertex(v -> v.withTimId("id2").withProperty(PROPERTY, "1234"))
    .withVertex(v -> v.withTimId("id3").withProperty(PROPERTY, "12"))
    .build()
    .traversal()
    .V();
  SortFieldDescription instance = newSortFieldDescription()
    .withName("name")
    .withDefaultValue("")
    .withProperty(localProperty().withName(PROPERTY))
    .build();

  GraphTraversal<?, ?> orderTraversal = instance.getTraversal().get(0);

  List<Vertex> vertices = traversal.order().by(orderTraversal, Order.incr).toList();
  assertThat(vertices, contains(
    likeVertex().withTimId("id3"),
    likeVertex().withTimId("id1"),
    likeVertex().withTimId("id2")));
}
 
Example #8
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOrderByWith2ShuffleComparator() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "a2");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b3");
    Vertex b4 = this.sqlgGraph.addVertex(T.label, "B", "name", "b4");
    Vertex b5 = this.sqlgGraph.addVertex(T.label, "B", "name", "b5");
    Vertex b6 = this.sqlgGraph.addVertex(T.label, "B", "name", "b6");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    a2.addEdge("ab", b4);
    a2.addEdge("ab", b5);
    a2.addEdge("ab", b6);
    this.sqlgGraph.tx().commit();

    List<Map<String, Object>> result = this.sqlgGraph.traversal().V().as("a")
            .order().by("name", Order.decr)
            .out("ab").as("b")
            .order().by(Order.shuffle)
            .select("a", "b")
            .toList();
    Assert.assertEquals(6, result.size());
}
 
Example #9
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOrderByWithShuffleComparator() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b3");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();

    List<Map<String, Object>> result = this.sqlgGraph.traversal().V().as("a")
            .out("ab").as("b")
            .order().by(Order.shuffle)
            .select("a", "b")
            .toList();
    Assert.assertEquals(3, result.size());
}
 
Example #10
Source File: TestVertexStepOrderBy.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testOrderOnEdge() {
    Vertex god = this.sqlgGraph.addVertex(T.label, "God");
    Vertex fantasy1 = this.sqlgGraph.addVertex(T.label, "Fantasy", "name", "fan1");
    Vertex fantasy2 = this.sqlgGraph.addVertex(T.label, "Fantasy", "name", "fan2");
    Vertex fantasy3 = this.sqlgGraph.addVertex(T.label, "Fantasy", "name", "fan3");
    Vertex fantasy4 = this.sqlgGraph.addVertex(T.label, "Fantasy", "name", "fan4");
    god.addEdge("godDream", fantasy1, "sequence", 1);
    god.addEdge("godDream", fantasy2, "sequence", 2);
    god.addEdge("godDream", fantasy3, "sequence", 3);
    god.addEdge("godDream", fantasy4, "sequence", 4);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal) this.sqlgGraph.traversal().V(god)
            .outE("godDream").as("e")
            .inV().as("v")
            .select("e", "v")
            .order().by(__.select("e").by("sequence"), Order.decr)
            .map(m -> (Vertex) m.get().get("v"));
    Assert.assertEquals(6, traversal.getSteps().size());
    List<Vertex> result = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());
    Assert.assertEquals(fantasy4, result.get(0));
    Assert.assertEquals(fantasy3, result.get(1));
    Assert.assertEquals(fantasy2, result.get(2));
    Assert.assertEquals(fantasy1, result.get(3));
}
 
Example #11
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 6 votes vote down vote up
@Test
public void testSimple() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 1);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .out()
            .order().by("order", Order.decr);
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, false, false, false);
    Assert.assertEquals(3, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
}
 
Example #12
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 6 votes vote down vote up
private void testOrderBy3_assert(SqlgGraph sqlgGraph) {
    DefaultGraphTraversal<Vertex, Map<String, Vertex>> traversal = (DefaultGraphTraversal<Vertex, Map<String, Vertex>>) sqlgGraph.traversal().V()
            .hasLabel("A").as("a")
            .out("ab").as("b")
            .<Vertex>select("a", "b")
            .order()
            .by(__.select("a").by("name"), Order.incr)
            .by(__.select("b").by("name"), Order.decr);
    Assert.assertEquals(5, traversal.getSteps().size());
    List<Map<String, Vertex>> result = traversal.toList();
    Assert.assertEquals(3, traversal.getSteps().size());

    Assert.assertEquals(4, result.size());
    Map<String, Vertex> map1 = result.get(0);
    Map<String, Vertex> map2 = result.get(1);
    Map<String, Vertex> map3 = result.get(2);
    Map<String, Vertex> map4 = result.get(3);
    Assert.assertEquals("aa", map1.get("a").value("name"));
    Assert.assertEquals("bb", map1.get("b").value("name"));
    Assert.assertEquals("aa", map2.get("a").value("name"));
    Assert.assertEquals("ba", map2.get("b").value("name"));
    Assert.assertEquals("ab", map3.get("a").value("name"));
    Assert.assertEquals("bd", map3.get("b").value("name"));
    Assert.assertEquals("ab", map4.get("a").value("name"));
    Assert.assertEquals("bc", map4.get("b").value("name"));
}
 
Example #13
Source File: AbstractIndexManagementIT.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Test
public void testRepairRelationIndex() throws InterruptedException, BackendException, ExecutionException {
    tx.commit();
    mgmt.commit();

    // Load the "Graph of the Gods" sample data (WITHOUT mixed index coverage)
    GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true);

    // Create and enable a relation index on lives edges by reason
    TitanManagement m = graph.openManagement();
    PropertyKey reason = m.getPropertyKey("reason");
    EdgeLabel lives = m.getEdgeLabel("lives");
    m.buildEdgeIndex(lives, "livesByReason", Direction.BOTH, Order.decr, reason);
    m.commit();
    graph.tx().commit();

    // Block until the SchemaStatus transitions to REGISTERED
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "livesByReason", "lives")
            .status(SchemaStatus.REGISTERED).call().getSucceeded());

    m = graph.openManagement();
    RelationTypeIndex index = m.getRelationIndex(m.getRelationType("lives"), "livesByReason");
    m.updateIndex(index, SchemaAction.ENABLE_INDEX);
    m.commit();
    graph.tx().commit();

    // Block until the SchemaStatus transitions to ENABLED
    assertTrue(ManagementSystem.awaitRelationIndexStatus(graph, "livesByReason", "lives")
            .status(SchemaStatus.ENABLED).call().getSucceeded());

    // Run a query that hits the index but erroneously returns nothing because we haven't repaired yet
    //assertFalse(graph.query().has("reason", "no fear of death").edges().iterator().hasNext());

    // Repair
    MapReduceIndexManagement mri = new MapReduceIndexManagement(graph);
    m = graph.openManagement();
    index = m.getRelationIndex(m.getRelationType("lives"), "livesByReason");
    ScanMetrics metrics = mri.updateIndex(index, SchemaAction.REINDEX).get();
    assertEquals(8, metrics.getCustom(IndexRepairJob.ADDED_RECORDS_COUNT));
}
 
Example #14
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderByInSchemas() {
    Vertex a = this.sqlgGraph.addVertex(T.label, "A.A", "name", "a");
    Vertex b = this.sqlgGraph.addVertex(T.label, "A.A", "name", "b");
    Vertex c = this.sqlgGraph.addVertex(T.label, "A.A", "name", "c");
    this.sqlgGraph.tx().commit();
    List<Vertex> vertices = this.sqlgGraph.traversal().V().hasLabel("A.A").order().by("name", Order.decr).toList();
    Assert.assertEquals(c, vertices.get(0));
    Assert.assertEquals(b, vertices.get(1));
    Assert.assertEquals(a, vertices.get(2));
}
 
Example #15
Source File: TestLocalVertexStepOptionalWithOrder.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testSimple() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "order", 1);
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "order", 1);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "order", 3);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .local(
                    __.out().order().by("order", Order.decr)
            );
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(2, traversal.getSteps().size());
    Assert.assertTrue(traversal.getSteps().get(1) instanceof SqlgLocalStepBarrier);
    SqlgLocalStepBarrier<?, ?> localStep = (SqlgLocalStepBarrier<?, ?>) traversal.getSteps().get(1);

    List<SqlgVertexStep> sqlgVertexSteps = TraversalHelper.getStepsOfAssignableClassRecursively(SqlgVertexStep.class, localStep.getLocalChildren().get(0));
    Assert.assertEquals(1, sqlgVertexSteps.size());
    SqlgVertexStep sqlgVertexStep = sqlgVertexSteps.get(0);
    assertStep(sqlgVertexStep, false, false, false, false);
    Assert.assertEquals(3, vertices.size());
    Assert.assertEquals(b3, vertices.get(0));
    Assert.assertEquals(b2, vertices.get(1));
    Assert.assertEquals(b1, vertices.get(2));
}
 
Example #16
Source File: TestVertexStepOrderBy.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderby() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "a");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "c");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V(a1)
            .out()
            .order()
            .by("name", Order.incr);
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> result = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(b1, result.get(0));
    Assert.assertEquals(b2, result.get(1));
    Assert.assertEquals(b3, result.get(2));

    DefaultGraphTraversal<Vertex, Vertex> traversal1 = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V(a1)
            .out()
            .order()
            .by("name", Order.decr);
    Assert.assertEquals(3, traversal1.getSteps().size());
    result = traversal1.toList();
    Assert.assertEquals(1, traversal1.getSteps().size());
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(b1, result.get(2));
    Assert.assertEquals(b2, result.get(1));
    Assert.assertEquals(b3, result.get(0));
}
 
Example #17
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderOnEdgeMultipleLabels() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C");
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C");
    Edge e1 = a1.addEdge("ab", b1, "order", 0);
    Edge e2 = a1.addEdge("ab", b2, "order", 1);
    Edge e3 = a1.addEdge("ab", b3, "order", 2);
    Edge ec1 = a1.addEdge("ac", c1, "order", 3);
    Edge ec2 = a1.addEdge("ac", c2, "order", 4);
    Edge ec3 = a1.addEdge("ac", c3, "order", 5);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Edge> traversal = (DefaultGraphTraversal<Vertex, Edge>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .outE()
            .order().by("order", Order.decr);
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Edge> edges = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, true, true, true);
    Assert.assertEquals(6, edges.size());
    Assert.assertEquals(ec3, edges.get(0));
    Assert.assertEquals(ec2, edges.get(1));
    Assert.assertEquals(ec1, edges.get(2));
    Assert.assertEquals(e3, edges.get(3));
    Assert.assertEquals(e2, edges.get(4));
    Assert.assertEquals(e1, edges.get(5));
}
 
Example #18
Source File: HasStepFolder.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
public static boolean validTitanOrder(OrderGlobalStep ostep, Traversal rootTraversal,
                                      boolean isVertexOrder) {
    for (Comparator comp : (List<Comparator>) ostep.getComparators()) {
        if (!(comp instanceof ElementValueComparator)) return false;
        ElementValueComparator evc = (ElementValueComparator) comp;
        if (!(evc.getValueComparator() instanceof Order)) return false;

        TitanTransaction tx = TitanTraversalUtil.getTx(rootTraversal.asAdmin());
        String key = evc.getPropertyKey();
        PropertyKey pkey = tx.getPropertyKey(key);
        if (pkey == null || !(Comparable.class.isAssignableFrom(pkey.dataType()))) return false;
        if (isVertexOrder && pkey.cardinality() != Cardinality.SINGLE) return false;
    }
    return true;
}
 
Example #19
Source File: TestPropertyValues.java    From sqlg with MIT License 5 votes vote down vote up
/**
 * If the order() does not happen on the database, i.e. in java code then the property also needs to be present.
 */
@Test
public void testInMemoryOrderByValues() {
    loadModern();
    final Traversal<Vertex, String> traversal = this.sqlgGraph.traversal().V().both().hasLabel("person").order().by("age", Order.decr).limit(5).values("name");
    printTraversalForm(traversal);
    checkOrderedResults(Arrays.asList("peter", "josh", "josh", "josh", "marko"), traversal);
}
 
Example #20
Source File: TestRepeatWithOrderAndRange.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testRepeatWithOrder() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1", "order", 1);
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b2", "order", 2);
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "b3", "order", 3);
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1", "order", 4);
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C", "name", "c2", "order", 5);
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C", "name", "c3", "order", 6);
    b1.addEdge("bc", c1);
    b1.addEdge("bc", c2);
    b1.addEdge("bc", c3);
    this.sqlgGraph.tx().commit();

    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .repeat(
                    __.out().order().by("order", Order.decr)
            ).times(2);
    Assert.assertEquals(3, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(3, vertices.size());
    Assert.assertEquals(c3, vertices.get(0));
    Assert.assertEquals(c2, vertices.get(1));
    Assert.assertEquals(c1, vertices.get(2));
}
 
Example #21
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderOnEdgeMultipleLabelsInV() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C");
    Vertex c2 = this.sqlgGraph.addVertex(T.label, "C");
    Vertex c3 = this.sqlgGraph.addVertex(T.label, "C");
    a1.addEdge("ab", b1, "order", 0);
    a1.addEdge("ab", b2, "order", 1);
    a1.addEdge("ab", b3, "order", 2);
    a1.addEdge("ac", c1, "order", 3);
    a1.addEdge("ac", c2, "order", 4);
    a1.addEdge("ac", c3, "order", 5);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V().hasLabel("A")
            .outE()
            .order().by("order", Order.decr)
            .inV();
    Assert.assertEquals(5, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, true, true, true);
    Assert.assertEquals(6, vertices.size());
    Assert.assertEquals(c3, vertices.get(0));
    Assert.assertEquals(c2, vertices.get(1));
    Assert.assertEquals(c1, vertices.get(2));
    Assert.assertEquals(b3, vertices.get(3));
    Assert.assertEquals(b2, vertices.get(4));
    Assert.assertEquals(b1, vertices.get(5));
}
 
Example #22
Source File: MultiComparator.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public MultiComparator(final List<Comparator<C>> comparators) {
    this.comparators = (List) comparators;
    this.isShuffle = !this.comparators.isEmpty() && Order.shuffle == this.comparators.get(this.comparators.size() - 1);
    for (int i = 0; i < this.comparators.size(); i++) {
        if (this.comparators.get(i) == Order.shuffle)
            this.startIndex = i + 1;
    }
}
 
Example #23
Source File: OrderLocalStepTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotThrowContractException() {
    for (int x = 0; x < 1000; x++) {
        final List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }
        __.inject(list).order(Scope.local).by(Order.shuffle).iterate();
        __.inject(list).order(Scope.local).by().by(Order.shuffle).iterate();
        __.inject(list).order(Scope.local).by(Order.shuffle).by().iterate();
        __.inject(list).order(Scope.local).by(__.identity(), Order.shuffle).iterate();
        __.inject(list).order(Scope.local).by().by(__.identity(), Order.shuffle).iterate();
        __.inject(list).order(Scope.local).by(__.identity(), Order.shuffle).by().iterate();
    }
}
 
Example #24
Source File: OrderGlobalStepTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
protected List<Traversal> getTraversals() {
    return Arrays.asList(
            __.order(),
            __.order().by(Order.desc),
            __.order().by("age", Order.asc),
            __.order().by("age", Order.desc),
            __.order().by(outE().count(), Order.asc),
            __.order().by("age", Order.asc).by(outE().count(), Order.asc),
            __.order().by(outE().count(), Order.asc).by("age", Order.asc)
    );
}
 
Example #25
Source File: OrderGlobalStepTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotThrowContractException() {
    for (int x = 0; x < 1000; x++) {
        final List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            list.add(i);
        }
        __.inject(list).unfold().order().by(Order.shuffle).iterate();
        __.inject(list).unfold().order().by().by(Order.shuffle).iterate();
        __.inject(list).unfold().order().by(Order.shuffle).by().iterate();
        __.inject(list).unfold().order().by(__.identity(), Order.shuffle).iterate();
        __.inject(list).unfold().order().by().by(__.identity(), Order.shuffle).iterate();
        __.inject(list).unfold().order().by(__.identity(), Order.shuffle).by().iterate();
    }
}
 
Example #26
Source File: LambdaRestrictionStrategyTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Iterable<Object[]> data() {
    return Arrays.asList(new Object[][]{
            {"map(Lambda.function('true')}", __.map(Lambda.function("true")), false},
            {"filter(Lambda.predicate('true')}", __.filter(Lambda.predicate("true")), false},
            {"filter(x->true)", __.filter(x -> true), false},
            {"map(Traverser::get)", __.map(Traverser::get), false},
            {"sideEffect(x -> {int i = 1+1;})", __.sideEffect(x -> {
                int i = 1 + 1;
            }), false},
            {"select('a').by(values)", __.select("a").by(values), true},
            {"select('a','b').by(Object::toString)", __.select("a", "b").by(Object::toString), false},
            {"order().by((a,b)->a.compareTo(b))", __.order().by((a, b) -> ((Integer) a).compareTo((Integer) b)), false},
            {"order(local).by((a,b)->a.compareTo(b))", __.order(Scope.local).by((a, b) -> ((Integer) a).compareTo((Integer) b)), false},
            {"__.choose(v->v.toString().equals(\"marko\"),__.out(),__.in())", __.choose(v -> v.toString().equals("marko"), __.out(), __.in()), false},
            {"order().by(label,desc)", __.order().by(T.label, Order.desc), true},
            {"order(local).by(values)", __.order(Scope.local).by(values), true},
            {"order(local).by(values,desc)", __.order(Scope.local).by(values,Order.desc), true},
            {"order(local).by(values,(a,b) -> a.compareTo(b))", __.order(Scope.local).by(values, (a, b) -> ((Double) a).compareTo((Double) b)), false},
            //
            {"groupCount().by(label)", __.groupCount().by(T.label), true},
            //
            {"sack(sum).by('age')", __.sack(sum).by("age"), true},
            {"sack{a,b -> a+b}.by('age')", __.sack((a, b) -> (int) a + (int) b).by("age"), false},
            //
            {"order().by(outE('rating').values('stars').mean()).profile()", __.order().by(outE("ratings").values("stars").mean()).profile(), true}
    });
}
 
Example #27
Source File: TestComplex.java    From sqlg with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Traversal<Vertex, Map<String, List<String>>> getPlaylistPaths() {
    return this.sqlgGraph.traversal().V().has("name", "Bob_Dylan").in("sungBy").as("a").
            repeat(__.out().order().by(Order.shuffle).simplePath().from("a")).
            until(__.out("writtenBy").has("name", "Johnny_Cash")).limit(1).as("b").
            repeat(__.out().order().by(Order.shuffle).as("c").simplePath().from("b").to("c")).
            until(__.out("sungBy").has("name", "Grateful_Dead")).limit(1).
            path().from("a").unfold().
            <List<String>>project("song", "artists").
            by("name").
            by(__.coalesce(__.out("sungBy", "writtenBy").dedup().values("name"), __.constant("Unknown")).fold());
}
 
Example #28
Source File: TestVertexStepOrderBy.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderbyDuplicatePathOrderInMemory() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "aa");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "a");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B", "name", "b");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B", "name", "c");
    Vertex a2 = this.sqlgGraph.addVertex(T.label, "A", "name", "ab");
    Vertex a3 = this.sqlgGraph.addVertex(T.label, "A", "name", "ac");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "ca");
    a1.addEdge("ab", b1);
    a1.addEdge("ab", b2);
    a1.addEdge("ab", b3);
    b1.addEdge("ba", a2);
    b1.addEdge("ba", a3);
    b1.addEdge("bc", c1);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V(a1)
            .out()
            .out()
            .order()
            .by("name", Order.incr);
    Assert.assertEquals(4, traversal.getSteps().size());
    List<Vertex> result = traversal.toList();
    Assert.assertEquals(1, traversal.getSteps().size());
    Assert.assertEquals(3, result.size());
    Assert.assertEquals(c1, result.get(2));
    Assert.assertEquals(a3, result.get(1));
    Assert.assertEquals(a2, result.get(0));
}
 
Example #29
Source File: TestGraphStepOrderBy.java    From sqlg with MIT License 5 votes vote down vote up
@Test
public void testOrderOnEdgeWithInV() {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b2 = this.sqlgGraph.addVertex(T.label, "B");
    Vertex b3 = this.sqlgGraph.addVertex(T.label, "B");
    a1.addEdge("ab", b1, "order", 0);
    a1.addEdge("ab", b2, "order", 11);
    a1.addEdge("ab", b3, "order", 2);
    this.sqlgGraph.tx().commit();
    DefaultGraphTraversal<Vertex, Vertex> traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V(a1.id())
            .outE("ab")
            .order().by("order", Order.decr)
            .inV();
    assertEquals(4, traversal.getSteps().size());
    List<Vertex> vertices = traversal.toList();
    assertEquals(1, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, false, false, false);
    assertEquals(3, vertices.size());
    assertEquals(b2, vertices.get(0));
    assertEquals(b3, vertices.get(1));
    assertEquals(b1, vertices.get(2));

    traversal = (DefaultGraphTraversal<Vertex, Vertex>) this.sqlgGraph.traversal()
            .V(a1.id())
            .outE("ab")
            .order().by("order", Order.incr)
            .inV();
    assertEquals(4, traversal.getSteps().size());
    vertices = traversal.toList();
    assertEquals(1, traversal.getSteps().size());
    assertStep(traversal.getSteps().get(0), true, false, false, false);
    assertEquals(3, vertices.size());
    assertEquals(b1, vertices.get(0));
    assertEquals(b3, vertices.get(1));
    assertEquals(b2, vertices.get(2));
}
 
Example #30
Source File: ComplexTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Traversal<Vertex, Map<String, List<String>>> getPlaylistPaths() {
    return g.V().has("name", "Bob_Dylan").in("sungBy").as("a").
            repeat(out().order().by(Order.shuffle).simplePath().from("a")).
            until(out("writtenBy").has("name", "Johnny_Cash")).limit(1).as("b").
            repeat(out().order().by(Order.shuffle).as("c").simplePath().from("b").to("c")).
            until(out("sungBy").has("name", "Grateful_Dead")).limit(1).
            path().from("a").unfold().
            <List<String>>project("song", "artists").
            by("name").
            by(__.coalesce(out("sungBy", "writtenBy").dedup().values("name"), constant("Unknown")).fold());
}