Search in sources :

Example 6 with Expression

use of org.apache.ratis.examples.arithmetic.expression.Expression in project incubator-ratis by apache.

the class ArithmeticStateMachine method query.

@Override
public CompletableFuture<Message> query(Message request) {
    final Expression q = Expression.Utils.bytes2Expression(request.getContent().toByteArray(), 0);
    final Double result;
    try (AutoCloseableLock readLock = readLock()) {
        result = q.evaluate(variables);
    }
    final Expression r = Expression.Utils.double2Expression(result);
    LOG.debug("QUERY: {} = {}", q, r);
    return CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
}
Also used : Expression(org.apache.ratis.examples.arithmetic.expression.Expression) AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock)

Example 7 with Expression

use of org.apache.ratis.examples.arithmetic.expression.Expression in project incubator-ratis by apache.

the class Get method operation.

@Override
protected void operation(RaftClient client) throws IOException {
    RaftClientReply getValue = client.io().sendReadOnly(Expression.Utils.toMessage(new Variable(name)));
    Expression response = Expression.Utils.bytes2Expression(getValue.getMessage().getContent().toByteArray(), 0);
    System.out.println(String.format("%s=%s", name, (DoubleValue) response).toString());
}
Also used : Variable(org.apache.ratis.examples.arithmetic.expression.Variable) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Expression(org.apache.ratis.examples.arithmetic.expression.Expression)

Aggregations

Expression (org.apache.ratis.examples.arithmetic.expression.Expression)7 Variable (org.apache.ratis.examples.arithmetic.expression.Variable)3 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)2 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)1 RaftPeerRole (org.apache.ratis.proto.RaftProtos.RaftPeerRole)1 Message (org.apache.ratis.protocol.Message)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1