Search in sources :

Example 61 with GraphDatabaseService

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

the class AStar method findSinglePath.

public WeightedPath findSinglePath(Node start, Node end) {
    Doer doer = new Doer(start, end);
    while (doer.hasNext()) {
        Node node = doer.next();
        GraphDatabaseService graphDb = node.getGraphDatabase();
        if (node.equals(end)) {
            // Hit, return path
            double weight = doer.score.get(node.getId()).wayLength;
            LinkedList<Relationship> rels = new LinkedList<Relationship>();
            Relationship rel = graphDb.getRelationshipById(doer.cameFrom.get(node.getId()));
            while (rel != null) {
                rels.addFirst(rel);
                node = rel.getOtherNode(node);
                Long nextRelId = doer.cameFrom.get(node.getId());
                rel = nextRelId == null ? null : graphDb.getRelationshipById(nextRelId);
            }
            Path path = toPath(start, rels);
            return new WeightedPathImpl(weight, path);
        }
    }
    return null;
}
Also used : Path(org.neo4j.graphdb.Path) WeightedPath(org.neo4j.graphalgo.WeightedPath) WeightedPathImpl(org.neo4j.graphalgo.impl.util.WeightedPathImpl) GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) Relationship(org.neo4j.graphdb.Relationship) LinkedList(java.util.LinkedList)

Example 62 with GraphDatabaseService

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

the class ShortestPath method getPaths.

private static Iterable<LinkedList<Relationship>> getPaths(Hit hit, DirectionData data) {
    LevelData levelData = data.visitedNodes.get(hit.connectingNode);
    if (levelData.depth == 0) {
        Collection<LinkedList<Relationship>> result = new ArrayList<LinkedList<Relationship>>();
        result.add(new LinkedList<Relationship>());
        return result;
    }
    Collection<PathData> set = new ArrayList<PathData>();
    GraphDatabaseService graphDb = data.startNode.getGraphDatabase();
    for (long rel : levelData.relsToHere) {
        set.add(new PathData(hit.connectingNode, new LinkedList<Relationship>(Arrays.asList(graphDb.getRelationshipById(rel)))));
    }
    for (int i = 0; i < levelData.depth - 1; i++) {
        // One level
        Collection<PathData> nextSet = new ArrayList<PathData>();
        for (PathData entry : set) {
            // One path...
            Node otherNode = entry.rels.getFirst().getOtherNode(entry.node);
            LevelData otherLevelData = data.visitedNodes.get(otherNode);
            int counter = 0;
            for (long rel : otherLevelData.relsToHere) {
                // ...may split into several paths
                LinkedList<Relationship> rels = ++counter == otherLevelData.relsToHere.length ? // lists being copied
                entry.rels : new LinkedList<Relationship>(entry.rels);
                rels.addFirst(graphDb.getRelationshipById(rel));
                nextSet.add(new PathData(otherNode, rels));
            }
        }
        set = nextSet;
    }
    return new IterableWrapper<LinkedList<Relationship>, PathData>(set) {

        @Override
        protected LinkedList<Relationship> underlyingObjectToObject(PathData object) {
            return object.rels;
        }
    };
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Node(org.neo4j.graphdb.Node) ArrayList(java.util.ArrayList) IterableWrapper(org.neo4j.helpers.collection.IterableWrapper) LinkedList(java.util.LinkedList) Relationship(org.neo4j.graphdb.Relationship)

Example 63 with GraphDatabaseService

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

the class TestConfiguration method testOffByDefault.

@Test
public void testOffByDefault() throws Exception {
    GraphDatabaseService db = newDb(null);
    try {
        OnlineBackup.from("localhost").full(BACKUP_DIR);
        fail("Shouldn't be possible");
    } catch (Exception e) {
    // Good
    }
    db.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Test(org.junit.Test)

Example 64 with GraphDatabaseService

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

the class LegacyIndexAddDropConcurrently method shouldHandleConcurrentIndexDropping.

@Test
public void shouldHandleConcurrentIndexDropping() throws Exception {
    // Given
    ExecutorService exec = Executors.newFixedThreadPool(4);
    final GraphDatabaseService db = dbRule.getGraphDatabaseAPI();
    List<Callable<Object>> jobs = new ArrayList<>();
    for (int i = 0; i < 4; i++) {
        jobs.add(new Callable<Object>() {

            private final Random rand = ThreadLocalRandom.current();

            @Override
            public Object call() throws Exception {
                for (int j = 0; j < 1000; j++) {
                    switch(rand.nextInt(5)) {
                        case 4:
                            // 1 in 5 chance, drop the index
                            try (Transaction tx = db.beginTx()) {
                                db.index().forNodes("users").delete();
                                tx.success();
                            } catch (NotFoundException e) {
                            // Occasionally expected
                            }
                            break;
                        default:
                            // Otherwise, write to it
                            try (Transaction tx = db.beginTx()) {
                                db.index().forNodes("users").add(db.createNode(), "name", "steve");
                                tx.success();
                            } catch (NotFoundException e) {
                            // Occasionally expected
                            }
                            break;
                    }
                }
                return null;
            }
        });
    }
    // When
    for (Future<Object> objectFuture : exec.invokeAll(jobs)) {
        objectFuture.get();
    }
// Then no errors should have occurred.
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) ArrayList(java.util.ArrayList) NotFoundException(org.neo4j.graphdb.NotFoundException) Callable(java.util.concurrent.Callable) NotFoundException(org.neo4j.graphdb.NotFoundException) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Transaction(org.neo4j.graphdb.Transaction) ExecutorService(java.util.concurrent.ExecutorService) Test(org.junit.Test)

Example 65 with GraphDatabaseService

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

the class TestMigration method providerGetsFilledInAutomatically.

@Test
public void providerGetsFilledInAutomatically() {
    Map<String, String> correctConfig = MapUtil.stringMap("type", "exact", IndexManager.PROVIDER, "lucene");
    File storeDir = new File("target/var/index");
    Neo4jTestCase.deleteFileOrDirectory(storeDir);
    GraphDatabaseService graphDb = startDatabase(storeDir);
    try (Transaction transaction = graphDb.beginTx()) {
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forNodes("default")));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forNodes("wo-provider", MapUtil.stringMap("type", "exact"))));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forNodes("w-provider", MapUtil.stringMap("type", "exact", IndexManager.PROVIDER, "lucene"))));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forRelationships("default")));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forRelationships("wo-provider", MapUtil.stringMap("type", "exact"))));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forRelationships("w-provider", MapUtil.stringMap("type", "exact", IndexManager.PROVIDER, "lucene"))));
        transaction.success();
    }
    graphDb.shutdown();
    removeProvidersFromIndexDbFile(storeDir);
    graphDb = startDatabase(storeDir);
    try (Transaction transaction = graphDb.beginTx()) {
        // Getting the index w/o exception means that the provider has been reinstated
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forNodes("default")));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forNodes("wo-provider", MapUtil.stringMap("type", "exact"))));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forNodes("w-provider", MapUtil.stringMap("type", "exact", IndexManager.PROVIDER, "lucene"))));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forRelationships("default")));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forRelationships("wo-provider", MapUtil.stringMap("type", "exact"))));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forRelationships("w-provider", MapUtil.stringMap("type", "exact", IndexManager.PROVIDER, "lucene"))));
    }
    graphDb.shutdown();
    removeProvidersFromIndexDbFile(storeDir);
    graphDb = startDatabase(storeDir);
    try (Transaction transaction = graphDb.beginTx()) {
        // Getting the index w/o exception means that the provider has been reinstated
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forNodes("default")));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forNodes("wo-provider")));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forNodes("w-provider")));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forRelationships("default")));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forRelationships("wo-provider")));
        assertEquals(correctConfig, graphDb.index().getConfiguration(graphDb.index().forRelationships("w-provider")));
    }
    graphDb.shutdown();
}
Also used : GraphDatabaseService(org.neo4j.graphdb.GraphDatabaseService) Transaction(org.neo4j.graphdb.Transaction) File(java.io.File) 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