From 38a475e733de40e15bb54c983717e5b68bbfda17 Mon Sep 17 00:00:00 2001 From: Arne Franken Date: Sat, 1 Jun 2024 10:03:00 +0200 Subject: [PATCH] Prepare version storage --- .../testing/s3mock/store/ObjectStore.java | 5 ++- .../s3mock/store/S3ObjectVersions.java | 44 +++++++++++++++++++ .../s3mock/store/S3ObjectVersionsTest.java | 21 +++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 server/src/main/java/com/adobe/testing/s3mock/store/S3ObjectVersions.java create mode 100644 server/src/test/kotlin/com/adobe/testing/s3mock/store/S3ObjectVersionsTest.java diff --git a/server/src/main/java/com/adobe/testing/s3mock/store/ObjectStore.java b/server/src/main/java/com/adobe/testing/s3mock/store/ObjectStore.java index 9fd6f602a..d411a4a5b 100644 --- a/server/src/main/java/com/adobe/testing/s3mock/store/ObjectStore.java +++ b/server/src/main/java/com/adobe/testing/s3mock/store/ObjectStore.java @@ -54,6 +54,7 @@ public class ObjectStore extends StoreBase { private static final Logger LOG = LoggerFactory.getLogger(ObjectStore.class); private static final String META_FILE = "objectMetadata.json"; private static final String DATA_FILE = "binaryData"; + private static final String VERSIONS_FILE = "versions.json"; /** * This map stores one lock object per S3Object ID. @@ -471,11 +472,11 @@ private Path getObjectFolderPath(BucketMetadata bucket, UUID id) { } private Path getMetaFilePath(BucketMetadata bucket, UUID id) { - return Paths.get(getObjectFolderPath(bucket, id).toString(), META_FILE); + return getObjectFolderPath(bucket, id).resolve(META_FILE); } private Path getDataFilePath(BucketMetadata bucket, UUID id) { - return Paths.get(getObjectFolderPath(bucket, id).toString(), DATA_FILE); + return getObjectFolderPath(bucket, id).resolve(DATA_FILE); } private void writeMetafile(BucketMetadata bucket, S3ObjectMetadata s3ObjectMetadata) { diff --git a/server/src/main/java/com/adobe/testing/s3mock/store/S3ObjectVersions.java b/server/src/main/java/com/adobe/testing/s3mock/store/S3ObjectVersions.java new file mode 100644 index 000000000..0b0deb151 --- /dev/null +++ b/server/src/main/java/com/adobe/testing/s3mock/store/S3ObjectVersions.java @@ -0,0 +1,44 @@ +/************************************************************************* + * ADOBE CONFIDENTIAL + * ___________________ + * + * Copyright 2024 Adobe + * All Rights Reserved. + * + * NOTICE: All information contained herein is, and remains + * the property of Adobe and its suppliers, if any. The intellectual + * and technical concepts contained herein are proprietary to Adobe + * and its suppliers and are protected by all applicable intellectual + * property laws, including trade secret and copyright laws. + * Dissemination of this information or reproduction of this material + * is strictly forbidden unless prior written permission is obtained + * from Adobe. + **************************************************************************/ + +package com.adobe.testing.s3mock.store; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.atomic.AtomicInteger; + +public record S3ObjectVersions( + UUID id, + Map versions, + AtomicInteger latestVersionPointer +) { + + public S3ObjectVersions(UUID id) { + this(id, new HashMap<>(), new AtomicInteger(0)); + } + + public String createVersion() { + String versionId = UUID.randomUUID().toString(); + versions.put(latestVersionPointer.incrementAndGet(), versionId); + return versionId; + } + + public String getLatestVersion() { + return versions.get(latestVersionPointer.get()); + } +} diff --git a/server/src/test/kotlin/com/adobe/testing/s3mock/store/S3ObjectVersionsTest.java b/server/src/test/kotlin/com/adobe/testing/s3mock/store/S3ObjectVersionsTest.java new file mode 100644 index 000000000..25e53e2b4 --- /dev/null +++ b/server/src/test/kotlin/com/adobe/testing/s3mock/store/S3ObjectVersionsTest.java @@ -0,0 +1,21 @@ +package com.adobe.testing.s3mock.store; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.util.UUID; +import org.junit.jupiter.api.Test; + +class S3ObjectVersionsTest { + + @Test + void testVersions() { + var iut = new S3ObjectVersions(UUID.randomUUID()); + assertThat(iut.getLatestVersion()).isNull(); + assertThat(iut.latestVersionPointer().get()).isZero(); + + var version = iut.createVersion(); + assertThat(version).isNotBlank(); + assertThat(iut.latestVersionPointer().get()).isOne(); + assertThat(iut.getLatestVersion()).isEqualTo(version); + } +}