Search in sources :

Example 11 with SchemaLabel

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

the class IndexLabelBuilder method hasSameProperties.

/**
 * Check whether this has same properties with existedIndexLabel.
 * Only baseType, baseValue, indexType, indexFields are checked.
 * The id, checkExist, userdata are not checked.
 * @param existedIndexLabel to be compared with
 * @return true if this has same properties with existedIndexLabel
 */
private boolean hasSameProperties(IndexLabel existedIndexLabel) {
    // baseType is null, it means HugeType.SYS_SCHEMA
    if ((this.baseType == null && existedIndexLabel.baseType() != HugeType.SYS_SCHEMA) || (this.baseType != null && this.baseType != existedIndexLabel.baseType())) {
        return false;
    }
    SchemaLabel schemaLabel = this.loadBaseLabel();
    if (!schemaLabel.id().equals(existedIndexLabel.baseValue())) {
        return false;
    }
    if (this.indexType == null) {
        // The default index type is SECONDARY
        if (existedIndexLabel.indexType() != IndexType.SECONDARY) {
            return false;
        }
    } else {
        // NOTE: IndexType.RANGE.isRange() return false
        if (this.indexType == IndexType.RANGE) {
            // existedIndexLabel index type format: RANGE_INT, RANGE_LONG
            if (!existedIndexLabel.indexType().isRange()) {
                return false;
            }
        } else if (this.indexType != existedIndexLabel.indexType()) {
            return false;
        }
    }
    List<Id> existedIndexFieldIds = existedIndexLabel.indexFields();
    if (this.indexFields.size() != existedIndexFieldIds.size()) {
        return false;
    }
    for (String field : this.indexFields) {
        PropertyKey propertyKey = graph().propertyKey(field);
        if (!existedIndexFieldIds.contains(propertyKey.id())) {
            return false;
        }
    }
    // all properties are same, return true.
    return true;
}
Also used : SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) PropertyKey(com.baidu.hugegraph.schema.PropertyKey)

Example 12 with SchemaLabel

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

the class IndexLabelBuilder method append.

@Override
public IndexLabel append() {
    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.APPEND);
    indexLabel.userdata(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)

Example 13 with SchemaLabel

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

the class IndexLabelRebuildJob method rebuildIndex.

private void rebuildIndex(SchemaElement schema) {
    switch(schema.type()) {
        case INDEX_LABEL:
            IndexLabel indexLabel = (IndexLabel) schema;
            SchemaLabel label;
            if (indexLabel.baseType() == HugeType.VERTEX_LABEL) {
                label = this.graph().vertexLabel(indexLabel.baseValue());
            } else {
                assert indexLabel.baseType() == HugeType.EDGE_LABEL;
                label = this.graph().edgeLabel(indexLabel.baseValue());
            }
            assert label != null;
            this.rebuildIndex(label, ImmutableSet.of(indexLabel.id()));
            break;
        case VERTEX_LABEL:
        case EDGE_LABEL:
            label = (SchemaLabel) schema;
            this.rebuildIndex(label, label.indexLabels());
            break;
        default:
            assert schema.type() == HugeType.PROPERTY_KEY;
            throw new AssertionError(String.format("The %s can't rebuild index", schema.type()));
    }
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel)

Example 14 with SchemaLabel

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

the class IndexLabelRebuildJob method rebuildIndex.

private void rebuildIndex(SchemaLabel label, Collection<Id> indexLabelIds) {
    SchemaTransaction schemaTx = this.params().schemaTransaction();
    GraphTransaction graphTx = this.params().graphTransaction();
    Consumer<?> indexUpdater = (elem) -> {
        for (Id id : indexLabelIds) {
            graphTx.updateIndex(id, (HugeElement) elem, false);
        }
    };
    LockUtil.Locks locks = new LockUtil.Locks(schemaTx.graphName());
    try {
        locks.lockWrites(LockUtil.INDEX_LABEL_REBUILD, indexLabelIds);
        Set<IndexLabel> ils = indexLabelIds.stream().map(this.graph()::indexLabel).collect(Collectors.toSet());
        for (IndexLabel il : ils) {
            if (il.status() == SchemaStatus.CREATING) {
                continue;
            }
            schemaTx.updateSchemaStatus(il, SchemaStatus.REBUILDING);
        }
        this.removeIndex(indexLabelIds);
        /*
             * Note: Here must commit index transaction firstly.
             * Because remove index convert to (id like <?>:personByCity):
             * `delete from index table where label = ?`,
             * But append index will convert to (id like Beijing:personByCity):
             * `update index element_ids += xxx where field_value = ?
             * and index_label_name = ?`,
             * They have different id lead to it can't compare and optimize
             */
        graphTx.commit();
        try {
            if (label.type() == HugeType.VERTEX_LABEL) {
                @SuppressWarnings("unchecked") Consumer<Vertex> consumer = (Consumer<Vertex>) indexUpdater;
                graphTx.traverseVerticesByLabel((VertexLabel) label, consumer, false);
            } else {
                assert label.type() == HugeType.EDGE_LABEL;
                @SuppressWarnings("unchecked") Consumer<Edge> consumer = (Consumer<Edge>) indexUpdater;
                graphTx.traverseEdgesByLabel((EdgeLabel) label, consumer, false);
            }
            graphTx.commit();
        } catch (Throwable e) {
            for (IndexLabel il : ils) {
                schemaTx.updateSchemaStatus(il, SchemaStatus.INVALID);
            }
            throw e;
        }
        for (IndexLabel il : ils) {
            schemaTx.updateSchemaStatus(il, SchemaStatus.CREATED);
        }
    } finally {
        locks.unlock();
    }
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) SchemaElement(com.baidu.hugegraph.schema.SchemaElement) ImmutableSet(com.google.common.collect.ImmutableSet) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Collection(java.util.Collection) Set(java.util.Set) HugeElement(com.baidu.hugegraph.structure.HugeElement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) SchemaStatus(com.baidu.hugegraph.type.define.SchemaStatus) LockUtil(com.baidu.hugegraph.util.LockUtil) Id(com.baidu.hugegraph.backend.id.Id) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeType(com.baidu.hugegraph.type.HugeType) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) LockUtil(com.baidu.hugegraph.util.LockUtil) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeElement(com.baidu.hugegraph.structure.HugeElement) Consumer(java.util.function.Consumer) Id(com.baidu.hugegraph.backend.id.Id) Edge(org.apache.tinkerpop.gremlin.structure.Edge) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction)

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