Search in sources :

Example 6 with SchemaLabel

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

the class GraphTransaction method traverseByLabel.

private <T> void traverseByLabel(SchemaLabel label, Function<Query, Iterator<T>> fetcher, Consumer<T> consumer, boolean deleting) {
    HugeType type = label.type() == HugeType.VERTEX_LABEL ? HugeType.VERTEX : HugeType.EDGE;
    Query query = label.enableLabelIndex() ? new ConditionQuery(type) : new Query(type);
    query.capacity(Query.NO_CAPACITY);
    query.limit(Query.NO_LIMIT);
    if (this.store().features().supportsQueryByPage()) {
        query.page(PageInfo.PAGE_NONE);
    }
    if (label.hidden()) {
        query.showHidden(true);
    }
    query.showDeleting(deleting);
    query.showExpired(deleting);
    if (label.enableLabelIndex()) {
        // Support label index, query by label index by paging
        assert query instanceof ConditionQuery;
        ((ConditionQuery) query).eq(HugeKeys.LABEL, label.id());
        Iterator<T> iter = fetcher.apply(query);
        try {
            // Fetch by paging automatically
            while (iter.hasNext()) {
                consumer.accept(iter.next());
                /*
                     * Commit per batch to avoid too much data in single commit,
                     * especially for Cassandra backend
                     */
                this.commitIfGtSize(GraphTransaction.COMMIT_BATCH);
            }
            // Commit changes if exists
            this.commit();
        } finally {
            CloseableIterator.closeIterator(iter);
        }
    } else {
        // Not support label index, query all and filter by label
        if (query.paging()) {
            query.limit(this.pageSize);
        }
        String page = null;
        do {
            Iterator<T> iter = fetcher.apply(query);
            try {
                while (iter.hasNext()) {
                    T e = iter.next();
                    SchemaLabel elemLabel = ((HugeElement) e).schemaLabel();
                    if (label.equals(elemLabel)) {
                        consumer.accept(e);
                        /*
                             * Commit per batch to avoid too much data in single
                             * commit, especially for Cassandra backend
                             */
                        this.commitIfGtSize(GraphTransaction.COMMIT_BATCH);
                    }
                }
                // Commit changes of every page before next page query
                this.commit();
                if (query.paging()) {
                    page = PageInfo.pageState(iter).toString();
                    query.page(page);
                }
            } finally {
                CloseableIterator.closeIterator(iter);
            }
        } while (page != null);
    }
}
Also used : Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) HugeType(com.baidu.hugegraph.type.HugeType) HugeElement(com.baidu.hugegraph.structure.HugeElement)

Example 7 with SchemaLabel

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

the class GraphIndexTransaction method collectMatchedIndexes.

@Watched(prefix = "index")
private Set<MatchedIndex> collectMatchedIndexes(ConditionQuery query) {
    SchemaTransaction schema = this.params().schemaTransaction();
    Id label = query.condition(HugeKeys.LABEL);
    List<? extends SchemaLabel> schemaLabels;
    if (label != null) {
        // Query has LABEL condition
        SchemaLabel schemaLabel;
        if (query.resultType().isVertex()) {
            schemaLabel = schema.getVertexLabel(label);
        } else if (query.resultType().isEdge()) {
            schemaLabel = schema.getEdgeLabel(label);
        } else {
            throw new AssertionError(String.format("Unsupported index query type: %s", query.resultType()));
        }
        schemaLabels = ImmutableList.of(schemaLabel);
    } else {
        // Query doesn't have LABEL condition
        if (query.resultType().isVertex()) {
            schemaLabels = schema.getVertexLabels();
        } else if (query.resultType().isEdge()) {
            schemaLabels = schema.getEdgeLabels();
        } else {
            throw new AssertionError(String.format("Unsupported index query type: %s", query.resultType()));
        }
    }
    // Collect MatchedIndex for each SchemaLabel
    Set<MatchedIndex> matchedIndexes = InsertionOrderUtil.newSet();
    for (SchemaLabel schemaLabel : schemaLabels) {
        MatchedIndex index = this.collectMatchedIndex(schemaLabel, query);
        if (index != null) {
            matchedIndexes.add(index);
        }
    }
    return matchedIndexes;
}
Also used : SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 8 with SchemaLabel

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

Example 9 with SchemaLabel

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

the class IndexLabelBuilder method build.

@Override
public IndexLabel build() {
    Id id = this.validOrGenerateId(HugeType.INDEX_LABEL, this.id, this.name);
    this.checkBaseType();
    this.checkIndexType();
    HugeGraph graph = this.graph();
    this.checkFields4Range();
    IndexLabel indexLabel = new IndexLabel(graph, id, this.name);
    indexLabel.baseType(this.baseType);
    SchemaLabel schemaLabel = this.loadBaseLabel();
    indexLabel.baseValue(schemaLabel.id());
    indexLabel.indexType(this.indexType);
    for (String field : this.indexFields) {
        PropertyKey propertyKey = graph.propertyKey(field);
        indexLabel.indexField(propertyKey.id());
    }
    indexLabel.userdata(this.userdata);
    return indexLabel;
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 10 with SchemaLabel

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

the class IndexLabelBuilder method eliminate.

@Override
public IndexLabel eliminate() {
    IndexLabel indexLabel = this.indexLabelOrNull(this.name);
    if (indexLabel == null) {
        throw new NotFoundException("Can't update index label '%s' " + "since it doesn't exist", this.name);
    }
    this.checkStableVars();
    Userdata.check(this.userdata, Action.ELIMINATE);
    indexLabel.removeUserdata(this.userdata);
    SchemaLabel schemaLabel = indexLabel.baseLabel();
    this.graph().addIndexLabel(schemaLabel, indexLabel);
    return indexLabel;
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel)

Aggregations

SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)14 Id (com.baidu.hugegraph.backend.id.Id)8 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)8 HugeType (com.baidu.hugegraph.type.HugeType)5 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)4 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)2 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)2 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)2 HugeElement (com.baidu.hugegraph.structure.HugeElement)2 HugeException (com.baidu.hugegraph.HugeException)1 HugeGraph (com.baidu.hugegraph.HugeGraph)1 IdHolder (com.baidu.hugegraph.backend.page.IdHolder)1 BatchIdHolder (com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder)1 FixedIdHolder (com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder)1 PagingIdHolder (com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder)1 IdHolderList (com.baidu.hugegraph.backend.page.IdHolderList)1 SortByCountIdHolderList (com.baidu.hugegraph.backend.page.SortByCountIdHolderList)1 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)1 Query (com.baidu.hugegraph.backend.query.Query)1 GraphTransaction (com.baidu.hugegraph.backend.tx.GraphTransaction)1