fix(dynamodb): hybrid persistence (#2677)

* fix: persist DynamoDB index definitions

* test(dynamodb): declare LSI sort key attribute in HybridStorageTest fixture

Real DynamoDB requires every attribute used in a table or index key
schema to appear in AttributeDefinitions; the LSI's sort key (lsiSk)
was missing from the fixture.
This commit is contained in:
Sam Fakhreddine authored and GitHub committed 2026-08-28 18:14:28 -04:00
1 parent 5033ace772
commit e5316839ca
4 files changed
+35

No files matched your search

@@ -64,6 +64,7 @@ public class GlobalSecondaryIndex {
public long getIndexSizeBytes() { return indexSizeBytes; }
public void setIndexSizeBytes(long indexSizeBytes) { this.indexSizeBytes = indexSizeBytes; }
@JsonIgnore
public String getPartitionKeyName() {
return keySchema.stream()
.filter(k -> "HASH".equals(k.getKeyType()))
@@ -72,6 +73,7 @@ public class GlobalSecondaryIndex {
.orElseThrow();
}
@JsonIgnore
public String getSortKeyName() {
return keySchema.stream()
.filter(k -> "RANGE".equals(k.getKeyType()))
@@ -63,6 +63,7 @@ public class LocalSecondaryIndex {
public long getItemCount() { return itemCount; }
public void setItemCount(long itemCount) { this.itemCount = itemCount; }
@JsonIgnore
public String getPartitionKeyName() {
return keySchema.stream()
.filter(k -> "HASH".equals(k.getKeyType()))
@@ -71,6 +72,7 @@ public class LocalSecondaryIndex {
.orElseThrow();
}
@JsonIgnore
public String getSortKeyName() {
return keySchema.stream()
.filter(k -> "RANGE".equals(k.getKeyType()))
@@ -207,6 +207,7 @@ public class TableDefinition {
public Integer getOnDemandMaxWriteRequestUnits() { return onDemandMaxWriteRequestUnits; }
public void setOnDemandMaxWriteRequestUnits(Integer v) { this.onDemandMaxWriteRequestUnits = v; }
@JsonIgnore
public String getPartitionKeyName() {
return keySchema.stream()
.filter(k -> "HASH".equals(k.getKeyType()))
@@ -216,6 +217,7 @@ public class TableDefinition {
}
/** Returns the sort key attribute name, or null if none. */
@JsonIgnore
public String getSortKeyName() {
return keySchema.stream()
.filter(k -> "RANGE".equals(k.getKeyType()))
@@ -1,12 +1,17 @@
package io.github.hectorvent.floci.core.storage;
import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.services.dynamodb.model.AttributeDefinition;
import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
import io.github.hectorvent.floci.services.dynamodb.model.LocalSecondaryIndex;
import io.github.hectorvent.floci.services.dynamodb.model.TableDefinition;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
@@ -50,6 +55,30 @@ class HybridStorageTest {
store2.shutdown();
}
@Test
void dynamoTableWithLocalSecondaryIndexSurvivesRestart() {
Path filePath = tempDir.resolve("dynamodb-tables.json");
TypeReference<Map<String, TableDefinition>> type = new TypeReference<>() {};
var table = new TableDefinition(
"ConfigTable",
List.of(new KeySchemaElement("pk", "HASH"), new KeySchemaElement("sk", "RANGE")),
List.of(new AttributeDefinition("pk", "S"), new AttributeDefinition("sk", "S"),
new AttributeDefinition("lsiSk", "S")));
table.setLocalSecondaryIndexes(List.of(new LocalSecondaryIndex(
"lsi", List.of(new KeySchemaElement("pk", "HASH"),
new KeySchemaElement("lsiSk", "RANGE")), null, "ALL")));
var store1 = new HybridStorage<String, TableDefinition>(filePath, type, 60000);
store1.put("000000000000/us-east-1::ConfigTable", table);
store1.shutdown();
var store2 = new HybridStorage<String, TableDefinition>(filePath, type, 60000);
store2.load();
TableDefinition restored = store2.get("000000000000/us-east-1::ConfigTable").orElseThrow();
assertEquals("lsiSk", restored.getLocalSecondaryIndexes().getFirst().getSortKeyName());
store2.shutdown();
}
@Test
void deleteRemovesFromMemory() {
storage.put("key1", "value1");