Search in sources :

Example 26 with Query

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

the class HugeGraphStep method edgesCount.

private long edgesCount() {
    if (!this.hasIds()) {
        HugeGraph graph = TraversalUtil.getGraph(this);
        Query query = this.makeQuery(graph, HugeType.EDGE);
        return graph.queryNumber(query).longValue();
    }
    return IteratorUtils.count(this.edges());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Query(com.baidu.hugegraph.backend.query.Query)

Example 27 with Query

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

the class HugeVariables method queryVariableVertex.

private HugeVertex queryVariableVertex(String key) {
    GraphTransaction tx = this.params.graphTransaction();
    Query query = this.createVariableQuery(key);
    Iterator<Vertex> vertices = tx.queryVertices(query);
    return (HugeVertex) QueryResults.one(vertices);
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 28 with Query

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

the class RocksDBStore method query.

@Override
public Iterator<BackendEntry> query(Query query) {
    Lock readLock = this.storeLock.readLock();
    readLock.lock();
    try {
        this.checkOpened();
        HugeType tableType = RocksDBTable.tableType(query);
        RocksDBTable table;
        RocksDBSessions.Session session;
        if (query.olap()) {
            table = this.table(this.olapTableName(tableType));
            session = this.session(HugeType.OLAP);
        } else {
            table = this.table(tableType);
            session = this.session(tableType);
        }
        Iterator<BackendEntry> entries = table.query(session, query);
        // Merge olap results as needed
        Set<Id> olapPks = query.olapPks();
        if (this.isGraphStore && !olapPks.isEmpty()) {
            List<Iterator<BackendEntry>> iterators = new ArrayList<>();
            for (Id pk : olapPks) {
                Query q = query.copy();
                table = this.table(this.olapTableName(pk));
                iterators.add(table.query(this.session(HugeType.OLAP), q));
            }
            entries = new MergeIterator<>(entries, iterators, BackendEntry::mergeable);
        }
        return entries;
    } finally {
        readLock.unlock();
    }
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) Query(com.baidu.hugegraph.backend.query.Query) ArrayList(java.util.ArrayList) HugeType(com.baidu.hugegraph.type.HugeType) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock) MergeIterator(com.baidu.hugegraph.backend.serializer.MergeIterator) Iterator(java.util.Iterator) Session(com.baidu.hugegraph.backend.store.rocksdb.RocksDBSessions.Session) Id(com.baidu.hugegraph.backend.id.Id)

Example 29 with Query

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

the class EdgeCoreTest method testQueryOutEdgesOfVertexBySortkeyWithMoreFieldsInPage.

@Test
public void testQueryOutEdgesOfVertexBySortkeyWithMoreFieldsInPage() {
    Assume.assumeTrue("Not support paging", storeFeatures().supportsQueryByPage());
    HugeGraph graph = graph();
    SchemaManager schema = graph.schema();
    schema.propertyKey("no").asText().create();
    schema.propertyKey("location").asText().create();
    schema.propertyKey("callType").asText().create();
    schema.propertyKey("calltime").asDate().create();
    schema.propertyKey("duration").asInt().create();
    schema.vertexLabel("phone").properties("no").primaryKeys("no").enableLabelIndex(false).create();
    schema.edgeLabel("call").multiTimes().properties("location", "callType", "duration", "calltime").sourceLabel("phone").targetLabel("phone").sortKeys("location", "callType", "duration", "calltime").create();
    Vertex v1 = graph.addVertex(T.label, "phone", "no", "13812345678");
    Vertex v2 = graph.addVertex(T.label, "phone", "no", "13866668888");
    Vertex v10086 = graph.addVertex(T.label, "phone", "no", "10086");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 3, "calltime", "2017-5-1 23:00:00");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 3, "calltime", "2017-5-2 12:00:01");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 3, "calltime", "2017-5-3 12:08:02");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "work", "duration", 8, "calltime", "2017-5-3 22:22:03");
    v1.addEdge("call", v2, "location", "Beijing", "callType", "fun", "duration", 10, "calltime", "2017-5-4 20:33:04");
    v1.addEdge("call", v10086, "location", "Nanjing", "callType", "work", "duration", 12, "calltime", "2017-5-2 15:30:05");
    v1.addEdge("call", v10086, "location", "Nanjing", "callType", "work", "duration", 14, "calltime", "2017-5-3 14:56:06");
    v2.addEdge("call", v10086, "location", "Nanjing", "callType", "fun", "duration", 15, "calltime", "2017-5-3 17:28:07");
    graph.tx().commit();
    Assert.assertEquals(8, graph.traversal().E().toList().size());
    // Query by sortkey prefix "location"
    List<Edge> edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").toList();
    Assert.assertEquals(5, edges.size());
    Assert.assertEquals(5, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Nanjing").toList();
    Assert.assertEquals(2, edges.size());
    Assert.assertEquals(2, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Nanjing").has("~page", page).limit(1);
    }));
    // Query by sortkey prefix "location", "callType"
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").toList();
    Assert.assertEquals(4, edges.size());
    Assert.assertEquals(4, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "fun").toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "fun").has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Nanjing").has("callType", "work").toList();
    Assert.assertEquals(2, edges.size());
    Assert.assertEquals(2, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Nanjing").has("callType", "work").has("~page", page).limit(1);
    }));
    // Query by sortkey prefix "location", "callType", "duration"
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).toList();
    Assert.assertEquals(3, edges.size());
    Assert.assertEquals(3, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 8).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 8).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "fun").has("duration", 10).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "fun").has("duration", 10).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Nanjing").has("callType", "work").has("duration", 12).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Nanjing").has("callType", "work").has("duration", 12).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Nanjing").has("callType", "work").has("duration", 14).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Nanjing").has("callType", "work").has("duration", 14).has("~page", page).limit(1);
    }));
    // Query by sortkey prefix "location", "callType" and range "duration"
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.lt(8)).toList();
    Assert.assertEquals(3, edges.size());
    Assert.assertEquals(3, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.lt(8)).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.lte(8)).toList();
    Assert.assertEquals(4, edges.size());
    Assert.assertEquals(4, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.lte(8)).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.gt(3)).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.gt(3)).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.gte(3)).toList();
    Assert.assertEquals(4, edges.size());
    Assert.assertEquals(4, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.gte(3)).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.between(3, 9)).toList();
    Assert.assertEquals(4, edges.size());
    Assert.assertEquals(4, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", P.between(3, 9)).has("~page", page).limit(1);
    }));
    // Query by sortkey prefix "location", "callType", "duration",
    // "callTime"
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", "2017-5-1 23:00:00").toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", "2017-5-1 23:00:00").has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", "2017-5-2 12:00:01").toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", "2017-5-2 12:00:01").has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", "2017-5-3 12:08:02").toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", "2017-5-3 12:08:02").has("~page", page).limit(1);
    }));
    // Query by sortkey prefix "location", "callType", "duration" and
    // range "callTime"
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.lt("2017-5-2 12:00:01")).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.lt("2017-5-2 12:00:01")).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.lte("2017-5-2 12:00:01")).toList();
    Assert.assertEquals(2, edges.size());
    Assert.assertEquals(2, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.lte("2017-5-2 12:00:01")).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.gt("2017-5-2 12:00:01")).toList();
    Assert.assertEquals(1, edges.size());
    Assert.assertEquals(1, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.gt("2017-5-2 12:00:01")).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.gte("2017-5-2 12:00:01")).toList();
    Assert.assertEquals(2, edges.size());
    Assert.assertEquals(2, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.gte("2017-5-2 12:00:01")).has("~page", page).limit(1);
    }));
    edges = graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.between("2017-5-2", "2017-5-4")).toList();
    Assert.assertEquals(2, edges.size());
    Assert.assertEquals(2, traverseInPage(page -> {
        return graph.traversal().V(v1).outE("call").has("location", "Beijing").has("callType", "work").has("duration", 3).has("calltime", P.between("2017-5-2", "2017-5-4")).has("~page", page).limit(1);
    }));
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        traverseInPage(page -> {
            // no location
            return graph.traversal().V(v1).outE("call").has("callType", "work").has("duration", 3).has("calltime", P.between("2017-5-2", "2017-5-4")).has("~page", page).limit(1);
        });
    }, e -> {
        Assert.assertContains("Can't query by paging and filtering", e.getMessage());
    });
}
Also used : Date(java.util.Date) BackendException(com.baidu.hugegraph.backend.BackendException) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) LimitExceedException(com.baidu.hugegraph.exception.LimitExceedException) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) Query(com.baidu.hugegraph.backend.query.Query) Whitebox(com.baidu.hugegraph.testutil.Whitebox) CollectionFactory(com.baidu.hugegraph.util.collection.CollectionFactory) IteratorUtils(org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils) P(org.apache.tinkerpop.gremlin.process.traversal.P) ImmutableSet(com.google.common.collect.ImmutableSet) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Set(java.util.Set) UUID(java.util.UUID) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) List(java.util.List) CloseableIterator(org.apache.tinkerpop.gremlin.structure.util.CloseableIterator) Id(com.baidu.hugegraph.backend.id.Id) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Utils(com.baidu.hugegraph.testutil.Utils) Userdata(com.baidu.hugegraph.schema.Userdata) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Function(java.util.function.Function) BackendTable(com.baidu.hugegraph.backend.store.BackendTable) Text(com.baidu.hugegraph.traversal.optimize.Text) Events(com.baidu.hugegraph.util.Events) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) HugeGraph(com.baidu.hugegraph.HugeGraph) Order(org.apache.tinkerpop.gremlin.process.traversal.Order) IdGenerator(com.baidu.hugegraph.backend.id.IdGenerator) NoIndexException(com.baidu.hugegraph.exception.NoIndexException) Assume(org.junit.Assume) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) Shard(com.baidu.hugegraph.backend.store.Shard) CoreOptions(com.baidu.hugegraph.config.CoreOptions) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Before(org.junit.Before) PageInfo(com.baidu.hugegraph.backend.page.PageInfo) Iterator(java.util.Iterator) Condition(com.baidu.hugegraph.backend.query.Condition) ConditionP(com.baidu.hugegraph.traversal.optimize.ConditionP) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) Test(org.junit.Test) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) T(org.apache.tinkerpop.gremlin.structure.T) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) GraphTraversal(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal) ExecutionException(java.util.concurrent.ExecutionException) HugeException(com.baidu.hugegraph.HugeException) Direction(org.apache.tinkerpop.gremlin.structure.Direction) Directions(com.baidu.hugegraph.type.define.Directions) TraversalUtil(com.baidu.hugegraph.traversal.optimize.TraversalUtil) Assert(com.baidu.hugegraph.testutil.Assert) Collections(java.util.Collections) HugeType(com.baidu.hugegraph.type.HugeType) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) SchemaManager(com.baidu.hugegraph.schema.SchemaManager) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Example 30 with Query

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

the class RamTableTest method edgesOfVertex.

private Iterator<Edge> edgesOfVertex(Id source, Directions dir, Id label) {
    Id[] labels = {};
    if (label != null) {
        labels = new Id[] { label };
    }
    Query query = GraphTransaction.constructEdgesQuery(source, dir, labels);
    return this.graph().edges(query);
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) Id(com.baidu.hugegraph.backend.id.Id)

Aggregations

Query (com.baidu.hugegraph.backend.query.Query)31 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)24 Id (com.baidu.hugegraph.backend.id.Id)11 HugeGraph (com.baidu.hugegraph.HugeGraph)9 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)9 HugeType (com.baidu.hugegraph.type.HugeType)7 Iterator (java.util.Iterator)7 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)6 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)6 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)6 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)6 BackendEntry (com.baidu.hugegraph.backend.store.BackendEntry)5 Edge (org.apache.tinkerpop.gremlin.structure.Edge)5 Test (org.junit.Test)5 HugeException (com.baidu.hugegraph.HugeException)4 BackendException (com.baidu.hugegraph.backend.BackendException)4 GraphTransaction (com.baidu.hugegraph.backend.tx.GraphTransaction)4 ArrayList (java.util.ArrayList)4 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)3 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)3