use of com.baidu.hugegraph.schema.VertexLabel 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();
}
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class HugeVariables method createVariableQuery.
private ConditionQuery createVariableQuery(String name) {
ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
VertexLabel vl = this.variableVertexLabel();
query.eq(HugeKeys.LABEL, vl.id());
if (name != null) {
PropertyKey pkey = this.params.graph().propertyKey(Hidden.hide(VARIABLE_KEY));
query.query(Condition.eq(pkey.id(), name));
}
query.showHidden(true);
return query;
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class Example1 method testRemove.
public static void testRemove(final HugeGraph graph) {
// remove vertex (and its edges)
List<Vertex> vertices = graph.traversal().V().hasLabel("person").has("age", 19).toList();
assert vertices.size() == 1;
Vertex james = vertices.get(0);
Vertex book6 = graph.addVertex(T.label, "book", "name", "java-6");
james.addEdge("look", book6, "timestamp", "2017-5-2 12:00:08.0");
james.addEdge("look", book6, "timestamp", "2017-5-3 12:00:08.0");
graph.tx().commit();
assert graph.traversal().V(book6.id()).bothE().hasNext();
LOG.info(">>>> removing vertex: {}", james);
james.remove();
graph.tx().commit();
assert !graph.traversal().V(james.id()).hasNext();
assert !graph.traversal().V(book6.id()).bothE().hasNext();
// remove edge
VertexLabel author = graph.schema().getVertexLabel("author");
String authorId = String.format("%s:%s", author.id().asString(), "11");
EdgeLabel authored = graph.edgeLabel("authored");
VertexLabel book = graph.schema().getVertexLabel("book");
String book2Id = String.format("%s:%s", book.id().asString(), "java-2");
String edgeId = String.format("S%s>%s>%s>S%s", authorId, authored.id(), "", book2Id);
List<Edge> edges = graph.traversal().E(edgeId).toList();
assert edges.size() == 1;
Edge edge = edges.get(0);
LOG.info(">>>> removing edge: {}", edge);
edge.remove();
graph.tx().commit();
assert !graph.traversal().E(edgeId).hasNext();
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class IndexLabelCoreTest method testRemoveIndexLabelOfVertex.
@Test
public void testRemoveIndexLabelOfVertex() {
Assume.assumeTrue("Not support range condition query", storeFeatures().supportsQueryWithRangeCondition());
super.initPropertyKeys();
SchemaManager schema = graph().schema();
schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name").create();
schema.indexLabel("personByCity").onV("person").secondary().by("city").create();
schema.indexLabel("personByAge").onV("person").range().by("age").create();
VertexLabel person = schema.getVertexLabel("person");
Assert.assertEquals(2, person.indexLabels().size());
assertContainsIl(person.indexLabels(), "personByCity", "personByAge");
graph().addVertex(T.label, "person", "name", "Baby", "city", "Hongkong", "age", 3);
graph().tx().commit();
Vertex vertex = graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
Assert.assertNotNull(vertex);
vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
Assert.assertNotNull(vertex);
schema.indexLabel("personByCity").remove();
Assert.assertThrows(NotFoundException.class, () -> {
schema.getIndexLabel("personByCity");
});
person = schema.getVertexLabel("person");
Assert.assertEquals(1, person.indexLabels().size());
assertNotContainsIl(person.indexLabels(), "personByCity");
assertContainsIl(person.indexLabels(), "personByAge");
Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
});
vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
Assert.assertNotNull(vertex);
schema.indexLabel("personByAge").remove();
Assert.assertThrows(NotFoundException.class, () -> {
schema.getIndexLabel("personByAge");
});
person = schema.getVertexLabel("person");
Assert.assertEquals(0, person.indexLabels().size());
Assert.assertThrows(NoIndexException.class, () -> {
graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
});
}
use of com.baidu.hugegraph.schema.VertexLabel in project incubator-hugegraph by apache.
the class IndexLabelCoreTest method testRebuildIndexLabelOfVertexLabel.
@Test
public void testRebuildIndexLabelOfVertexLabel() {
Assume.assumeTrue("Not support range condition query", storeFeatures().supportsQueryWithRangeCondition());
super.initPropertyKeys();
SchemaManager schema = graph().schema();
schema.vertexLabel("person").properties("name", "age", "city").primaryKeys("name").create();
schema.indexLabel("personByCity").onV("person").secondary().by("city").create();
schema.indexLabel("personByAge").onV("person").range().by("age").create();
VertexLabel person = schema.getVertexLabel("person");
Assert.assertEquals(2, person.indexLabels().size());
assertContainsIl(person.indexLabels(), "personByCity", "personByAge");
graph().addVertex(T.label, "person", "name", "Baby", "city", "Hongkong", "age", 3);
graph().tx().commit();
Vertex vertex = graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
Assert.assertNotNull(vertex);
vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
Assert.assertNotNull(vertex);
schema.vertexLabel("person").rebuildIndex();
vertex = graph().traversal().V().hasLabel("person").has("city", "Hongkong").next();
Assert.assertNotNull(vertex);
vertex = graph().traversal().V().hasLabel("person").has("age", P.inside(2, 4)).next();
Assert.assertNotNull(vertex);
}
Aggregations