fix(s3): complete multipart uploads larger than 2 GiB in disk storage modes (#4976)

CompleteMultipartUpload built the whole object as one byte array, so an
object over Integer.MAX_VALUE bytes failed with an ArithmeticException,
and anything smaller still needed the full object in the heap.

In the persistent, hybrid and wal modes the parts are now copied file to
file into one assembled file in the upload's directory, before the bucket
lock is taken, and that file is renamed into place under the lock. A
versioned bucket hard-links the current file to the versioned one rather
than writing a second copy, which is safe because object files are only
ever replaced by a rename, never modified in place.

The full-object checksum is combined from each part's CRC and size,
which is how S3 linearizes full-object checksums and why it offers them
only for the CRC algorithms, so the object's bytes are never read for it
and a mismatched expected checksum is rejected before assembly.

Since the ETag and checksum now come from the part records, each
UploadPart stores its bytes under its own storage ID kept on the record
and drops the bytes it replaced only after publishing the new record.
CompleteMultipartUpload reads each record once and assembles exactly the
bytes those records name, so an UploadPart racing a Complete makes that
Complete fail with InvalidPart instead of storing a checksum that
describes different bytes.

Memory mode still holds an object in one byte array, so an UploadPart
that takes an upload past the largest array the JDK allocates, or a
CompleteMultipartUpload over it, now fails with EntityTooLarge instead of
exhausting the heap or overflowing the array size.

Multipart uploads are tracked only in memory, so the part files under
.multipart are removed at startup and on reset. Without that, a crash
during assembly would leave a file as large as the object on disk for
good. The S3 docs describe the memory-mode limit, the disk space a
completion needs, and the operations that still read a whole object into
memory.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-03 14:34:54 -05:00
1 parent ba944df922
commit 2e87534941
8 files changed
+724 -100

No files matched your search

@@ -3,15 +3,19 @@ package io.github.hectorvent.floci.services.s3;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.s3.model.ChecksumAlgorithm;
import io.github.hectorvent.floci.services.s3.model.ChecksumType;
import io.github.hectorvent.floci.services.s3.model.Part;
import io.github.hectorvent.floci.services.s3.model.S3Checksum;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Random;
import java.util.zip.CRC32C;
import static org.junit.jupiter.api.Assertions.*;
@@ -109,6 +113,88 @@ class S3ChecksumTest {
assertEquals(ChecksumType.FULL_OBJECT, checksum.getChecksumType());
}
@Test
void fullObjectCombinedFromPartsMatchesTheChecksumOfTheWholeObject() {
Random random = new Random(4145);
List<int[]> splits = List.of(
new int[]{0},
new int[]{1},
new int[]{5 * 1024 * 1024, 17},
new int[]{0, 9, 0, 64, 1},
new int[]{1023, 1024, 1025, 65537, 3});
for (ChecksumAlgorithm algorithm : List.of(ChecksumAlgorithm.CRC32, ChecksumAlgorithm.CRC32C,
ChecksumAlgorithm.CRC64NVME)) {
for (int[] sizes : splits) {
ByteArrayOutputStream whole = new ByteArrayOutputStream();
List<Part> parts = new ArrayList<>();
for (int i = 0; i < sizes.length; i++) {
byte[] data = new byte[sizes[i]];
random.nextBytes(data);
whole.writeBytes(data);
parts.add(part(i + 1, algorithm, data));
}
S3Checksum combined = S3Checksum.fullObject(algorithm, parts);
assertEquals(algorithm.compute(whole.toByteArray()), combined.valueFor(algorithm),
algorithm + " " + Arrays.toString(sizes));
assertEquals(ChecksumType.FULL_OBJECT, combined.getChecksumType());
}
}
}
@Test
void fullObjectCombinesPartsLongerThanAnArrayCanHold() {
long zeroRunLength = (1L << 31) + 13;
byte[] head = "head".getBytes(StandardCharsets.UTF_8);
byte[] tail = "tail".getBytes(StandardCharsets.UTF_8);
CRC32C zeroRun = new CRC32C();
CRC32C whole = new CRC32C();
whole.update(head);
byte[] zeros = new byte[1 << 20];
for (long remaining = zeroRunLength; remaining > 0; remaining -= zeros.length) {
int chunk = (int) Math.min(zeros.length, remaining);
zeroRun.update(zeros, 0, chunk);
whole.update(zeros, 0, chunk);
}
whole.update(tail);
Part middle = new Part(2, "\"etag\"", zeroRunLength);
middle.getChecksum().setChecksumCRC32C(crc32Base64(zeroRun.getValue()));
S3Checksum combined = S3Checksum.fullObject(ChecksumAlgorithm.CRC32C, List.of(
part(1, ChecksumAlgorithm.CRC32C, head), middle, part(3, ChecksumAlgorithm.CRC32C, tail)));
assertEquals(crc32Base64(whole.getValue()), combined.getChecksumCRC32C());
}
@Test
void fullObjectFromPartsWithoutDeclaredAlgorithmGetsCrc64Nvme() {
byte[] first = "part1".getBytes(StandardCharsets.UTF_8);
byte[] second = "part2".getBytes(StandardCharsets.UTF_8);
S3Checksum combined = S3Checksum.fullObject(null, List.of(part(1, null, first), part(2, null, second)));
assertEquals(S3Checksum.crc64NvmeBase64("part1part2".getBytes(StandardCharsets.UTF_8)),
combined.getChecksumCRC64NVME());
}
@Test
void fullObjectFromPartsRejectsShaAlgorithms() {
List<Part> parts = List.of(part(1, ChecksumAlgorithm.SHA256, "data".getBytes(StandardCharsets.UTF_8)));
assertThrows(IllegalArgumentException.class, () -> S3Checksum.fullObject(ChecksumAlgorithm.SHA256, parts));
}
private static Part part(int partNumber, ChecksumAlgorithm algorithm, byte[] data) {
Part part = new Part(partNumber, "\"etag\"", data.length);
part.setChecksum(S3Checksum.of(algorithm, data));
return part;
}
private static String crc32Base64(long value) {
return Base64.getEncoder().encodeToString(ByteBuffer.allocate(Integer.BYTES).putInt((int) value).array());
}
@Test
void objectAttributesViewDropsTheSuffixOfCompositeValuesOnly() {
S3Checksum composite = S3Checksum.composite(ChecksumAlgorithm.CRC32, List.of("W0QpDQ==", "anCi7Q=="));
@@ -4,26 +4,32 @@ import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.s3.model.ChecksumType;
import io.github.hectorvent.floci.services.s3.model.MultipartUpload;
import io.github.hectorvent.floci.services.s3.model.Part;
import io.github.hectorvent.floci.services.s3.model.S3Checksum;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HexFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -129,39 +135,192 @@ class S3MultipartDiskAssemblyTest {
// Metadata (the Part record with its ETag) lives in memory, separate from the on-disk part
// file that assembly reads; deleting only the file makes assembly fail after the
// file-independent validation checks have passed.
Path part2File = dataRoot.resolve(".multipart").resolve(upload.getUploadId()).resolve("2");
assertTrue(Files.deleteIfExists(part2File), "test setup: part 2 file should exist before deletion");
assertTrue(Files.deleteIfExists(partFile(upload, 2)), "test setup: part 2 file should exist before deletion");
assertThrows(UncheckedIOException.class, () -> s3Service.completeMultipartUpload(
AwsException error = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "vanished.bin", upload.getUploadId(), List.of(1, 2), null, null));
assertEquals("InvalidPart", error.getErrorCode());
assertThrows(AwsException.class,
() -> s3Service.getObject("test-bucket", "vanished.bin"));
assertEquals(List.of(), assembledFiles(upload), "the partial assembly should be removed");
}
@Test
void partReadCopiesExactlyTheMeasuredBytes() throws IOException {
byte[] dest = new byte[7];
void failedStoreRemovesTheAssembledFileAndKeepsThePartsForARetry() throws Exception {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "public.bin", null,
null, null, null, null, "public-read");
s3Service.uploadPart("test-bucket", "public.bin", upload.getUploadId(), 1, "part1".getBytes(StandardCharsets.UTF_8));
s3Service.uploadPart("test-bucket", "public.bin", upload.getUploadId(), 2, "part2".getBytes(StandardCharsets.UTF_8));
// The public ACL is only rejected once the parts are assembled, when the object is stored.
s3Service.putPublicAccessBlock("test-bucket",
"<PublicAccessBlockConfiguration><BlockPublicAcls>true</BlockPublicAcls></PublicAccessBlockConfiguration>");
S3Service.readPart(new ByteArrayInputStream("abcde".getBytes(StandardCharsets.UTF_8)), dest, 1, 5, 1);
assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "public.bin", upload.getUploadId(), List.of(1, 2), null, null));
assertArrayEquals(new byte[] {0, 'a', 'b', 'c', 'd', 'e', 0}, dest);
assertEquals(List.of(), assembledFiles(upload), "the unstored assembly should be removed");
s3Service.putPublicAccessBlock("test-bucket",
"<PublicAccessBlockConfiguration><BlockPublicAcls>false</BlockPublicAcls></PublicAccessBlockConfiguration>");
s3Service.completeMultipartUpload("test-bucket", "public.bin", upload.getUploadId(), List.of(1, 2), null, null);
assertArrayEquals("part1part2".getBytes(StandardCharsets.UTF_8),
s3Service.getObject("test-bucket", "public.bin").getData());
assertFalse(Files.exists(partsDir(upload)), "a completed upload should leave no files behind");
}
@Test
void partThatGrewAfterBeingMeasuredFailsAssembly() {
byte[] dest = new byte[5];
void versionedMultipartObjectIsStoredOnceAndKeepsItsBytesWhenTheKeyIsOverwritten() throws Exception {
s3Service.putBucketVersioning("test-bucket", "Enabled");
byte[] part1 = repeatingBytes((byte) 'V', 9_000);
byte[] part2 = repeatingBytes((byte) 'W', 1_000);
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "versioned.bin", null);
s3Service.uploadPart("test-bucket", "versioned.bin", upload.getUploadId(), 1, part1);
s3Service.uploadPart("test-bucket", "versioned.bin", upload.getUploadId(), 2, part2);
assertThrows(IOException.class, () -> S3Service.readPart(
new ByteArrayInputStream("abcdef".getBytes(StandardCharsets.UTF_8)), dest, 0, 5, 1));
String versionId = s3Service.completeMultipartUpload("test-bucket", "versioned.bin", upload.getUploadId(),
List.of(1, 2), null, null).getVersionId();
List<Path> stored = storedObjectFiles();
assertEquals(2, stored.size(), "a versioned object has a versioned file and a current file: " + stored);
assertTrue(Files.isSameFile(stored.get(0), stored.get(1)), "the two should be one file, not a copy");
byte[] replacement = "replacement".getBytes(StandardCharsets.UTF_8);
String replacementVersionId = s3Service.putObject("test-bucket", "versioned.bin", replacement,
"text/plain", Map.of()).getVersionId();
assertArrayEquals(replacement, s3Service.getObject("test-bucket", "versioned.bin").getData());
assertArrayEquals(concat(part1, part2), s3Service.getObject("test-bucket", "versioned.bin", versionId).getData());
s3Service.deleteObject("test-bucket", "versioned.bin", replacementVersionId);
assertArrayEquals(concat(part1, part2), s3Service.getObject("test-bucket", "versioned.bin").getData());
}
@Test
void partThatShrankAfterBeingMeasuredFailsAssembly() {
byte[] dest = new byte[5];
void mismatchedFullObjectChecksumIsRejectedBeforeAnyPartIsRead() throws Exception {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "bad-digest.bin", null);
s3Service.uploadPart("test-bucket", "bad-digest.bin", upload.getUploadId(), 1, "part1".getBytes(StandardCharsets.UTF_8));
s3Service.uploadPart("test-bucket", "bad-digest.bin", upload.getUploadId(), 2, "part2".getBytes(StandardCharsets.UTF_8));
// Without its part files, a Complete that reached assembly would fail with an I/O error instead.
assertTrue(Files.deleteIfExists(partFile(upload, 1)), "test setup: part 1 file should exist before deletion");
assertTrue(Files.deleteIfExists(partFile(upload, 2)), "test setup: part 2 file should exist before deletion");
S3Checksum wrong = new S3Checksum();
wrong.setChecksumCRC64NVME(S3Checksum.crc64NvmeBase64("other".getBytes(StandardCharsets.UTF_8)));
assertThrows(IOException.class, () -> S3Service.readPart(
new ByteArrayInputStream("abcd".getBytes(StandardCharsets.UTF_8)), dest, 0, 5, 1));
AwsException error = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "bad-digest.bin", upload.getUploadId(), List.of(1, 2), null, wrong));
assertEquals("BadDigest", error.getErrorCode());
}
@Test
void reuploadedPartGetsANewFileAndTheReplacedFileIsRemoved() throws Exception {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "reupload.bin", null);
s3Service.uploadPart("test-bucket", "reupload.bin", upload.getUploadId(), 1, "first".getBytes(StandardCharsets.UTF_8));
Path firstFile = partFile(upload, 1);
s3Service.uploadPart("test-bucket", "reupload.bin", upload.getUploadId(), 1, "other".getBytes(StandardCharsets.UTF_8));
Path secondFile = partFile(upload, 1);
assertNotEquals(firstFile, secondFile, "a part file is never rewritten in place");
assertFalse(Files.exists(firstFile), "the replaced part's file should be removed");
assertArrayEquals("other".getBytes(StandardCharsets.UTF_8), Files.readAllBytes(secondFile));
s3Service.completeMultipartUpload("test-bucket", "reupload.bin", upload.getUploadId(), List.of(1), null, null);
assertArrayEquals("other".getBytes(StandardCharsets.UTF_8), s3Service.getObject("test-bucket", "reupload.bin").getData());
}
@Test
void completeThatReadAReplacedPartsRecordFailsInsteadOfStoringTheNewBytes() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "raced.bin", null);
s3Service.uploadPart("test-bucket", "raced.bin", upload.getUploadId(), 1, "first".getBytes(StandardCharsets.UTF_8));
Part staleRecord = upload.getParts().get(1);
s3Service.uploadPart("test-bucket", "raced.bin", upload.getUploadId(), 1, "other".getBytes(StandardCharsets.UTF_8));
// A record whose bytes a same-size re-upload replaced. The upload lock keeps a Complete from
// reading one, but if it ever did, it must fail rather than store the new bytes.
upload.getParts().put(1, staleRecord);
AwsException error = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "raced.bin", upload.getUploadId(), List.of(1), null, null));
assertEquals("InvalidPart", error.getErrorCode());
assertThrows(AwsException.class, () -> s3Service.getObject("test-bucket", "raced.bin"));
}
@Test
void restartRemovesTheFilesOfUploadsThatWereInProgress() throws Exception {
s3Service.putObject("test-bucket", "kept.bin", "kept".getBytes(StandardCharsets.UTF_8), "text/plain", Map.of());
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "interrupted.bin", null);
s3Service.uploadPart("test-bucket", "interrupted.bin", upload.getUploadId(), 1, "part1".getBytes(StandardCharsets.UTF_8));
// What a crash in the middle of CompleteMultipartUpload leaves next to the parts.
Files.write(partsDir(upload).resolve("assembled-interrupted"), new byte[1024]);
new S3Service(new InMemoryStorage<>(), new InMemoryStorage<>(), dataRoot, false);
assertFalse(Files.exists(dataRoot.resolve(".multipart")), "no upload outlives the process that held it");
assertEquals(1, storedObjectFiles().size(), "stored objects are left alone");
}
@Test
void resetRemovesTheFilesOfUploadsInProgress() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "reset.bin", null);
s3Service.uploadPart("test-bucket", "reset.bin", upload.getUploadId(), 1, "part1".getBytes(StandardCharsets.UTF_8));
s3Service.clear();
assertFalse(Files.exists(dataRoot.resolve(".multipart")), "a reset forgets every upload, so their files go too");
}
@Test
void appendedPartAddsExactlyItsRecordedBytes() throws IOException {
Path partFile = Files.write(tempDir.resolve("part"), "abcde".getBytes(StandardCharsets.UTF_8));
Path assembled = tempDir.resolve("assembled");
try (FileChannel out = FileChannel.open(assembled, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
out.write(ByteBuffer.wrap("X".getBytes(StandardCharsets.UTF_8)));
S3Service.appendPart(partFile, new Part(1, "\"etag\"", 5), out);
}
assertArrayEquals("Xabcde".getBytes(StandardCharsets.UTF_8), Files.readAllBytes(assembled));
}
@Test
void partThatGrewSinceItWasUploadedFailsAssembly() throws IOException {
Path partFile = Files.write(tempDir.resolve("part"), "abcdef".getBytes(StandardCharsets.UTF_8));
assertThrows(IOException.class, () -> appendToScratchFile(partFile, 5));
}
@Test
void partThatShrankSinceItWasUploadedFailsAssembly() throws IOException {
Path partFile = Files.write(tempDir.resolve("part"), "abcd".getBytes(StandardCharsets.UTF_8));
assertThrows(IOException.class, () -> appendToScratchFile(partFile, 5));
}
private void appendToScratchFile(Path partFile, long recordedSize) throws IOException {
try (FileChannel out = FileChannel.open(tempDir.resolve("scratch"),
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
S3Service.appendPart(partFile, new Part(1, "\"etag\"", recordedSize), out);
}
}
private Path partsDir(MultipartUpload upload) {
return dataRoot.resolve(".multipart").resolve(upload.getUploadId());
}
private Path partFile(MultipartUpload upload, int partNumber) {
return partsDir(upload).resolve(upload.getParts().get(partNumber).getStorageId());
}
private List<Path> assembledFiles(MultipartUpload upload) throws IOException {
try (Stream<Path> files = Files.list(partsDir(upload))) {
return files.filter(path -> path.getFileName().toString().startsWith("assembled")).toList();
}
}
private List<Path> storedObjectFiles() throws IOException {
try (Stream<Path> files = Files.walk(dataRoot)) {
return files.filter(path -> path.getFileName().toString().endsWith(".s3data")).toList();
}
}
private static byte[] repeatingBytes(byte value, int length) {
@@ -7,6 +7,7 @@ import io.github.hectorvent.floci.services.s3.model.ChecksumType;
import io.github.hectorvent.floci.services.s3.model.GetObjectAttributesResult;
import io.github.hectorvent.floci.services.s3.model.MultipartUpload;
import io.github.hectorvent.floci.services.s3.model.ObjectAttributeName;
import io.github.hectorvent.floci.services.s3.model.Part;
import io.github.hectorvent.floci.services.s3.model.S3Checksum;
import io.github.hectorvent.floci.services.s3.model.S3Object;
import org.junit.jupiter.api.BeforeEach;
@@ -555,6 +556,57 @@ class S3MultipartServiceTest {
assertEquals(compositeSha256(part1, part2), result.getChecksum().getChecksumSHA256());
}
@Test
void uploadPartThatTakesAnInMemoryUploadPastOneArrayIsRejected() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "huge.bin", null);
// The check reads the recorded part sizes, so recording one avoids allocating 2 GiB here.
Part recorded = new Part(1, "\"etag\"", Integer.MAX_VALUE - 16L);
recorded.setStorageId("1-recorded");
upload.getParts().put(1, recorded);
AwsException error = assertThrows(AwsException.class, () -> s3Service.uploadPart(
"test-bucket", "huge.bin", upload.getUploadId(), 2, new byte[16]));
assertEquals("EntityTooLarge", error.getErrorCode());
assertEquals(400, error.getHttpStatus());
assertEquals("Your proposed upload exceeds the maximum allowed object size.", error.getMessage());
assertNull(upload.getParts().get(2), "a rejected part is not recorded");
s3Service.uploadPart("test-bucket", "huge.bin", upload.getUploadId(), 1, new byte[16]);
assertEquals(16, upload.getParts().get(1).getSize(), "re-uploading a part replaces its size in the total");
}
@Test
void completeMultipartUploadOverOneArrayIsRejectedInMemoryMode() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "huge.bin", null);
s3Service.uploadPart("test-bucket", "huge.bin", upload.getUploadId(), 1, "part1".getBytes(StandardCharsets.UTF_8));
s3Service.uploadPart("test-bucket", "huge.bin", upload.getUploadId(), 2, "part2".getBytes(StandardCharsets.UTF_8));
// Concurrent UploadParts can each pass their own check, so Complete checks the total again.
upload.getParts().get(2).setSize(Integer.MAX_VALUE);
AwsException error = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "huge.bin", upload.getUploadId(), List.of(1, 2), null, null));
assertEquals("EntityTooLarge", error.getErrorCode());
assertThrows(AwsException.class, () -> s3Service.getObject("test-bucket", "huge.bin"));
}
@Test
void completeThatReadAReplacedPartsRecordFailsInsteadOfStoringTheNewBytes() {
MultipartUpload upload = s3Service.initiateMultipartUpload("test-bucket", "raced.bin", null);
s3Service.uploadPart("test-bucket", "raced.bin", upload.getUploadId(), 1, "first".getBytes(StandardCharsets.UTF_8));
Part staleRecord = upload.getParts().get(1);
s3Service.uploadPart("test-bucket", "raced.bin", upload.getUploadId(), 1, "other".getBytes(StandardCharsets.UTF_8));
// A record whose bytes a same-size re-upload replaced. The upload lock keeps a Complete from
// reading one, but if it ever did, it must fail rather than store the new bytes.
upload.getParts().put(1, staleRecord);
AwsException error = assertThrows(AwsException.class, () -> s3Service.completeMultipartUpload(
"test-bucket", "raced.bin", upload.getUploadId(), List.of(1), null, null));
assertEquals("InvalidPart", error.getErrorCode());
assertThrows(AwsException.class, () -> s3Service.getObject("test-bucket", "raced.bin"));
}
private static Map<Integer, S3Checksum> partChecksums(ChecksumAlgorithm algorithm, byte[]... parts) {
Map<Integer, S3Checksum> checksums = new HashMap<>();
for (int i = 0; i < parts.length; i++) {