Search in sources :

Example 1 with Node

use of org.neo4j.driver.v1.types.Node 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 2 with Node

use of org.neo4j.driver.v1.types.Node 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 3 with Node

use of org.neo4j.driver.v1.types.Node in project structr by structr.

the class EntityWrapper method assertNotStale.

// ----- protected methods -----
protected synchronized void assertNotStale() {
    if (stale) {
        // invalidate caches
        onRemoveFromCache();
        // if a node/rel was deleted in a previous transaction but the caller keeps a
        // reference to this entity, we need to make sure that the reference is fresh.
        final SessionTransaction tx = db.getCurrentTransaction();
        final Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        try {
            // update data
            data.clear();
            update(tx.getEntity(getQueryPrefix() + " WHERE ID(n) = {id} RETURN n", map).asMap());
        } catch (NoSuchRecordException nex) {
            throw new NotFoundException(nex);
        }
        stale = false;
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) SessionTransaction(org.structr.bolt.SessionTransaction) NotFoundException(org.structr.api.NotFoundException) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException)

Example 4 with Node

use of org.neo4j.driver.v1.types.Node 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)

Example 5 with Node

use of org.neo4j.driver.v1.types.Node in project open-kilda by telstra.

the class PathComputerTest method testGetFlowInfo.

@Test
public void testGetFlowInfo() {
    try (Transaction tx = graphDb.beginTx()) {
        Node node1, node2;
        node1 = graphDb.createNode(Label.label("switch"));
        node1.setProperty("name", "00:03");
        node2 = graphDb.createNode(Label.label("switch"));
        node2.setProperty("name", "00:04");
        Relationship rel1 = node1.createRelationshipTo(node2, RelationshipType.withName("flow"));
        rel1.setProperty("flowid", "f1");
        rel1.setProperty("cookie", 3);
        rel1.setProperty("meter_id", 2);
        rel1.setProperty("transit_vlan", 1);
        rel1.setProperty("src_switch", "00:03");
        tx.success();
    }
    Driver driver = GraphDatabase.driver("bolt://localhost:7878", AuthTokens.basic("neo4j", "password"));
    NeoDriver nd = new NeoDriver(driver);
    List<FlowInfo> fi = nd.getFlowInfo();
    Assert.assertEquals(fi.get(0).getFlowId(), "f1");
    Assert.assertEquals(fi.get(0).getCookie(), 3);
    Assert.assertEquals(fi.get(0).getMeterId(), 2);
    Assert.assertEquals(fi.get(0).getTransitVlanId(), 1);
    Assert.assertEquals(fi.get(0).getSrcSwitchId(), "00:03");
}
Also used : OkNode(org.openkilda.neo.OkNode) Driver(org.neo4j.driver.v1.Driver)

Aggregations

Driver (org.neo4j.driver.v1.Driver)3 Node (org.neo4j.driver.v1.types.Node)3 Test (org.junit.Test)2 Record (org.neo4j.driver.v1.Record)2 Session (org.neo4j.driver.v1.Session)2 StatementResult (org.neo4j.driver.v1.StatementResult)2 Relationship (org.neo4j.driver.v1.types.Relationship)2 VirtualNode (apoc.result.VirtualNode)1 File (java.io.File)1 HashMap (java.util.HashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 InternalEntity (org.neo4j.driver.internal.InternalEntity)1 NoSuchRecordException (org.neo4j.driver.v1.exceptions.NoSuchRecordException)1 ServiceUnavailableException (org.neo4j.driver.v1.exceptions.ServiceUnavailableException)1 Path (org.neo4j.driver.v1.types.Path)1 Label (org.neo4j.graphdb.Label)1 GraphDatabaseBuilder (org.neo4j.graphdb.factory.GraphDatabaseBuilder)1 GraphDatabaseFactory (org.neo4j.graphdb.factory.GraphDatabaseFactory)1 BoltConnector (org.neo4j.kernel.configuration.BoltConnector)1 OkNode (org.openkilda.neo.OkNode)1