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;
}
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;
}
};
}
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();
}
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.
}
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();
}
Aggregations