Light, fluffy, and always free - The AWS Local Emulator alternative
fix(dynamodb): size numbers by base-100 digit pairs (#4530)
TATSUNO “Taz” Yasuhiroc9427b583296
diff --git a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSize.java b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSize.javaindex 6373e4f358..75ad3e221c 100644--- a/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSize.java+++ b/src/main/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSize.java@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.dynamodb;import com.fasterxml.jackson.databind.JsonNode;import io.github.hectorvent.floci.core.common.AwsException;+import java.math.BigDecimal;import java.nio.charset.StandardCharsets;import java.util.Base64;import java.util.Iterator;@@ -25,6 +26,7 @@ final class DynamoDbItemSize {private static final int SET_OR_ADD_COST = 19;private static final int REMOVE_OR_DELETE_COST = 2;private static final int LIST_INDEX_COST = 1;+ private static final int MAX_NUMBER_PAIRS = 20;private DynamoDbItemSize() {}@@ -117,7 +119,9 @@ final class DynamoDbItemSize {static int attributeValueSize(JsonNode attr) {if (attr == null) return 0;if (attr.has("S")) return utf8Length(attr.get("S").asText());- if (attr.has("N")) return attr.get("N").asText().length();+ if (attr.has("N")) {+ return numberSize(attr.get("N").asText());+ }if (attr.has("B")) return binarySize(attr.get("B").asText());if (attr.has("BOOL")) return 1;if (attr.has("NULL")) return 1;@@ -128,7 +132,9 @@ final class DynamoDbItemSize {}if (attr.has("NS")) {int size = 0;- for (JsonNode e : attr.get("NS")) size += e.asText().length() + 1;+ for (JsonNode e : attr.get("NS")) {+ size += numberSize(e.asText());+ }return size;}if (attr.has("BS")) {@@ -153,6 +159,25 @@ final class DynamoDbItemSize {return 0;}+ // DynamoDB stores a number as base-100 digits paired from the decimal point, plus one byte,+ // plus one more for a negative value below the 20-pair maximum.+ static int numberSize(String number) {+ BigDecimal value;+ try {+ value = new BigDecimal(number).stripTrailingZeros();+ } catch (NumberFormatException | ArithmeticException ignored) {+ // A number BigDecimal cannot hold is never stored, so its size never matters.+ return number.length();+ }+ if (value.signum() == 0) {+ return 1;+ }+ long lowestPower = -(long) value.scale();+ long highestPower = value.precision() + lowestPower - 1;+ long pairs = Math.floorDiv(highestPower, 2) - Math.floorDiv(lowestPower, 2) + 1;+ return (int) (1 + pairs + (value.signum() < 0 && pairs < MAX_NUMBER_PAIRS ? 1 : 0));+ }+static int utf8Length(String s) {if (s == null) return 0;return s.getBytes(StandardCharsets.UTF_8).length;diff --git a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSizeServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSizeServiceTest.javaindex 15d3eb1d02..9311f3012f 100644--- a/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSizeServiceTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/dynamodb/DynamoDbItemSizeServiceTest.java@@ -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());+ }+@Testvoid 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);