Search in sources :

Example 21 with IndexLabel

use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.

the class GraphIndexTransaction method updateVertexOlapIndex.

private void updateVertexOlapIndex(HugeVertex vertex, boolean removed) {
    Set<Id> propKeys = vertex.getPropertyKeys();
    E.checkArgument(propKeys.size() == 1, "Expect only 1 property for olap vertex, but got %s", propKeys.size());
    Id pkId = propKeys.iterator().next();
    List<IndexLabel> indexLabels = this.params().schemaTransaction().getIndexLabels();
    for (IndexLabel il : indexLabels) {
        if (il.indexFields().contains(pkId)) {
            this.updateIndex(il.id(), vertex, removed);
        }
    }
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id)

Example 22 with IndexLabel

use of com.baidu.hugegraph.schema.IndexLabel 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 23 with IndexLabel

use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.

the class GraphIndexTransaction method matchRangeOrSearchIndexLabels.

private static Set<IndexLabel> matchRangeOrSearchIndexLabels(ConditionQuery query, Set<IndexLabel> indexLabels) {
    Set<IndexLabel> matchedIndexLabels = InsertionOrderUtil.newSet();
    for (Condition.Relation relation : query.userpropRelations()) {
        if (!relation.relation().isRangeType() && !relation.relation().isSearchType()) {
            continue;
        }
        Id key = (Id) relation.key();
        boolean matched = false;
        for (IndexLabel indexLabel : indexLabels) {
            if (indexLabel.indexType().isRange() || indexLabel.indexType().isSearch()) {
                if (indexLabel.indexField().equals(key)) {
                    matched = true;
                    matchedIndexLabels.add(indexLabel);
                    break;
                }
            }
        }
        if (!matched) {
            return ImmutableSet.of();
        }
    }
    return matchedIndexLabels;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) Relation(com.baidu.hugegraph.backend.query.Condition.Relation) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id)

Example 24 with IndexLabel

use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.

the class GraphIndexTransaction method constructSearchQuery.

private ConditionQuery constructSearchQuery(ConditionQuery query, MatchedIndex index) {
    ConditionQuery newQuery = query;
    Set<Id> indexFields = new HashSet<>();
    // Convert has(key, text) to has(key, textContainsAny(word1, word2))
    for (IndexLabel il : index.indexLabels()) {
        if (il.indexType() != IndexType.SEARCH) {
            continue;
        }
        Id indexField = il.indexField();
        String fieldValue = (String) newQuery.userpropValue(indexField);
        Set<String> words = this.segmentWords(fieldValue);
        indexFields.add(indexField);
        newQuery = newQuery.copy();
        newQuery.unsetCondition(indexField);
        newQuery.query(Condition.textContainsAny(indexField, words));
    }
    // Register results filter to compare property value and search text
    newQuery.registerResultsFilter(element -> {
        assert element != null;
        for (Condition cond : query.conditions()) {
            Object key = cond.isRelation() ? ((Relation) cond).key() : null;
            if (key instanceof Id && indexFields.contains(key)) {
                // This is an index field of search index
                Id field = (Id) key;
                HugeProperty<?> property = element.getProperty(field);
                String propValue = propertyValueToString(property.value());
                String fieldValue = (String) query.userpropValue(field);
                if (this.matchSearchIndexWords(propValue, fieldValue)) {
                    continue;
                }
                return false;
            }
            if (!cond.test(element)) {
                return false;
            }
        }
        return true;
    });
    return newQuery;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id) HashSet(java.util.HashSet)

Example 25 with IndexLabel

use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.

the class GraphIndexTransaction method constructQueries.

private static IndexQueries constructQueries(ConditionQuery query, Set<IndexLabel> ils, Set<Id> propKeys) {
    IndexQueries queries = IndexQueries.of(query);
    for (IndexLabel il : ils) {
        List<Id> fields = il.indexFields();
        ConditionQuery newQuery = query.copy();
        newQuery.resetUserpropConditions();
        for (Id field : fields) {
            if (!propKeys.contains(field)) {
                break;
            }
            for (Condition c : query.userpropConditions(field)) {
                newQuery.query(c);
            }
        }
        ConditionQuery q = constructQuery(newQuery, il);
        assert q != null;
        queries.put(il, q);
    }
    return queries;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id)

Aggregations

IndexLabel (com.baidu.hugegraph.schema.IndexLabel)53 Id (com.baidu.hugegraph.backend.id.Id)24 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)8 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)8 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)8 Test (org.junit.Test)8 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)7 HugeGraph (com.baidu.hugegraph.HugeGraph)5 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)5 HugeType (com.baidu.hugegraph.type.HugeType)5 GraphTransaction (com.baidu.hugegraph.backend.tx.GraphTransaction)4 SchemaTransaction (com.baidu.hugegraph.backend.tx.SchemaTransaction)4 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)4 HugeIndex (com.baidu.hugegraph.structure.HugeIndex)4 LockUtil (com.baidu.hugegraph.util.LockUtil)4 IdHolder (com.baidu.hugegraph.backend.page.IdHolder)3 BatchIdHolder (com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder)3 FixedIdHolder (com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder)3 PagingIdHolder (com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder)3 Condition (com.baidu.hugegraph.backend.query.Condition)3