mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(dynamodb): resume a Query after the ExclusiveStartKey position, whether or not an item has that key (#8021)
Closes #7970
This commit is contained in:
1 parent
d0d0e0eef8
commit
29253ff7f8
2 files changed
+96
-44
No files matched your search
@@ -1614,54 +1614,29 @@ public class DynamoDbService {
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Sort by the full (possibly composite) sort key, comparing each attribute in key-schema
|
||||
// order. Using only the first sort-key attribute would ignore the remaining components
|
||||
// (e.g. a "requestStateQuery RANGE, createdAt RANGE" index would never order by createdAt),
|
||||
// which in turn breaks ScanIndexForward=false. See floci-io/floci#1675.
|
||||
if (!sortKeyNames.isEmpty()) {
|
||||
List<String> finalSortKeyNames = sortKeyNames;
|
||||
results = new ArrayList<>(results);
|
||||
results.sort((a, b) -> {
|
||||
for (String name : finalSortKeyNames) {
|
||||
JsonNode aAttr = a.get(name);
|
||||
JsonNode bAttr = b.get(name);
|
||||
int cmp;
|
||||
if (aAttr == null && bAttr == null) cmp = 0;
|
||||
else if (aAttr == null) cmp = -1;
|
||||
else if (bAttr == null) cmp = 1;
|
||||
else cmp = ExpressionEvaluator.compareAttributeValues(aAttr, bAttr);
|
||||
if (cmp != 0) return cmp;
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
if (Boolean.FALSE.equals(scanIndexForward)) {
|
||||
Collections.reverse(results);
|
||||
// Order by the full (possibly composite) sort key, comparing each attribute in key-schema
|
||||
// order (floci-io/floci#1675), then, on an index, by the table key, which is what
|
||||
// tells apart items whose index keys tie.
|
||||
List<String> orderKeyNames = new ArrayList<>(sortKeyNames);
|
||||
if (accessPath.isIndex()) {
|
||||
orderKeyNames.add(table.getPartitionKeyName());
|
||||
if (table.getSortKeyName() != null) {
|
||||
orderKeyNames.add(table.getSortKeyName());
|
||||
}
|
||||
}
|
||||
Comparator<JsonNode> keyOrder = (a, b) -> compareKeyAttributes(a, b, orderKeyNames);
|
||||
int direction = Boolean.FALSE.equals(scanIndexForward) ? -1 : 1;
|
||||
if (!orderKeyNames.isEmpty()) {
|
||||
results = new ArrayList<>(results);
|
||||
results.sort((a, b) -> direction * keyOrder.compare(a, b));
|
||||
}
|
||||
|
||||
// Apply ExclusiveStartKey offset
|
||||
// ExclusiveStartKey is a position in key order, not a pointer to an item, so the
|
||||
// query resumes after it even when no item has that key any more.
|
||||
if (exclusiveStartKey != null) {
|
||||
String tablePkName = table.getPartitionKeyName();
|
||||
String tableSkName = table.getSortKeyName();
|
||||
boolean hasTableKeys = exclusiveStartKey.has(tablePkName);
|
||||
|
||||
String startItemKey = hasTableKeys
|
||||
? buildItemKeyFromNode(exclusiveStartKey, tablePkName, tableSkName)
|
||||
: buildItemKeyFromNode(exclusiveStartKey, pkName, sortKeyNames);
|
||||
|
||||
int startIdx = -1;
|
||||
for (int i = 0; i < results.size(); i++) {
|
||||
String thisKey = hasTableKeys
|
||||
? buildItemKeyFromNode(results.get(i), tablePkName, tableSkName)
|
||||
: buildItemKeyFromNode(results.get(i), pkName, sortKeyNames);
|
||||
if (thisKey.equals(startItemKey)) {
|
||||
startIdx = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (startIdx >= 0) {
|
||||
results = new ArrayList<>(results.subList(startIdx + 1, results.size()));
|
||||
}
|
||||
results = results.stream()
|
||||
.filter(item -> direction * keyOrder.compare(item, exclusiveStartKey) > 0)
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<JsonNode> evaluatedItems = results;
|
||||
@@ -5004,6 +4979,27 @@ public class DynamoDbService {
|
||||
};
|
||||
}
|
||||
|
||||
private static int compareKeyAttributes(JsonNode a, JsonNode b, List<String> keyNames) {
|
||||
for (String name : keyNames) {
|
||||
JsonNode aAttr = a.get(name);
|
||||
JsonNode bAttr = b.get(name);
|
||||
int cmp;
|
||||
if (aAttr == null && bAttr == null) {
|
||||
cmp = 0;
|
||||
} else if (aAttr == null) {
|
||||
cmp = -1;
|
||||
} else if (bAttr == null) {
|
||||
cmp = 1;
|
||||
} else {
|
||||
cmp = ExpressionEvaluator.compareAttributeValues(aAttr, bAttr);
|
||||
}
|
||||
if (cmp != 0) {
|
||||
return cmp;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private void validateExclusiveStartKeyWithinQuery(JsonNode exclusiveStartKey, TableDefinition table,
|
||||
DynamoDbAccessPath accessPath, JsonNode keyConditions,
|
||||
String keyConditionExpression, JsonNode expressionAttrValues,
|
||||
|
||||
@@ -879,6 +879,62 @@ class DynamoDbServiceTest {
|
||||
.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryResumesAfterTheStartKeyOfAnItemDeletedSinceThePreviousPage() {
|
||||
createPagingTable();
|
||||
service.deleteItem("Paging", item("PK", "p", "SK", "s2"), "us-east-1");
|
||||
|
||||
assertEquals(List.of("s3", "s4", "s5"),
|
||||
pagingQuery(null, true, item("PK", "p", "SK", "s2")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void queryResumesAfterAStartKeyThatNeverExisted() {
|
||||
createPagingTable();
|
||||
|
||||
assertEquals(List.of("s4", "s5"), pagingQuery(null, true, item("PK", "p", "SK", "s3x")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void descendingQueryResumesBeforeAStartKeyThatNeverExisted() {
|
||||
createPagingTable();
|
||||
|
||||
assertEquals(List.of("s3", "s2", "s1"), pagingQuery(null, false, item("PK", "p", "SK", "s3x")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void indexQueryResumesAfterTheStartKeyOfAnItemReKeyedSinceThePreviousPage() {
|
||||
createPagingTable();
|
||||
service.putItem("Paging", item("PK", "p", "SK", "s3", "G", "live", "GS", "t9"), "us-east-1");
|
||||
|
||||
assertEquals(List.of("s4", "s5", "s3"),
|
||||
pagingQuery("gsi1", true, item("PK", "p", "SK", "s3", "G", "live", "GS", "t3")));
|
||||
}
|
||||
|
||||
private void createPagingTable() {
|
||||
GlobalSecondaryIndex gsi = new GlobalSecondaryIndex("gsi1",
|
||||
List.of(new KeySchemaElement("G", "HASH"), new KeySchemaElement("GS", "RANGE")),
|
||||
null, "ALL", null);
|
||||
service.createTable("Paging",
|
||||
List.of(new KeySchemaElement("PK", "HASH"), new KeySchemaElement("SK", "RANGE")),
|
||||
List.of(new AttributeDefinition("PK", "S"), new AttributeDefinition("SK", "S"),
|
||||
new AttributeDefinition("G", "S"), new AttributeDefinition("GS", "S")),
|
||||
5L, 5L, List.of(gsi), "us-east-1");
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
service.putItem("Paging", item("PK", "p", "SK", "s" + i, "G", "live", "GS", "t" + i), "us-east-1");
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> pagingQuery(String indexName, boolean forward, ObjectNode exclusiveStartKey) {
|
||||
ObjectNode exprValues = mapper.createObjectNode();
|
||||
exprValues.set(":v", attributeValue("S", indexName == null ? "p" : "live"));
|
||||
String keyCondition = indexName == null ? "PK = :v" : "G = :v";
|
||||
return service.query("Paging", null, exprValues, keyCondition, null, null, forward, indexName,
|
||||
exclusiveStartKey, null, "us-east-1").items().stream()
|
||||
.map(result -> result.get("SK").get("S").asText())
|
||||
.toList();
|
||||
}
|
||||
|
||||
private void createRequestsTableWithCompositeSortKeyGsi(String region) {
|
||||
GlobalSecondaryIndex gsi = new GlobalSecondaryIndex(
|
||||
"memberIndex",
|
||||
|
||||
Reference in new issue
Block a user