Search in sources :

Example 6 with GraphTransaction

use of com.baidu.hugegraph.backend.tx.GraphTransaction in project incubator-hugegraph by apache.

the class DeleteExpiredIndexJob method deleteExpiredIndex.

/*
     * Delete expired element(if exist) of the index,
     * otherwise just delete expired index only
     */
private void deleteExpiredIndex(HugeGraphParams graph, HugeIndex index) {
    GraphTransaction tx = graph.graphTransaction();
    HugeType type = index.indexLabel().queryType().isVertex() ? HugeType.VERTEX : HugeType.EDGE;
    IdQuery query = new IdQuery(type);
    query.query(index.elementId());
    query.showExpired(true);
    Iterator<?> elements = type.isVertex() ? tx.queryVertices(query) : tx.queryEdges(query);
    if (elements.hasNext()) {
        HugeElement element = (HugeElement) elements.next();
        if (element.expiredTime() == index.expiredTime()) {
            element.remove();
        } else {
            tx.removeIndex(index);
        }
    } else {
        tx.removeIndex(index);
    }
}
Also used : IdQuery(com.baidu.hugegraph.backend.query.IdQuery) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) HugeType(com.baidu.hugegraph.type.HugeType) HugeElement(com.baidu.hugegraph.structure.HugeElement)

Example 7 with GraphTransaction

use of com.baidu.hugegraph.backend.tx.GraphTransaction in project incubator-hugegraph by apache.

the class EdgeLabelRemoveJob method removeEdgeLabel.

private static void removeEdgeLabel(HugeGraphParams graph, Id id) {
    GraphTransaction graphTx = graph.graphTransaction();
    SchemaTransaction schemaTx = graph.schemaTransaction();
    EdgeLabel edgeLabel = schemaTx.getEdgeLabel(id);
    // If the edge label does not exist, return directly
    if (edgeLabel == null) {
        return;
    }
    if (edgeLabel.status().deleting()) {
        LOG.info("The edge label '{}' has been in {} status, " + "please check if it's expected to delete it again", edgeLabel, edgeLabel.status());
    }
    // Remove index related data(include schema) of this edge label
    Set<Id> indexIds = ImmutableSet.copyOf(edgeLabel.indexLabels());
    LockUtil.Locks locks = new LockUtil.Locks(graph.name());
    try {
        locks.lockWrites(LockUtil.EDGE_LABEL_DELETE, id);
        schemaTx.updateSchemaStatus(edgeLabel, SchemaStatus.DELETING);
        try {
            for (Id indexId : indexIds) {
                IndexLabelRemoveJob.removeIndexLabel(graph, indexId);
            }
            // Remove all edges which has matched label
            // TODO: use event to replace direct call
            graphTx.removeEdges(edgeLabel);
            /*
                 * Should commit changes to backend store before release
                 * delete lock
                 */
            graph.graph().tx().commit();
            // Remove edge label
            removeSchema(schemaTx, edgeLabel);
        } catch (Throwable e) {
            schemaTx.updateSchemaStatus(edgeLabel, SchemaStatus.UNDELETED);
            throw e;
        }
    } finally {
        locks.unlock();
    }
}
Also used : LockUtil(com.baidu.hugegraph.util.LockUtil) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) GraphTransaction(com.baidu.hugegraph.backend.tx.GraphTransaction) Id(com.baidu.hugegraph.backend.id.Id) SchemaTransaction(com.baidu.hugegraph.backend.tx.SchemaTransaction)

Example 8 with GraphTransaction

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

Example 9 with GraphTransaction

use of com.baidu.hugegraph.backend.tx.GraphTransaction in project incubator-hugegraph by apache.

the class EdgeCoreTest method testRemoveEdgeAfterAddEdgeWithTx.

@Test
public void testRemoveEdgeAfterAddEdgeWithTx() {
    HugeGraph graph = graph();
    GraphTransaction tx = params().openTransaction();
    Vertex james = tx.addVertex(T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian");
    Vertex java = tx.addVertex(T.label, "language", "name", "java");
    Edge created = james.addEdge("created", java);
    created.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)

Example 10 with GraphTransaction

use of com.baidu.hugegraph.backend.tx.GraphTransaction in project incubator-hugegraph by apache.

the class HugeVariables method queryAllVariableVertices.

private Iterator<Vertex> queryAllVariableVertices() {
    GraphTransaction tx = this.params.graphTransaction();
    Query query = this.createVariableQuery(null);
    Iterator<Vertex> vertices = tx.queryVertices(query);
    return 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)

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