Asynchronous API
TCStore provides an asynchronous API based around the Java 9 CompletionStage API.
AsyncDatasetWriterReader<String> asyncAccess = counterAccess.async(); // <1>
Operation<Boolean> addOp = asyncAccess.add("counter10", counterCell.newCell(10L)); // <2>
Operation<Optional<Record<String>>> getOp =
addOp.thenCompose((b) -> asyncAccess.get("counter10")); // <3>
Operation<Void> acceptOp = getOp.thenAccept(or -> or.ifPresent( // <4>
r -> out.println("The record with key " + r.getKey() + " was added")));
try {
acceptOp.get(); // <5>
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
1 | The asynchronous API is accessed through the async() method on an existing reader or writer-reader. |
2 | Methods then create asynchronous executions represented by Operation instances. |
3 | Operations can be composed with other operations using the usual CompletionStage methods. |
4 | Operations can also be composed with synchronous operations still yielding Operation instances. |
5 | Operation also extends Future for easy interoperability with synchronous code. |
The current asynchronous implementation is a simple thread-pool based skin over the synchronous API. It is not currently interacting optimally with the asynchronous nature of the underlying Terracotta platform.