Search in sources :

Example 11 with Query

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

the class StoreDumper method dump.

public void dump(HugeType table, long offset, long limit) {
    BackendStore store = this.backendStore(table);
    Query query = new Query(table);
    Iterator<BackendEntry> rs = store.query(query);
    for (long i = 0; i < offset && rs.hasNext(); i++) {
        rs.next();
    }
    LOG.info("Dump table {} (offset {} limit {}):", table, offset, limit);
    for (long i = 0; i < limit && rs.hasNext(); i++) {
        BackendEntry entry = rs.next();
        LOG.info("{}", entry);
    }
    CloseableIterator.closeIterator(rs);
}
Also used : BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) Query(com.baidu.hugegraph.backend.query.Query) BackendStore(com.baidu.hugegraph.backend.store.BackendStore)

Example 12 with Query

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

the class GraphTransaction method joinTxEdges.

private Iterator<?> joinTxEdges(Query query, Iterator<HugeEdge> edges, Map<Id, HugeVertex> removingVertices) {
    assert query.resultType().isEdge();
    BiFunction<Query, HugeEdge, HugeEdge> matchTxFunc = (q, e) -> {
        assert q.resultType() == HugeType.EDGE;
        if (e.expired() && !q.showExpired()) {
            // Filter expired edges with TTL
            return null;
        }
        // Filter edges matched conditions
        return q.test(e) ? e : q.test(e = e.switchOwner()) ? e : null;
    };
    edges = this.joinTxRecords(query, edges, matchTxFunc, this.addedEdges, this.removedEdges, this.updatedEdges);
    if (removingVertices.isEmpty()) {
        return edges;
    }
    // Filter edges that belong to deleted vertex
    return new FilterIterator<HugeEdge>(edges, edge -> {
        for (HugeVertex v : removingVertices.values()) {
            if (edge.belongToVertex(v)) {
                return false;
            }
        }
        return true;
    });
}
Also used : Arrays(java.util.Arrays) QueryResults(com.baidu.hugegraph.backend.query.QueryResults) ListIterator(com.baidu.hugegraph.iterator.ListIterator) BiFunction(java.util.function.BiFunction) Graph(org.apache.tinkerpop.gremlin.structure.Graph) BackendException(com.baidu.hugegraph.backend.BackendException) HugeProperty(com.baidu.hugegraph.structure.HugeProperty) HugeElement(com.baidu.hugegraph.structure.HugeElement) ElementHelper(org.apache.tinkerpop.gremlin.structure.util.ElementHelper) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) SplicingIdGenerator(com.baidu.hugegraph.backend.id.SplicingIdGenerator) LimitExceedException(com.baidu.hugegraph.exception.LimitExceedException) DeleteExpiredJob(com.baidu.hugegraph.job.system.DeleteExpiredJob) LockUtil(com.baidu.hugegraph.util.LockUtil) Map(java.util.Map) Query(com.baidu.hugegraph.backend.query.Query) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) IteratorUtils(org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) BackendStore(com.baidu.hugegraph.backend.store.BackendStore) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Collection(java.util.Collection) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Set(java.util.Set) QueryList(com.baidu.hugegraph.backend.page.QueryList) InsertionOrderUtil(com.baidu.hugegraph.util.InsertionOrderUtil) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) ForbiddenException(jakarta.ws.rs.ForbiddenException) List(java.util.List) Element(org.apache.tinkerpop.gremlin.structure.Element) CloseableIterator(org.apache.tinkerpop.gremlin.structure.util.CloseableIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) IdStrategy(com.baidu.hugegraph.type.define.IdStrategy) Id(com.baidu.hugegraph.backend.id.Id) AggregateFunc(com.baidu.hugegraph.backend.query.Aggregate.AggregateFunc) HugeVertexFeatures(com.baidu.hugegraph.structure.HugeFeatures.HugeVertexFeatures) HugeGraphParams(com.baidu.hugegraph.HugeGraphParams) BackendEntry(com.baidu.hugegraph.backend.store.BackendEntry) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ConditionQueryFlatten(com.baidu.hugegraph.backend.query.ConditionQueryFlatten) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) ImmutableList(com.google.common.collect.ImmutableList) CollectionUtils(org.apache.commons.collections.CollectionUtils) HugeGraph(com.baidu.hugegraph.HugeGraph) OptimizedType(com.baidu.hugegraph.backend.query.ConditionQuery.OptimizedType) E(com.baidu.hugegraph.util.E) CoreOptions(com.baidu.hugegraph.config.CoreOptions) Edge(org.apache.tinkerpop.gremlin.structure.Edge) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) PageInfo(com.baidu.hugegraph.backend.page.PageInfo) Iterator(java.util.Iterator) HugeEdgeProperty(com.baidu.hugegraph.structure.HugeEdgeProperty) Condition(com.baidu.hugegraph.backend.query.Condition) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) HugeKeys(com.baidu.hugegraph.type.define.HugeKeys) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched) Consumer(java.util.function.Consumer) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Action(com.baidu.hugegraph.type.define.Action) HugeException(com.baidu.hugegraph.HugeException) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Directions(com.baidu.hugegraph.type.define.Directions) Aggregate(com.baidu.hugegraph.backend.query.Aggregate) HugeVertexProperty(com.baidu.hugegraph.structure.HugeVertexProperty) HugeIndex(com.baidu.hugegraph.structure.HugeIndex) HugeConfig(com.baidu.hugegraph.config.HugeConfig) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) HugeType(com.baidu.hugegraph.type.HugeType) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 13 with Query

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

the class GraphTransaction method prepareDeletions.

protected void prepareDeletions(Map<Id, HugeVertex> removedVertices, Map<Id, HugeEdge> removedEdges) {
    // Remove related edges of each vertex
    for (HugeVertex v : removedVertices.values()) {
        if (!v.schemaLabel().existsLinkLabel()) {
            continue;
        }
        // Query all edges of the vertex and remove them
        Query query = constructEdgesQuery(v.id(), Directions.BOTH);
        Iterator<HugeEdge> vedges = this.queryEdgesFromBackend(query);
        try {
            while (vedges.hasNext()) {
                this.checkTxEdgesCapacity();
                HugeEdge edge = vedges.next();
                // NOTE: will change the input parameter
                removedEdges.put(edge.id(), edge);
                // Commit first if enabled commit-part mode
                if (this.commitPartOfAdjacentEdges > 0 && removedEdges.size() >= this.commitPartOfAdjacentEdges) {
                    this.commitPartOfEdgeDeletions(removedEdges);
                }
            }
        } finally {
            CloseableIterator.closeIterator(vedges);
        }
    }
    // Remove vertices
    for (HugeVertex v : removedVertices.values()) {
        this.checkAggregateProperty(v);
        /*
             * If the backend stores vertex together with edges, it's edges
             * would be removed after removing vertex. Otherwise, if the
             * backend stores vertex which is separated from edges, it's
             * edges should be removed manually when removing vertex.
             */
        this.doRemove(this.serializer.writeVertex(v.prepareRemoved()));
        this.indexTx.updateVertexIndex(v, true);
        this.indexTx.updateLabelIndex(v, true);
    }
    // Remove edges
    this.prepareDeletions(removedEdges);
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 14 with Query

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

the class AbstractTransaction method query.

@Watched(prefix = "tx")
public QueryResults<BackendEntry> query(Query query) {
    LOG.debug("Transaction query: {}", query);
    /*
         * NOTE: it's dangerous if an IdQuery/ConditionQuery is empty
         * check if the query is empty and its class is not the Query itself
         */
    if (query.empty() && !query.getClass().equals(Query.class)) {
        throw new BackendException("Query without any id or condition");
    }
    Query squery = this.serializer.writeQuery(query);
    // Do rate limit if needed
    RateLimiter rateLimiter = this.graph.readRateLimiter();
    if (rateLimiter != null && query.resultType().isGraph()) {
        double time = rateLimiter.acquire(1);
        if (time > 0) {
            LOG.debug("Waited for {}s to query", time);
        }
        BackendEntryIterator.checkInterrupted();
    }
    this.beforeRead();
    try {
        this.injectOlapPkIfNeeded(squery);
        return new QueryResults<>(this.store.query(squery), query);
    } finally {
        // TODO: not complete the iteration currently
        this.afterRead();
    }
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) RateLimiter(com.google.common.util.concurrent.RateLimiter) QueryResults(com.baidu.hugegraph.backend.query.QueryResults) BackendException(com.baidu.hugegraph.backend.BackendException) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 15 with Query

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

the class EdgeCoreTest method testQueryAllWithLimitByQueryEdges.

@Test
public void testQueryAllWithLimitByQueryEdges() {
    HugeGraph graph = graph();
    init18Edges();
    Query query = new Query(HugeType.EDGE);
    query.limit(1);
    Iterator<Edge> iter = graph.edges(query);
    List<Edge> edges = IteratorUtils.list(iter);
    Assert.assertEquals(1, edges.size());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

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