Search in sources :

Example 6 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class VerticesAPI method scan.

@GET
@Timed
@Path("scan")
@Compress
@Produces(APPLICATION_JSON_WITH_CHARSET)
public String scan(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("start") String start, @QueryParam("end") String end, @QueryParam("page") String page, @QueryParam("page_limit") @DefaultValue(DEFAULT_PAGE_LIMIT) long pageLimit) {
    LOG.debug("Graph [{}] query vertices by shard(start: {}, end: {}, " + "page: {}) ", graph, start, end, page);
    HugeGraph g = graph(manager, graph);
    ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
    query.scan(start, end);
    query.page(page);
    if (query.paging()) {
        query.limit(pageLimit);
    }
    Iterator<Vertex> vertices = g.vertices(query);
    return manager.serializer(g).writeVertices(vertices, query.paging());
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Path(jakarta.ws.rs.Path) Compress(com.baidu.hugegraph.api.filter.CompressInterceptor.Compress) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Example 7 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class QueryTest method testToString.

@Test
public void testToString() {
    Query query = new Query(HugeType.VERTEX);
    Assert.assertEquals("`Query * from VERTEX`", query.toString());
    query.page("p1");
    Assert.assertEquals("`Query * from VERTEX page 'p1'`", query.toString());
    query = new Query(HugeType.VERTEX);
    query.limit(10L);
    Assert.assertEquals("`Query * from VERTEX limit 10`", query.toString());
    query = new Query(HugeType.VERTEX);
    query.aggregate(AggregateFunc.COUNT, null);
    query.limit(10L);
    Assert.assertEquals("`Query count(*) from VERTEX limit 10`", query.toString());
    query = new Query(HugeType.VERTEX);
    query.aggregate(AggregateFunc.MAX, "age");
    query.limit(10L);
    Assert.assertEquals("`Query max(age) from VERTEX limit 10`", query.toString());
    query = new Query(HugeType.VERTEX);
    query.page("p2");
    query.limit(10L);
    Assert.assertEquals("`Query * from VERTEX page 'p2', limit 10`", query.toString());
    query = new Query(HugeType.VERTEX);
    query.page("p3");
    query.offset(100L);
    query.limit(10L);
    Assert.assertEquals("`Query * from VERTEX page 'p3', offset 100, " + "limit 10`", query.toString());
    query = new Query(HugeType.VERTEX);
    query.page("");
    query.offset(100L);
    query.limit(10L);
    query.order(HugeKeys.NAME, Order.ASC);
    query.order(HugeKeys.FIELDS, Order.DESC);
    Assert.assertEquals("`Query * from VERTEX page '', offset 100, " + "limit 10, order by {NAME=ASC, FIELDS=DESC}`", query.toString());
    IdQuery query2 = new IdQuery(HugeType.VERTEX, IdGenerator.of(1));
    query2.query(IdGenerator.of(3));
    query2.limit(10L);
    Assert.assertEquals("`Query * from VERTEX limit 10 where id in [1, 3]`", query2.toString());
    ConditionQuery query3 = new ConditionQuery(HugeType.EDGE);
    query3.eq(HugeKeys.LABEL, 3);
    query3.gt(HugeKeys.PROPERTIES, 10);
    query3.lt(HugeKeys.PROPERTIES, 18);
    query3.limit(10L);
    String qStr = query3.toString();
    Assert.assertTrue(qStr.contains("`Query * from EDGE limit 10 where ["));
    Assert.assertTrue(qStr.contains("LABEL == 3"));
    Assert.assertTrue(qStr.contains("PROPERTIES > 10"));
    Assert.assertTrue(qStr.contains("PROPERTIES < 18"));
    ConditionQuery query4 = new ConditionQuery(HugeType.EDGE);
    query4.query(ImmutableSet.of(IdGenerator.of(1), IdGenerator.of(3)));
    query4.eq(HugeKeys.LABEL, 3);
    query4.lt(HugeKeys.PROPERTIES, 18);
    query4.limit(10L);
    qStr = query4.toString();
    Assert.assertTrue(qStr.contains("`Query * from EDGE limit 10 " + "where id in [1, 3] and ["));
    Assert.assertTrue(qStr.contains("LABEL == 3"));
    Assert.assertTrue(qStr.contains("PROPERTIES < 18"));
}
Also used : IdPrefixQuery(com.baidu.hugegraph.backend.query.IdPrefixQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdRangeQuery(com.baidu.hugegraph.backend.query.IdRangeQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Test(org.junit.Test)

Example 8 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class ConditionQueryFlattenTest method testFlattenWithOrAndTree.

@Test
public void testFlattenWithOrAndTree() {
    Condition c1 = Condition.eq(IdGenerator.of("c1"), "1");
    Condition c2 = Condition.eq(IdGenerator.of("c2"), "2");
    Condition c3 = Condition.eq(IdGenerator.of("c3"), "3");
    Condition c4 = Condition.eq(IdGenerator.of("c4"), "4");
    ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
    query.query(c1.and(c2).or(c3.and(c4)));
    Assert.assertEquals(1, query.conditions().size());
    List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query);
    Assert.assertEquals(2, queries.size());
    List<Collection<Condition>> expect;
    expect = ImmutableList.of(ImmutableList.of(c1, c2), ImmutableList.of(c3, c4));
    List<Collection<Condition>> actual = new ArrayList<>();
    for (ConditionQuery q : queries) {
        actual.add(q.conditions());
    }
    Assert.assertEquals(expect, actual);
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ArrayList(java.util.ArrayList) Collection(java.util.Collection) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 9 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class ConditionQueryFlattenTest method testFlattenWithOr.

@Test
public void testFlattenWithOr() {
    Condition c1 = Condition.eq(IdGenerator.of("c1"), "1");
    Condition c2 = Condition.eq(IdGenerator.of("c2"), "2");
    ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
    query.query(c1.or(c2));
    Assert.assertEquals(1, query.conditions().size());
    List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query);
    Assert.assertEquals(2, queries.size());
    List<Collection<Condition>> expect;
    expect = ImmutableList.of(ImmutableList.of(c1), ImmutableList.of(c2));
    List<Collection<Condition>> actual = new ArrayList<>();
    for (ConditionQuery q : queries) {
        actual.add(q.conditions());
    }
    Assert.assertEquals(expect, actual);
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ArrayList(java.util.ArrayList) Collection(java.util.Collection) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 10 with ConditionQuery

use of com.baidu.hugegraph.backend.query.ConditionQuery in project incubator-hugegraph by apache.

the class ConditionQueryFlattenTest method testFlattenWithOrTree.

@Test
public void testFlattenWithOrTree() {
    Condition c1 = Condition.eq(IdGenerator.of("c1"), "1");
    Condition c2 = Condition.eq(IdGenerator.of("c2"), "2");
    Condition c3 = Condition.eq(IdGenerator.of("c3"), "3");
    Condition c4 = Condition.eq(IdGenerator.of("c4"), "4");
    ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
    query.query(c1.or(c2).or(c3.or(c4)));
    Assert.assertEquals(1, query.conditions().size());
    List<ConditionQuery> queries = ConditionQueryFlatten.flatten(query);
    Assert.assertEquals(4, queries.size());
    List<Collection<Condition>> expect;
    expect = ImmutableList.of(ImmutableList.of(c1), ImmutableList.of(c2), ImmutableList.of(c3), ImmutableList.of(c4));
    List<Collection<Condition>> actual = new ArrayList<>();
    for (ConditionQuery q : queries) {
        actual.add(q.conditions());
    }
    Assert.assertEquals(expect, actual);
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ArrayList(java.util.ArrayList) Collection(java.util.Collection) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)59 Id (com.baidu.hugegraph.backend.id.Id)19 Test (org.junit.Test)19 Condition (com.baidu.hugegraph.backend.query.Condition)17 HugeGraph (com.baidu.hugegraph.HugeGraph)13 ArrayList (java.util.ArrayList)12 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)10 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)10 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)8 Collection (java.util.Collection)8 Query (com.baidu.hugegraph.backend.query.Query)7 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)7 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)7 Map (java.util.Map)7 Edge (org.apache.tinkerpop.gremlin.structure.Edge)7 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)6 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)5 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)5 HugeType (com.baidu.hugegraph.type.HugeType)5 ImmutableMap (com.google.common.collect.ImmutableMap)5