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;
}
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;
}
});
}
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;
}
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;
}
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();
}
}
Aggregations