mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(glue): paginate catalog database, table, partition and version lists (#7827)
This commit is contained in:
1 parent
3eb01eb8e9
commit
aea5c9ad05
11 files changed
+639
-18
No files matched your search
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsJsonTimestamp;
|
||||
import io.floci.aws.core.common.EpochSeconds;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.services.glue.model.*;
|
||||
import io.floci.aws.services.glue.schemaregistry.GlueSchemaRegistryService;
|
||||
import io.floci.aws.services.glue.schemaregistry.model.Registry;
|
||||
@@ -90,9 +91,7 @@ public class GlueJsonHandler {
|
||||
Database db = glueService.getDatabase(name);
|
||||
yield Response.ok(Map.of("Database", db)).build();
|
||||
}
|
||||
case "GetDatabases" -> {
|
||||
yield Response.ok(Map.of("DatabaseList", glueService.getDatabases())).build();
|
||||
}
|
||||
case "GetDatabases" -> handleGetDatabases(request);
|
||||
case "DeleteDatabase" -> {
|
||||
String name = request.get("Name").asText();
|
||||
glueService.deleteDatabase(name, region);
|
||||
@@ -141,9 +140,13 @@ public class GlueJsonHandler {
|
||||
glueService.getPartitionIndexes(dbName, tableName))).build();
|
||||
}
|
||||
case "GetTables" -> {
|
||||
Integer maxResults = Pagination.parseMaxResults(request, "MaxResults", "InvalidInputException");
|
||||
String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidInputException");
|
||||
String dbName = request.get("DatabaseName").asText();
|
||||
List<Table> tables = glueService.getTables(dbName).stream().map(GlueService::withReadDefaults).toList();
|
||||
yield Response.ok(Map.of("TableList", tables)).build();
|
||||
GlueService.Page<Table> page = glueService.getTables(
|
||||
dbName, maxResults, nextToken);
|
||||
List<Table> tables = page.items().stream().map(GlueService::withReadDefaults).toList();
|
||||
yield Response.ok(pageResponse("TableList", tables, page.nextToken())).build();
|
||||
}
|
||||
case "UpdateTable" -> {
|
||||
String dbName = request.get("DatabaseName").asText();
|
||||
@@ -153,9 +156,13 @@ public class GlueJsonHandler {
|
||||
yield Response.ok().build();
|
||||
}
|
||||
case "GetTableVersions" -> {
|
||||
Integer maxResults = Pagination.parseMaxResults(request, "MaxResults", "InvalidInputException");
|
||||
String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidInputException");
|
||||
String dbName = request.get("DatabaseName").asText();
|
||||
String tableName = request.get("TableName").asText();
|
||||
yield Response.ok(Map.of("TableVersions", glueService.getTableVersions(dbName, tableName))).build();
|
||||
GlueService.Page<Map<String, Object>> page = glueService.getTableVersions(
|
||||
dbName, tableName, maxResults, nextToken);
|
||||
yield Response.ok(pageResponse("TableVersions", page.items(), page.nextToken())).build();
|
||||
}
|
||||
case "GetTableVersion" -> {
|
||||
String dbName = request.get("DatabaseName").asText();
|
||||
@@ -203,13 +210,16 @@ public class GlueJsonHandler {
|
||||
case "GetPartition" -> handleGetPartition(request);
|
||||
case "BatchGetPartition" -> handleBatchGetPartition(request);
|
||||
case "GetPartitions" -> {
|
||||
Integer maxResults = Pagination.parseMaxResults(request, "MaxResults", "InvalidInputException");
|
||||
String nextToken = Pagination.parseNextToken(request, "NextToken", "InvalidInputException");
|
||||
String dbName = request.get("DatabaseName").asText();
|
||||
String tableName = request.get("TableName").asText();
|
||||
String expression = request.path("Expression").asText(null);
|
||||
GlueService.Segment segment = request.hasNonNull("Segment") ? segment(request.get("Segment")) : null;
|
||||
List<Partition> partitions = glueService.getPartitions(dbName, tableName, expression, segment).stream()
|
||||
.map(GlueService::withReadDefaults).toList();
|
||||
yield Response.ok(Map.of("Partitions", partitions)).build();
|
||||
GlueService.Page<Partition> page = glueService.getPartitions(dbName, tableName, expression, segment,
|
||||
maxResults, nextToken);
|
||||
List<Partition> partitions = page.items().stream().map(GlueService::withReadDefaults).toList();
|
||||
yield Response.ok(pageResponse("Partitions", partitions, page.nextToken())).build();
|
||||
}
|
||||
case "DeletePartition" -> handleDeletePartition(request);
|
||||
case "BatchDeletePartition" -> handleBatchDeletePartition(request);
|
||||
@@ -1102,6 +1112,13 @@ public class GlueJsonHandler {
|
||||
return vn.asLong();
|
||||
}
|
||||
|
||||
private Response handleGetDatabases(JsonNode request) {
|
||||
GlueService.Page<Database> page = glueService.getDatabases(
|
||||
Pagination.parseMaxResults(request, "MaxResults", "InvalidInputException"),
|
||||
Pagination.parseNextToken(request, "NextToken", "InvalidInputException"));
|
||||
return Response.ok(pageResponse("DatabaseList", page.items(), page.nextToken())).build();
|
||||
}
|
||||
|
||||
private Integer readMaxResults(JsonNode request) {
|
||||
JsonNode node = request.get("MaxResults");
|
||||
if (node == null || node.isNull()) {
|
||||
|
||||
@@ -10,6 +10,8 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.Base64Url;
|
||||
import io.floci.aws.core.common.Digests;
|
||||
import io.floci.aws.core.common.EpochSeconds;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
@@ -62,6 +64,7 @@ import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.Comparator;
|
||||
import java.util.HexFormat;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
@@ -69,6 +72,7 @@ import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
@@ -388,6 +392,12 @@ public class GlueService {
|
||||
return databases;
|
||||
}
|
||||
|
||||
public Page<Database> getDatabases(Integer maxResults, String nextToken) {
|
||||
return paginate(() -> getDatabases().stream()
|
||||
.sorted(Comparator.comparing(Database::getName)).toList(), maxResults, nextToken, 100,
|
||||
Database::getName, "glue-databases");
|
||||
}
|
||||
|
||||
/**
|
||||
* Heals a database persisted before CatalogId was modelled. The store is namespaced by
|
||||
* account, so the reader is always the owning account and the healed value is stable —
|
||||
@@ -466,6 +476,13 @@ public class GlueService {
|
||||
return resolved;
|
||||
}
|
||||
|
||||
public Page<Table> getTables(String databaseName, Integer maxResults, String nextToken) {
|
||||
String prefix = normalizeName(databaseName) + ":";
|
||||
Page<Table> page = paginate(() -> tableStore.scan(key -> key.startsWith(prefix)),
|
||||
maxResults, nextToken, 100, Table::getName, "glue-tables:" + prefix);
|
||||
return new Page<>(page.items().stream().map(this::withResolvedSchemaReference).toList(), page.nextToken());
|
||||
}
|
||||
|
||||
/**
|
||||
* SearchTables over every table of the catalog. SearchText matches a substring of the name,
|
||||
* database, description, owner, column names and comments and parameter values, or the whole
|
||||
@@ -698,6 +715,13 @@ public class GlueService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public Page<Map<String, Object>> getTableVersions(String databaseName, String tableName,
|
||||
Integer maxResults, String nextToken) {
|
||||
return paginate(() -> getTableVersions(databaseName, tableName), maxResults, nextToken, 100,
|
||||
version -> String.format("%019d", Long.MAX_VALUE - versionIdAsLong((Table) version.get("Table"))),
|
||||
"glue-versions:" + tableKey(databaseName, tableName));
|
||||
}
|
||||
|
||||
/** One archived version, or the current table when no VersionId is given, as AWS answers. */
|
||||
public Map<String, Object> getTableVersion(String databaseName, String tableName, String versionId) {
|
||||
Table current = getTable(databaseName, tableName);
|
||||
@@ -953,6 +977,29 @@ public class GlueService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
public Page<Partition> getPartitions(String databaseName, String tableName, String expression,
|
||||
Segment segment, Integer maxResults, String nextToken) {
|
||||
return paginate(() -> getPartitions(databaseName, tableName, expression, segment),
|
||||
maxResults, nextToken, 1000, GlueService::partitionCursor,
|
||||
"glue-partitions:" + tableKey(databaseName, tableName));
|
||||
}
|
||||
|
||||
/** Fixed-width UTF-16 digits and terminators preserve the null-first tuple ordering. */
|
||||
private static String partitionCursor(Partition partition) {
|
||||
StringBuilder cursor = new StringBuilder();
|
||||
List<String> values = partition.getValues() == null ? List.of() : partition.getValues();
|
||||
for (String value : values) {
|
||||
cursor.append(value == null ? '0' : '1');
|
||||
if (value != null) {
|
||||
for (int index = 0; index < value.length(); index++) {
|
||||
cursor.append(HexFormat.of().toHexDigits(value.charAt(index)));
|
||||
}
|
||||
}
|
||||
cursor.append('/');
|
||||
}
|
||||
return cursor.toString();
|
||||
}
|
||||
|
||||
private static void validateSegment(Segment segment) {
|
||||
if (segment == null) {
|
||||
return;
|
||||
@@ -3430,12 +3477,18 @@ public class GlueService {
|
||||
return paginate(() -> all, maxResults, nextToken, 1000);
|
||||
}
|
||||
|
||||
private <T> Page<T> paginate(Supplier<List<T>> items, Integer maxResults, String nextToken, int maximum,
|
||||
Function<T, String> cursorOf, String namespace) {
|
||||
int limit = pageLimit(maxResults, maximum);
|
||||
PaginatedResult<T> page = Pagination.paginate(items.get(), cursorOf,
|
||||
limit, nextToken, namespace,
|
||||
token -> new AwsException("InvalidInputException", "Invalid NextToken", 400));
|
||||
return new Page<>(page.items(), page.nextToken());
|
||||
}
|
||||
|
||||
private <T> Page<T> paginate(Supplier<List<T>> items, Integer maxResults, String nextToken, int maximum) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > maximum)) {
|
||||
throw new AwsException("InvalidInputException", "MaxResults must be between 1 and " + maximum, 400);
|
||||
}
|
||||
int limit = pageLimit(maxResults, maximum);
|
||||
List<T> all = items.get();
|
||||
int limit = maxResults == null ? 100 : maxResults;
|
||||
int start = 0;
|
||||
if (nextToken != null && !nextToken.isBlank()) {
|
||||
try {
|
||||
@@ -3451,4 +3504,11 @@ public class GlueService {
|
||||
String newToken = end < all.size() ? String.valueOf(end) : null;
|
||||
return new Page<>(List.copyOf(all.subList(start, end)), newToken);
|
||||
}
|
||||
|
||||
private static int pageLimit(Integer maxResults, int maximum) {
|
||||
if (maxResults != null && (maxResults < 1 || maxResults > maximum)) {
|
||||
throw new AwsException("InvalidInputException", "MaxResults must be between 1 and " + maximum, 400);
|
||||
}
|
||||
return maxResults == null ? 100 : maxResults;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user