feat(s3tables): emulate metadata service (#2322)

* feat(s3tables): emulate metadata service

* fix(s3tables): serialize metadata updates
This commit is contained in:
Dixit R Jain authored and GitHub committed 2026-08-15 12:48:27 -04:00
1 parent b71bfc46b6
commit b45de1bb7d
20 files changed
+1796 -2

No files matched your search

@@ -272,6 +272,7 @@ public interface EmulatorConfig {
LightsailStorageConfig lightsail();
CodePipelineStorageConfig codepipeline();
S3VectorsStorageConfig s3vectors();
S3TablesStorageConfig s3tables();
EcsStorageConfig ecs();
CodeBuildStorageConfig codebuild();
ConfigStorageConfig config();
@@ -440,6 +441,13 @@ public interface EmulatorConfig {
long flushIntervalMs();
}
interface S3TablesStorageConfig {
Optional<String> mode();
@WithDefault("5000")
long flushIntervalMs();
}
interface EcsStorageConfig {
Optional<String> mode();
@@ -595,6 +603,7 @@ public interface EmulatorConfig {
LightsailServiceConfig lightsail();
UiServiceConfig ui();
S3VectorsServiceConfig s3vectors();
S3TablesServiceConfig s3tables();
IotServiceConfig iot();
IotDataServiceConfig iotdata();
RumServiceConfig rum();
@@ -645,6 +654,11 @@ public interface EmulatorConfig {
boolean enabled();
}
interface S3TablesServiceConfig {
@WithDefault("true")
boolean enabled();
}
interface TransferServiceConfig {
@WithDefault("true")
boolean enabled();
@@ -25,6 +25,7 @@ import io.github.hectorvent.floci.services.appsync.AppSyncController;
import io.github.hectorvent.floci.services.rdsdata.RdsDataController;
import io.github.hectorvent.floci.services.rum.RumController;
import io.github.hectorvent.floci.services.s3vectors.S3VectorsController;
import io.github.hectorvent.floci.services.s3tables.S3TablesController;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -417,6 +418,11 @@ public class ResolvedServiceCatalog {
config.storage().services().s3vectors().flushIntervalMs(), null, ServiceProtocol.REST_JSON,
protocols(ServiceProtocol.REST_JSON),
Set.of(), Set.of("s3vectors"), Set.of(), Set.of(S3VectorsController.class)),
descriptor("s3tables", "s3tables", config.services().s3tables().enabled(), true,
"s3tables", storageMode(config.storage().services().s3tables().mode(), config.storage().mode()),
config.storage().services().s3tables().flushIntervalMs(), null, ServiceProtocol.REST_JSON,
protocols(ServiceProtocol.REST_JSON),
Set.of(), Set.of("s3tables"), Set.of(), Set.of(S3TablesController.class)),
descriptor("iot", "iot", config.services().iot().enabled(), true,
"iot", config.storage().mode(), 5000L, null, ServiceProtocol.REST_JSON,
protocols(ServiceProtocol.REST_JSON),
@@ -0,0 +1,325 @@
package io.github.hectorvent.floci.services.s3tables;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.services.s3tables.model.Namespace;
import io.github.hectorvent.floci.services.s3tables.model.S3Table;
import io.github.hectorvent.floci.services.s3tables.model.TableBucket;
import jakarta.inject.Inject;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.Encoded;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class S3TablesController {
private final S3TablesService service;
private final RegionResolver regionResolver;
@Inject
public S3TablesController(S3TablesService service, RegionResolver regionResolver) {
this.service = service;
this.regionResolver = regionResolver;
}
@PUT
@Path("/buckets")
public Response createTableBucket(@Context HttpHeaders headers, Map<String, Object> request) {
TableBucket bucket = service.createTableBucket(string(request, "name"), request.get("encryptionConfiguration"),
request.get("storageClassConfiguration"), stringMap(request.get("tags")), region(headers));
return Response.ok(Map.of("arn", bucket.getArn())).build();
}
@GET
@Path("/buckets")
public Response listTableBuckets(@Context HttpHeaders headers, @QueryParam("prefix") String prefix) {
List<Map<String, Object>> buckets = service.listTableBuckets(prefix, region(headers)).stream()
.map(this::bucketRepresentation).toList();
return Response.ok(Map.of("tableBuckets", buckets)).build();
}
@GET
@Path("/buckets/{tableBucketARN}")
public Response getTableBucket(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn) {
return Response.ok(bucketRepresentation(service.getTableBucket(decode(tableBucketArn), region(headers)))).build();
}
@DELETE
@Path("/buckets/{tableBucketARN}")
public Response deleteTableBucket(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn) {
service.deleteTableBucket(decode(tableBucketArn), region(headers));
return Response.noContent().build();
}
@PUT
@Path("/buckets/{tableBucketARN}/policy")
public Response putTableBucketPolicy(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
Map<String, Object> request) {
service.putTableBucketPolicy(decode(tableBucketArn), string(request, "resourcePolicy"), region(headers));
return Response.ok().build();
}
@GET
@Path("/buckets/{tableBucketARN}/policy")
public Response getTableBucketPolicy(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn) {
return Response.ok(Map.of("resourcePolicy", service.getTableBucketPolicy(decode(tableBucketArn), region(headers)))).build();
}
@DELETE
@Path("/buckets/{tableBucketARN}/policy")
public Response deleteTableBucketPolicy(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn) {
service.deleteTableBucketPolicy(decode(tableBucketArn), region(headers));
return Response.noContent().build();
}
@PUT
@Path("/buckets/{tableBucketARN}/maintenance/{type}")
public Response putTableBucketMaintenanceConfiguration(@Context HttpHeaders headers,
@Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("type") String type, Map<String, Object> request) {
service.putTableBucketMaintenance(decode(tableBucketArn), type, request.get("value"), region(headers));
return Response.ok().build();
}
@GET
@Path("/buckets/{tableBucketARN}/maintenance")
public Response getTableBucketMaintenanceConfiguration(@Context HttpHeaders headers,
@Encoded @PathParam("tableBucketARN") String tableBucketArn) {
String arn = decode(tableBucketArn);
return Response.ok(Map.of("tableBucketARN", arn,
"configuration", service.getTableBucketMaintenanceConfigurations(arn, region(headers)))).build();
}
@PUT
@Path("/namespaces/{tableBucketARN}")
public Response createNamespace(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
Map<String, Object> request) {
String arn = decode(tableBucketArn);
Namespace namespace = service.createNamespace(arn, stringList(request.get("namespace")), region(headers));
return Response.ok(namespaceRepresentation(arn, namespace)).build();
}
@GET
@Path("/namespaces/{tableBucketARN}")
public Response listNamespaces(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@QueryParam("prefix") String prefix) {
String arn = decode(tableBucketArn);
List<Map<String, Object>> namespaces = service.listNamespaces(arn, prefix, region(headers)).stream()
.map(namespace -> namespaceRepresentation(arn, namespace)).toList();
return Response.ok(Map.of("namespaces", namespaces)).build();
}
@GET
@Path("/namespaces/{tableBucketARN}/{namespace}")
public Response getNamespace(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace) {
String arn = decode(tableBucketArn);
return Response.ok(namespaceRepresentation(arn, service.getNamespace(arn, namespace, region(headers)))).build();
}
@DELETE
@Path("/namespaces/{tableBucketARN}/{namespace}")
public Response deleteNamespace(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace) {
service.deleteNamespace(decode(tableBucketArn), namespace, region(headers));
return Response.noContent().build();
}
@PUT
@Path("/tables/{tableBucketARN}/{namespace}")
public Response createTable(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, Map<String, Object> request) {
S3Table table = service.createTable(decode(tableBucketArn), namespace, string(request, "name"), string(request, "format"),
request.get("metadata"), request.get("encryptionConfiguration"), request.get("storageClassConfiguration"),
stringMap(request.get("tags")), region(headers));
return Response.ok(Map.of("tableARN", table.getArn(), "versionToken", table.getVersionToken())).build();
}
@GET
@Path("/get-table")
public Response getTable(@Context HttpHeaders headers, @QueryParam("tableBucketARN") String tableBucketArn,
@QueryParam("namespace") String namespace, @QueryParam("name") String name) {
return Response.ok(tableRepresentation(service.getTable(decode(tableBucketArn), namespace, name, region(headers)))).build();
}
@GET
@Path("/tables/{tableBucketARN}")
public Response listTables(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@QueryParam("namespace") String namespace, @QueryParam("prefix") String prefix) {
List<Map<String, Object>> tables = service.listTables(decode(tableBucketArn), namespace, prefix, region(headers)).stream()
.map(this::tableRepresentation).toList();
return Response.ok(Map.of("tables", tables)).build();
}
@DELETE
@Path("/tables/{tableBucketARN}/{namespace}/{name}")
public Response deleteTable(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, @PathParam("name") String name) {
service.deleteTable(decode(tableBucketArn), namespace, name, region(headers));
return Response.noContent().build();
}
@PUT
@Path("/tables/{tableBucketARN}/{namespace}/{name}/rename")
public Response renameTable(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, @PathParam("name") String name,
Map<String, Object> request) {
service.renameTable(decode(tableBucketArn), namespace, name, string(request, "newNamespaceName"),
string(request, "newName"), string(request, "versionToken"), region(headers));
return Response.noContent().build();
}
@GET
@Path("/tables/{tableBucketARN}/{namespace}/{name}/metadata-location")
public Response getTableMetadataLocation(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, @PathParam("name") String name) {
S3Table table = service.getTable(decode(tableBucketArn), namespace, name, region(headers));
return Response.ok(metadataLocationRepresentation(table)).build();
}
@PUT
@Path("/tables/{tableBucketARN}/{namespace}/{name}/metadata-location")
public Response updateTableMetadataLocation(@Context HttpHeaders headers,
@Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, @PathParam("name") String name,
Map<String, Object> request) {
S3Table table = service.updateTableMetadataLocation(decode(tableBucketArn), namespace, name,
string(request, "metadataLocation"), string(request, "versionToken"), region(headers));
Map<String, Object> response = metadataLocationRepresentation(table);
response.put("name", table.getName());
response.put("namespace", List.of(table.getNamespace()));
response.put("tableARN", table.getArn());
return Response.ok(response).build();
}
@PUT
@Path("/tables/{tableBucketARN}/{namespace}/{name}/policy")
public Response putTablePolicy(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, @PathParam("name") String name,
Map<String, Object> request) {
service.putTablePolicy(decode(tableBucketArn), namespace, name, string(request, "resourcePolicy"), region(headers));
return Response.ok().build();
}
@GET
@Path("/tables/{tableBucketARN}/{namespace}/{name}/policy")
public Response getTablePolicy(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, @PathParam("name") String name) {
return Response.ok(Map.of("resourcePolicy", service.getTablePolicy(decode(tableBucketArn), namespace, name, region(headers)))).build();
}
@DELETE
@Path("/tables/{tableBucketARN}/{namespace}/{name}/policy")
public Response deleteTablePolicy(@Context HttpHeaders headers, @Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, @PathParam("name") String name) {
service.deleteTablePolicy(decode(tableBucketArn), namespace, name, region(headers));
return Response.noContent().build();
}
@PUT
@Path("/tables/{tableBucketARN}/{namespace}/{name}/maintenance/{type}")
public Response putTableMaintenanceConfiguration(@Context HttpHeaders headers,
@Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, @PathParam("name") String name,
@PathParam("type") String type, Map<String, Object> request) {
service.putTableMaintenance(decode(tableBucketArn), namespace, name, type, request.get("value"), region(headers));
return Response.ok().build();
}
@GET
@Path("/tables/{tableBucketARN}/{namespace}/{name}/maintenance")
public Response getTableMaintenanceConfiguration(@Context HttpHeaders headers,
@Encoded @PathParam("tableBucketARN") String tableBucketArn,
@PathParam("namespace") String namespace, @PathParam("name") String name) {
String arn = decode(tableBucketArn);
return Response.ok(Map.of("tableARN", service.getTable(arn, namespace, name, region(headers)).getArn(),
"configuration", service.getTableMaintenanceConfigurations(arn, namespace, name, region(headers)))).build();
}
private Map<String, Object> bucketRepresentation(TableBucket bucket) {
Map<String, Object> response = new LinkedHashMap<>();
response.put("arn", bucket.getArn());
response.put("name", bucket.getName());
response.put("createdAt", bucket.getCreatedAt());
response.put("ownerAccountId", bucket.getOwnerAccountId());
putIfNotNull(response, "encryptionConfiguration", bucket.getEncryptionConfiguration());
putIfNotNull(response, "storageClassConfiguration", bucket.getStorageClassConfiguration());
return response;
}
private Map<String, Object> namespaceRepresentation(String tableBucketArn, Namespace namespace) {
Map<String, Object> response = new LinkedHashMap<>();
response.put("namespace", List.of(namespace.getName()));
response.put("tableBucketARN", tableBucketArn);
response.put("createdAt", namespace.getCreatedAt());
return response;
}
private Map<String, Object> tableRepresentation(S3Table table) {
Map<String, Object> response = new LinkedHashMap<>();
response.put("name", table.getName());
response.put("type", "customer");
response.put("tableARN", table.getArn());
response.put("namespace", List.of(table.getNamespace()));
response.put("versionToken", table.getVersionToken());
putIfNotNull(response, "metadataLocation", table.getMetadataLocation());
response.put("createdAt", table.getCreatedAt());
response.put("modifiedAt", table.getModifiedAt());
response.put("ownerAccountId", table.getOwnerAccountId());
response.put("format", table.getFormat());
return response;
}
private Map<String, Object> metadataLocationRepresentation(S3Table table) {
Map<String, Object> response = new LinkedHashMap<>();
putIfNotNull(response, "metadataLocation", table.getMetadataLocation());
response.put("versionToken", table.getVersionToken());
return response;
}
private void putIfNotNull(Map<String, Object> target, String key, Object value) {
if (value != null) {
target.put(key, value);
}
}
private String region(HttpHeaders headers) { return regionResolver.resolveRegion(headers); }
private String decode(String value) { return value == null ? null : URLDecoder.decode(value, StandardCharsets.UTF_8); }
private String string(Map<String, Object> value, String key) { return value == null ? null : (String) value.get(key); }
@SuppressWarnings("unchecked")
private Map<String, String> stringMap(Object value) {
if (!(value instanceof Map<?, ?> map)) {
return Map.of();
}
Map<String, String> result = new LinkedHashMap<>();
map.forEach((key, entry) -> result.put(String.valueOf(key), String.valueOf(entry)));
return result;
}
private List<String> stringList(Object value) {
if (!(value instanceof List<?> list)) {
return List.of();
}
List<String> result = new ArrayList<>();
list.forEach(entry -> result.add(String.valueOf(entry)));
return result;
}
}
@@ -0,0 +1,375 @@
package io.github.hectorvent.floci.services.s3tables;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.storage.StorageBackend;
import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.s3tables.model.Namespace;
import io.github.hectorvent.floci.services.s3tables.model.S3Table;
import io.github.hectorvent.floci.services.s3tables.model.TableBucket;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ApplicationScoped
public class S3TablesService {
private static final Logger LOG = Logger.getLogger(S3TablesService.class);
private static final String BUCKET_PREFIX = "bucket/";
private final StorageBackend<String, TableBucket> store;
private final RegionResolver regionResolver;
@Inject
public S3TablesService(StorageFactory factory, RegionResolver regionResolver) {
this.store = factory.create("s3tables", "s3tables.json", new TypeReference<Map<String, TableBucket>>() {});
this.regionResolver = regionResolver;
}
public synchronized TableBucket createTableBucket(String name, Object encryptionConfiguration,
Object storageClassConfiguration, Map<String, String> tags, String region) {
validateBucketName(name);
String key = storageKey(region, name);
if (store.get(key).isPresent()) {
throw conflict("The table bucket " + name + " already exists.");
}
TableBucket bucket = new TableBucket(name, bucketArn(region, name), now(), regionResolver.getAccountId(),
encryptionConfiguration, storageClassConfiguration, tags);
store.put(key, bucket);
LOG.infov("Created S3 Tables bucket {0} in region {1}", name, region);
return bucket;
}
public synchronized TableBucket getTableBucket(String tableBucketArn, String region) {
String name = resourceName(tableBucketArn, BUCKET_PREFIX);
TableBucket bucket = store.get(storageKey(region, name))
.orElseThrow(() -> notFound("The table bucket does not exist."));
if (!bucket.getArn().equals(tableBucketArn)) {
throw notFound("The table bucket does not exist.");
}
return bucket;
}
public synchronized List<TableBucket> listTableBuckets(String prefix, String region) {
return store.scan(key -> key.startsWith(region + "::")).stream()
.filter(bucket -> prefix == null || bucket.getName().startsWith(prefix))
.sorted(Comparator.comparing(TableBucket::getName))
.toList();
}
public synchronized void deleteTableBucket(String tableBucketArn, String region) {
TableBucket bucket = getTableBucket(tableBucketArn, region);
if (!bucket.getNamespaces().isEmpty()) {
throw conflict("The table bucket is not empty.");
}
store.delete(storageKey(region, bucket.getName()));
}
public synchronized Namespace createNamespace(String tableBucketArn, List<String> namespace, String region) {
String namespaceName = namespaceName(namespace);
TableBucket bucket = getTableBucket(tableBucketArn, region);
if (bucket.getNamespaces().containsKey(namespaceName)) {
throw conflict("The namespace already exists.");
}
Namespace value = new Namespace(namespaceName, now());
bucket.getNamespaces().put(namespaceName, value);
persist(bucket, region);
return value;
}
public synchronized Namespace getNamespace(String tableBucketArn, String namespaceName, String region) {
return getNamespace(getTableBucket(tableBucketArn, region), namespaceName);
}
public synchronized List<Namespace> listNamespaces(String tableBucketArn, String prefix, String region) {
return getTableBucket(tableBucketArn, region).getNamespaces().values().stream()
.filter(namespace -> prefix == null || namespace.getName().startsWith(prefix))
.sorted(Comparator.comparing(Namespace::getName))
.toList();
}
public synchronized void deleteNamespace(String tableBucketArn, String namespaceName, String region) {
TableBucket bucket = getTableBucket(tableBucketArn, region);
Namespace namespace = getNamespace(bucket, namespaceName);
if (!namespace.getTables().isEmpty()) {
throw conflict("The namespace is not empty.");
}
bucket.getNamespaces().remove(namespaceName);
persist(bucket, region);
}
public synchronized S3Table createTable(String tableBucketArn, String namespaceName, String name, String format,
Object metadata, Object encryptionConfiguration, Object storageClassConfiguration,
Map<String, String> tags, String region) {
requireName(name, "Table name is required.");
if (!"ICEBERG".equals(format)) {
throw badRequest("format must be ICEBERG.");
}
TableBucket bucket = getTableBucket(tableBucketArn, region);
Namespace namespace = getNamespace(bucket, namespaceName);
if (namespace.getTables().containsKey(name)) {
throw conflict("The table already exists.");
}
S3Table table = new S3Table(name, bucket.getArn() + "/table/" + name, namespaceName, format,
versionToken(), metadataLocation(metadata), now(), regionResolver.getAccountId(), metadata,
encryptionConfiguration, storageClassConfiguration, tags);
namespace.getTables().put(name, table);
persist(bucket, region);
return table;
}
public synchronized S3Table getTable(String tableBucketArn, String namespaceName, String name, String region) {
return getTable(getTableBucket(tableBucketArn, region), namespaceName, name);
}
public synchronized List<S3Table> listTables(String tableBucketArn, String namespaceName, String prefix, String region) {
TableBucket bucket = getTableBucket(tableBucketArn, region);
List<S3Table> tables = new ArrayList<>();
if (namespaceName == null) {
bucket.getNamespaces().values().forEach(namespace -> tables.addAll(namespace.getTables().values()));
} else {
tables.addAll(getNamespace(bucket, namespaceName).getTables().values());
}
return tables.stream().filter(table -> prefix == null || table.getName().startsWith(prefix))
.sorted(Comparator.comparing(S3Table::getName)).toList();
}
public synchronized void deleteTable(String tableBucketArn, String namespaceName, String name, String region) {
TableBucket bucket = getTableBucket(tableBucketArn, region);
Namespace namespace = getNamespace(bucket, namespaceName);
getTable(bucket, namespaceName, name);
namespace.getTables().remove(name);
persist(bucket, region);
}
public synchronized S3Table renameTable(String tableBucketArn, String namespaceName, String name, String newNamespaceName,
String newName, String versionToken, String region) {
TableBucket bucket = getTableBucket(tableBucketArn, region);
S3Table table = getTable(bucket, namespaceName, name);
assertCurrentToken(table, versionToken);
String targetNamespaceName = newNamespaceName == null ? namespaceName : newNamespaceName;
String targetName = newName == null ? name : newName;
requireName(targetName, "newName is required.");
Namespace sourceNamespace = getNamespace(bucket, namespaceName);
Namespace targetNamespace = getNamespace(bucket, targetNamespaceName);
if ((!namespaceName.equals(targetNamespaceName) || !name.equals(targetName))
&& targetNamespace.getTables().containsKey(targetName)) {
throw conflict("The target table already exists.");
}
sourceNamespace.getTables().remove(name);
table.setNamespace(targetNamespaceName);
table.setName(targetName);
table.setArn(bucket.getArn() + "/table/" + targetName);
touch(table);
targetNamespace.getTables().put(targetName, table);
persist(bucket, region);
return table;
}
public synchronized S3Table updateTableMetadataLocation(String tableBucketArn, String namespaceName, String name,
String metadataLocation, String versionToken, String region) {
if (metadataLocation == null || metadataLocation.isBlank() || versionToken == null || versionToken.isBlank()) {
throw badRequest("metadataLocation and versionToken are required.");
}
TableBucket bucket = getTableBucket(tableBucketArn, region);
S3Table table = getTable(bucket, namespaceName, name);
assertCurrentToken(table, versionToken);
table.setMetadataLocation(metadataLocation);
touch(table);
persist(bucket, region);
return table;
}
public synchronized void putTableBucketPolicy(String tableBucketArn, String resourcePolicy, String region) {
if (resourcePolicy == null) {
throw badRequest("resourcePolicy is required.");
}
TableBucket bucket = getTableBucket(tableBucketArn, region);
bucket.setResourcePolicy(resourcePolicy);
persist(bucket, region);
}
public synchronized String getTableBucketPolicy(String tableBucketArn, String region) {
String policy = getTableBucket(tableBucketArn, region).getResourcePolicy();
if (policy == null) {
throw notFound("The table bucket policy does not exist.");
}
return policy;
}
public synchronized void deleteTableBucketPolicy(String tableBucketArn, String region) {
TableBucket bucket = getTableBucket(tableBucketArn, region);
if (bucket.getResourcePolicy() == null) {
throw notFound("The table bucket policy does not exist.");
}
bucket.setResourcePolicy(null);
persist(bucket, region);
}
public synchronized void putTableBucketMaintenance(String tableBucketArn, String type, Object value, String region) {
requireName(type, "Maintenance configuration type is required.");
TableBucket bucket = getTableBucket(tableBucketArn, region);
bucket.getMaintenanceConfigurations().put(type, value);
persist(bucket, region);
}
public synchronized Object getTableBucketMaintenance(String tableBucketArn, String type, String region) {
Object value = getTableBucket(tableBucketArn, region).getMaintenanceConfigurations().get(type);
if (value == null) {
throw notFound("The table bucket maintenance configuration does not exist.");
}
return value;
}
public synchronized Map<String, Object> getTableBucketMaintenanceConfigurations(String tableBucketArn, String region) {
return new LinkedHashMap<>(getTableBucket(tableBucketArn, region).getMaintenanceConfigurations());
}
public synchronized void putTablePolicy(String tableBucketArn, String namespaceName, String name, String resourcePolicy, String region) {
if (resourcePolicy == null) {
throw badRequest("resourcePolicy is required.");
}
TableBucket bucket = getTableBucket(tableBucketArn, region);
S3Table table = getTable(bucket, namespaceName, name);
table.setResourcePolicy(resourcePolicy);
persist(bucket, region);
}
public synchronized String getTablePolicy(String tableBucketArn, String namespaceName, String name, String region) {
String policy = getTable(tableBucketArn, namespaceName, name, region).getResourcePolicy();
if (policy == null) {
throw notFound("The table policy does not exist.");
}
return policy;
}
public synchronized void deleteTablePolicy(String tableBucketArn, String namespaceName, String name, String region) {
TableBucket bucket = getTableBucket(tableBucketArn, region);
S3Table table = getTable(bucket, namespaceName, name);
if (table.getResourcePolicy() == null) {
throw notFound("The table policy does not exist.");
}
table.setResourcePolicy(null);
persist(bucket, region);
}
public synchronized void putTableMaintenance(String tableBucketArn, String namespaceName, String name, String type,
Object value, String region) {
requireName(type, "Maintenance configuration type is required.");
TableBucket bucket = getTableBucket(tableBucketArn, region);
S3Table table = getTable(bucket, namespaceName, name);
table.getMaintenanceConfigurations().put(type, value);
persist(bucket, region);
}
public synchronized Object getTableMaintenance(String tableBucketArn, String namespaceName, String name, String type, String region) {
Object value = getTable(tableBucketArn, namespaceName, name, region).getMaintenanceConfigurations().get(type);
if (value == null) {
throw notFound("The table maintenance configuration does not exist.");
}
return value;
}
public synchronized Map<String, Object> getTableMaintenanceConfigurations(String tableBucketArn, String namespaceName,
String name, String region) {
return new LinkedHashMap<>(getTable(tableBucketArn, namespaceName, name, region).getMaintenanceConfigurations());
}
private Namespace getNamespace(TableBucket bucket, String name) {
Namespace namespace = bucket.getNamespaces().get(name);
if (namespace == null) {
throw notFound("The namespace does not exist.");
}
return namespace;
}
private S3Table getTable(TableBucket bucket, String namespaceName, String name) {
S3Table table = getNamespace(bucket, namespaceName).getTables().get(name);
if (table == null) {
throw notFound("The table does not exist.");
}
return table;
}
private String bucketArn(String region, String name) {
return AwsArnUtils.Arn.of("s3tables", region, regionResolver.getAccountId(), BUCKET_PREFIX + name).toString();
}
private String storageKey(String region, String name) {
return region + "::" + name;
}
private String resourceName(String arn, String prefix) {
if (arn == null || !arn.startsWith("arn:") || !arn.contains(":" + prefix)) {
throw notFound("The table bucket does not exist.");
}
return arn.substring(arn.lastIndexOf(prefix) + prefix.length());
}
private void persist(TableBucket bucket, String region) {
store.put(storageKey(region, bucket.getName()), bucket);
}
private String namespaceName(List<String> namespace) {
if (namespace == null || namespace.size() != 1) {
throw badRequest("namespace must contain exactly one name.");
}
requireName(namespace.getFirst(), "Namespace name is required.");
return namespace.getFirst();
}
private void validateBucketName(String name) {
if (name == null || !name.matches("[a-z0-9][a-z0-9-]{1,61}[a-z0-9]")) {
throw badRequest("Table bucket names must be 3-63 lowercase letters, numbers, or hyphens.");
}
}
private void requireName(String value, String message) {
if (value == null || value.isBlank()) {
throw badRequest(message);
}
}
@SuppressWarnings("unchecked")
private String metadataLocation(Object metadata) {
if (metadata instanceof Map<?, ?> map) {
Object direct = map.get("metadataLocation");
if (direct instanceof String value) {
return value;
}
for (Object value : map.values()) {
String nested = metadataLocation(value);
if (nested != null) {
return nested;
}
}
}
return null;
}
private void assertCurrentToken(S3Table table, String suppliedToken) {
if (suppliedToken != null && !suppliedToken.equals(table.getVersionToken())) {
throw conflict("The supplied version token is stale.");
}
}
private void touch(S3Table table) {
table.setVersionToken(versionToken());
table.setModifiedAt(now());
}
private String now() { return Instant.now().toString(); }
private String versionToken() { return UUID.randomUUID().toString(); }
private AwsException badRequest(String message) { return new AwsException("BadRequestException", message, 400); }
private AwsException conflict(String message) { return new AwsException("ConflictException", message, 409); }
private AwsException notFound(String message) { return new AwsException("NotFoundException", message, 404); }
}
@@ -0,0 +1,28 @@
package io.github.hectorvent.floci.services.s3tables.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.LinkedHashMap;
import java.util.Map;
@RegisterForReflection
public class Namespace {
private String name;
private String createdAt;
private Map<String, S3Table> tables = new LinkedHashMap<>();
public Namespace() {
}
public Namespace(String name, String createdAt) {
this.name = name;
this.createdAt = createdAt;
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getCreatedAt() { return createdAt; }
public void setCreatedAt(String createdAt) { this.createdAt = createdAt; }
public Map<String, S3Table> getTables() { return tables; }
public void setTables(Map<String, S3Table> tables) { this.tables = tables == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tables); }
}
@@ -0,0 +1,79 @@
package io.github.hectorvent.floci.services.s3tables.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.LinkedHashMap;
import java.util.Map;
@RegisterForReflection
public class S3Table {
private String name;
private String arn;
private String namespace;
private String format;
private String versionToken;
private String metadataLocation;
private String createdAt;
private String modifiedAt;
private String ownerAccountId;
private Object metadata;
private Object encryptionConfiguration;
private Object storageClassConfiguration;
private Map<String, String> tags = new LinkedHashMap<>();
private String resourcePolicy;
private Map<String, Object> maintenanceConfigurations = new LinkedHashMap<>();
public S3Table() {
}
public S3Table(String name, String arn, String namespace, String format, String versionToken,
String metadataLocation, String createdAt, String ownerAccountId, Object metadata,
Object encryptionConfiguration, Object storageClassConfiguration, Map<String, String> tags) {
this.name = name;
this.arn = arn;
this.namespace = namespace;
this.format = format;
this.versionToken = versionToken;
this.metadataLocation = metadataLocation;
this.createdAt = createdAt;
this.modifiedAt = createdAt;
this.ownerAccountId = ownerAccountId;
this.metadata = metadata;
this.encryptionConfiguration = encryptionConfiguration;
this.storageClassConfiguration = storageClassConfiguration;
if (tags != null) {
this.tags = new LinkedHashMap<>(tags);
}
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getArn() { return arn; }
public void setArn(String arn) { this.arn = arn; }
public String getNamespace() { return namespace; }
public void setNamespace(String namespace) { this.namespace = namespace; }
public String getFormat() { return format; }
public void setFormat(String format) { this.format = format; }
public String getVersionToken() { return versionToken; }
public void setVersionToken(String versionToken) { this.versionToken = versionToken; }
public String getMetadataLocation() { return metadataLocation; }
public void setMetadataLocation(String metadataLocation) { this.metadataLocation = metadataLocation; }
public String getCreatedAt() { return createdAt; }
public void setCreatedAt(String createdAt) { this.createdAt = createdAt; }
public String getModifiedAt() { return modifiedAt; }
public void setModifiedAt(String modifiedAt) { this.modifiedAt = modifiedAt; }
public String getOwnerAccountId() { return ownerAccountId; }
public void setOwnerAccountId(String ownerAccountId) { this.ownerAccountId = ownerAccountId; }
public Object getMetadata() { return metadata; }
public void setMetadata(Object metadata) { this.metadata = metadata; }
public Object getEncryptionConfiguration() { return encryptionConfiguration; }
public void setEncryptionConfiguration(Object encryptionConfiguration) { this.encryptionConfiguration = encryptionConfiguration; }
public Object getStorageClassConfiguration() { return storageClassConfiguration; }
public void setStorageClassConfiguration(Object storageClassConfiguration) { this.storageClassConfiguration = storageClassConfiguration; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags); }
public String getResourcePolicy() { return resourcePolicy; }
public void setResourcePolicy(String resourcePolicy) { this.resourcePolicy = resourcePolicy; }
public Map<String, Object> getMaintenanceConfigurations() { return maintenanceConfigurations; }
public void setMaintenanceConfigurations(Map<String, Object> maintenanceConfigurations) { this.maintenanceConfigurations = maintenanceConfigurations == null ? new LinkedHashMap<>() : new LinkedHashMap<>(maintenanceConfigurations); }
}
@@ -0,0 +1,57 @@
package io.github.hectorvent.floci.services.s3tables.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.util.LinkedHashMap;
import java.util.Map;
@RegisterForReflection
public class TableBucket {
private String name;
private String arn;
private String createdAt;
private String ownerAccountId;
private Object encryptionConfiguration;
private Object storageClassConfiguration;
private Map<String, String> tags = new LinkedHashMap<>();
private String resourcePolicy;
private Map<String, Object> maintenanceConfigurations = new LinkedHashMap<>();
private Map<String, Namespace> namespaces = new LinkedHashMap<>();
public TableBucket() {
}
public TableBucket(String name, String arn, String createdAt, String ownerAccountId,
Object encryptionConfiguration, Object storageClassConfiguration, Map<String, String> tags) {
this.name = name;
this.arn = arn;
this.createdAt = createdAt;
this.ownerAccountId = ownerAccountId;
this.encryptionConfiguration = encryptionConfiguration;
this.storageClassConfiguration = storageClassConfiguration;
if (tags != null) {
this.tags = new LinkedHashMap<>(tags);
}
}
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getArn() { return arn; }
public void setArn(String arn) { this.arn = arn; }
public String getCreatedAt() { return createdAt; }
public void setCreatedAt(String createdAt) { this.createdAt = createdAt; }
public String getOwnerAccountId() { return ownerAccountId; }
public void setOwnerAccountId(String ownerAccountId) { this.ownerAccountId = ownerAccountId; }
public Object getEncryptionConfiguration() { return encryptionConfiguration; }
public void setEncryptionConfiguration(Object encryptionConfiguration) { this.encryptionConfiguration = encryptionConfiguration; }
public Object getStorageClassConfiguration() { return storageClassConfiguration; }
public void setStorageClassConfiguration(Object storageClassConfiguration) { this.storageClassConfiguration = storageClassConfiguration; }
public Map<String, String> getTags() { return tags; }
public void setTags(Map<String, String> tags) { this.tags = tags == null ? new LinkedHashMap<>() : new LinkedHashMap<>(tags); }
public String getResourcePolicy() { return resourcePolicy; }
public void setResourcePolicy(String resourcePolicy) { this.resourcePolicy = resourcePolicy; }
public Map<String, Object> getMaintenanceConfigurations() { return maintenanceConfigurations; }
public void setMaintenanceConfigurations(Map<String, Object> maintenanceConfigurations) { this.maintenanceConfigurations = maintenanceConfigurations == null ? new LinkedHashMap<>() : new LinkedHashMap<>(maintenanceConfigurations); }
public Map<String, Namespace> getNamespaces() { return namespaces; }
public void setNamespaces(Map<String, Namespace> namespaces) { this.namespaces = namespaces == null ? new LinkedHashMap<>() : new LinkedHashMap<>(namespaces); }
}