use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.
the class IndexLabelRemoveJob method removeIndexLabel.
protected static void removeIndexLabel(HugeGraphParams graph, Id id) {
GraphTransaction graphTx = graph.graphTransaction();
SchemaTransaction schemaTx = graph.schemaTransaction();
IndexLabel indexLabel = schemaTx.getIndexLabel(id);
// If the index label does not exist, return directly
if (indexLabel == null) {
return;
}
if (indexLabel.status().deleting()) {
LOG.info("The index label '{}' has been in {} status, " + "please check if it's expected to delete it again", indexLabel, indexLabel.status());
}
LockUtil.Locks locks = new LockUtil.Locks(graph.name());
try {
locks.lockWrites(LockUtil.INDEX_LABEL_DELETE, id);
// TODO add update lock
// Set index label to "deleting" status
schemaTx.updateSchemaStatus(indexLabel, SchemaStatus.DELETING);
try {
// Remove indexLabel from indexLabels of vertex/edge label
schemaTx.removeIndexLabelFromBaseLabel(indexLabel);
// Remove index data
// TODO: use event to replace direct call
graphTx.removeIndex(indexLabel);
/*
* Should commit changes to backend store before release
* delete lock
*/
graph.graph().tx().commit();
// Remove index label
removeSchema(schemaTx, indexLabel);
} catch (Throwable e) {
schemaTx.updateSchemaStatus(indexLabel, SchemaStatus.UNDELETED);
throw e;
}
} finally {
locks.unlock();
}
}
use of com.baidu.hugegraph.schema.IndexLabel 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.IndexLabel in project incubator-hugegraph by apache.
the class TableSerializer method readIndex.
@Override
public HugeIndex readIndex(HugeGraph graph, ConditionQuery query, BackendEntry backendEntry) {
E.checkNotNull(graph, "serializer graph");
if (backendEntry == null) {
return null;
}
TableBackendEntry entry = this.convertEntry(backendEntry);
Object indexValues = entry.column(HugeKeys.FIELD_VALUES);
Number indexLabelId = entry.column(HugeKeys.INDEX_LABEL_ID);
Set<Object> elemIds = this.parseIndexElemIds(entry);
Number expiredTime = entry.column(HugeKeys.EXPIRED_TIME);
IndexLabel indexLabel = graph.indexLabel(this.toId(indexLabelId));
HugeIndex index = new HugeIndex(graph, indexLabel);
index.fieldValues(indexValues);
long expired = index.hasTtl() ? expiredTime.longValue() : 0L;
for (Object elemId : elemIds) {
index.elementIds(this.readId(elemId), expired);
}
return index;
}
use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.
the class TextSerializer method readIndexLabel.
@Override
public IndexLabel readIndexLabel(HugeGraph graph, BackendEntry backendEntry) {
if (backendEntry == null) {
return null;
}
TextBackendEntry entry = this.convertEntry(backendEntry);
Id id = readId(entry.id());
String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME), String.class);
String baseType = entry.column(HugeKeys.BASE_TYPE);
String baseValue = entry.column(HugeKeys.BASE_VALUE);
String indexType = entry.column(HugeKeys.INDEX_TYPE);
String indexFields = entry.column(HugeKeys.FIELDS);
String status = entry.column(HugeKeys.STATUS);
IndexLabel indexLabel = new IndexLabel(graph, id, name);
indexLabel.baseType(JsonUtil.fromJson(baseType, HugeType.class));
indexLabel.baseValue(readId(baseValue));
indexLabel.indexType(JsonUtil.fromJson(indexType, IndexType.class));
indexLabel.indexFields(readIds(indexFields));
readUserdata(indexLabel, entry);
indexLabel.status(JsonUtil.fromJson(status, SchemaStatus.class));
return indexLabel;
}
use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.
the class MultiGraphsTest method testCopySchemaWithMultiGraphs.
@Test
public void testCopySchemaWithMultiGraphs() {
List<HugeGraph> graphs = openGraphs("schema_g1", "schema_g2");
for (HugeGraph graph : graphs) {
graph.initBackend();
}
HugeGraph g1 = graphs.get(0);
g1.serverStarted(IdGenerator.of("server-g2"), NodeRole.MASTER);
HugeGraph g2 = graphs.get(1);
g2.serverStarted(IdGenerator.of("server-g3"), NodeRole.MASTER);
SchemaManager schema = g1.schema();
schema.propertyKey("id").asInt().create();
schema.propertyKey("name").asText().create();
schema.propertyKey("age").asInt().valueSingle().create();
schema.propertyKey("city").asText().create();
schema.propertyKey("weight").asDouble().valueList().create();
schema.propertyKey("born").asDate().ifNotExist().create();
schema.propertyKey("time").asDate().ifNotExist().create();
schema.vertexLabel("person").properties("id", "name", "age", "city", "weight", "born").primaryKeys("id").create();
schema.vertexLabel("person2").properties("id", "name", "age", "city").primaryKeys("id").create();
schema.edgeLabel("friend").sourceLabel("person").targetLabel("person").properties("time").create();
schema.indexLabel("personByName").onV("person").secondary().by("name").create();
schema.indexLabel("personByCity").onV("person").search().by("city").create();
schema.indexLabel("personByAge").onV("person").range().by("age").create();
schema.indexLabel("friendByTime").onE("friend").range().by("time").create();
Assert.assertFalse(g2.existsPropertyKey("id"));
Assert.assertFalse(g2.existsPropertyKey("name"));
Assert.assertFalse(g2.existsPropertyKey("age"));
Assert.assertFalse(g2.existsPropertyKey("city"));
Assert.assertFalse(g2.existsPropertyKey("weight"));
Assert.assertFalse(g2.existsPropertyKey("born"));
Assert.assertFalse(g2.existsPropertyKey("time"));
Assert.assertFalse(g2.existsVertexLabel("person"));
Assert.assertFalse(g2.existsVertexLabel("person2"));
Assert.assertFalse(g2.existsEdgeLabel("friend"));
Assert.assertFalse(g2.existsIndexLabel("personByName"));
Assert.assertFalse(g2.existsIndexLabel("personByCity"));
Assert.assertFalse(g2.existsIndexLabel("personByAge"));
Assert.assertFalse(g2.existsIndexLabel("friendByTime"));
// Copy schema from g1 to g2
g2.schema().copyFrom(g1.schema());
Assert.assertTrue(g2.existsPropertyKey("id"));
Assert.assertTrue(g2.existsPropertyKey("name"));
Assert.assertTrue(g2.existsPropertyKey("age"));
Assert.assertTrue(g2.existsPropertyKey("city"));
Assert.assertTrue(g2.existsPropertyKey("weight"));
Assert.assertTrue(g2.existsPropertyKey("born"));
Assert.assertTrue(g2.existsPropertyKey("time"));
Assert.assertTrue(g2.existsVertexLabel("person"));
Assert.assertTrue(g2.existsVertexLabel("person2"));
Assert.assertTrue(g2.existsEdgeLabel("friend"));
Assert.assertTrue(g2.existsIndexLabel("personByName"));
Assert.assertTrue(g2.existsIndexLabel("personByCity"));
Assert.assertTrue(g2.existsIndexLabel("personByAge"));
Assert.assertTrue(g2.existsIndexLabel("friendByTime"));
for (PropertyKey copied : g2.schema().getPropertyKeys()) {
PropertyKey origin = g1.schema().getPropertyKey(copied.name());
Assert.assertTrue(origin.hasSameContent(copied));
}
for (VertexLabel copied : schema.getVertexLabels()) {
VertexLabel origin = g1.schema().getVertexLabel(copied.name());
Assert.assertTrue(origin.hasSameContent(copied));
}
for (EdgeLabel copied : schema.getEdgeLabels()) {
EdgeLabel origin = g1.schema().getEdgeLabel(copied.name());
Assert.assertTrue(origin.hasSameContent(copied));
}
for (IndexLabel copied : schema.getIndexLabels()) {
IndexLabel origin = g1.schema().getIndexLabel(copied.name());
Assert.assertTrue(origin.hasSameContent(copied));
}
// Copy schema again from g1 to g2 (ignore identical content)
g2.schema().copyFrom(g1.schema());
for (PropertyKey copied : g2.schema().getPropertyKeys()) {
PropertyKey origin = g1.schema().getPropertyKey(copied.name());
Assert.assertTrue(origin.hasSameContent(copied));
}
for (VertexLabel copied : schema.getVertexLabels()) {
VertexLabel origin = g1.schema().getVertexLabel(copied.name());
Assert.assertTrue(origin.hasSameContent(copied));
}
for (EdgeLabel copied : schema.getEdgeLabels()) {
EdgeLabel origin = g1.schema().getEdgeLabel(copied.name());
Assert.assertTrue(origin.hasSameContent(copied));
}
for (IndexLabel copied : schema.getIndexLabels()) {
IndexLabel origin = g1.schema().getIndexLabel(copied.name());
Assert.assertTrue(origin.hasSameContent(copied));
}
for (HugeGraph graph : graphs) {
graph.clearBackend();
}
destroyGraphs(graphs);
}
Aggregations