Search in sources :

Example 1 with IdHolder

use of com.baidu.hugegraph.backend.page.IdHolder in project incubator-hugegraph by apache.

the class GraphIndexTransaction method doSearchIndex.

@Watched(prefix = "index")
private IdHolderList doSearchIndex(ConditionQuery query, MatchedIndex index) {
    query = this.constructSearchQuery(query, index);
    // Sorted by matched count
    IdHolderList holders = new SortByCountIdHolderList(query.paging());
    List<ConditionQuery> flatten = ConditionQueryFlatten.flatten(query);
    for (ConditionQuery q : flatten) {
        if (!q.noLimit() && flatten.size() > 1) {
            // Increase limit for union operation
            increaseLimit(q);
        }
        IndexQueries queries = index.constructIndexQueries(q);
        assert !query.paging() || queries.size() <= 1;
        IdHolder holder = this.doSingleOrJointIndex(queries);
        // NOTE: ids will be merged into one IdHolder if not in paging
        holders.add(holder);
    }
    return holders;
}
Also used : SortByCountIdHolderList(com.baidu.hugegraph.backend.page.SortByCountIdHolderList) SortByCountIdHolderList(com.baidu.hugegraph.backend.page.SortByCountIdHolderList) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdHolder(com.baidu.hugegraph.backend.page.IdHolder) FixedIdHolder(com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder) PagingIdHolder(com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder) BatchIdHolder(com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 2 with IdHolder

use of com.baidu.hugegraph.backend.page.IdHolder in project incubator-hugegraph by apache.

the class GraphIndexTransaction method queryByUserprop.

@Watched(prefix = "index")
private IdHolderList queryByUserprop(ConditionQuery query) {
    // related index labels
    if (!this.graph().readMode().showOlap()) {
        for (Id pkId : query.userpropKeys()) {
            PropertyKey propertyKey = this.graph().propertyKey(pkId);
            if (propertyKey.olap()) {
                throw new NotAllowException("Not allowed to query by olap property key '%s'" + " when graph-read-mode is '%s'", propertyKey, this.graph().readMode());
            }
        }
    }
    Set<MatchedIndex> indexes = this.collectMatchedIndexes(query);
    if (indexes.isEmpty()) {
        Id label = query.condition(HugeKeys.LABEL);
        throw noIndexException(this.graph(), query, label);
    }
    // Value type of Condition not matched
    boolean paging = query.paging();
    if (!validQueryConditionValues(this.graph(), query)) {
        return IdHolderList.empty(paging);
    }
    // Do index query
    IdHolderList holders = new IdHolderList(paging);
    for (MatchedIndex index : indexes) {
        for (IndexLabel il : index.indexLabels()) {
            validateIndexLabel(il);
        }
        if (paging && index.indexLabels().size() > 1) {
            throw new NotSupportException("joint index query in paging");
        }
        if (index.containsSearchIndex()) {
            // Do search-index query
            holders.addAll(this.doSearchIndex(query, index));
        } else {
            // Do secondary-index, range-index or shard-index query
            IndexQueries queries = index.constructIndexQueries(query);
            assert !paging || queries.size() <= 1;
            IdHolder holder = this.doSingleOrJointIndex(queries);
            holders.add(holder);
        }
    /*
             * NOTE: need to skip the offset if offset > 0, but can't handle
             * it here because the query may a sub-query after flatten,
             * so the offset will be handle in QueryList.IndexQuery
             *
             * TODO: finish early here if records exceeds required limit with
             *       FixedIdHolder.
             */
    }
    return holders;
}
Also used : SortByCountIdHolderList(com.baidu.hugegraph.backend.page.SortByCountIdHolderList) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) IdHolder(com.baidu.hugegraph.backend.page.IdHolder) FixedIdHolder(com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder) PagingIdHolder(com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder) BatchIdHolder(com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder) Id(com.baidu.hugegraph.backend.id.Id) NotSupportException(com.baidu.hugegraph.exception.NotSupportException) NotAllowException(com.baidu.hugegraph.exception.NotAllowException) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 3 with IdHolder

use of com.baidu.hugegraph.backend.page.IdHolder in project incubator-hugegraph by apache.

the class GraphIndexTransaction method doJointIndex.

@Watched(prefix = "index")
private IdHolder doJointIndex(IndexQueries queries) {
    if (queries.oomRisk()) {
        LOG.warn("There is OOM risk if the joint operation is based on a " + "large amount of data, please use single index + filter " + "instead of joint index: {}", queries.rootQuery());
    }
    // All queries are joined with AND
    Set<Id> intersectIds = null;
    boolean filtering = false;
    IdHolder resultHolder = null;
    for (Map.Entry<IndexLabel, ConditionQuery> e : queries.entrySet()) {
        IndexLabel indexLabel = e.getKey();
        ConditionQuery query = e.getValue();
        assert !query.paging();
        if (!query.noLimit() && queries.size() > 1) {
            // Unset limit for intersection operation
            query.limit(Query.NO_LIMIT);
        }
        /*
             * Try to query by joint indexes:
             * 1 If there is any index exceeded the threshold, transform into
             *   partial index query, then filter after back-table.
             * 1.1 Return the holder of the first index that not exceeded the
             *     threshold if there exists one index, this holder will be used
             *     as the only query condition.
             * 1.2 Return the holder of the first index if all indexes exceeded
             *     the threshold.
             * 2 Else intersect holders for all indexes, and return intersection
             *   ids of all indexes.
             */
        IdHolder holder = this.doIndexQuery(indexLabel, query);
        if (resultHolder == null) {
            resultHolder = holder;
            this.storeSelectedIndexField(indexLabel, query);
        }
        // default value is 1000
        assert this.indexIntersectThresh > 0;
        Set<Id> ids = ((BatchIdHolder) holder).peekNext(this.indexIntersectThresh).ids();
        if (ids.size() >= this.indexIntersectThresh) {
            // Transform into filtering
            filtering = true;
            query.optimized(OptimizedType.INDEX_FILTER);
        } else if (filtering) {
            assert ids.size() < this.indexIntersectThresh;
            resultHolder = holder;
            this.storeSelectedIndexField(indexLabel, query);
            break;
        } else {
            if (intersectIds == null) {
                intersectIds = ids;
            } else {
                CollectionUtil.intersectWithModify(intersectIds, ids);
            }
            if (intersectIds.isEmpty()) {
                break;
            }
        }
    }
    if (filtering) {
        return resultHolder;
    } else {
        assert intersectIds != null;
        return new FixedIdHolder(queries.asJointQuery(), intersectIds);
    }
}
Also used : FixedIdHolder(com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdHolder(com.baidu.hugegraph.backend.page.IdHolder) FixedIdHolder(com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder) PagingIdHolder(com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder) BatchIdHolder(com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id) Map(java.util.Map) HashMap(java.util.HashMap) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 4 with IdHolder

use of com.baidu.hugegraph.backend.page.IdHolder in project incubator-hugegraph by apache.

the class GraphIndexTransaction method queryByLabel.

@Watched(prefix = "index")
private IdHolderList queryByLabel(ConditionQuery query) {
    HugeType queryType = query.resultType();
    IndexLabel il = IndexLabel.label(queryType);
    validateIndexLabel(il);
    Id label = query.condition(HugeKeys.LABEL);
    assert label != null;
    HugeType indexType;
    SchemaLabel schemaLabel;
    if (queryType.isVertex()) {
        indexType = HugeType.VERTEX_LABEL_INDEX;
        schemaLabel = this.graph().vertexLabel(label);
    } else if (queryType.isEdge()) {
        indexType = HugeType.EDGE_LABEL_INDEX;
        schemaLabel = this.graph().edgeLabel(label);
    } else {
        throw new HugeException("Can't query %s by label", queryType);
    }
    if (!this.store().features().supportsQueryByLabel() && !schemaLabel.enableLabelIndex()) {
        throw new NoIndexException("Don't accept query by label '%s', " + "label index is disabled", schemaLabel);
    }
    ConditionQuery indexQuery = new ConditionQuery(indexType, query);
    indexQuery.eq(HugeKeys.INDEX_LABEL_ID, il.id());
    indexQuery.eq(HugeKeys.FIELD_VALUES, label);
    /*
         * We can avoid redundant element ids if set limit, but if there are
         * label index overridden by other vertices with different label,
         * query with limit like g.V().hasLabel('xx').limit(10) may lose some
         * results, so can't set limit here. But in this case, the following
         * query results may be still different:
         *   g.V().hasLabel('xx').count()  // label index count
         *   g.V().hasLabel('xx').limit(-1).count()  // actual vertices count
         * It’s a similar situation for the offset, like:
         *   g.V().hasLabel('xx').range(26, 27)
         *   g.V().hasLabel('xx').range(27, 28)
         * we just reset limit here, but don't reset offset due to performance
         * optimization with index+offset query, see Query.skipOffsetIfNeeded().
         * NOTE: if set offset the backend itself will skip the offset
         */
    indexQuery.copyBasic(query);
    indexQuery.limit(Query.NO_LIMIT);
    IdHolder idHolder = this.doIndexQuery(il, indexQuery);
    IdHolderList holders = new IdHolderList(query.paging());
    holders.add(idHolder);
    return holders;
}
Also used : NoIndexException(com.baidu.hugegraph.exception.NoIndexException) SortByCountIdHolderList(com.baidu.hugegraph.backend.page.SortByCountIdHolderList) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) IdHolder(com.baidu.hugegraph.backend.page.IdHolder) FixedIdHolder(com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder) PagingIdHolder(com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder) BatchIdHolder(com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType) HugeException(com.baidu.hugegraph.HugeException) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

IdHolder (com.baidu.hugegraph.backend.page.IdHolder)4 BatchIdHolder (com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder)4 FixedIdHolder (com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder)4 PagingIdHolder (com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder)4 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)4 Id (com.baidu.hugegraph.backend.id.Id)3 IdHolderList (com.baidu.hugegraph.backend.page.IdHolderList)3 SortByCountIdHolderList (com.baidu.hugegraph.backend.page.SortByCountIdHolderList)3 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)3 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)3 HugeException (com.baidu.hugegraph.HugeException)1 NoIndexException (com.baidu.hugegraph.exception.NoIndexException)1 NotAllowException (com.baidu.hugegraph.exception.NotAllowException)1 NotSupportException (com.baidu.hugegraph.exception.NotSupportException)1 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)1 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)1 HugeType (com.baidu.hugegraph.type.HugeType)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1