Search in sources :

Example 1 with IndexLabel

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

the class StandardHugeGraph method indexLabel.

@Override
public IndexLabel indexLabel(Id id) {
    IndexLabel il = this.schemaTransaction().getIndexLabel(id);
    E.checkArgument(il != null, "Undefined index label with id: '%s'", id);
    return il;
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel)

Example 2 with IndexLabel

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

the class IndexLabelBuilder method createWithTask.

/**
 * Create index label with async mode
 */
@Override
public SchemaElement.TaskWithSchema createWithTask() {
    HugeType type = HugeType.INDEX_LABEL;
    this.checkSchemaName(this.name);
    return this.lockCheckAndCreateSchema(type, this.name, name -> {
        IndexLabel indexLabel = this.indexLabelOrNull(name);
        if (indexLabel != null) {
            if (this.checkExist || !hasSameProperties(indexLabel)) {
                throw new ExistedException(type, name);
            }
            return new SchemaElement.TaskWithSchema(indexLabel, IdGenerator.ZERO);
        }
        this.checkSchemaIdIfRestoringMode(type, this.id);
        this.checkBaseType();
        this.checkIndexType();
        if (VertexLabel.OLAP_VL.name().equals(this.baseValue)) {
            return new SchemaElement.TaskWithSchema(this.build(), IdGenerator.ZERO);
        }
        SchemaLabel schemaLabel = this.loadBaseLabel();
        /*
             * If new index label is prefix of existed index label, or has
             * the same fields, fail to create new index label.
             */
        this.checkFields(schemaLabel.properties());
        this.checkRepeatIndex(schemaLabel);
        Userdata.check(this.userdata, Action.INSERT);
        // Async delete index label which is prefix of the new index label
        // TODO: use event to replace direct call
        Set<Id> removeTasks = this.removeSubIndex(schemaLabel);
        indexLabel = this.build();
        assert indexLabel.name().equals(name);
        /*
             * If not rebuild, just create index label and return.
             * The actual indexes may be rebuilt later as needed
             */
        if (!this.rebuild) {
            indexLabel.status(SchemaStatus.CREATED);
            this.graph().addIndexLabel(schemaLabel, indexLabel);
            return new SchemaElement.TaskWithSchema(indexLabel, IdGenerator.ZERO);
        }
        // Create index label (just schema)
        indexLabel.status(SchemaStatus.CREATING);
        this.graph().addIndexLabel(schemaLabel, indexLabel);
        try {
            // Async rebuild index
            Id rebuildTask = this.rebuildIndex(indexLabel, removeTasks);
            E.checkNotNull(rebuildTask, "rebuild-index task");
            return new SchemaElement.TaskWithSchema(indexLabel, rebuildTask);
        } catch (Throwable e) {
            this.updateSchemaStatus(indexLabel, SchemaStatus.INVALID);
            throw e;
        }
    });
}
Also used : ExistedException(com.baidu.hugegraph.exception.ExistedException) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType)

Example 3 with IndexLabel

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

the class IndexLabelBuilder method removeSubIndex.

private Set<Id> removeSubIndex(SchemaLabel schemaLabel) {
    Set<Id> overrideIndexLabelIds = InsertionOrderUtil.newSet();
    for (Id id : schemaLabel.indexLabels()) {
        IndexLabel old = this.graph().indexLabel(id);
        if (!this.hasSubIndex(old)) {
            continue;
        }
        List<String> oldFields = this.graph().mapPkId2Name(old.indexFields());
        List<String> newFields = this.indexFields;
        /*
             * Remove the existed index label if:
             * 1. new unique index label is subset of existed unique index label
             * or
             * 2. existed index label is prefix of new created index label
             * (except for unique index)
             */
        if (this.indexType.isUnique() && oldFields.containsAll(newFields) || !this.indexType.isUnique() && CollectionUtil.prefixOf(oldFields, newFields)) {
            overrideIndexLabelIds.add(id);
        }
    }
    Set<Id> tasks = InsertionOrderUtil.newSet();
    for (Id id : overrideIndexLabelIds) {
        Id task = this.graph().removeIndexLabel(id);
        E.checkNotNull(task, "remove sub index label task");
        tasks.add(task);
    }
    return tasks;
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id)

Example 4 with IndexLabel

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

the class HugeIndex method parseIndexId.

public static HugeIndex parseIndexId(HugeGraph graph, HugeType type, byte[] id) {
    Object values;
    IndexLabel indexLabel;
    if (type.isStringIndex()) {
        Id idObject = IdGenerator.of(id, IdType.STRING);
        String[] parts = SplicingIdGenerator.parse(idObject);
        E.checkState(parts.length == 2, "Invalid secondary index id");
        Id label = IdGenerator.ofStoredString(parts[0], IdType.LONG);
        indexLabel = IndexLabel.label(graph, label);
        values = parts[1];
    } else {
        assert type.isRange4Index() || type.isRange8Index();
        final int labelLength = 4;
        E.checkState(id.length > labelLength, "Invalid range index id");
        BytesBuffer buffer = BytesBuffer.wrap(id);
        Id label = IdGenerator.of(buffer.readInt());
        indexLabel = IndexLabel.label(graph, label);
        List<Id> fields = indexLabel.indexFields();
        E.checkState(fields.size() == 1, "Invalid range index fields");
        DataType dataType = graph.propertyKey(fields.get(0)).dataType();
        E.checkState(dataType.isNumber() || dataType.isDate(), "Invalid range index field type");
        Class<?> clazz = dataType.isNumber() ? dataType.clazz() : DataType.LONG.clazz();
        values = bytes2number(buffer.read(id.length - labelLength), clazz);
    }
    HugeIndex index = new HugeIndex(graph, indexLabel);
    index.fieldValues(values);
    return index;
}
Also used : IndexLabel(com.baidu.hugegraph.schema.IndexLabel) BytesBuffer(com.baidu.hugegraph.backend.serializer.BytesBuffer) DataType(com.baidu.hugegraph.type.define.DataType) Id(com.baidu.hugegraph.backend.id.Id)

Example 5 with IndexLabel

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

the class OlapPropertyKeyClearJob method clearIndexLabel.

protected static void clearIndexLabel(HugeGraphParams graph, Id id) {
    Id olapIndexLabel = findOlapIndexLabel(graph, id);
    if (olapIndexLabel == null) {
        return;
    }
    GraphTransaction graphTx = graph.graphTransaction();
    SchemaTransaction schemaTx = graph.schemaTransaction();
    IndexLabel indexLabel = schemaTx.getIndexLabel(olapIndexLabel);
    // If the index label does not exist, return directly
    if (indexLabel == null) {
        return;
    }
    LockUtil.Locks locks = new LockUtil.Locks(graph.name());
    try {
        locks.lockWrites(LockUtil.INDEX_LABEL_DELETE, olapIndexLabel);
        // Set index label to "rebuilding" status
        schemaTx.updateSchemaStatus(indexLabel, SchemaStatus.REBUILDING);
        try {
            // Remove index data
            graphTx.removeIndex(indexLabel);
            /*
                 * Should commit changes to backend store before release
                 * delete lock
                 */
            graph.graph().tx().commit();
            schemaTx.updateSchemaStatus(indexLabel, SchemaStatus.CREATED);
        } catch (Throwable e) {
            schemaTx.updateSchemaStatus(indexLabel, SchemaStatus.INVALID);
            throw e;
        }
    } finally {
        locks.unlock();
    }
}
Also used : LockUtil(com.baidu.hugegraph.util.LockUtil) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Id(com.baidu.hugegraph.backend.id.Id) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction)

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