Search in sources :

Example 81 with GraphDatabaseService

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

the class AStarPerformanceIT method somePerformanceTesting.

@Test
public void somePerformanceTesting() throws Exception {
    // GIVEN
    int numberOfNodes = 200000;
    GeoDataGenerator generator = new GeoDataGenerator(numberOfNodes, 5d, 1000, 1000);
    generator.generate(directory);
    // WHEN
    long[][] points = new long[][] { new long[] { 9415, 158154 }, new long[] { 89237, 192863 }, new long[] { 68072, 150484 }, new long[] { 186309, 194495 }, new long[] { 152097, 99289 }, new long[] { 92150, 161182 }, new long[] { 188446, 115873 }, new long[] { 85033, 7772 }, new long[] { 291, 86707 }, new long[] { 188345, 158468 } };
    GraphDatabaseService db = new TestGraphDatabaseFactory().newEmbeddedDatabase(directory.getAbsoluteFile());
    PathFinder<WeightedPath> algo = aStar(allTypesAndDirections(), doubleCostEvaluator("weight", 0), GeoDataGenerator.estimateEvaluator());
    for (int i = 0; i < 10; i++) {
        System.out.println("----- " + i);
        for (long[] p : points) {
            try (Transaction tx = db.beginTx()) {
                Node start = db.getNodeById(p[0]);
                Node end = db.getNodeById(p[1]);
                long time = currentTimeMillis();
                WeightedPath path = algo.findSinglePath(start, end);
                time = currentTimeMillis() - time;
                System.out.println("time: " + time + ", len:" + path.length() + ", weight:" + path.weight());
                tx.success();
            }
        }
    }
    // THEN
    db.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) WeightedPath(org.neo4j.graphalgo.WeightedPath) Transaction(org.neo4j.graphdb.Transaction) Node(org.neo4j.graphdb.Node) TestGraphDatabaseFactory(org.neo4j.test.TestGraphDatabaseFactory) Test(org.junit.Test)

Example 82 with GraphDatabaseService

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

the class AllPaths method instantiateTraverser.

@Override
protected Traverser instantiateTraverser(Node start, Node end) {
    //        // Legacy single-directional traversal (for reference or something)
    //        return traversal().expand( expander ).depthFirst().uniqueness( uniqueness() )
    //                .evaluator( toDepth( maxDepth ) ).evaluator( Evaluators.includeWhereEndNodeIs( end ) )
    //                .traverse( start );
    // Bidirectional traversal
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription base = db.traversalDescription().depthFirst().uniqueness(uniqueness());
    return db.bidirectionalTraversalDescription().startSide(base.expand(expander).evaluator(toDepth(maxDepth / 2))).endSide(base.expand(expander.reverse()).evaluator(toDepth(maxDepth - maxDepth / 2))).traverse(start, end);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription)

Example 83 with GraphDatabaseService

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

the class DijkstraBidirectional method traverser.

private Traverser traverser(Node start, final Node end, PathInterest interest) {
    final MutableDouble shortestSoFar = new MutableDouble(Double.MAX_VALUE);
    final MutableDouble startSideShortest = new MutableDouble(0);
    final MutableDouble endSideShortest = new MutableDouble(0);
    PathExpander dijkstraExpander = new DijkstraBidirectionalPathExpander(expander, shortestSoFar, true, startSideShortest, endSideShortest, epsilon);
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription side = db.traversalDescription().expand(dijkstraExpander, stateFactory).order(new DijkstraSelectorFactory(interest, costEvaluator)).evaluator(new DijkstraBidirectionalEvaluator(costEvaluator)).uniqueness(Uniqueness.NODE_PATH);
    TraversalDescription startSide = side;
    TraversalDescription endSide = side.reverse();
    BidirectionalTraversalDescription traversal = db.bidirectionalTraversalDescription().startSide(startSide).endSide(endSide).collisionEvaluator(Evaluators.all()).collisionPolicy(new BranchCollisionPolicy() {

        @Override
        public BranchCollisionDetector create(Evaluator evaluator, Predicate<Path> pathPredicate) {
            return new DijkstraBranchCollisionDetector(evaluator, costEvaluator, shortestSoFar, epsilon, pathPredicate);
        }
    });
    lastTraverser = traversal.traverse(start, end);
    return lastTraverser;
}
Also used : WeightedPath(org.neo4j.graphalgo.WeightedPath) Path(org.neo4j.graphdb.Path) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) BranchCollisionPolicy(org.neo4j.graphdb.traversal.BranchCollisionPolicy) MutableDouble(org.apache.commons.lang3.mutable.MutableDouble) PathExpander(org.neo4j.graphdb.PathExpander) DijkstraSelectorFactory(org.neo4j.graphalgo.impl.util.DijkstraSelectorFactory) PathEvaluator(org.neo4j.graphdb.traversal.PathEvaluator) Evaluator(org.neo4j.graphdb.traversal.Evaluator) CostEvaluator(org.neo4j.graphalgo.CostEvaluator) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) BranchCollisionDetector(org.neo4j.graphdb.traversal.BranchCollisionDetector) DijkstraBranchCollisionDetector(org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector) BidirectionalTraversalDescription(org.neo4j.graphdb.traversal.BidirectionalTraversalDescription) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) DijkstraBranchCollisionDetector(org.neo4j.graphalgo.impl.util.DijkstraBranchCollisionDetector)

Example 84 with GraphDatabaseService

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

the class ExactDepthPathFinder method instantiateTraverser.

@Override
protected Traverser instantiateTraverser(Node start, Node end) {
    GraphDatabaseService db = start.getGraphDatabase();
    TraversalDescription side = db.traversalDescription().breadthFirst().uniqueness(uniqueness).order(new BranchOrderingPolicy() {

        @Override
        public BranchSelector create(TraversalBranch startSource, PathExpander expander) {
            return new LiteDepthFirstSelector(startSource, startThreshold, expander);
        }
    });
    return db.bidirectionalTraversalDescription().startSide(side.expand(expander).evaluator(toDepth(onDepth / 2))).endSide(side.expand(expander.reverse()).evaluator(toDepth(onDepth - onDepth / 2))).collisionEvaluator(atDepth(onDepth)).traverse(start, end);
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) BranchOrderingPolicy(org.neo4j.graphdb.traversal.BranchOrderingPolicy) TraversalDescription(org.neo4j.graphdb.traversal.TraversalDescription) LiteDepthFirstSelector(org.neo4j.graphalgo.impl.util.LiteDepthFirstSelector) PathExpander(org.neo4j.graphdb.PathExpander) TraversalBranch(org.neo4j.graphdb.traversal.TraversalBranch) BranchSelector(org.neo4j.graphdb.traversal.BranchSelector)

Example 85 with GraphDatabaseService

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

the class KernelRecoveryTest method shouldHandleWritesProperlyAfterRecovery.

@Test
public void shouldHandleWritesProperlyAfterRecovery() throws Exception {
    // Given
    EphemeralFileSystemAbstraction fs = fsRule.get();
    GraphDatabaseService db = newDB(fs);
    long node1 = createNode(db);
    // And given the power goes out
    EphemeralFileSystemAbstraction crashedFs = fs.snapshot();
    db.shutdown();
    try {
        db = newDB(crashedFs);
        long node2 = createNode(db);
        db.shutdown();
        // Then the logical log should be in sync
        File logFile = new File(storeDir, PhysicalLogFile.DEFAULT_NAME + PhysicalLogFile.DEFAULT_VERSION_SUFFIX + "0");
        assertThat(logEntries(crashedFs, logFile), containsExactly(// Tx before recovery
        startEntry(-1, -1), commandEntry(node1, NodeCommand.class), commandEntry(ReadOperations.ANY_LABEL, NodeCountsCommand.class), commitEntry(2), // Tx after recovery
        startEntry(-1, -1), commandEntry(node2, NodeCommand.class), commandEntry(ReadOperations.ANY_LABEL, NodeCountsCommand.class), commitEntry(3), // checkpoint
        checkPoint(new LogPosition(0, 250))));
    } finally {
        crashedFs.close();
    }
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) EphemeralFileSystemAbstraction(org.neo4j.graphdb.mockfs.EphemeralFileSystemAbstraction) File(java.io.File) PhysicalLogFile(org.neo4j.kernel.impl.transaction.log.PhysicalLogFile) LogPosition(org.neo4j.kernel.impl.transaction.log.LogPosition) Test(org.junit.Test)

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