mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(dynamodb): return set members in DynamoDB's order (#6520)
* fix(dynamodb): return set members in DynamoDB's order DynamoDB stores a set's members in its own order, not as written. A string set is ordered by code point, a number set by value and a binary set by unsigned byte. Every read of the item shows that order, ReturnValues included. Floci kept the order of the write, so a set came back as written and an ADD appended its new members at the end. Sets are now put in that order when an item is stored, on PutItem, batch and transact writes, PartiQL inserts and the UpdateItem paths. * perf(dynamodb): parse each set member once when ordering it The number and binary comparators parsed both members on every comparison, so one sort of a large set decoded each member many times. The key is now computed once per member and per sort. * chore(dynamodb): sort set members through one keyed sort The three set types now share one sort that computes each member's key once and keeps the member nodes. A string set is ordered by its UTF-8 bytes, which is code point order. The integration test uses the shared DynamoDB request helper. * chore(dynamodb): order export set members through DynamoDbSetOrder
This commit is contained in:
1 parent
ba82e0aeaa
commit
2a5f52e014
6 files changed
+308
-17
No files matched your search
+107
@@ -0,0 +1,107 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.github.hectorvent.floci.testing.DynamoDbRequests.ddb;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
|
||||
/**
|
||||
* A set comes back in DynamoDB's order however it was written: strings by code point,
|
||||
* numbers by value, binaries by unsigned byte.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class DynamoDbSetOrderIntegrationTest {
|
||||
|
||||
private static final String TABLE = "set-member-order";
|
||||
|
||||
@BeforeAll
|
||||
static void configureRestAssured() {
|
||||
RestAssuredJsonUtils.configureAwsContentTypes();
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void putItemStoresEverySetInDynamoDbOrder() {
|
||||
ddb("CreateTable", """
|
||||
{
|
||||
"TableName": "%s",
|
||||
"AttributeDefinitions": [{"AttributeName":"pk","AttributeType":"S"}],
|
||||
"KeySchema": [{"AttributeName":"pk","KeyType":"HASH"}],
|
||||
"BillingMode": "PAY_PER_REQUEST"
|
||||
}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(200);
|
||||
ddb("PutItem", """
|
||||
{"TableName":"%s","Item":{"pk":{"S":"a"},
|
||||
"ss":{"SS":["b","a","B","\\uff5e","\\ud83d\\ude00"]},
|
||||
"ns":{"NS":["10","9","1e2","-1"]},
|
||||
"bs":{"BS":["/w==","gA==","fw==","AQ==","AP8="]},
|
||||
"m":{"M":{"deep":{"SS":["b","a",""]}}},
|
||||
"l":{"L":[{"NS":["2","1"]}]}}}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(200);
|
||||
getItem("a")
|
||||
.body("Item.ss.SS", contains("B", "a", "b", "~", "😀"))
|
||||
.body("Item.ns.NS", contains("-1", "9", "10", "100"))
|
||||
.body("Item.bs.BS", contains("AP8=", "AQ==", "fw==", "gA==", "/w=="))
|
||||
.body("Item.m.M.deep.SS", contains("", "a", "b"))
|
||||
.body("Item.l.L[0].NS", contains("1", "2"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void updateExpressionsKeepTheOrder() {
|
||||
ddb("UpdateItem", """
|
||||
{"TableName":"%s","Key":{"pk":{"S":"a"}},"ReturnValues":"ALL_NEW",
|
||||
"UpdateExpression":"ADD ss :s, ns :n SET s2 = :v",
|
||||
"ExpressionAttributeValues":{":s":{"SS":["c",""]},":n":{"NS":["5","-3"]},":v":{"SS":["z","y"]}}}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(200)
|
||||
.body("Attributes.ss.SS", contains("", "B", "a", "b", "c", "~", "😀"))
|
||||
.body("Attributes.ns.NS", contains("-3", "-1", "5", "9", "10", "100"))
|
||||
.body("Attributes.s2.SS", contains("y", "z"));
|
||||
ddb("UpdateItem", """
|
||||
{"TableName":"%s","Key":{"pk":{"S":"a"}},"ReturnValues":"UPDATED_NEW",
|
||||
"AttributeUpdates":{"s2":{"Action":"ADD","Value":{"SS":["x"]}}}}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(200)
|
||||
.body("Attributes.s2.SS", contains("x", "y", "z"));
|
||||
getItem("a")
|
||||
.body("Item.ss.SS", contains("", "B", "a", "b", "c", "~", "😀"))
|
||||
.body("Item.s2.SS", contains("x", "y", "z"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void batchAndTransactWritesKeepTheOrder() {
|
||||
ddb("BatchWriteItem", """
|
||||
{"RequestItems":{"%s":[{"PutRequest":{"Item":{"pk":{"S":"batch"},"ss":{"SS":["b","a"]}}}}]}}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(200);
|
||||
ddb("TransactWriteItems", """
|
||||
{"TransactItems":[
|
||||
{"Put":{"TableName":"%1$s","Item":{"pk":{"S":"tx"},"ns":{"NS":["2","1"]}}}},
|
||||
{"Update":{"TableName":"%1$s","Key":{"pk":{"S":"tx2"}},"UpdateExpression":"ADD ss :v",
|
||||
"ExpressionAttributeValues":{":v":{"SS":["d","c"]}}}}]}
|
||||
""".formatted(TABLE))
|
||||
.statusCode(200);
|
||||
getItem("batch").body("Item.ss.SS", contains("a", "b"));
|
||||
getItem("tx").body("Item.ns.NS", contains("1", "2"));
|
||||
getItem("tx2").body("Item.ss.SS", contains("c", "d"));
|
||||
}
|
||||
|
||||
private static ValidatableResponse getItem(String pk) {
|
||||
return ddb("GetItem", """
|
||||
{"TableName":"%s","Key":{"pk":{"S":"%s"}},"ConsistentRead":true}
|
||||
""".formatted(TABLE, pk))
|
||||
.statusCode(200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package io.github.hectorvent.floci.services.dynamodb;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
|
||||
class DynamoDbSetOrderTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void stringsOrderByCodePoint() {
|
||||
List<String> sorted = sorted("SS",
|
||||
List.of("b", "a", "", "B", "aa", "é", "Z", "~", "ab", "~", "😀"));
|
||||
assertEquals(List.of("", "B", "Z", "a", "aa", "ab", "b", "~", "é", "~", "😀"), sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void numbersOrderByValue() {
|
||||
List<String> sorted = sorted("NS", List.of("10", "9", "-1", "1.5", "0", "1e2", "0.001", "-0.5"));
|
||||
assertEquals(List.of("-1", "-0.5", "0", "0.001", "1.5", "9", "10", "1e2"), sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void binariesOrderByUnsignedByte() {
|
||||
List<String> sorted = sorted("BS", List.of(base64(0x80), base64(0x02), base64(0xff), base64(0x01, 0x00),
|
||||
base64(0x00), base64(0xc0), base64(0x7f), base64(0x01), base64(0x00, 0xff), base64(0x01, 0x01)));
|
||||
assertEquals(List.of(base64(0x00), base64(0x00, 0xff), base64(0x01), base64(0x01, 0x00), base64(0x01, 0x01),
|
||||
base64(0x02), base64(0x7f), base64(0x80), base64(0xc0), base64(0xff)), sorted);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sortSetsInReachesMapsAndLists() throws Exception {
|
||||
ObjectNode item = (ObjectNode) MAPPER.readTree("""
|
||||
{"pk":{"S":"a"},"ss":{"SS":["b","a"]},
|
||||
"m":{"M":{"deep":{"SS":["b","a",""]},"nums":{"NS":["10","9"]}}},
|
||||
"l":{"L":[{"NS":["2","1"]},{"M":{"inner":{"BS":["gA==","AQ=="]}}}]}}
|
||||
""");
|
||||
DynamoDbSetOrder.sortSetsIn(item);
|
||||
assertEquals(MAPPER.readTree("""
|
||||
{"pk":{"S":"a"},"ss":{"SS":["a","b"]},
|
||||
"m":{"M":{"deep":{"SS":["","a","b"]},"nums":{"NS":["9","10"]}}},
|
||||
"l":{"L":[{"NS":["1","2"]},{"M":{"inner":{"BS":["AQ==","gA=="]}}}]}}
|
||||
"""), item);
|
||||
}
|
||||
|
||||
@Test
|
||||
void membersThatDoNotParseKeepAPlace() {
|
||||
assertEquals(List.of("abc", "0", "1"), sorted("NS", List.of("1", "abc", "0")));
|
||||
assertEquals(List.of("AQ==", "not base64"), sorted("BS", List.of("not base64", "AQ==")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aMemberListThatIsNotAnArrayIsLeftAlone() throws Exception {
|
||||
JsonNode members = MAPPER.readTree("\"x\"");
|
||||
assertSame(members, DynamoDbSetOrder.sorted("SS", members));
|
||||
}
|
||||
|
||||
private static List<String> sorted(String setType, List<String> members) {
|
||||
List<String> result = new ArrayList<>();
|
||||
DynamoDbSetOrder.sorted(setType, MAPPER.valueToTree(members)).forEach(node -> result.add(node.asText()));
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String base64(int... bytes) {
|
||||
byte[] raw = new byte[bytes.length];
|
||||
for (int i = 0; i < bytes.length; i++) {
|
||||
raw[i] = (byte) bytes[i];
|
||||
}
|
||||
return Base64.getEncoder().encodeToString(raw);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user