[Neo4j] Delete a RelationshipType

Amir Hossein Jadidinejad amir.jadidi at yahoo.com
Sun Oct 31 07:56:47 CET 2010


Hi,
I have a graph with two different RelationshipType: R1, R2
I want to replace all RelationshipTypes with a new R3. It means that previous 
relations are exist but its types change to R3. Iterate over all nodes, delete 
previous relations and create new relation with the following source code:
            int pc = 0;
            Node ref = graphDb.getReferenceNode();
            for (Iterator<Node> itr = graphDb.getAllNodes().iterator(); 
itr.hasNext();) {
                Node current_node = itr.next();
                if (current_node.equals(ref))
                    continue;
                ArrayList<Long> current_relations = new ArrayList<Long>();
                for (Iterator<Relationship> rel_itr = 
current_node.getRelationships().iterator(); rel_itr
                        .hasNext();) {
                    Relationship rel = rel_itr.next();
                    current_relations.add(rel.getId());
                }
                for (long r : current_relations)
                    updateRelation(r);
                if (++pc % 100 == 0)
                    System.out.println("\t\t\t" + pc);
                if (pc % 10000 == 0) {
                    System.out.println("\t\t\tsaving to disk...");
                    tx.success();
                    tx.finish();
                    tx = graphDb.beginTx();
                }
            }


    private void updateRelation(long relID) {
        Relationship rel = graphDb.getRelationshipById(relID);
        Node startNode = rel.getStartNode();
        Node endNode = rel.getEndNode();
        rel.delete();
        startNode.createRelationshipTo(endNode, 
NodeRelationshipTypes2.related_to);
    }


After running, size of the graph is changed from 2GB to 3.5GB and when I print 
the relationship types with:

        System.out.println("\tRelationship types:");
        for (Iterator<RelationshipType> rel_type_itr = 
graphDb.getRelationshipTypes().iterator(); rel_type_itr
                .hasNext();)
            System.out.println("\t\t" + rel_type_itr.next().name());

R1, R2 and R3 are in the output. Why?

I checked all node's relations, there is no relation which its type is equal to 
R1 or R2!

Kind regards



      


More information about the User mailing list