use of io.vertx.ext.sql.SQLOperations in project hono by eclipse.
the class AbstractTenantStore method readTenantBy.
/**
* Read a tenant, using the provided statement.
*
* @param operations The operations to use.
* @param expanded The statement to use.
* @param spanContext The span to contribute to.
* @return A future, tracking the outcome of the operation.
*/
protected Future<Optional<TenantReadResult>> readTenantBy(final SQLOperations operations, final ExpandedStatement expanded, final SpanContext spanContext) {
final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "read tenant by", getClass().getSimpleName()).start();
return expanded.trace(this.tracer, span.context()).query(operations).<Optional<TenantReadResult>>flatMap(r -> {
final var entries = r.getRows(true);
span.log(Map.of("event", "read result", "rows", entries.size()));
switch(entries.size()) {
case 0:
return Future.succeededFuture(Optional.empty());
case 1:
return Future.succeededFuture(entries.get(0)).map(entry -> {
final var id = entry.getString("tenant_id");
final var tenant = Json.decodeValue(entry.getString("data"), Tenant.class);
final var version = Optional.ofNullable(entry.getString("version"));
return Optional.of(new TenantReadResult(id, tenant, version));
});
default:
return Future.failedFuture(new IllegalStateException("Found multiple entries for a single tenant"));
}
}).flatMap(result -> {
if (result.isPresent()) {
return fillTrustAnchors(operations, result.get(), span.context()).map(Optional::ofNullable);
} else {
return Future.succeededFuture(result);
}
}).onComplete(x -> span.finish());
}
Aggregations