Search in sources :

Example 16 with GraphTransaction

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();
        }
    }
}
Also used : LockUtil(com.baidu.hugegraph.util.LockUtil) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) Id(com.baidu.hugegraph.backend.id.Id) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction)

Example 17 with GraphTransaction

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();
    }
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) SchemaElement(com.baidu.hugegraph.schema.SchemaElement) ImmutableSet(com.google.common.collect.ImmutableSet) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) Collection(java.util.Collection) Set(java.util.Set) HugeElement(com.baidu.hugegraph.structure.HugeElement) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) SchemaStatus(com.baidu.hugegraph.type.define.SchemaStatus) LockUtil(com.baidu.hugegraph.util.LockUtil) Id(com.baidu.hugegraph.backend.id.Id) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeType(com.baidu.hugegraph.type.HugeType) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) LockUtil(com.baidu.hugegraph.util.LockUtil) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeElement(com.baidu.hugegraph.structure.HugeElement) Consumer(java.util.function.Consumer) Id(com.baidu.hugegraph.backend.id.Id) Edge(org.apache.tinkerpop.gremlin.structure.Edge) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction)

Example 18 with GraphTransaction

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);
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Query(com.baidu.hugegraph.backend.query.Query) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeVertex(com.baidu.hugegraph.structure.HugeVertex)

Example 19 with GraphTransaction

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();
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) BackendException(com.baidu.hugegraph.backend.BackendException)

Example 20 with GraphTransaction

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());
}
Also used : HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeGraph(com.baidu.hugegraph.HugeGraph) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) FakeEdge(com.baidu.hugegraph.testutil.FakeObjects.FakeEdge) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Test(org.junit.Test)

Aggregations

GraphTransaction (com.baidu.hugegraph.backend.tx.GraphTransaction)22 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)7 SchemaTransaction (com.baidu.hugegraph.backend.tx.SchemaTransaction)6 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)6 LockUtil (com.baidu.hugegraph.util.LockUtil)6 Id (com.baidu.hugegraph.backend.id.Id)5 HugeGraph (com.baidu.hugegraph.HugeGraph)4 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)4 Test (org.junit.Test)4 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)3 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)3 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)3 HugeElement (com.baidu.hugegraph.structure.HugeElement)3 Edge (org.apache.tinkerpop.gremlin.structure.Edge)3 HugeException (com.baidu.hugegraph.HugeException)2 HugeGraphParams (com.baidu.hugegraph.HugeGraphParams)2 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)2 Query (com.baidu.hugegraph.backend.query.Query)2 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)2 FakeEdge (com.baidu.hugegraph.testutil.FakeObjects.FakeEdge)2