use of com.baidu.hugegraph.backend.tx.GraphTransaction in project incubator-hugegraph by apache.
the class IndexLabelRebuildJob method removeIndex.
private void removeIndex(Collection<Id> indexLabelIds) {
SchemaTransaction schemaTx = this.params().schemaTransaction();
GraphTransaction graphTx = this.params().graphTransaction();
for (Id id : indexLabelIds) {
IndexLabel il = schemaTx.getIndexLabel(id);
if (il == null || il.status() == SchemaStatus.CREATING) {
/*
* TODO: How to deal with non-existent index name:
* continue or throw exception?
*/
continue;
}
LockUtil.Locks locks = new LockUtil.Locks(schemaTx.graphName());
try {
locks.lockWrites(LockUtil.INDEX_LABEL_DELETE, indexLabelIds);
graphTx.removeIndex(il);
} catch (Throwable e) {
schemaTx.updateSchemaStatus(il, SchemaStatus.INVALID);
throw e;
} finally {
locks.unlock();
}
}
}
use of com.baidu.hugegraph.backend.tx.GraphTransaction 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.backend.tx.GraphTransaction in project incubator-hugegraph by apache.
the class HugeVariables method queryVariableVertex.
private HugeVertex queryVariableVertex(String key) {
GraphTransaction tx = this.params.graphTransaction();
Query query = this.createVariableQuery(key);
Iterator<Vertex> vertices = tx.queryVertices(query);
return (HugeVertex) QueryResults.one(vertices);
}
use of com.baidu.hugegraph.backend.tx.GraphTransaction in project incubator-hugegraph by apache.
the class Example1 method loadData.
public static void loadData(final HugeGraph graph) {
// will auto open tx (would not auto commit)
graph.addVertex(T.label, "book", "name", "java-3");
graph.addVertex(T.label, "person", "name", "Baby", "city", "Hongkong", "age", 3);
graph.addVertex(T.label, "person", "name", "James", "city", "Beijing", "age", 19);
graph.addVertex(T.label, "person", "name", "Tom Cat", "city", "Beijing", "age", 20);
graph.addVertex(T.label, "person", "name", "Lisa", "city", "Beijing", "age", 20);
graph.addVertex(T.label, "person", "name", "Hebe", "city", "Taipei", "age", 21);
graph.tx().commit();
// must commit manually with new backend tx (independent of tinkerpop)
GraphTransaction tx = Whitebox.invoke(graph.getClass(), "openGraphTransaction", graph);
LOG.info("=============== addVertex ================");
Vertex james = tx.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "San Francisco Bay Area");
Vertex java = tx.addVertex(T.label, "language", "name", "java", "versions", Arrays.asList(6, 7, 8));
Vertex book1 = tx.addVertex(T.label, "book", "name", "java-1");
Vertex book2 = tx.addVertex(T.label, "book", "name", "java-2");
Vertex book3 = tx.addVertex(T.label, "book", "name", "java-3");
james.addEdge("created", java);
james.addEdge("authored", book1, "contribution", "1990-1-1", "comment", "it's a good book", "comment", "it's a good book", "comment", "it's a good book too");
james.addEdge("authored", book2, "contribution", "2017-4-28");
james.addEdge("write", book2, "time", "2017-4-28");
james.addEdge("write", book3, "time", "2016-1-1");
james.addEdge("write", book3, "time", "2017-4-28");
// commit data changes
try {
tx.commit();
} catch (BackendException e) {
e.printStackTrace();
try {
tx.rollback();
} catch (BackendException e2) {
e2.printStackTrace();
}
} finally {
tx.close();
}
// use the manually open transaction (tinkerpop tx)
graph.tx().open();
graph.addVertex(T.label, "book", "name", "java-3");
graph.addVertex(T.label, "book", "name", "java-4");
graph.addVertex(T.label, "book", "name", "java-5");
graph.tx().commit();
}
use of com.baidu.hugegraph.backend.tx.GraphTransaction in project incubator-hugegraph by apache.
the class EdgeCoreTest method testRemoveVertexAfterAddEdgesWithTx.
@Test
public void testRemoveVertexAfterAddEdgesWithTx() {
HugeGraph graph = graph();
GraphTransaction tx = params().openTransaction();
Vertex james = tx.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian");
Vertex guido = tx.addVertex(T.label, "author", "id", 2, "name", "Guido van Rossum", "age", 61, "lived", "California");
Vertex java = tx.addVertex(T.label, "language", "name", "java");
Vertex python = tx.addVertex(T.label, "language", "name", "python", "dynamic", true);
Vertex java1 = tx.addVertex(T.label, "book", "name", "java-1");
Vertex java2 = tx.addVertex(T.label, "book", "name", "java-2");
Vertex java3 = tx.addVertex(T.label, "book", "name", "java-3");
james.addEdge("created", java);
guido.addEdge("created", python);
james.addEdge("authored", java1);
james.addEdge("authored", java2);
james.addEdge("authored", java3);
james.remove();
guido.remove();
try {
tx.commit();
} finally {
tx.close();
}
List<Edge> edges = graph.traversal().E().toList();
Assert.assertEquals(0, edges.size());
}
Aggregations