mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(dynamodb): add ScanFilter support for Scan operation (#175)
* feat(dynamodb): add ScanFilter support for Scan operation * fix(dynamodb): address review feedback for ScanFilter PR
This commit is contained in:
1 parent
39eacd56a8
commit
fe4ffd1ee4
4 files changed
+133
-26
No files matched your search
@@ -312,12 +312,14 @@ public class DynamoDbJsonHandler {
|
||||
? request.get("ExpressionAttributeNames") : null;
|
||||
JsonNode exprAttrValues = request.has("ExpressionAttributeValues")
|
||||
? request.get("ExpressionAttributeValues") : null;
|
||||
JsonNode scanFilter = request.has("ScanFilter")
|
||||
? request.get("ScanFilter") : null;
|
||||
Integer limit = request.has("Limit") ? request.get("Limit").asInt() : null;
|
||||
JsonNode exclusiveStartKey = request.has("ExclusiveStartKey")
|
||||
? request.get("ExclusiveStartKey") : null;
|
||||
|
||||
DynamoDbService.ScanResult result = dynamoDbService.scan(
|
||||
tableName, filterExpr, exprAttrNames, exprAttrValues, limit, exclusiveStartKey, region);
|
||||
tableName, filterExpr, exprAttrNames, exprAttrValues, scanFilter, limit, exclusiveStartKey, region);
|
||||
|
||||
ObjectNode response = objectMapper.createObjectNode();
|
||||
ArrayNode itemsArray = objectMapper.createArrayNode();
|
||||
|
||||
@@ -513,21 +513,14 @@ public class DynamoDbService {
|
||||
|
||||
public ScanResult scan(String tableName, String filterExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
Integer limit, String startKey) {
|
||||
JsonNode scanFilter, Integer limit, JsonNode exclusiveStartKey) {
|
||||
return scan(tableName, filterExpression, expressionAttrNames, expressionAttrValues,
|
||||
limit, (JsonNode) null, regionResolver.getDefaultRegion());
|
||||
scanFilter, limit, exclusiveStartKey, regionResolver.getDefaultRegion());
|
||||
}
|
||||
|
||||
public ScanResult scan(String tableName, String filterExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
Integer limit, String startKey, String region) {
|
||||
return scan(tableName, filterExpression, expressionAttrNames, expressionAttrValues,
|
||||
limit, (JsonNode) null, region);
|
||||
}
|
||||
|
||||
public ScanResult scan(String tableName, String filterExpression,
|
||||
JsonNode expressionAttrNames, JsonNode expressionAttrValues,
|
||||
Integer limit, JsonNode exclusiveStartKey, String region) {
|
||||
JsonNode scanFilter, Integer limit, JsonNode exclusiveStartKey, String region) {
|
||||
String storageKey = regionKey(region, tableName);
|
||||
TableDefinition table = tableStore.get(storageKey)
|
||||
.orElseThrow(() -> resourceNotFoundException(tableName));
|
||||
@@ -551,10 +544,14 @@ public class DynamoDbService {
|
||||
if (isExpired(item, table)) {
|
||||
continue;
|
||||
}
|
||||
if (filterExpression == null
|
||||
|| matchesFilterExpression(item, filterExpression, expressionAttrNames, expressionAttrValues)) {
|
||||
results.add(item);
|
||||
if (filterExpression != null
|
||||
&& !matchesFilterExpression(item, filterExpression, expressionAttrNames, expressionAttrValues)) {
|
||||
continue;
|
||||
}
|
||||
if (scanFilter != null && !matchesScanFilter(item, scanFilter)) {
|
||||
continue;
|
||||
}
|
||||
results.add(item);
|
||||
}
|
||||
|
||||
JsonNode lastEvaluatedKey = null;
|
||||
@@ -567,6 +564,20 @@ public class DynamoDbService {
|
||||
return new ScanResult(results, totalScanned, lastEvaluatedKey);
|
||||
}
|
||||
|
||||
private boolean matchesScanFilter(JsonNode item, JsonNode scanFilter) {
|
||||
Iterator<Map.Entry<String, JsonNode>> fields = scanFilter.fields();
|
||||
while (fields.hasNext()) {
|
||||
var entry = fields.next();
|
||||
String attrName = entry.getKey();
|
||||
JsonNode condition = entry.getValue();
|
||||
JsonNode attrValue = item.get(attrName);
|
||||
if (!matchesKeyCondition(attrValue, condition)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// --- Batch Operations ---
|
||||
|
||||
public record BatchWriteResult(Map<String, List<JsonNode>> unprocessedItems) {}
|
||||
@@ -1388,6 +1399,7 @@ public class DynamoDbService {
|
||||
return null;
|
||||
}
|
||||
|
||||
// NE, CONTAINS, NOT_CONTAINS, IN, NULL, NOT_NULL not yet supported
|
||||
private boolean matchesKeyCondition(JsonNode attrValue, JsonNode condition) {
|
||||
if (condition == null) return true;
|
||||
String op = condition.has("ComparisonOperator") ? condition.get("ComparisonOperator").asText() : "EQ";
|
||||
|
||||
Reference in new issue
Block a user