Search in sources :

Example 1 with SQLOperations

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());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Json(io.vertx.core.json.Json) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) LoggerFactory(org.slf4j.LoggerFactory) AbstractStore(org.eclipse.hono.service.base.jdbc.store.AbstractStore) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) ExpandedStatement(org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement) SpanContext(io.opentracing.SpanContext) List(java.util.List) JDBCClient(io.vertx.ext.jdbc.JDBCClient) TrustedCertificateAuthority(org.eclipse.hono.service.management.tenant.TrustedCertificateAuthority) Map(java.util.Map) ResultSet(io.vertx.ext.sql.ResultSet) Optional(java.util.Optional) Span(io.opentracing.Span) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) SQLOperations(io.vertx.ext.sql.SQLOperations) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Optional(java.util.Optional) Span(io.opentracing.Span)

Aggregations

Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Tracer (io.opentracing.Tracer)1 Future (io.vertx.core.Future)1 Json (io.vertx.core.json.Json)1 JsonObject (io.vertx.core.json.JsonObject)1 JDBCClient (io.vertx.ext.jdbc.JDBCClient)1 ResultSet (io.vertx.ext.sql.ResultSet)1 SQLOperations (io.vertx.ext.sql.SQLOperations)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Collectors (java.util.stream.Collectors)1 AbstractStore (org.eclipse.hono.service.base.jdbc.store.AbstractStore)1 SQL (org.eclipse.hono.service.base.jdbc.store.SQL)1 Statement (org.eclipse.hono.service.base.jdbc.store.Statement)1 ExpandedStatement (org.eclipse.hono.service.base.jdbc.store.Statement.ExpandedStatement)1 StatementConfiguration (org.eclipse.hono.service.base.jdbc.store.StatementConfiguration)1 Tenant (org.eclipse.hono.service.management.tenant.Tenant)1 TrustedCertificateAuthority (org.eclipse.hono.service.management.tenant.TrustedCertificateAuthority)1