mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(dynamodb): fix GSI query pagination infinite loop when items share sort key (#637)
When paginating a GSI query where multiple items have the same index sort key, LastEvaluatedKey only contained the index keys (GSI1PK + GSI1SK), making it impossible to distinguish between items on the same "page boundary". This caused ExclusiveStartKey matching to never advance past that boundary, looping forever. Fix: include the base-table PK and SK in LastEvaluatedKey for index queries, and prefer base-table keys when matching ExclusiveStartKey (AWS SDK behaviour). Closes #587
This commit is contained in:
1 parent
94d7f411f2
commit
77db7d24ef
2 files changed
+158
-4
No files matched your search
@@ -533,11 +533,19 @@ public class DynamoDbService {
|
||||
|
||||
// Apply ExclusiveStartKey offset
|
||||
if (exclusiveStartKey != null) {
|
||||
TableDefinition finalTable = table;
|
||||
String startItemKey = buildItemKeyFromNode(exclusiveStartKey, pkName, skName);
|
||||
String tablePkName = table.getPartitionKeyName();
|
||||
String tableSkName = table.getSortKeyName();
|
||||
boolean hasTableKeys = exclusiveStartKey.has(tablePkName);
|
||||
|
||||
String startItemKey = hasTableKeys
|
||||
? buildItemKeyFromNode(exclusiveStartKey, tablePkName, tableSkName)
|
||||
: buildItemKeyFromNode(exclusiveStartKey, pkName, skName);
|
||||
|
||||
int startIdx = -1;
|
||||
for (int i = 0; i < results.size(); i++) {
|
||||
String thisKey = buildItemKeyFromNode(results.get(i), pkName, skName);
|
||||
String thisKey = hasTableKeys
|
||||
? buildItemKeyFromNode(results.get(i), tablePkName, tableSkName)
|
||||
: buildItemKeyFromNode(results.get(i), pkName, skName);
|
||||
if (thisKey.equals(startItemKey)) {
|
||||
startIdx = i;
|
||||
break;
|
||||
@@ -553,7 +561,7 @@ public class DynamoDbService {
|
||||
|
||||
if (limit != null && limit > 0 && evaluatedItems.size() > limit) {
|
||||
JsonNode lastItem = evaluatedItems.get(limit - 1);
|
||||
lastEvaluatedKey = buildKeyNode(table, lastItem, pkName, skName);
|
||||
lastEvaluatedKey = buildKeyNode(table, lastItem, pkName, skName, indexName != null);
|
||||
evaluatedItems = new ArrayList<>(evaluatedItems.subList(0, limit));
|
||||
}
|
||||
|
||||
@@ -1775,6 +1783,11 @@ public class DynamoDbService {
|
||||
}
|
||||
|
||||
JsonNode buildKeyNode(TableDefinition table, JsonNode item, String pkName, String skName) {
|
||||
return buildKeyNode(table, item, pkName, skName, false);
|
||||
}
|
||||
|
||||
JsonNode buildKeyNode(TableDefinition table, JsonNode item,
|
||||
String pkName, String skName, boolean isIndexQuery) {
|
||||
com.fasterxml.jackson.databind.node.ObjectNode keyNode =
|
||||
com.fasterxml.jackson.databind.node.JsonNodeFactory.instance.objectNode();
|
||||
JsonNode pkAttr = item.get(pkName);
|
||||
@@ -1787,6 +1800,16 @@ public class DynamoDbService {
|
||||
keyNode.set(skName, skAttr);
|
||||
}
|
||||
}
|
||||
if (isIndexQuery) {
|
||||
String tablePk = table.getPartitionKeyName();
|
||||
String tableSk = table.getSortKeyName();
|
||||
if (!tablePk.equals(pkName) && item.get(tablePk) != null) {
|
||||
keyNode.set(tablePk, item.get(tablePk));
|
||||
}
|
||||
if (tableSk != null && !tableSk.equals(skName) && item.get(tableSk) != null) {
|
||||
keyNode.set(tableSk, item.get(tableSk));
|
||||
}
|
||||
}
|
||||
return keyNode;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user