Search in sources :

Example 1 with IndexType

use of com.baidu.hugegraph.type.define.IndexType in project incubator-hugegraph by apache.

the class TextSerializer method readIndexLabel.

@Override
public IndexLabel readIndexLabel(HugeGraph graph, BackendEntry backendEntry) {
    if (backendEntry == null) {
        return null;
    }
    TextBackendEntry entry = this.convertEntry(backendEntry);
    Id id = readId(entry.id());
    String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME), String.class);
    String baseType = entry.column(HugeKeys.BASE_TYPE);
    String baseValue = entry.column(HugeKeys.BASE_VALUE);
    String indexType = entry.column(HugeKeys.INDEX_TYPE);
    String indexFields = entry.column(HugeKeys.FIELDS);
    String status = entry.column(HugeKeys.STATUS);
    IndexLabel indexLabel = new IndexLabel(graph, id, name);
    indexLabel.baseType(JsonUtil.fromJson(baseType, HugeType.class));
    indexLabel.baseValue(readId(baseValue));
    indexLabel.indexType(JsonUtil.fromJson(indexType, IndexType.class));
    indexLabel.indexFields(readIds(indexFields));
    readUserdata(indexLabel, entry);
    indexLabel.status(JsonUtil.fromJson(status, SchemaStatus.class));
    return indexLabel;
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) IndexType(com.baidu.hugegraph.type.define.IndexType) HugeType(com.baidu.hugegraph.type.HugeType) SchemaStatus(com.baidu.hugegraph.type.define.SchemaStatus)

Example 2 with IndexType

use of com.baidu.hugegraph.type.define.IndexType in project incubator-hugegraph by apache.

the class GraphIndexTransaction method matchSingleOrCompositeIndex.

private static Set<IndexLabel> matchSingleOrCompositeIndex(ConditionQuery query, Set<IndexLabel> indexLabels) {
    if (query.hasNeqCondition()) {
        return ImmutableSet.of();
    }
    boolean requireRange = query.hasRangeCondition();
    boolean requireSearch = query.hasSearchCondition();
    Set<Id> queryPropKeys = query.userpropKeys();
    for (IndexLabel indexLabel : indexLabels) {
        List<Id> indexFields = indexLabel.indexFields();
        // Try to match fields
        if (!matchIndexFields(queryPropKeys, indexFields)) {
            continue;
        }
        /*
             * Matched all fields, try to match index type.
             * For range-index or search-index there must be only one condition.
             * The following terms are legal:
             *   1.hasSearchCondition and IndexType.SEARCH
             *   2.hasRangeCondition and IndexType.RANGE
             *   3.not hasRangeCondition but has range-index equal-condition
             *   4.secondary (composite) index
             */
        IndexType indexType = indexLabel.indexType();
        if ((requireSearch && !indexType.isSearch()) || (!requireSearch && indexType.isSearch())) {
            continue;
        }
        if (requireRange && !indexType.isNumeric()) {
            continue;
        }
        return ImmutableSet.of(indexLabel);
    }
    return ImmutableSet.of();
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id) IndexType(com.baidu.hugegraph.type.define.IndexType)

Example 3 with IndexType

use of com.baidu.hugegraph.type.define.IndexType in project incubator-hugegraph by apache.

the class TableSerializer method readIndexLabel.

@Override
public IndexLabel readIndexLabel(HugeGraph graph, BackendEntry backendEntry) {
    if (backendEntry == null) {
        return null;
    }
    TableBackendEntry entry = this.convertEntry(backendEntry);
    Number id = schemaColumn(entry, HugeKeys.ID);
    String name = schemaColumn(entry, HugeKeys.NAME);
    HugeType baseType = schemaEnum(entry, HugeKeys.BASE_TYPE, HugeType.class);
    Number baseValueId = schemaColumn(entry, HugeKeys.BASE_VALUE);
    IndexType indexType = schemaEnum(entry, HugeKeys.INDEX_TYPE, IndexType.class);
    Object indexFields = schemaColumn(entry, HugeKeys.FIELDS);
    SchemaStatus status = schemaEnum(entry, HugeKeys.STATUS, SchemaStatus.class);
    IndexLabel indexLabel = new IndexLabel(graph, this.toId(id), name);
    indexLabel.baseType(baseType);
    indexLabel.baseValue(this.toId(baseValueId));
    indexLabel.indexType(indexType);
    indexLabel.indexFields(this.toIdArray(indexFields));
    indexLabel.status(status);
    this.readUserdata(indexLabel, entry);
    return indexLabel;
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) IndexType(com.baidu.hugegraph.type.define.IndexType) HugeType(com.baidu.hugegraph.type.HugeType) SchemaStatus(com.baidu.hugegraph.type.define.SchemaStatus)

Example 4 with IndexType

use of com.baidu.hugegraph.type.define.IndexType in project incubator-hugegraph by apache.

the class GraphIndexTransaction method constructQuery.

private static ConditionQuery constructQuery(ConditionQuery query, IndexLabel indexLabel) {
    IndexType indexType = indexLabel.indexType();
    boolean requireRange = query.hasRangeCondition();
    boolean supportRange = indexType.isNumeric();
    if (requireRange && !supportRange) {
        LOG.debug("There is range query condition in '{}', " + "but the index label '{}' is unable to match", query, indexLabel.name());
        return null;
    }
    Set<Id> queryKeys = query.userpropKeys();
    List<Id> indexFields = indexLabel.indexFields();
    if (!matchIndexFields(queryKeys, indexFields)) {
        return null;
    }
    LOG.debug("Matched index fields: {} of index '{}'", indexFields, indexLabel);
    ConditionQuery indexQuery;
    switch(indexType) {
        case SEARCH:
            E.checkState(indexFields.size() == 1, "Invalid index fields size for %s: %s", indexType, indexFields);
            Object fieldValue = query.userpropValue(indexFields.get(0));
            assert fieldValue instanceof String;
            // Will escape special char inside concatValues()
            fieldValue = ConditionQuery.concatValues(fieldValue);
            indexQuery = new ConditionQuery(indexType.type(), query);
            indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id());
            indexQuery.eq(HugeKeys.FIELD_VALUES, fieldValue);
            break;
        case SECONDARY:
            List<Id> joinedKeys = indexFields.subList(0, queryKeys.size());
            // Will escape special char inside userpropValuesString()
            String joinedValues = query.userpropValuesString(joinedKeys);
            indexQuery = new ConditionQuery(indexType.type(), query);
            indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id());
            indexQuery.eq(HugeKeys.FIELD_VALUES, joinedValues);
            break;
        case RANGE_INT:
        case RANGE_FLOAT:
        case RANGE_LONG:
        case RANGE_DOUBLE:
            if (query.userpropConditions().size() > 2) {
                throw new HugeException("Range query has two conditions at most, " + "but got: %s", query.userpropConditions());
            }
            // Replace the query key with PROPERTY_VALUES, set number value
            indexQuery = new ConditionQuery(indexType.type(), query);
            indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id());
            for (Condition condition : query.userpropConditions()) {
                assert condition instanceof Condition.Relation;
                Condition.Relation r = (Condition.Relation) condition;
                Number value = NumericUtil.convertToNumber(r.value());
                Condition.Relation sys = new Condition.SyspropRelation(HugeKeys.FIELD_VALUES, r.relation(), value);
                condition = condition.replace(r, sys);
                indexQuery.query(condition);
            }
            break;
        case SHARD:
            HugeType type = indexLabel.indexType().type();
            indexQuery = new ConditionQuery(type, query);
            indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id());
            List<Condition> conditions = constructShardConditions(query, indexLabel.indexFields(), HugeKeys.FIELD_VALUES);
            indexQuery.query(conditions);
            break;
        default:
            throw new AssertionError(String.format("Unknown index type '%s'", indexType));
    }
    /*
         * Set limit for single index or composite index, also for joint index,
         * to avoid redundant element ids and out of capacity.
         * NOTE: not set offset because this query might be a sub-query,
         * see queryByUserprop()
         */
    indexQuery.page(query.page());
    indexQuery.limit(query.total());
    indexQuery.capacity(query.capacity());
    indexQuery.olap(indexLabel.olap());
    return indexQuery;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) HugeException(com.baidu.hugegraph.HugeException) HugeType(com.baidu.hugegraph.type.HugeType) Relation(com.baidu.hugegraph.backend.query.Condition.Relation) Relation(com.baidu.hugegraph.backend.query.Condition.Relation) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Id(com.baidu.hugegraph.backend.id.Id) IndexType(com.baidu.hugegraph.type.define.IndexType)

Aggregations

IndexType (com.baidu.hugegraph.type.define.IndexType)4 Id (com.baidu.hugegraph.backend.id.Id)3 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)3 HugeType (com.baidu.hugegraph.type.HugeType)3 SchemaStatus (com.baidu.hugegraph.type.define.SchemaStatus)2 HugeException (com.baidu.hugegraph.HugeException)1 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)1 Condition (com.baidu.hugegraph.backend.query.Condition)1 Relation (com.baidu.hugegraph.backend.query.Condition.Relation)1 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)1