Search in sources :

Example 1 with ConditionQuery

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

the class ServerInfoManager method serverInfos.

private Iterator<HugeServerInfo> serverInfos(Map<String, Object> conditions, long limit, String page) {
    return this.call(() -> {
        ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
        if (page != null) {
            query.page(page);
        }
        HugeGraph graph = this.graph.graph();
        VertexLabel vl = graph.vertexLabel(HugeServerInfo.P.SERVER);
        query.eq(HugeKeys.LABEL, vl.id());
        for (Map.Entry<String, Object> entry : conditions.entrySet()) {
            PropertyKey pk = graph.propertyKey(entry.getKey());
            query.query(Condition.eq(pk.id(), entry.getValue()));
        }
        query.showHidden(true);
        if (limit != NO_LIMIT) {
            query.limit(limit);
        }
        Iterator<Vertex> vertices = this.tx().queryVertices(query);
        Iterator<HugeServerInfo> servers = new MapperIterator<>(vertices, HugeServerInfo::fromVertex);
        // Convert iterator to list to avoid across thread tx accessed
        return QueryResults.toList(servers);
    });
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 2 with ConditionQuery

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

the class StandardTaskScheduler method queryTask.

private <V> Iterator<HugeTask<V>> queryTask(Map<String, Object> conditions, long limit, String page) {
    return this.call(() -> {
        ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
        if (page != null) {
            query.page(page);
        }
        VertexLabel vl = this.graph().vertexLabel(P.TASK);
        query.eq(HugeKeys.LABEL, vl.id());
        for (Map.Entry<String, Object> entry : conditions.entrySet()) {
            PropertyKey pk = this.graph().propertyKey(entry.getKey());
            query.query(Condition.eq(pk.id(), entry.getValue()));
        }
        query.showHidden(true);
        if (limit != NO_LIMIT) {
            query.limit(limit);
        }
        Iterator<Vertex> vertices = this.tx().queryVertices(query);
        Iterator<HugeTask<V>> tasks = new MapperIterator<>(vertices, HugeTask::fromVertex);
        // Convert iterator to list to avoid across thread tx accessed
        return QueryResults.toList(tasks);
    });
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 3 with ConditionQuery

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

the class VertexCoreTest method testQueryFilterByPropName.

@Test
public void testQueryFilterByPropName() {
    HugeGraph graph = graph();
    Assume.assumeTrue("Not support CONTAINS_KEY query", storeFeatures().supportsQueryWithContainsKey());
    init10Vertices();
    VertexLabel language = graph.vertexLabel("language");
    PropertyKey dynamic = graph.propertyKey("dynamic");
    // Query vertex by condition (does contain the property name?)
    ConditionQuery q = new ConditionQuery(HugeType.VERTEX);
    q.eq(HugeKeys.LABEL, language.id());
    q.key(HugeKeys.PROPERTIES, dynamic.id());
    List<Vertex> vertices = ImmutableList.copyOf(graph.vertices(q));
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "language", "name", "python", "dynamic", true);
    // Query vertex by condition (does contain the property name?)
    q = new ConditionQuery(HugeType.VERTEX);
    q.key(HugeKeys.PROPERTIES, dynamic.id());
    vertices = ImmutableList.copyOf(graph.vertices(q));
    Assert.assertEquals(1, vertices.size());
    assertContains(vertices, T.label, "language", "name", "python", "dynamic", true);
}
Also used : FakeVertex(com.baidu.hugegraph.testutil.FakeObjects.FakeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Test(org.junit.Test)

Example 4 with ConditionQuery

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

the class VertexCoreTest method testScanVertex.

@Test
public void testScanVertex() {
    HugeGraph graph = graph();
    // TODO: also support test scan by range
    Assume.assumeTrue("Not support scan", storeFeatures().supportsScanToken() || storeFeatures().supportsScanKeyRange());
    this.init10VerticesAndCommit();
    List<Vertex> vertices = new LinkedList<>();
    long splitSize = 1 * 1024 * 1024;
    List<Shard> splits = graph.metadata(HugeType.VERTEX, "splits", splitSize);
    for (Shard split : splits) {
        ConditionQuery q = new ConditionQuery(HugeType.VERTEX);
        q.scan(split.start(), split.end());
        vertices.addAll(ImmutableList.copyOf(graph.vertices(q)));
    }
    Assert.assertEquals(10, vertices.size());
}
Also used : FakeVertex(com.baidu.hugegraph.testutil.FakeObjects.FakeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Shard(com.baidu.hugegraph.backend.store.Shard) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 5 with ConditionQuery

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

the class Vertices method vertices.

public Iterator<Vertex> vertices(HugeGraph g) {
    Map<String, Object> props = this.properties;
    E.checkArgument(!((this.ids == null || this.ids.isEmpty()) && (props == null || props.isEmpty()) && this.label == null), "No source vertices provided");
    Iterator<Vertex> iterator;
    if (this.ids != null && !this.ids.isEmpty()) {
        List<Id> sourceIds = new ArrayList<>(this.ids.size());
        for (Object id : this.ids) {
            sourceIds.add(HugeVertex.getIdValue(id));
        }
        iterator = g.vertices(sourceIds.toArray());
        E.checkArgument(iterator.hasNext(), "Not exist source vertices with ids %s", this.ids);
    } else {
        ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
        if (this.label != null) {
            Id label = g.vertexLabel(this.label).id();
            query.eq(HugeKeys.LABEL, label);
        }
        if (props != null && !props.isEmpty()) {
            Map<Id, Object> pks = TraversalUtil.transProperties(g, props);
            TraversalUtil.fillConditionQuery(query, pks, g);
        }
        assert !query.empty();
        iterator = g.vertices(query);
        E.checkArgument(iterator.hasNext(), "Not exist source vertex " + "with label '%s' and properties '%s'", this.label, props);
    }
    return iterator;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id)

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