Search in sources :

Example 1 with Relationship

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));
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Statement(org.neo4j.driver.v1.Statement) PathNode(org.openkilda.messaging.info.event.PathNode) PathInfoData(org.openkilda.messaging.info.event.PathInfoData) ImmutablePair(org.openkilda.messaging.model.ImmutablePair) Relationship(org.neo4j.driver.v1.types.Relationship) Record(org.neo4j.driver.v1.Record) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException) Session(org.neo4j.driver.v1.Session)

Example 2 with Relationship

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);
    }
}
Also used : Path(org.neo4j.driver.v1.types.Path) StatementResult(org.neo4j.driver.v1.StatementResult) Node(org.neo4j.driver.v1.types.Node) Relationship(org.neo4j.driver.v1.types.Relationship) Driver(org.neo4j.driver.v1.Driver) Record(org.neo4j.driver.v1.Record) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Example 3 with Relationship

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;
}
Also used : BoltConnector(org.neo4j.kernel.configuration.BoltConnector) GraphDatabaseFactory(org.neo4j.graphdb.factory.GraphDatabaseFactory) ServiceUnavailableException(org.neo4j.driver.v1.exceptions.ServiceUnavailableException) File(java.io.File) GraphDatabaseBuilder(org.neo4j.graphdb.factory.GraphDatabaseBuilder)

Example 4 with Relationship

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());
}
Also used : VirtualRelationship(apoc.result.VirtualRelationship) InternalEntity(org.neo4j.driver.internal.InternalEntity) VirtualNode(apoc.result.VirtualNode) VirtualRelationship(apoc.result.VirtualRelationship) Relationship(org.neo4j.driver.v1.types.Relationship)

Example 5 with Relationship

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();
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Node(org.neo4j.driver.v1.types.Node) Relationship(org.neo4j.driver.v1.types.Relationship) Driver(org.neo4j.driver.v1.Driver) Record(org.neo4j.driver.v1.Record) Session(org.neo4j.driver.v1.Session) Test(org.junit.Test)

Aggregations

Relationship (org.neo4j.driver.v1.types.Relationship)4 Driver (org.neo4j.driver.v1.Driver)3 Record (org.neo4j.driver.v1.Record)3 Session (org.neo4j.driver.v1.Session)3 StatementResult (org.neo4j.driver.v1.StatementResult)3 Test (org.junit.Test)2 Node (org.neo4j.driver.v1.types.Node)2 VirtualNode (apoc.result.VirtualNode)1 VirtualRelationship (apoc.result.VirtualRelationship)1 File (java.io.File)1 InternalEntity (org.neo4j.driver.internal.InternalEntity)1 Statement (org.neo4j.driver.v1.Statement)1 NoSuchRecordException (org.neo4j.driver.v1.exceptions.NoSuchRecordException)1 ServiceUnavailableException (org.neo4j.driver.v1.exceptions.ServiceUnavailableException)1 TransientException (org.neo4j.driver.v1.exceptions.TransientException)1 Path (org.neo4j.driver.v1.types.Path)1 GraphDatabaseBuilder (org.neo4j.graphdb.factory.GraphDatabaseBuilder)1 GraphDatabaseFactory (org.neo4j.graphdb.factory.GraphDatabaseFactory)1 BoltConnector (org.neo4j.kernel.configuration.BoltConnector)1 PathInfoData (org.openkilda.messaging.info.event.PathInfoData)1