Search in sources :

Example 16 with IndexLabel

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

the class IndexLabelAPI method list.

@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=index_label_read" })
public String list(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("names") List<String> names) {
    boolean listAll = CollectionUtils.isEmpty(names);
    if (listAll) {
        LOG.debug("Graph [{}] list index labels", graph);
    } else {
        LOG.debug("Graph [{}] get index labels by names {}", graph, names);
    }
    HugeGraph g = graph(manager, graph);
    List<IndexLabel> labels;
    if (listAll) {
        labels = g.schema().getIndexLabels();
    } else {
        labels = new ArrayList<>(names.size());
        for (String name : names) {
            labels.add(g.schema().getIndexLabel(name));
        }
    }
    return manager.serializer(g).writeIndexlabels(mapIndexLabels(labels));
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) RolesAllowed(jakarta.annotation.security.RolesAllowed) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(jakarta.ws.rs.GET)

Example 17 with IndexLabel

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

the class JsonUtilTest method testSerializeIndexLabel.

@Test
public void testSerializeIndexLabel() {
    FakeObjects fakeObject = new FakeObjects();
    PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = fakeObject.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
    PropertyKey city = fakeObject.newPropertyKey(IdGenerator.of(3), "city");
    VertexLabel vl = fakeObject.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
    IndexLabel il = fakeObject.newIndexLabel(IdGenerator.of(1), "personByAgeAndCity", HugeType.VERTEX_LABEL, vl.id(), IndexType.SECONDARY, age.id(), city.id());
    Mockito.when(fakeObject.graph().vertexLabel(vl.id())).thenReturn(vl);
    Mockito.when(fakeObject.graph().mapPkId2Name(il.indexFields())).thenReturn(Arrays.asList(age.name(), city.name()));
    String json = JsonUtil.toJson(il);
    Assert.assertEquals("{\"id\":1," + "\"name\":\"personByAgeAndCity\"," + "\"base_type\":\"VERTEX_LABEL\"," + "\"base_value\":\"person\"," + "\"index_type\":\"SECONDARY\"," + "\"fields\":[\"age\",\"city\"]," + "\"status\":\"CREATED\"," + "\"user_data\":{}}", json);
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 18 with IndexLabel

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

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

the class GraphIndexTransaction method matchJointIndexes.

/**
 * Collect index label(s) whose prefix index fields are contained in
 * property-keys in query
 */
private static Set<IndexLabel> matchJointIndexes(ConditionQuery query, Set<IndexLabel> indexLabels) {
    if (query.hasNeqCondition()) {
        return ImmutableSet.of();
    }
    Set<Id> queryPropKeys = query.userpropKeys();
    assert !queryPropKeys.isEmpty();
    Set<IndexLabel> allILs = InsertionOrderUtil.newSet(indexLabels);
    // Handle range/search index first
    Set<IndexLabel> matchedIndexLabels = InsertionOrderUtil.newSet();
    if (query.hasRangeCondition() || query.hasSearchCondition()) {
        matchedIndexLabels = matchRangeOrSearchIndexLabels(query, allILs);
        if (matchedIndexLabels.isEmpty()) {
            return ImmutableSet.of();
        }
        allILs.removeAll(matchedIndexLabels);
        // Remove matched queryPropKeys
        for (IndexLabel il : matchedIndexLabels) {
            // Only one field each range/search index-label
            queryPropKeys.remove(il.indexField());
        }
        // Return if all fields are matched
        if (queryPropKeys.isEmpty()) {
            return matchedIndexLabels;
        }
    }
    // Handle secondary indexes
    Set<Id> indexFields = InsertionOrderUtil.newSet();
    for (IndexLabel indexLabel : allILs) {
        // Range index equal-condition and secondary index can joint
        if (indexLabel.indexType().isSearch()) {
            // Search index must be handled at the previous step
            continue;
        }
        List<Id> fields = indexLabel.indexFields();
        // Collect all fields prefix
        for (Id field : fields) {
            if (!queryPropKeys.contains(field)) {
                break;
            }
            matchedIndexLabels.add(indexLabel);
            indexFields.add(field);
        }
    }
    // Must match all fields
    if (indexFields.equals(queryPropKeys)) {
        return matchedIndexLabels;
    } else {
        return ImmutableSet.of();
    }
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id)

Example 20 with IndexLabel

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

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