From 287d3c1348c8d468c55a010c595a842f074a8e3e Mon Sep 17 00:00:00 2001 From: Tobias Hafner Date: Sun, 22 Dec 2024 13:43:39 +0100 Subject: [PATCH] Bugfix --- .../locking/CommitInstantsLog.java | 3 +- .../locking/CommitInstantsLogTest.java | 56 ++++++++++--------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/core/src/main/java/org/polypheny/db/transaction/locking/CommitInstantsLog.java b/core/src/main/java/org/polypheny/db/transaction/locking/CommitInstantsLog.java index 797bde6ba0..15a2cb899f 100644 --- a/core/src/main/java/org/polypheny/db/transaction/locking/CommitInstantsLog.java +++ b/core/src/main/java/org/polypheny/db/transaction/locking/CommitInstantsLog.java @@ -20,6 +20,7 @@ public class CommitInstantsLog { + public static final long NO_COMMIT_INSTANT = 0; private static HashMap lastCommit = new HashMap<>(); @@ -36,7 +37,7 @@ public void setOrUpdateLastCommit( long entryIdentifier, long version, long inst public long getLastCommit( long entryIdentifier, long version ) { VersionedEntryIdentifier identifier = getIdentifier( entryIdentifier, version ); - return lastCommit.get( identifier ); + return lastCommit.getOrDefault(identifier, NO_COMMIT_INSTANT); } diff --git a/core/src/test/java/org/polypheny/db/transaction/locking/CommitInstantsLogTest.java b/core/src/test/java/org/polypheny/db/transaction/locking/CommitInstantsLogTest.java index 94bda74382..f1494579e5 100644 --- a/core/src/test/java/org/polypheny/db/transaction/locking/CommitInstantsLogTest.java +++ b/core/src/test/java/org/polypheny/db/transaction/locking/CommitInstantsLogTest.java @@ -18,32 +18,34 @@ import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class CommitInstantsLogTest { + CommitInstantsLog log; + @BeforeEach void setUp() { log = new CommitInstantsLog(); } + @Test void testSetOrUpdateLastCommitAndGetLastCommit() { long entryId = 1L; long version = 1L; long instant = 1000L; - log.setOrUpdateLastCommit(entryId, version, instant); - long retrievedInstant = log.getLastCommit(entryId, version); + log.setOrUpdateLastCommit( entryId, version, instant ); + long retrievedInstant = log.getLastCommit( entryId, version ); - assertEquals(instant, retrievedInstant, "The retrieved instant should match the updated instant."); + assertEquals( instant, retrievedInstant, "The retrieved instant should match the updated instant." ); } + @Test void testSetOrUpdateLastCommitOverridesPreviousCommit() { long entryId = 1L; @@ -51,48 +53,50 @@ void testSetOrUpdateLastCommitOverridesPreviousCommit() { long firstInstant = 1000L; long secondInstant = 2000L; - log.setOrUpdateLastCommit(entryId, version, firstInstant); - log.setOrUpdateLastCommit(entryId, version, secondInstant); - long retrievedInstant = log.getLastCommit(entryId, version); + log.setOrUpdateLastCommit( entryId, version, firstInstant ); + log.setOrUpdateLastCommit( entryId, version, secondInstant ); + long retrievedInstant = log.getLastCommit( entryId, version ); - assertEquals(secondInstant, retrievedInstant, "The retrieved instant should match the most recent update."); + assertEquals( secondInstant, retrievedInstant, "The retrieved instant should match the most recent update." ); } + @Test void testGetLastCommitForNonexistentEntry() { long entryId = 1L; long version = 1L; - Exception exception = assertThrows(NullPointerException.class, () -> { - log.getLastCommit(entryId, version); - }); + long retrievedInstant = log.getLastCommit( entryId, version ); - assertNotNull(exception, "An exception should be thrown for a nonexistent entry."); + assertEquals( CommitInstantsLog.NO_COMMIT_INSTANT, retrievedInstant ); } + @Test void testRemoveEntry() { long entryId = 1L; long version = 1L; long instant = 1000L; - log.setOrUpdateLastCommit(entryId, version, instant); - log.removeEntry(entryId, version); + log.setOrUpdateLastCommit( entryId, version, instant ); + log.removeEntry( entryId, version ); + + long retrievedInstant = log.getLastCommit( entryId, version ); - assertThrows(NullPointerException.class, () -> { - log.getLastCommit(entryId, version); - }, "An exception should be thrown after removing the entry."); + assertEquals( CommitInstantsLog.NO_COMMIT_INSTANT, retrievedInstant ); } + @Test void testRemoveEntryForNonexistentEntry() { long entryId = 1L; long version = 1L; - assertDoesNotThrow(() -> log.removeEntry(entryId, version), - "Removing a nonexistent entry should not throw an exception."); + assertDoesNotThrow( () -> log.removeEntry( entryId, version ), + "Removing a nonexistent entry should not throw an exception." ); } + @Test void testMultipleEntries() { long entryId1 = 1L; @@ -103,13 +107,13 @@ void testMultipleEntries() { long version2 = 2L; long instant2 = 2000L; - log.setOrUpdateLastCommit(entryId1, version1, instant1); - log.setOrUpdateLastCommit(entryId2, version2, instant2); + log.setOrUpdateLastCommit( entryId1, version1, instant1 ); + log.setOrUpdateLastCommit( entryId2, version2, instant2 ); - assertEquals(instant1, log.getLastCommit(entryId1, version1), - "The first entry's instant should match its updated value."); - assertEquals(instant2, log.getLastCommit(entryId2, version2), - "The second entry's instant should match its updated value."); + assertEquals( instant1, log.getLastCommit( entryId1, version1 ), + "The first entry's instant should match its updated value." ); + assertEquals( instant2, log.getLastCommit( entryId2, version2 ), + "The second entry's instant should match its updated value." ); } }