@Test
  public void removeNodesWithARelation() {
    int originalNodeCount = countNodesOf(graphDb);
    int removedNodeCount = 0;

    Transaction tx = graphDb.beginTx();
    try {
      Set<Node> toRemove = new HashSet<Node>();
      for (Node node : graphDb.getAllNodes()) {
        for (Relationship relationship : node.getRelationships(RelationType.ON)) {
          toRemove.add(relationship.getStartNode());
          toRemove.add(relationship.getEndNode());
          relationship.delete();
        }
      }
      for (Node node : toRemove) {
        node.delete();
        removedNodeCount++;
      }
      tx.success();
    } finally {
      tx.finish();
    }

    int finalNodeCount = countNodesOf(graphDb);
    assertEquals(
        Integer.valueOf(originalNodeCount), Integer.valueOf(finalNodeCount + removedNodeCount));
  }
 private int countNodesOf(GraphDatabaseService aGraphDb) {
   int nodeCount = 0;
   for (@SuppressWarnings("unused") Node node : aGraphDb.getAllNodes()) {
     nodeCount++;
   }
   return nodeCount;
 }
 public static void destroy(Map<String, Node> nodes) {
   if (nodes.isEmpty()) return;
   GraphDatabaseService db = nodes.values().iterator().next().getGraphDatabase();
   Transaction tx = db.beginTx();
   try {
     for (Node node : db.getAllNodes()) {
       for (Relationship rel : node.getRelationships()) rel.delete();
       node.delete();
     }
     tx.success();
   } finally {
     tx.finish();
   }
 }
  public void testNeo4jRaw() throws Exception {
    double totalTime = 0.0d;
    Graph graph = graphTest.getGraphInstance();
    GraphMLReader.inputGraph(graph, GraphMLReader.class.getResourceAsStream("graph-example-2.xml"));
    graph.shutdown();

    for (int i = 0; i < TOTAL_RUNS; i++) {
      graph = graphTest.getGraphInstance();
      GraphDatabaseService neo4j = ((Neo4jGraph) graph).getRawGraph();
      int counter = 0;
      this.stopWatch();
      for (final Node node : neo4j.getAllNodes()) {
        counter++;
        for (final Relationship relationship : node.getRelationships(Direction.OUTGOING)) {
          counter++;
          final Node node2 = relationship.getEndNode();
          counter++;
          for (final Relationship relationship2 : node2.getRelationships(Direction.OUTGOING)) {
            counter++;
            final Node node3 = relationship2.getEndNode();
            counter++;
            for (final Relationship relationship3 : node3.getRelationships(Direction.OUTGOING)) {
              counter++;
              relationship3.getEndNode();
              counter++;
            }
          }
        }
      }
      double currentTime = this.stopWatch();
      totalTime = totalTime + currentTime;
      BaseTest.printPerformance(
          neo4j.toString(), counter, "Neo4j raw elements touched", currentTime);
      graph.shutdown();
    }
    BaseTest.printPerformance(
        "Neo4jRaw", 1, "Neo4j Raw experiment average", totalTime / (double) TOTAL_RUNS);
  }
  @Override
  public void write(GraphDatabaseService transNeo) {

    BufferedWriter bufferedWriter = null;

    Transaction tx = transNeo.beginTx();

    try {
      bufferedWriter = new BufferedWriter(new FileWriter(chacoFile));

      long nodeCount = 0;
      long edgeCount = 0;
      for (Node node : transNeo.getAllNodes()) {

        nodeCount++;

        // Only consider OUTGOING so edges counted once
        for (Relationship rel : node.getRelationships(Direction.OUTGOING)) {
          edgeCount++;
        }
      }

      String firstLine = String.format("%d %d 0", nodeCount, edgeCount * 2);
      bufferedWriter.write(firstLine);

      int flushBuffer = 0;

      for (Node node : transNeo.getAllNodes()) {

        flushBuffer++;

        bufferedWriter.newLine();

        // Chaco files assumed to be undirected. Edges are bidirectional
        for (Relationship rel : node.getRelationships(Direction.BOTH)) {

          Long gId = (Long) rel.getOtherNode(node).getProperty(Consts.NODE_GID);
          bufferedWriter.write(" " + gId.toString());
        }

        // Temporary flush to reduce memory consumption
        if (flushBuffer % Consts.STORE_BUF == 0) {
          bufferedWriter.flush();
        }
      }

    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
    } catch (IOException ex) {
      ex.printStackTrace();
    } finally {

      tx.finish();

      // Close the BufferedWriter
      try {
        if (bufferedWriter != null) {
          bufferedWriter.flush();
          bufferedWriter.close();
        }
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
  }