Biblioteca Java - Blame information for rev 18
Subversion Repositories:
(root)/Frameworks and Technologies/Neo4J Samples/Neo4JTutorial/src/main/java/com/linkscreens/graphsin/util/Neo4jDatabaseCleaner.java
Rev | Author | Line No. | Line |
---|---|---|---|
18 | mihai | 1 | package com.linkscreens.graphsin.util; |
2 | |||
3 | import java.util.Arrays; | ||
4 | import java.util.HashMap; | ||
5 | import java.util.Map; | ||
6 | |||
7 | import org.neo4j.graphdb.Direction; | ||
8 | import org.neo4j.graphdb.GraphDatabaseService; | ||
9 | import org.neo4j.graphdb.Node; | ||
10 | import org.neo4j.graphdb.Relationship; | ||
11 | import org.neo4j.graphdb.Transaction; | ||
12 | import org.neo4j.graphdb.index.IndexManager; | ||
13 | |||
14 | |||
15 | /** | ||
16 | * Clean database. | ||
17 | * @author evo2 | ||
18 | * | ||
19 | */ | ||
20 | public class Neo4jDatabaseCleaner { | ||
21 | /*private GraphDatabaseService graph; | ||
22 | |||
23 | public Neo4jDatabaseCleaner(Neo4jOperations template) { | ||
24 | this.graph = graph; | ||
25 | } | ||
26 | |||
27 | public Map<String, Object> cleanDb() { | ||
28 | Map<String, Object> result = new HashMap<String, Object>(); | ||
29 | Transaction tx = graph.beginTx(); | ||
30 | try { | ||
31 | removeNodes(result); | ||
32 | clearIndex(result); | ||
33 | tx.success(); | ||
34 | } finally { | ||
35 | tx.close(); | ||
36 | } | ||
37 | return result; | ||
38 | } | ||
39 | |||
40 | private void removeNodes(Map<String, Object> result) { | ||
41 | Node refNode = graph.getReferenceNode(); | ||
42 | int nodes = 0, relationships = 0; | ||
43 | for (Node node : graph.getAllNodes()) { | ||
44 | for (Relationship rel : node.getRelationships(Direction.OUTGOING)) { | ||
45 | rel.delete(); | ||
46 | relationships++; | ||
47 | } | ||
48 | if (!refNode.equals(node)) { | ||
49 | node.delete(); | ||
50 | nodes++; | ||
51 | } | ||
52 | } | ||
53 | result.put("nodes", nodes); | ||
54 | result.put("relationships", relationships); | ||
55 | |||
56 | } | ||
57 | |||
58 | private void clearIndex(Map<String, Object> result) { | ||
59 | IndexManager indexManager = graph.index(); | ||
60 | result.put("node-indexes", Arrays.asList(indexManager.nodeIndexNames())); | ||
61 | result.put("relationship-indexes", Arrays.asList(indexManager.relationshipIndexNames())); | ||
62 | for (String ix : indexManager.nodeIndexNames()) { | ||
63 | indexManager.forNodes(ix).delete(); | ||
64 | } | ||
65 | for (String ix : indexManager.relationshipIndexNames()) { | ||
66 | indexManager.forRelationships(ix).delete(); | ||
67 | } | ||
68 | } | ||
69 | */ | ||
70 | } |