fix(dynamodb): size numbers by base-100 digit pairs (#4530)

* fix(dynamodb): size numbers by base-100 digit pairs

DynamoDB stores a number as base-100 digits paired from the decimal point.
It costs one byte per pair plus one, and one more when negative. Floci
counted the characters of the number text, so items full of numbers hit
the 400 KB limit too early or too late, and capacity was off too.

On AWS, 110 and 0.015 cost 3 bytes, 1100 and 0.0015 cost 2, 3.14159
costs 5 and 1E125 costs 2.

* fix(dynamodb): drop the sign byte at 20 pairs and survive huge exponents

On AWS a negative number of 20 base-100 pairs costs 21 bytes, the same as
a positive one. Below 20 pairs it costs one byte more.

A number whose exponent overflows BigDecimal's scale now falls back to its
text length instead of throwing, and the pair math runs in long.

* fix(dynamodb): brace the number size branch

* fix(dynamodb): charge a number set only for its members

On AWS an NS set costs the sum of its members and nothing per member.
{"NS":["1"]} costs 2 bytes like {"N":"1"}, and ["1","2","3"] costs 6.
This commit is contained in:
TATSUNO “Taz” Yasuhiro authored and GitHub committed 2026-09-28 00:33:41 -04:00
1 parent c26f782ea8
commit c9427b5832
2 files changed
+95 -4

No files matched your search

@@ -12,6 +12,8 @@ import io.github.hectorvent.floci.services.dynamodb.model.KeySchemaElement;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import java.util.List;
@@ -44,6 +46,60 @@ class DynamoDbItemSizeServiceTest {
List.of(new AttributeDefinition("pk", "S")), 5L, 5L, REGION);
}
@ParameterizedTest
@CsvSource({
"0, 1", "-0, 1", "1, 2", "12, 2", "123, 3", "1234, 3", "123456, 4",
"12345678901234567890123456789012345678, 20",
"0042, 2", "100, 2", "110, 3", "1010, 3", "1100, 2", "11000, 3",
"0.1, 2", "0.15, 2", "0.015, 3", "0.0015, 2", "0.0000001, 2",
"1.5, 3", "1.05, 3", "1.200, 3", "3.14159, 5", "100.5, 4", "110.5, 4",
"1E125, 2", "1E-100, 2", "1.1E2, 3", "-42, 3", "-110, 4", "-0.015, 4",
"-12345678901234567890123456789012345678, 21",
"-1234567890123456789012345678901234567.8, 21",
"1234567890123456789012345678901234567.8, 21",
"-0.012345678901234567890123456789012345678, 21"
})
void numberCostsOneBytePerDigitPairFromTheDecimalPoint(String number, int bytes) {
assertEquals(bytes, DynamoDbItemSize.numberSize(number));
}
@Test
void numberSizeSurvivesAnExponentAtTheEdgeOfTheScaleRange() {
assertEquals("100E2147483647".length(), DynamoDbItemSize.numberSize("100E2147483647"));
assertEquals(4, DynamoDbItemSize.numberSize("1234E2147483645"));
}
@Test
void putItemMeasuresANumberByItsDigitPairs() {
ObjectNode item = key("a");
item.set("n", numberValue("3.14159"));
item.set("b", stringValue(padding("a", MAX_ITEM_SIZE, "n".length() + 5)));
service.putItem(TABLE, item, REGION);
assertEquals(MAX_ITEM_SIZE, storedBytes("a"));
ObjectNode over = key("b");
over.set("n", numberValue("3.14159"));
over.set("b", stringValue(padding("b", MAX_ITEM_SIZE + 1, "n".length() + 5)));
AwsException refused = assertThrows(AwsException.class, () -> service.putItem(TABLE, over, REGION));
assertEquals("Item size has exceeded the maximum allowed size", refused.getMessage());
}
@Test
void putItemMeasuresANumberSetByItsMembersAlone() {
int setBytes = "n".length() + 2 + 3 + 3;
ObjectNode item = key("a");
item.set("n", numberSetValue("1", "110", "0.015"));
item.set("b", stringValue(padding("a", MAX_ITEM_SIZE, setBytes)));
service.putItem(TABLE, item, REGION);
assertEquals(MAX_ITEM_SIZE, storedBytes("a"));
ObjectNode over = key("b");
over.set("n", numberSetValue("1", "110", "0.015"));
over.set("b", stringValue(padding("b", MAX_ITEM_SIZE + 1, setBytes)));
AwsException refused = assertThrows(AwsException.class, () -> service.putItem(TABLE, over, REGION));
assertEquals("Item size has exceeded the maximum allowed size", refused.getMessage());
}
@Test
void updateItemSitsBelowTheLimitAtAShortKey() {
int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES - (UPDATE_COST + SET_OR_ADD_COST);
@@ -76,10 +132,11 @@ class DynamoDbItemSizeServiceTest {
void updateItemChargesNineteenBytesForAnAdd() {
int ceiling = MAX_ITEM_SIZE + SHORT_KEY_BYTES - (UPDATE_COST + SET_OR_ADD_COST * 2);
ObjectNode one = values(":one", numberValue("1"));
int numberBytes = "n".length() + 2;
update("a", "SET b = :pad ADD n :one", ceiling, 2, one);
update("a", "SET b = :pad ADD n :one", ceiling, numberBytes, one);
assertEquals(ceiling, storedBytes("a"));
assertRefused(() -> update("b", "SET b = :pad ADD n :one", ceiling + 1, 2, one));
assertRefused(() -> update("b", "SET b = :pad ADD n :one", ceiling + 1, numberBytes, one));
}
@Test
@@ -252,6 +309,15 @@ class DynamoDbItemSizeServiceTest {
return JsonNodeFactory.instance.objectNode().put("N", number);
}
private ObjectNode numberSetValue(String... members) {
ObjectNode value = JsonNodeFactory.instance.objectNode();
ArrayNode set = value.putArray("NS");
for (String member : members) {
set.add(member);
}
return value;
}
private ObjectNode stringSetValue(String member) {
ObjectNode value = JsonNodeFactory.instance.objectNode();
value.putArray("SS").add(member);