Search in sources :

Example 1 with Statement

use of org.neo4j.driver.v1.Statement 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 Statement

use of org.neo4j.driver.v1.Statement in project cypher-for-gremlin by opencypher.

the class GremlinServerStatementResultTest method singleMore.

@Test(expected = NoSuchRecordException.class)
public void singleMore() {
    List<Map<String, Object>> results = Arrays.asList(getRow(1), getRow(2));
    StatementResult statementResult = new GremlinServerDriver.GremlinServerStatementResult(serverInfo, statement, results.iterator());
    statementResult.single();
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 3 with Statement

use of org.neo4j.driver.v1.Statement in project cypher-for-gremlin by opencypher.

the class GremlinServerStatementResultTest method consume.

@Test
public void consume() {
    List<Map<String, Object>> results = Arrays.asList(getRow(1), getRow(2));
    StatementResult statementResult = new GremlinServerDriver.GremlinServerStatementResult(serverInfo, statement, results.iterator());
    assertThat(statementResult.hasNext()).isTrue();
    assertThat(statementResult.consume().server()).isEqualTo(serverInfo);
    assertThat(statementResult.hasNext()).isFalse();
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 4 with Statement

use of org.neo4j.driver.v1.Statement in project cypher-for-gremlin by opencypher.

the class GremlinServerStatementResultTest method single.

@Test
public void single() {
    List<Map<String, Object>> results = Arrays.asList(getRow(1));
    StatementResult statementResult = new GremlinServerDriver.GremlinServerStatementResult(serverInfo, statement, results.iterator());
    assertThat(statementResult.single().get(KEY1).asInt()).isEqualTo(1);
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 5 with Statement

use of org.neo4j.driver.v1.Statement in project structr by structr.

the class SessionTransaction method getStrings.

public QueryResult<String> getStrings(final String statement, final Map<String, Object> map) {
    final long t0 = System.currentTimeMillis();
    try {
        final StatementResult result = tx.run(statement, map);
        final Record record = result.next();
        final Value value = record.get(0);
        return new QueryResult<String>() {

            @Override
            public void close() {
                result.consume();
            }

            @Override
            public Iterator<String> iterator() {
                return value.asList(Values.ofString()).iterator();
            }
        };
    } catch (TransientException tex) {
        closed = true;
        throw new RetryException(tex);
    } catch (NoSuchRecordException nex) {
        throw new NotFoundException(nex);
    } catch (ServiceUnavailableException ex) {
        throw new NetworkException(ex.getMessage(), ex);
    } finally {
        logQuery(statement, map, t0);
    }
}
Also used : StatementResult(org.neo4j.driver.v1.StatementResult) QueryResult(org.structr.api.QueryResult) TransientException(org.neo4j.driver.v1.exceptions.TransientException) Value(org.neo4j.driver.v1.Value) NotFoundException(org.structr.api.NotFoundException) Record(org.neo4j.driver.v1.Record) ServiceUnavailableException(org.neo4j.driver.v1.exceptions.ServiceUnavailableException) RetryException(org.structr.api.RetryException) NetworkException(org.structr.api.NetworkException) NoSuchRecordException(org.neo4j.driver.v1.exceptions.NoSuchRecordException)

Aggregations

StatementResult (org.neo4j.driver.v1.StatementResult)8 Test (org.junit.Test)6 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Record (org.neo4j.driver.v1.Record)2 NoSuchRecordException (org.neo4j.driver.v1.exceptions.NoSuchRecordException)2 ArrayList (java.util.ArrayList)1 Session (org.neo4j.driver.v1.Session)1 Statement (org.neo4j.driver.v1.Statement)1 Value (org.neo4j.driver.v1.Value)1 ServiceUnavailableException (org.neo4j.driver.v1.exceptions.ServiceUnavailableException)1 TransientException (org.neo4j.driver.v1.exceptions.TransientException)1 Relationship (org.neo4j.driver.v1.types.Relationship)1 PathInfoData (org.openkilda.messaging.info.event.PathInfoData)1 PathNode (org.openkilda.messaging.info.event.PathNode)1 ImmutablePair (org.openkilda.messaging.model.ImmutablePair)1 NetworkException (org.structr.api.NetworkException)1 NotFoundException (org.structr.api.NotFoundException)1 QueryResult (org.structr.api.QueryResult)1 RetryException (org.structr.api.RetryException)1