Java Code Examples for org.neo4j.graphdb.Transaction#close()

The following examples show how to use org.neo4j.graphdb.Transaction#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReachabilityIndex.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
public void dropIndex() {
  if (indexExists()) {
    Transaction tx = graphDb.beginTx();

    // ...cleanup the index.
    int counter = 0;
    for (Node n : graphDb.getAllNodes()) {
      n.removeProperty(IN_LIST_PROPERTY);
      n.removeProperty(OUT_LIST_PROPERTY);
      tx = batchTransactions(tx, counter++);
    }

    // reset the flag.
    metaDataNode.setProperty(INDEX_EXISTS_PROPERTY, false);

    tx.success();
    tx.close();
    logger.info("Reachability index dropped.");
  } else {
    logger.warning("There was no reachability index to drop.");
  }
}
 
Example 2
Source File: AtlasTransformerTest.java    From atlas with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void simpleRoadContraction() {
    List<FastNode> dummyNodesForWay1 = getDummyNodesForWay(20);
    Way way1 = getDummyWay(dummyNodesForWay1);

    fastWriter.start(graphDatabaseService);
    dummyNodesForWay1.stream().forEach(fastNode ->  fastWriter.addNode(fastNode));
    fastWriter.addWay(way1);
    fastWriter.finish();

    // this is a bug, i should count way start node as an inserted node
    assertFastNodeCountInDatabase(20,21);

    Transaction tx = graphDatabaseService.beginTx();
    ContractCriteria contractCriteria = new ContractCriteria();
    atlasTransformer.applyContraction(this.graphDatabaseService, contractCriteria);
    tx.success();
    tx.close();

    assertFastNodeCountInDatabase(20,4);
}
 
Example 3
Source File: AnonymousNodeTagger.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
  logger.info("Starting anonymous nodes tagger...");
  int taggedNodes = 0;
  Transaction tx = graphDb.beginTx();

  ResourceIterable<Node> allNodes = graphDb.getAllNodes();
  for (Node n : allNodes) {
    if (n.hasProperty(anonymousProperty)) {
      n.addLabel(OwlLabels.OWL_ANONYMOUS);
      taggedNodes++;
    }
    if (taggedNodes % batchCommitSize == 0) {
      tx.success();
      tx.close();
      tx = graphDb.beginTx();
    }
  }

  logger.info(taggedNodes + " nodes tagged.");
  tx.success();
  tx.close();
}
 
Example 4
Source File: AtlasTest.java    From atlas with GNU General Public License v3.0 5 votes vote down vote up
public void assertFastNodeCountInDatabase(int noOfFastNodesExpected, int noOfNodesInDatabaseExpected) {
    Transaction tx = graphDatabaseService.beginTx();
    assertTrue(graphDatabaseService.getAllNodes().stream().count() == noOfNodesInDatabaseExpected);
    assertTrue(fastWriter.numberOfNodesAdded == noOfFastNodesExpected);
    tx.success();
    tx.close();
}
 
Example 5
Source File: NodeManager.java    From graphify with Apache License 2.0 5 votes vote down vote up
public boolean setNodeProperty(Long id, String key, Object value, GraphDatabaseService graphDb)
{
    boolean success = true;

    // Update the node's property in cache
    Map<String, Object> node = globalNodeCache.getIfPresent(id);

    if(node == null)
    {
        // The node isn't available in the cache, go to the database and retrieve it
        success = addNodeToCache((gdb, cache) -> getNodeHashMap(id, gdb, cache), graphDb);
        if(success) node = globalNodeCache.getIfPresent(id);
    }

    // Set the node property
    if (node != null) {
        node.put(key, value);
    }

    // TODO: Remove this in favor of a distributed messaging bus architecture
    Transaction tx = graphDb.beginTx();
    graphDb.getNodeById(id).setProperty(key, value);
    tx.success();
    tx.close();

    return success;
}
 
Example 6
Source File: ReachabilityIndex.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
Transaction batchTransactions(Transaction tx, int operationCount) {
  if (operationCount % transactionBatchSize == 0) {
    tx.success();
    tx.close();
    return graphDb.beginTx();
  } else {
    return tx;
  }
}
 
Example 7
Source File: Neo4jHelper.java    From timbuctoo with GNU General Public License v3.0 5 votes vote down vote up
public static void cleanDb( GraphDatabaseService graphDatabaseService, boolean includeReferenceNode ) {
  Transaction tx = graphDatabaseService.beginTx();
  try {
    clearIndex(graphDatabaseService);
    removeNodes(graphDatabaseService, includeReferenceNode);
    tx.success();
  } catch (Throwable t) {
    tx.failure();
    throw new RuntimeException("Error cleaning database ",t);
  } finally {
    tx.close();
  }
}
 
Example 8
Source File: Neo4jModuleTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test(expected = WriteOperationsNotAllowedException.class)
public void graphDbReadOnlyWithApi() {
  GraphDatabaseService graphDb = injectorReadOnly.getInstance(GraphDatabaseService.class);
  Transaction tx = graphDb.beginTx();
  try {
    graphDb.createNode(Label.label("test"));
  } finally {
    tx.close();
  }
}
 
Example 9
Source File: ETransactionImpl.java    From jcypher with Apache License 2.0 5 votes vote down vote up
@Override
public List<JcError> close() {
	List<JcError> errors;
	if (isClosed())
		throw new RuntimeException(ERR_CLOSED);
	if (!isMyThread())
		throw new RuntimeException(ERR_THREAD);
	
	AbstractEmbeddedDBAccess edba = getEDBAccess();
	edba.removeTx();
	if (this.transaction != null) {
		Transaction tx = getTransaction();
		if (failed)
			tx.failure();
		else
			tx.success();
		
		Throwable dbException = null;
		try {
			tx.close();
		} catch(Throwable e) {
			dbException = e;
		}
		errors = DBUtil.buildErrorList(null, dbException);
	} else 
		errors = new ArrayList<JcError>();
	
	if (errors.size() > 0)
		failure();
	setClosed();
	
	return errors;
}
 
Example 10
Source File: ParallelBatchTransaction.java    From neo4j-mazerunner with Apache License 2.0 5 votes vote down vote up
/**
 * Computes a section of the source array and applies updates to those nodes referenced
 * in each block.
 * @throws IOException
 */
protected void computeDirectly() throws IOException {

    Transaction tx = db.beginTx();

    Node partitionNode = null;

    if(analysis.getMode() == ProcessorMode.Partitioned)
        partitionNode = db.getNodeById(analysis.getPartitionDescription().getPartitionId());

    for (int i = mStart; i < mStart + mLength; i++) {
        final Node finalPartitionNode = partitionNode;
        mSource[i].forEachRemaining(line -> {
            switch (analysis.getMode()) {
                case Partitioned:
                    Writer.updatePartitionBlockForRow(line, db, reportBlock, analysis, finalPartitionNode);
                    break;
                case Unpartitioned:
                    Writer.updateBlockForRow(line, db, reportBlock, analysis.getAnalysis());
                    break;
            }

        });
    }

    tx.success();
    tx.close();

}
 
Example 11
Source File: ParallelReader.java    From neo4j-mazerunner with Apache License 2.0 5 votes vote down vote up
/**
 * Computes a section of the source array and applies updates to those nodes referenced
 * in each block.
 * @throws IOException
 */
protected void computeDirectly() throws IOException {
    Transaction tx = ((CFBatchTransaction)configuration).getGraphDatabaseService().beginTx();
    for (int i = configuration.getStart(); i < configuration.getStart() + configuration.getLength(); i++)
        src[i].forEachRemaining(configuration::transactBlock);
    tx.success();
    tx.close();
}
 
Example 12
Source File: AtlasTransformerTest.java    From atlas with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void contractRoadTest() {
    List<FastNode> dummyNodesForWay1 = getDummyNodesForWay(20);
    List<FastNode> dummyNodesForWay2 = getDummyNodesForWay(19);

    FastNode middleNode = dummyNodesForWay1.get(10);
    dummyNodesForWay2.add(10,middleNode);

    Way way1 = getDummyWay(dummyNodesForWay1);
    Way way2 = getDummyWay(dummyNodesForWay2);

    fastWriter.start(graphDatabaseService);
    dummyNodesForWay1.stream().forEach(fastNode ->  fastWriter.addNode(fastNode));
    dummyNodesForWay2.stream().forEach(fastNode ->  fastWriter.addNode(fastNode));
    fastWriter.addWay(way1);
    fastWriter.addWay(way2);

    // because there are two WAY_START nodes
    assertFastNodeCountInDatabase(39,41);

    System.out.println(dummyNodesForWay1.toString());
    System.out.println(dummyNodesForWay2.toString());

    Transaction tx = graphDatabaseService.beginTx();
    ContractCriteria contractCriteria = new ContractCriteria();
    atlasTransformer.applyContraction(this.graphDatabaseService, contractCriteria);
    tx.success();
    tx.close();
}
 
Example 13
Source File: AtlasTraversalTest.java    From atlas with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void getWayNodes() {
    List<FastNode> dummyNodesForWay1 = getDummyNodesForWay(20);
    List<FastNode> dummyNodesForWay2 = getDummyNodesForWay(19);

    FastNode middleNode = dummyNodesForWay1.get(10);
    dummyNodesForWay2.add(10,middleNode);

    Way way1 = getDummyWay(dummyNodesForWay1);
    Way way2 = getDummyWay(dummyNodesForWay2);

    fastWriter.start(graphDatabaseService);
    dummyNodesForWay1.stream().forEach(fastNode ->  fastWriter.addNode(fastNode));
    dummyNodesForWay2.stream().forEach(fastNode ->  fastWriter.addNode(fastNode));
    Node wayNode1 = fastWriter.addWay(way1);
    Node wayNode2 = fastWriter.addWay(way2);

    // because there are two WAY_START nodes
    assertFastNodeCountInDatabase(39,41);

    Transaction tx = graphDatabaseService.beginTx();
    //getNodesInTraversal(wayNode1).forEach(node -> System.out.println(node.getAllProperties().toString()));
    assertPathContainsNodesAndJustNodes(wayNode1, dummyNodesForWay1);
    assertPathContainsNodesAndJustNodes(wayNode2, dummyNodesForWay2);
    tx.success();
    tx.close();
}
 
Example 14
Source File: Neo4jModuleTest.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
@Test(expected = WriteOperationsNotAllowedException.class)
public void graphDbReadOnlyWithCypher() {
  GraphDatabaseService graphDb = injectorReadOnly.getInstance(GraphDatabaseService.class);
  Transaction tx = graphDb.beginTx();
  try {
    graphDb.execute("CREATE (n: test)");
  } finally {
    tx.close();
  }
}
 
Example 15
Source File: RoadContracter.java    From atlas with GNU General Public License v3.0 5 votes vote down vote up
public boolean contractRoad() {
    Transaction tx = db.beginTx();
    Iterator<Node> wayStartIterator = db.findNodes(Label.label("WAY_START"));
    while(wayStartIterator.hasNext()) {
        Node wayStartNode = wayStartIterator.next();

    }
    tx.success();
    tx.close();
    return true;
}
 
Example 16
Source File: WriterTest.java    From neo4j-mazerunner with Apache License 2.0 4 votes vote down vote up
private void createSampleGraph(GraphDatabaseService db) {

        List<Node> nodes = new ArrayList<>();

        int max = 200;
        Transaction tx = db.beginTx();
        Node partitionNode = db.createNode();
        partitionNode.addLabel(DynamicLabel.label("Category"));
        tx.success();
        tx.close();
        int count = 0;
        int partitionBlockCount = 50;

        tx = db.beginTx();
        // Create nodes
        for (int i = 0; i < max; i++) {

            nodes.add(db.createNode());
            nodes.get(i).addLabel(DynamicLabel.label("Node"));
            partitionNode.createRelationshipTo(nodes.get(i), withName("HAS_CATEGORY"));
            count++;
            if(count >= partitionBlockCount && i != max - 1) {

                count = 0;
                partitionNode = db.createNode();
                partitionNode.addLabel(DynamicLabel.label("Category"));

                tx.success();
                tx.close();
                tx = db.beginTx();

                System.out.println(i);
            }
        }

        tx.success();
        tx.close();

        tx = db.beginTx();
        // Create PageRank test graph
        for (int i = 0; i < (max / 2) - 1; i++) {
            nodes.get(i).createRelationshipTo(nodes.get(i + (max / 2)), withName("CONNECTED_TO"));
            nodes.get(i + (max / 2)).createRelationshipTo(nodes.get(i + 1), withName("CONNECTED_TO"));
            if(count >= partitionBlockCount / 2 && i != max - 1) {
                tx.success();
                tx.close();
                tx = db.beginTx();

                System.out.println("B: " + i);
            }
            if(i == (max / 2) - 2) {
                nodes.get((i + 1) + (max / 2)).createRelationshipTo(nodes.get(0), withName("CONNECTED_TO"));
                nodes.get(i + 1).createRelationshipTo(nodes.get((max / 2)), withName("CONNECTED_TO"));
            }
        }

        tx.success();
        tx.close();
    }
 
Example 17
Source File: EdgeLabeler.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
  logger.info("Starting edge labeling...");
  Map<String, String> map = new HashMap<String, String>();
  int processedRels = 0;

  Transaction tx = graphDb.beginTx();
  ResourceIterable<Relationship> rels = graphDb.getAllRelationships();

  for (Relationship rel : rels) {

    if (processedRels % batchCommitSize == 0) {
      tx.success();
      tx.close();
      tx = graphDb.beginTx();
    }

    String relName = rel.getType().name();
    if (map.containsKey(relName)) {
      rel.setProperty(edgeProperty, map.get(relName));
    } else {
      String relLabel = relName;
      String query = "START n = node:node_auto_index(iri='" + relName + "') match (n) return n";
      Result result = graphDb.execute(query);
      if (result.hasNext()) {
        Node n = (Node) result.next().get("n");
        if (n.hasProperty(NodeProperties.LABEL)) {
          relLabel =
              GraphUtil.getProperties(n, NodeProperties.LABEL, String.class).iterator().next();
        }
      }
      rel.setProperty(edgeProperty, relLabel);
      map.put(relName, relLabel);
    }

    processedRels++;
  }

  logger.info(processedRels + " relations labeled.");
  tx.success();
  tx.close();
}
 
Example 18
Source File: Clique.java    From SciGraph with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
  logger.info("Starting clique merge");

  Transaction tx = graphDb.beginTx();
  ResourceIterable<Node> allNodes = graphDb.getAllNodes();
  int size = Iterators.size(allNodes.iterator());
  tx.success();
  tx.close();

  logger.info(size + " nodes left to process");

  tx = graphDb.beginTx();
  TraversalDescription traversalDescription =
      graphDb.traversalDescription().breadthFirst().uniqueness(Uniqueness.NODE_GLOBAL);
  for (RelationshipType rel : relationships) {
    traversalDescription = traversalDescription.relationships(rel, Direction.BOTH);
  }

  Set<Long> processedNodes = new HashSet<Long>();

  for (Node baseNode : allNodes) {

    size -= 1;

    if (size % 100000 == 0) {
      logger.info(size + " nodes left to process");
    }

    if (size % batchCommitSize == 0) {
      logger.fine("Node batch commit");
      tx.success();
      tx.close();
      tx = graphDb.beginTx();
    }

    logger.fine("Processing Node - " + baseNode.getProperty(NodeProperties.IRI));

    if (!processedNodes.contains(baseNode.getId())) {
      // Keep a list of equivalentNodes
      List<Node> clique = new ArrayList<Node>();
      for (Node node : traversalDescription.traverse(baseNode).nodes()) {
        logger.fine("-- " + node.getProperty(NodeProperties.IRI));
        clique.add(node);
        processedNodes.add(node.getId());
      }

      logger.fine("clique size: " + clique.size());
      if (clique.size() == 1) {
        Node defactoLeader = clique.get(0);
        markAsCliqueLeader(defactoLeader);
      } else {
        Node leader = electCliqueLeader(clique, prefixLeaderPriority);
        markAsCliqueLeader(leader);
        clique.remove(leader); // keep only the peasants
        moveEdgesToLeader(leader, clique, tx);
        ensureLabel(leader, clique);
      }

    }

  }

  tx.success();
  tx.close();
}
 
Example 19
Source File: Neo4JIndexHandlerTest.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private void createIndexFor(TinkerPopGraphManager tinkerPopGraphManager, Collection collection) {
  Transaction tx = tinkerPopGraphManager.getGraphDatabase().beginTx();
  tinkerPopGraphManager.getGraphDatabase().index().forNodes(collection.getCollectionName());
  tx.close();
}
 
Example 20
Source File: WriterTest.java    From neo4j-mazerunner with Apache License 2.0 2 votes vote down vote up
@Test
public void testParallelUpdate() throws Exception {

    GraphDatabaseService db = setUpDb();

    Transaction tx = db.beginTx();


    // Use test configurations
    ConfigurationLoader.testPropertyAccess = true;

    Node nodePartition = db.createNode();
    nodePartition.addLabel(DynamicLabel.label("Category"));

    PartitionDescription partitionDescription = new PartitionDescription(nodePartition.getId(), "Category");

    // Create sample PageRank result
    String nodeList = "";

    for(int i = 0; i < 100; i++)
    {
        db.createNode();
        nodeList += i + " .001\n";
    }

    tx.success();
    tx.close();

    // Create test path
    String path = ConfigurationLoader.getInstance().getHadoopHdfsUri() + "/test/propertyNodeList.txt";

    writeListFile(path, nodeList);

    ProcessorMessage processorMessage = new ProcessorMessage(path, "pagerank", ProcessorMode.Partitioned);
    processorMessage.setPartitionDescription(partitionDescription);

    BufferedReader br = FileUtil.readGraphAdjacencyList(processorMessage);
    BufferedReader br2 = FileUtil.readGraphAdjacencyList(processorMessage);

    // Test parallel update
    PartitionedAnalysis.updatePartition(processorMessage, br, db);
    PartitionedAnalysis.updatePartition(processorMessage, br2, db);
}