use of org.eclipse.hono.service.base.jdbc.store.Statement in project hono by eclipse.
the class AbstractTenantStore method readTenant.
/**
* Read the content of a tenant.
* <p>
* This will execute the {@code read} statement and return the unprocessed information.
* <p>
* If there are no entries, an empty result will be returned.
* <p>
* If more than one entry is being found, the result will be failed with an
* {@link IllegalStateException} exception.
*
* @param id The key to the tenant entry.
* @param spanContext The span to contribute to.
* @return The future, tracking the outcome of the operation.
*/
public Future<Optional<TenantReadResult>> readTenant(final String id, final SpanContext spanContext) {
final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "read tenant", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, id).start();
final var expanded = this.readStatement.expand(map -> {
map.put("tenant_id", id);
});
return SQL.runTransactionally(this.client, this.tracer, span.context(), (connection, context) -> readTenantBy(connection, expanded, context)).onComplete(x -> span.finish());
}
use of org.eclipse.hono.service.base.jdbc.store.Statement in project hono by eclipse.
the class ManagementStore method delete.
/**
* Delete the tenant.
*
* @param tenantId The tenant to delete.
* @param resourceVersion The version of the resource to delete.
* @param spanContext The span to contribute to.
* @return The future, tracking the outcome of the operation.
*/
public Future<UpdateResult> delete(final String tenantId, final Optional<String> resourceVersion, final SpanContext spanContext) {
final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "delete tenant", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, tenantId).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", tenantId);
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 -> readTenantEntryById(this.client, tenantId, checkSpan.context())).onComplete(x -> span.finish());
}
use of org.eclipse.hono.service.base.jdbc.store.Statement in project hono by eclipse.
the class ManagementStore method update.
/**
* Create a new tenant.
* <p>
* The operation may fail with a {@link org.eclipse.hono.service.base.jdbc.store.EntityNotFoundException} if the
* specified tenant does not exist.
* <p>
* The operation may fail with a {@link org.eclipse.hono.service.base.jdbc.store.DuplicateKeyException} if a
* tenant with the ID or trust anchor already exists.
* <p>
* The operation may fail with an {@link org.eclipse.hono.service.base.jdbc.store.OptimisticLockingException} if
* an expected resource version was provided, but the current version did not match.
*
* @param tenantId The ID of the new tenant.
* @param tenant The tenant information.
* @param resourceVersion An optional resource version.
* @param spanContext The span to contribute to.
* @return A future, tracking the outcome of the operation.
*/
public Future<Versioned<Void>> update(final String tenantId, final Tenant tenant, final Optional<String> resourceVersion, final SpanContext spanContext) {
final var json = tenantToJson(tenant);
final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "update tenant", getClass().getSimpleName()).withTag(TracingHelper.TAG_TENANT_ID, tenantId).start();
final var nextVersion = UUID.randomUUID().toString();
resourceVersion.ifPresent(version -> span.setTag("version", version));
final Statement statement = resourceVersion.isPresent() ? this.updateVersionedStatement : this.updateStatement;
return SQL.runTransactionally(this.client, this.tracer, span.context(), (connection, context) -> updateJsonField(connection, tenantId, statement, json, resourceVersion, nextVersion, span).flatMap(r -> {
if (r.getUpdated() <= 0) {
return Future.failedFuture(new EntityNotFoundException());
} else {
return Future.succeededFuture();
}
}).flatMap(x -> deleteAllTrustAnchors(connection, tenantId, span)).flatMap(r -> insertAllTrustAnchors(connection, tenantId, tenant, span))).map(new Versioned<Void>(nextVersion, null)).onComplete(x -> span.finish());
}
use of org.eclipse.hono.service.base.jdbc.store.Statement in project hono by eclipse.
the class TableManagementStore method updateDevice.
/**
* Update device registration information.
* <p>
* This called the {@link #updateJsonField(DeviceKey, Statement, String, Optional, String, Span)} method
* with either the {@code updateRegistration} or {@code updateRegistrationVersioned}
* statement.
*
* @param key The key of the device to update.
* @param device The device data to store.
* @param resourceVersion The optional resource version.
* @param spanContext The span to contribute to.
* @return A future, tracking the outcome of the operation.
*/
public Future<Versioned<Void>> updateDevice(final DeviceKey key, final Device device, final Optional<String> resourceVersion, final SpanContext spanContext) {
final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "update 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 var memberOf = Optional.ofNullable(device.getMemberOf()).<Set<String>>map(HashSet::new).orElse(Collections.emptySet());
final JdbcBasedDeviceDto deviceDto = JdbcBasedDeviceDto.forUpdate(key, device, DeviceRegistryUtils.getUniqueIdentifier());
return SQL.runTransactionally(this.client, this.tracer, span.context(), (connection, context) -> readDeviceForUpdate(connection, key, context).compose(result -> extractVersionForUpdate(result, resourceVersion)).compose(version -> deleteGroups(connection, key, context).map(version)).compose(version -> createGroups(connection, key, memberOf, context).map(version)).compose(version -> this.updateRegistrationVersionedStatement.expand(map -> {
map.put("tenant_id", deviceDto.getTenantId());
map.put("device_id", deviceDto.getDeviceId());
map.put("data", deviceDto.getDeviceJson());
map.put("expected_version", version);
map.put("next_version", deviceDto.getVersion());
map.put("updated_on", Timestamp.from(deviceDto.getUpdatedOn()));
map.put("auto_provisioning_notification_sent", deviceDto.isAutoProvisioningNotificationSent());
}).trace(this.tracer, span.context()).update(connection).compose(TableManagementStore::checkUpdateOutcome).map(version))).map(x -> new Versioned<Void>(deviceDto.getVersion(), null)).onComplete(x -> span.finish());
}
use of org.eclipse.hono.service.base.jdbc.store.Statement 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());
}
Aggregations