use of com.baidu.hugegraph.schema.SchemaLabel 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.SchemaLabel in project incubator-hugegraph by apache.
the class HugeElement method setExpiredTimeIfNeeded.
public void setExpiredTimeIfNeeded() {
SchemaLabel label = this.schemaLabel();
if (label.ttl() == 0L) {
return;
}
long now = this.graph.now();
if (SchemaLabel.NONE_ID.equals(label.ttlStartTime())) {
this.expiredTime(now + label.ttl());
return;
}
Date date = this.getPropertyValue(label.ttlStartTime());
if (date == null) {
this.expiredTime(now + label.ttl());
return;
}
long expired = date.getTime() + label.ttl();
E.checkArgument(expired > now, "The expired time '%s' of '%s' is prior to now: %s", new Date(expired), this, now);
this.expiredTime(expired);
}
use of com.baidu.hugegraph.schema.SchemaLabel in project incubator-hugegraph by apache.
the class HugeGraphAuthProxy method rebuildIndex.
@Override
public Id rebuildIndex(SchemaElement schema) {
if (schema.type() == HugeType.INDEX_LABEL) {
verifySchemaPermission(HugePermission.WRITE, schema);
} else {
SchemaLabel label = (SchemaLabel) schema;
for (Id il : label.indexLabels()) {
IndexLabel indexLabel = this.hugegraph.indexLabel(il);
verifySchemaPermission(HugePermission.WRITE, indexLabel);
}
}
return this.hugegraph.rebuildIndex(schema);
}
use of com.baidu.hugegraph.schema.SchemaLabel in project incubator-hugegraph by apache.
the class GraphIndexTransaction method updateLabelIndex.
@Watched(prefix = "index")
public void updateLabelIndex(HugeElement element, boolean removed) {
if (element instanceof HugeVertex && ((HugeVertex) element).olap()) {
return;
}
if (!this.needIndexForLabel()) {
return;
}
// Don't update label index if it's not enabled
SchemaLabel label = element.schemaLabel();
if (!label.enableLabelIndex()) {
return;
}
// Update label index if backend store not supports label-query
HugeIndex index = new HugeIndex(this.graph(), IndexLabel.label(element.type()));
index.fieldValues(element.schemaLabel().id());
index.elementIds(element.id(), element.expiredTime());
if (removed) {
this.doEliminate(this.serializer.writeIndex(index));
} else {
this.doAppend(this.serializer.writeIndex(index));
}
}
use of com.baidu.hugegraph.schema.SchemaLabel in project incubator-hugegraph by apache.
the class SchemaTransaction method removeIndexLabelFromBaseLabel.
@Watched(prefix = "schema")
public void removeIndexLabelFromBaseLabel(IndexLabel indexLabel) {
HugeType baseType = indexLabel.baseType();
Id baseValue = indexLabel.baseValue();
SchemaLabel schemaLabel;
if (baseType == HugeType.VERTEX_LABEL) {
schemaLabel = this.getVertexLabel(baseValue);
} else {
assert baseType == HugeType.EDGE_LABEL;
schemaLabel = this.getEdgeLabel(baseValue);
}
if (schemaLabel == null) {
LOG.info("The base label '{}' of index label '{}' " + "may be deleted before", baseValue, indexLabel);
return;
}
if (schemaLabel.equals(VertexLabel.OLAP_VL)) {
return;
}
// FIXME: move schemaLabel update into updateSchema() lock block instead
synchronized (schemaLabel) {
schemaLabel.removeIndexLabel(indexLabel.id());
this.updateSchema(schemaLabel);
}
}
Aggregations