use of org.neo4j.driver.v1.types.Relationship in project open-kilda by telstra.
the class NeoDriver method getPath.
/**
* {@inheritDoc}
*/
@Override
public ImmutablePair<PathInfoData, PathInfoData> getPath(Flow flow, Strategy strategy) throws UnroutablePathException {
long latency = 0L;
List<PathNode> forwardNodes = new LinkedList<>();
List<PathNode> reverseNodes = new LinkedList<>();
if (!flow.isOneSwitchFlow()) {
Statement statement = getPathQuery(flow, strategy);
logger.debug("QUERY: {}", statement.toString());
try (Session session = driver.session()) {
StatementResult result = session.run(statement);
try {
Record record = result.next();
LinkedList<Relationship> isls = new LinkedList<>();
record.get(0).asPath().relationships().forEach(isls::add);
int seqId = 0;
for (Relationship isl : isls) {
latency += isl.get("latency").asLong();
forwardNodes.add(new PathNode(isl.get("src_switch").asString(), isl.get("src_port").asInt(), seqId, isl.get("latency").asLong()));
seqId++;
forwardNodes.add(new PathNode(isl.get("dst_switch").asString(), isl.get("dst_port").asInt(), seqId, 0L));
seqId++;
}
seqId = 0;
Collections.reverse(isls);
for (Relationship isl : isls) {
reverseNodes.add(new PathNode(isl.get("dst_switch").asString(), isl.get("dst_port").asInt(), seqId, isl.get("latency").asLong()));
seqId++;
reverseNodes.add(new PathNode(isl.get("src_switch").asString(), isl.get("src_port").asInt(), seqId, 0L));
seqId++;
}
} catch (NoSuchRecordException e) {
throw new UnroutablePathException(flow);
}
}
} else {
logger.info("No path computation for one-switch flow");
}
return new ImmutablePair<>(new PathInfoData(latency, forwardNodes), new PathInfoData(latency, reverseNodes));
}
use of org.neo4j.driver.v1.types.Relationship in project cypher-for-gremlin by opencypher.
the class GremlinNeo4jDriverTest method returnPath.
@Test
public void returnPath() {
Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort());
try (Session session = driver.session()) {
StatementResult setup = session.run("CREATE (n1:Person {name: 'Anders'})-[r:knows]->(n2:Person)" + "RETURN n1,r,n2");
Record createdNodes = setup.single();
Node n1 = createdNodes.get("n1").asNode();
Node n2 = createdNodes.get("n2").asNode();
Relationship r = createdNodes.get("r").asRelationship();
StatementResult result = session.run("MATCH p =(b1 { name: 'Anders' })-->()" + "RETURN p");
Path path = result.single().get("p").asPath();
assertThat(path.contains(n1)).isTrue();
assertThat(path.contains(n2)).isTrue();
assertThat(path.contains(r)).isTrue();
assertThat(path.relationships()).hasSize(1);
assertThat(path.nodes()).hasSize(2);
}
}
use of org.neo4j.driver.v1.types.Relationship in project structr by structr.
the class BoltDatabaseService method initialize.
@Override
public boolean initialize() {
this.databasePath = Settings.DatabasePath.getValue();
this.tenantId = Settings.TenantIdentifier.getValue();
if (StringUtils.isBlank(this.tenantId)) {
this.tenantId = null;
}
final BoltConnector bolt = new BoltConnector("0");
databaseUrl = Settings.ConnectionUrl.getValue();
final String username = Settings.ConnectionUser.getValue();
final String password = Settings.ConnectionPassword.getValue();
final String driverMode = Settings.DatabaseDriverMode.getValue();
final String confPath = databasePath + "/neo4j.conf";
final File confFile = new File(confPath);
// see https://github.com/neo4j/neo4j-java-driver/issues/364 for an explanation
final String databaseServerUrl;
final String databaseDriverUrl;
if (databaseUrl.length() >= 7 && databaseUrl.substring(0, 7).equalsIgnoreCase("bolt://")) {
databaseServerUrl = databaseUrl.substring(7);
databaseDriverUrl = databaseUrl;
} else if (databaseUrl.length() >= 15 && databaseUrl.substring(0, 15).equalsIgnoreCase("bolt+routing://")) {
databaseServerUrl = databaseUrl.substring(15);
databaseDriverUrl = databaseUrl;
} else {
databaseServerUrl = databaseUrl;
databaseDriverUrl = "bolt://" + databaseUrl;
}
// create db directory if it does not exist
new File(databasePath).mkdirs();
if (!"remote".equals(driverMode)) {
final GraphDatabaseBuilder builder = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(new File(databasePath)).setConfig(GraphDatabaseSettings.allow_store_upgrade, "true").setConfig("dbms.allow_format_migration", "true").setConfig(bolt.type, "BOLT").setConfig(bolt.enabled, "true").setConfig(bolt.address, databaseServerUrl);
if (confFile.exists()) {
builder.loadPropertiesFromFile(confPath);
}
graphDb = builder.newGraphDatabase();
}
try {
driver = GraphDatabase.driver(databaseDriverUrl, AuthTokens.basic(username, password), Config.build().withoutEncryption().toConfig());
final int relCacheSize = Settings.RelationshipCacheSize.getValue();
final int nodeCacheSize = Settings.NodeCacheSize.getValue();
NodeWrapper.initialize(nodeCacheSize);
logger.info("Node cache size set to {}", nodeCacheSize);
RelationshipWrapper.initialize(relCacheSize);
logger.info("Relationship cache size set to {}", relCacheSize);
// signal success
return true;
} catch (ServiceUnavailableException ex) {
logger.error("Neo4j service is not available.");
}
// service failed to initialize
return false;
}
use of org.neo4j.driver.v1.types.Relationship in project neo4j-apoc-procedures by neo4j-contrib.
the class Bolt method toRelationship.
private Object toRelationship(Object value, boolean virtual, Map<Long, Object> nodesCache) {
Value internalValue = ((InternalEntity) value).asValue();
Relationship relationship = internalValue.asRelationship();
if (virtual) {
VirtualNode start = (VirtualNode) nodesCache.getOrDefault(relationship.startNodeId(), new VirtualNode(relationship.startNodeId(), db));
VirtualNode end = (VirtualNode) nodesCache.getOrDefault(relationship.endNodeId(), new VirtualNode(relationship.endNodeId(), db));
VirtualRelationship virtualRelationship = new VirtualRelationship(relationship.id(), start, end, RelationshipType.withName(relationship.type()), relationship.asMap());
return virtualRelationship;
} else
return Util.map("entityType", internalValue.type().name(), "type", relationship.type(), "id", relationship.id(), "start", relationship.startNodeId(), "end", relationship.endNodeId(), "properties", relationship.asMap());
}
use of org.neo4j.driver.v1.types.Relationship in project cypher-for-gremlin by opencypher.
the class GremlinNeo4jDriverTest method returnNodeAndRelationship.
@Test
public void returnNodeAndRelationship() {
Driver driver = GremlinDatabase.driver("//localhost:" + server.getPort());
try (Session session = driver.session()) {
StatementResult result = session.run("CREATE (n1:Person {name: 'Marko'})-[r:knows {since:1999}]->(n2:Person)" + "RETURN n1,r,n2", parameters("message", "Hello"));
Record record = result.single();
Node n1 = record.get("n1").asNode();
Relationship r = record.get("r").asRelationship();
Node n2 = record.get("n2").asNode();
assertThat(n1.hasLabel("Person")).isTrue();
assertThat(n1.get("name").asString()).isEqualTo("Marko");
assertThat(r.hasType("knows")).isTrue();
assertThat(r.startNodeId()).isEqualTo(n1.id());
assertThat(r.endNodeId()).isEqualTo(n2.id());
assertThat(r.get("since").asLong()).isEqualTo(1999L);
assertThat(n2.hasLabel("Person")).isTrue();
}
}
Aggregations