Search in sources :

Example 76 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class RelationshipCountsTest method shouldReportTotalNumberOfRelationships.

@Test
public void shouldReportTotalNumberOfRelationships() throws Exception {
    // given
    GraphDatabaseService graphDb = db.getGraphDatabaseAPI();
    long before = numberOfRelationships();
    long during;
    try (Transaction tx = graphDb.beginTx()) {
        Node node = graphDb.createNode();
        node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
        node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
        node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
        during = countsForRelationship(null, null, null);
        tx.success();
    }
    // when
    long after = numberOfRelationships();
    // then
    assertEquals(0, before);
    assertEquals(3, during);
    assertEquals(3, after);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 77 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class RelationshipCountsTest method shouldAccountForDeletedRelationships.

@Test
public void shouldAccountForDeletedRelationships() throws Exception {
    // given
    GraphDatabaseService graphDb = db.getGraphDatabaseAPI();
    Relationship rel;
    try (Transaction tx = graphDb.beginTx()) {
        Node node = graphDb.createNode();
        node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
        rel = node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
        node.createRelationshipTo(graphDb.createNode(), withName("KNOWS"));
        tx.success();
    }
    long before = numberOfRelationships(), during;
    try (Transaction tx = graphDb.beginTx()) {
        rel.delete();
        during = countsForRelationship(null, null, null);
        tx.success();
    }
    // when
    long after = numberOfRelationships();
    // then
    assertEquals(3, before);
    assertEquals(2, during);
    assertEquals(2, after);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) Relationship(org.neo4j.graphdb.Relationship) Node(org.neo4j.graphdb.Node) Test(org.junit.Test)

Example 78 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class StoreLockerTest method mustPreventMultipleInstancesFromStartingOnSameStore.

@Test
public void mustPreventMultipleInstancesFromStartingOnSameStore() {
    File storeDir = target.graphDbDir();
    GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(storeDir);
    try (Transaction tx = db.beginTx()) {
        db.createNode();
        tx.success();
    }
    try {
        new TestGraphDatabaseFactory().newEmbeddedDatabase(storeDir);
        fail("Should not be able to start up another db in the same dir");
    } catch (Exception e) {
    // Good
    } finally {
        db.shutdown();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) File(java.io.File) IOException(java.io.IOException) StoreLockException(org.neo4j.kernel.StoreLockException) Test(org.junit.Test)

Example 79 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class TraversalAStar method findPaths.

private Iterable<WeightedPath> findPaths(Node start, Node end, boolean multiplePaths) {
    PathInterest interest;
    if (multiplePaths) {
        interest = stopAfterLowestWeight ? PathInterestFactory.allShortest() : PathInterestFactory.all();
    } else {
        interest = PathInterestFactory.single();
    }
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription traversalDescription = db.traversalDescription().uniqueness(Uniqueness.NONE).expand(expander, initialState);
    lastTraverser = traversalDescription.order(new SelectorFactory(end, interest)).evaluator(includeWhereEndNodeIs(end)).traverse(start);
    return new Iterable<WeightedPath>() {

        @Override
        public Iterator<WeightedPath> iterator() {
            return new WeightedPathIterator(lastTraverser.iterator(), costEvaluator, stopAfterLowestWeight);
        }
    };
}
Also used : WeightedPathIterator(org.neo4j.graphalgo.impl.util.WeightedPathIterator) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) WeightedPath(org.neo4j.graphalgo.WeightedPath) BestFirstSelectorFactory(org.neo4j.graphalgo.impl.util.BestFirstSelectorFactory) PathInterest(org.neo4j.graphalgo.impl.util.PathInterest) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription)

Example 80 with GraphDatabaseService

use of org.neo4j.graphdb.GraphDatabaseService in project neo4j by neo4j.

the class TraversalShortestPath method instantiateTraverser.

@Override
protected Traverser instantiateTraverser(Node start, Node end) {
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription sideBase = db.traversalDescription().breadthFirst().uniqueness(NODE_PATH);
    return db.bidirectionalTraversalDescription().mirroredSides(sideBase.expand(expander)).sideSelector(LEVEL_STOP_DESCENT_ON_RESULT, maxDepth).collisionEvaluator(toDepth(maxDepth)).traverse(start, end);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription)

Aggregations

GraphDatabaseService (org.neo4j.graphdb.GraphDatabaseService)322 Test (org.junit.Test)225 Transaction (org.neo4j.graphdb.Transaction)182 Node (org.neo4j.graphdb.Node)142 TestGraphDatabaseFactory (org.neo4j.test.TestGraphDatabaseFactory)77 File (java.io.File)70 Relationship (org.neo4j.graphdb.Relationship)49 EmbeddedGraphDatabase (org.neo4j.kernel.EmbeddedGraphDatabase)32 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)17 Result (org.neo4j.graphdb.Result)14 Label (org.neo4j.graphdb.Label)13 GraphDatabaseAPI (org.neo4j.kernel.internal.GraphDatabaseAPI)12 HashMap (java.util.HashMap)11 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 ArrayList (java.util.ArrayList)10 Map (java.util.Map)10 PageCache (org.neo4j.io.pagecache.PageCache)10 DbRepresentation (org.neo4j.test.DbRepresentation)10 GraphDatabaseFactory (org.neo4j.graphdb.factory.GraphDatabaseFactory)9 TransactionFailureException (org.neo4j.graphdb.TransactionFailureException)8