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