Search in sources :

Example 1 with SQL

use of org.eclipse.hono.service.base.jdbc.store.SQL in project hono by eclipse.

the class ClasspathSchemaCreator method runScript.

private Future<Void> runScript(final JdbcProperties jdbcProperties, final String script, final SpanContext ctx) {
    final JDBCClient jdbcClient = JdbcProperties.dataSource(vertx, jdbcProperties);
    final Promise<Void> clientCloseTracker = Promise.promise();
    SQL.runTransactionally(jdbcClient, tracer, ctx, (connection, context) -> {
        final var expanded = Statement.statement(script).expand();
        log.debug("Creating database schema in [{}] with script: {}", jdbcProperties.getUrl(), expanded);
        return expanded.query(jdbcClient).recover(SQL::translateException);
    }).onComplete(ar -> jdbcClient.close(clientCloseTracker));
    return clientCloseTracker.future();
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) JdbcProperties(org.eclipse.hono.service.base.jdbc.config.JdbcProperties) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) Vertx(io.vertx.core.Vertx) Future(io.vertx.core.Future) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) JDBCClient(io.vertx.ext.jdbc.JDBCClient) Buffer(io.vertx.core.buffer.Buffer) SchemaCreator(org.eclipse.hono.deviceregistry.jdbc.SchemaCreator) Span(io.opentracing.Span) TracingHelper(org.eclipse.hono.tracing.TracingHelper) JDBCClient(io.vertx.ext.jdbc.JDBCClient)

Example 2 with SQL

use of org.eclipse.hono.service.base.jdbc.store.SQL in project hono by eclipse.

the class TableManagementStore method deleteDevice.

/**
 * Delete a single device.
 * <p>
 * This will execute the {@code delete} or {@code deleteVersioned} SQL statement and provide
 * the named parameters {@code tenant_id}, {@code device_id}, and {@code expected_version} (if set).
 * It will return the plain update result of the operation.
 *
 * @param key The key of the device to delete.
 * @param resourceVersion An optional resource version.
 * @param spanContext The span to contribute to.
 * @return A future, tracking the outcome of the operation.
 */
public Future<UpdateResult> deleteDevice(final DeviceKey key, final Optional<String> resourceVersion, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "delete device", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, key.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, key.getDeviceId()).start();
    resourceVersion.ifPresent(version -> span.setTag("version", version));
    final Statement statement;
    if (resourceVersion.isPresent()) {
        statement = this.deleteVersionedStatement;
    } else {
        statement = this.deleteStatement;
    }
    final var expanded = statement.expand(map -> {
        map.put("tenant_id", key.getTenantId());
        map.put("device_id", key.getDeviceId());
        resourceVersion.ifPresent(version -> map.put("expected_version", version));
    });
    log.debug("delete - statement: {}", expanded);
    final var result = expanded.trace(this.tracer, span.context()).update(this.client);
    return checkOptimisticLock(result, span, resourceVersion, checkSpan -> readDevice(this.client, key, checkSpan)).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Json(io.vertx.core.json.Json) JdbcBasedDeviceDto(org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto) LoggerFactory(org.slf4j.LoggerFactory) Supplier(java.util.function.Supplier) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) Map(java.util.Map) Fields(io.opentracing.log.Fields) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Timestamp(java.sql.Timestamp) Promise(io.vertx.core.Promise) Set(java.util.Set) DeviceKey(org.eclipse.hono.deviceregistry.service.device.DeviceKey) OptimisticLockingException(org.eclipse.hono.service.base.jdbc.store.OptimisticLockingException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) JDBCClient(io.vertx.ext.jdbc.JDBCClient) CredentialsDto(org.eclipse.hono.service.management.credentials.CredentialsDto) UpdateResult(io.vertx.ext.sql.UpdateResult) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) ResultSet(io.vertx.ext.sql.ResultSet) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Collections(java.util.Collections) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) Span(io.opentracing.Span)

Example 3 with SQL

use of org.eclipse.hono.service.base.jdbc.store.SQL in project hono by eclipse.

the class TableManagementStore method createDevice.

/**
 * Creates a new device.
 * <p>
 * This method executes the {@code create} statement, providing the named parameters
 * {@code tenant_id}, {@code device_id}, {@code version}, and {@code data}.
 * <p>
 * It returns the plain update result. In case a device with the same ID already
 * exists, the underlying database must throw an {@link java.sql.SQLException}, indicating
 * a duplicate entity or constraint violation. This will be translated into a
 * failed future with an {@link org.eclipse.hono.service.base.jdbc.store.DuplicateKeyException}.
 *
 * @param key The key of the device to create.
 * @param device The device data.
 * @param tenant The configuration of the tenant that the device belongs to.
 * @param globalDevicesPerTenantLimit The globally defined maximum number of devices per tenant. A value
 *                                    &lt;= 0 will be interpreted as no limit being defined.
 * @param spanContext The span to contribute to.
 * @return A future, tracking the outcome of the operation.
 */
public Future<Versioned<Void>> createDevice(final DeviceKey key, final Device device, final Tenant tenant, final int globalDevicesPerTenantLimit, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "create device", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, key.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, key.getDeviceId()).start();
    final JdbcBasedDeviceDto deviceDto = JdbcBasedDeviceDto.forCreation(key, device, DeviceRegistryUtils.getUniqueIdentifier());
    return SQL.runTransactionally(this.client, this.tracer, span.context(), (connection, context) -> {
        final var expanded = this.createStatement.expand(params -> {
            params.put("tenant_id", deviceDto.getTenantId());
            params.put("device_id", deviceDto.getDeviceId());
            params.put("version", deviceDto.getVersion());
            params.put("data", deviceDto.getDeviceJson());
            params.put("created", Timestamp.from(deviceDto.getCreationTime()));
            params.put("auto_provisioned", deviceDto.isAutoProvisioned());
        });
        log.debug("createDevice - statement: {}", expanded);
        return getDeviceCount(key.getTenantId(), span.context()).compose(currentDeviceCount -> tenant.checkDeviceLimitReached(key.getTenantId(), currentDeviceCount, globalDevicesPerTenantLimit)).compose(ok -> expanded.trace(this.tracer, context).update(this.client).recover(SQL::translateException)).compose(x -> createGroups(connection, key, new HashSet<>(device.getMemberOf()), context));
    }).map(new Versioned<Void>(deviceDto.getVersion(), null)).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Json(io.vertx.core.json.Json) JdbcBasedDeviceDto(org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto) LoggerFactory(org.slf4j.LoggerFactory) Supplier(java.util.function.Supplier) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) Map(java.util.Map) Fields(io.opentracing.log.Fields) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Timestamp(java.sql.Timestamp) Promise(io.vertx.core.Promise) Set(java.util.Set) DeviceKey(org.eclipse.hono.deviceregistry.service.device.DeviceKey) OptimisticLockingException(org.eclipse.hono.service.base.jdbc.store.OptimisticLockingException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) JDBCClient(io.vertx.ext.jdbc.JDBCClient) CredentialsDto(org.eclipse.hono.service.management.credentials.CredentialsDto) UpdateResult(io.vertx.ext.sql.UpdateResult) EntityNotFoundException(org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException) ResultSet(io.vertx.ext.sql.ResultSet) SQLConnection(io.vertx.ext.sql.SQLConnection) Optional(java.util.Optional) Span(io.opentracing.Span) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Collections(java.util.Collections) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Versioned(org.eclipse.hono.deviceregistry.util.Versioned) JdbcBasedDeviceDto(org.eclipse.hono.service.base.jdbc.store.model.JdbcBasedDeviceDto) Span(io.opentracing.Span) SQL(org.eclipse.hono.service.base.jdbc.store.SQL) HashSet(java.util.HashSet)

Aggregations

Span (io.opentracing.Span)3 SpanContext (io.opentracing.SpanContext)3 Tracer (io.opentracing.Tracer)3 Future (io.vertx.core.Future)3 Promise (io.vertx.core.Promise)3 JDBCClient (io.vertx.ext.jdbc.JDBCClient)3 Objects (java.util.Objects)3 SQL (org.eclipse.hono.service.base.jdbc.store.SQL)3 Statement (org.eclipse.hono.service.base.jdbc.store.Statement)3 TracingHelper (org.eclipse.hono.tracing.TracingHelper)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 Fields (io.opentracing.log.Fields)2 CompositeFuture (io.vertx.core.CompositeFuture)2 Json (io.vertx.core.json.Json)2 JsonObject (io.vertx.core.json.JsonObject)2 ResultSet (io.vertx.ext.sql.ResultSet)2 SQLConnection (io.vertx.ext.sql.SQLConnection)2 UpdateResult (io.vertx.ext.sql.UpdateResult)2 Timestamp (java.sql.Timestamp)2