Search in sources :

Example 6 with IdQuery

use of com.baidu.hugegraph.backend.query.IdQuery 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 7 with IdQuery

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

the class AbstractSerializer method writeQuery.

@Override
public Query writeQuery(Query query) {
    HugeType type = query.resultType();
    // Serialize edge condition query (TODO: add VEQ(for EOUT/EIN))
    if (type.isEdge() && query.conditionsSize() > 0) {
        if (query.idsSize() > 0) {
            throw new BackendException("Not supported query edge by id " + "and by condition at the same time");
        }
        Query result = this.writeQueryEdgeCondition(query);
        if (result != null) {
            return result;
        }
    }
    // Serialize id in query
    if (query.idsSize() == 1 && query instanceof IdQuery.OneIdQuery) {
        IdQuery.OneIdQuery result = (IdQuery.OneIdQuery) query.copy();
        result.resetId(this.writeQueryId(type, result.id()));
        query = result;
    } else if (query.idsSize() > 0 && query instanceof IdQuery) {
        IdQuery result = (IdQuery) query.copy();
        result.resetIds();
        for (Id id : query.ids()) {
            result.query(this.writeQueryId(type, id));
        }
        query = result;
    }
    // Serialize condition(key/value) in query
    if (query instanceof ConditionQuery && query.conditionsSize() > 0) {
        query = this.writeQueryCondition(query);
    }
    return query;
}
Also used : IdQuery(com.baidu.hugegraph.backend.query.IdQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 8 with IdQuery

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

the class GraphTransaction method optimizeQuery.

private Query optimizeQuery(ConditionQuery query) {
    if (query.idsSize() > 0) {
        throw new HugeException("Not supported querying by id and conditions: %s", query);
    }
    Id label = (Id) query.condition(HugeKeys.LABEL);
    // Optimize vertex query
    if (label != null && query.resultType().isVertex()) {
        VertexLabel vertexLabel = this.graph().vertexLabel(label);
        if (vertexLabel.idStrategy() == IdStrategy.PRIMARY_KEY) {
            List<Id> keys = vertexLabel.primaryKeys();
            E.checkState(!keys.isEmpty(), "The primary keys can't be empty when using " + "'%s' id strategy for vertex label '%s'", IdStrategy.PRIMARY_KEY, vertexLabel.name());
            if (query.matchUserpropKeys(keys)) {
                // Query vertex by label + primary-values
                query.optimized(OptimizedType.PRIMARY_KEY);
                String primaryValues = query.userpropValuesString(keys);
                LOG.debug("Query vertices by primaryKeys: {}", query);
                // Convert {vertex-label + primary-key} to vertex-id
                Id id = SplicingIdGenerator.splicing(label.asString(), primaryValues);
                /*
                     * Just query by primary-key(id), ignore other userprop(if
                     * exists) that it will be filtered by queryVertices(Query)
                     */
                return new IdQuery(query, id);
            }
        }
    }
    // Optimize edge query
    if (query.resultType().isEdge() && label != null && query.condition(HugeKeys.OWNER_VERTEX) != null && query.condition(HugeKeys.DIRECTION) != null && matchEdgeSortKeys(query, false, this.graph())) {
        // Query edge by sourceVertex + direction + label + sort-values
        query.optimized(OptimizedType.SORT_KEYS);
        query = query.copy();
        // Serialize sort-values
        List<Id> keys = this.graph().edgeLabel(label).sortKeys();
        List<Condition> conditions = GraphIndexTransaction.constructShardConditions(query, keys, HugeKeys.SORT_VALUES);
        query.query(conditions);
        /*
             * Reset all userprop since transferred to sort-keys, ignore other
             * userprop(if exists) that it will be filtered by queryEdges(Query)
             */
        query.resetUserpropConditions();
        LOG.debug("Query edges by sortKeys: {}", query);
        return query;
    }
    /*
         * Query only by sysprops, like: by vertex label, by edge label.
         * NOTE: we assume sysprops would be indexed by backend store
         * but we don't support query edges only by direction/target-vertex.
         */
    if (query.allSysprop()) {
        if (query.resultType().isVertex()) {
            verifyVerticesConditionQuery(query);
        } else if (query.resultType().isEdge()) {
            verifyEdgesConditionQuery(query);
        }
        /*
             * Just support:
             *  1.not query by label
             *  2.or query by label and store supports this feature
             */
        boolean byLabel = (label != null && query.conditionsSize() == 1);
        if (!byLabel || this.store().features().supportsQueryByLabel()) {
            return query;
        }
    }
    return null;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeException(com.baidu.hugegraph.HugeException)

Example 9 with IdQuery

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

the class GraphTransaction method queryVerticesByIds.

protected Iterator<Vertex> queryVerticesByIds(Object[] vertexIds, boolean adjacentVertex, boolean checkMustExist) {
    Query.checkForceCapacity(vertexIds.length);
    // NOTE: allowed duplicated vertices if query by duplicated ids
    List<Id> ids = InsertionOrderUtil.newList();
    Map<Id, HugeVertex> vertices = new HashMap<>(vertexIds.length);
    IdQuery query = new IdQuery(HugeType.VERTEX);
    for (Object vertexId : vertexIds) {
        HugeVertex vertex;
        Id id = HugeVertex.getIdValue(vertexId);
        if (id == null || this.removedVertices.containsKey(id)) {
            // The record has been deleted
            continue;
        } else if ((vertex = this.addedVertices.get(id)) != null || (vertex = this.updatedVertices.get(id)) != null) {
            if (vertex.expired()) {
                continue;
            }
            // Found from local tx
            vertices.put(vertex.id(), vertex);
        } else {
            // Prepare to query from backend store
            query.query(id);
        }
        ids.add(id);
    }
    if (!query.empty()) {
        // Query from backend store
        query.mustSortByInput(false);
        Iterator<HugeVertex> it = this.queryVerticesFromBackend(query);
        QueryResults.fillMap(it, vertices);
    }
    return new MapperIterator<>(ids.iterator(), id -> {
        HugeVertex vertex = vertices.get(id);
        if (vertex == null) {
            if (checkMustExist) {
                throw new NotFoundException("Vertex '%s' does not exist", id);
            } else if (adjacentVertex) {
                assert !checkMustExist;
                // Return undefined if adjacentVertex but !checkMustExist
                vertex = HugeVertex.undefined(this.graph(), id);
            } else {
                // Return null
                assert vertex == null;
            }
        }
        return vertex;
    });
}
Also used : MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) HashMap(java.util.HashMap) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 10 with IdQuery

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

the class GraphTransaction method queryEdgesByIds.

protected Iterator<Edge> queryEdgesByIds(Object[] edgeIds, boolean verifyId) {
    Query.checkForceCapacity(edgeIds.length);
    // NOTE: allowed duplicated edges if query by duplicated ids
    List<Id> ids = InsertionOrderUtil.newList();
    Map<Id, HugeEdge> edges = new HashMap<>(edgeIds.length);
    IdQuery query = new IdQuery(HugeType.EDGE);
    for (Object edgeId : edgeIds) {
        HugeEdge edge;
        EdgeId id = HugeEdge.getIdValue(edgeId, !verifyId);
        if (id == null) {
            continue;
        }
        if (id.direction() == Directions.IN) {
            id = id.switchDirection();
        }
        if (this.removedEdges.containsKey(id)) {
            // The record has been deleted
            continue;
        } else if ((edge = this.addedEdges.get(id)) != null || (edge = this.updatedEdges.get(id)) != null) {
            if (edge.expired()) {
                continue;
            }
            // Found from local tx
            edges.put(edge.id(), edge);
        } else {
            // Prepare to query from backend store
            query.query(id);
        }
        ids.add(id);
    }
    if (!query.empty()) {
        // Query from backend store
        if (edges.isEmpty() && query.idsSize() == ids.size()) {
            /*
                 * Sort at the lower layer and return directly if there is no
                 * local vertex and duplicated id.
                 */
            Iterator<HugeEdge> it = this.queryEdgesFromBackend(query);
            @SuppressWarnings({ "unchecked", "rawtypes" }) Iterator<Edge> r = (Iterator) it;
            return r;
        }
        query.mustSortByInput(false);
        Iterator<HugeEdge> it = this.queryEdgesFromBackend(query);
        QueryResults.fillMap(it, edges);
    }
    return new MapperIterator<>(ids.iterator(), id -> {
        Edge edge = edges.get(id);
        return edge;
    });
}
Also used : MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) HashMap(java.util.HashMap) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) ListIterator(com.baidu.hugegraph.iterator.ListIterator) MapperIterator(com.baidu.hugegraph.iterator.MapperIterator) ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) FlatMapperIterator(com.baidu.hugegraph.iterator.FlatMapperIterator) LimitIterator(com.baidu.hugegraph.iterator.LimitIterator) CloseableIterator(org.apache.tinkerpop.gremlin.structure.util.CloseableIterator) BatchMapperIterator(com.baidu.hugegraph.iterator.BatchMapperIterator) FilterIterator(com.baidu.hugegraph.iterator.FilterIterator) Iterator(java.util.Iterator) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge)

Aggregations

IdQuery (com.baidu.hugegraph.backend.query.IdQuery)11 Id (com.baidu.hugegraph.backend.id.Id)7 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)5 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)4 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)3 Query (com.baidu.hugegraph.backend.query.Query)3 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)3 HugeException (com.baidu.hugegraph.HugeException)2 BackendException (com.baidu.hugegraph.backend.BackendException)2 BatchMapperIterator (com.baidu.hugegraph.iterator.BatchMapperIterator)2 ExtendableIterator (com.baidu.hugegraph.iterator.ExtendableIterator)2 FlatMapperIterator (com.baidu.hugegraph.iterator.FlatMapperIterator)2 MapperIterator (com.baidu.hugegraph.iterator.MapperIterator)2 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)2 HugeType (com.baidu.hugegraph.type.HugeType)2 HashMap (java.util.HashMap)2 QueryId (com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId)1 Condition (com.baidu.hugegraph.backend.query.Condition)1 IdPrefixQuery (com.baidu.hugegraph.backend.query.IdPrefixQuery)1 IdRangeQuery (com.baidu.hugegraph.backend.query.IdRangeQuery)1