From: David ‘Bombe’ Roden Date: Sun, 7 Dec 2014 00:32:02 +0000 (+0100) Subject: Remove updated time setter from Sone, store update time in database. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=f333f58180a7f112394cd768d86c95a3c9edf794;p=Sone.git Remove updated time setter from Sone, store update time in database. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 291cb95..c3ac305 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -607,7 +607,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity)); LocalSone sone = database.registerLocalSone(ownIdentity); - SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId()); + SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, database, ownIdentity.getId()); eventBus.register(soneInserter); synchronized (soneInserters) { soneInserters.put(sone, soneInserter); diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 5ae2f73..8c24fc0 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -44,6 +44,7 @@ import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Reply; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.Sone.SoneStatus; +import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.freenet.StringBucket; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.util.io.Closer; @@ -100,7 +101,7 @@ public class SoneInserter extends AbstractService { /** The Freenet interface. */ private final FreenetInterface freenetInterface; - + private final Database database; private final SoneModificationDetector soneModificationDetector; private final long delay; private final String soneId; @@ -117,8 +118,8 @@ public class SoneInserter extends AbstractService { * @param soneId * The ID of the Sone to insert */ - public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, final String soneId) { - this(core, eventBus, freenetInterface, soneId, new SoneModificationDetector(new LockableFingerprintProvider() { + public SoneInserter(final Core core, EventBus eventBus, FreenetInterface freenetInterface, Database database, final String soneId) { + this(core, eventBus, freenetInterface, database, soneId, new SoneModificationDetector(new LockableFingerprintProvider() { @Override public boolean isLocked() { final Optional sone = core.getLocalSone(soneId); @@ -140,11 +141,12 @@ public class SoneInserter extends AbstractService { } @VisibleForTesting - SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, String soneId, SoneModificationDetector soneModificationDetector, long delay) { + SoneInserter(Core core, EventBus eventBus, FreenetInterface freenetInterface, Database database, String soneId, SoneModificationDetector soneModificationDetector, long delay) { super("Sone Inserter for “" + soneId + "”", false); this.core = core; this.eventBus = eventBus; this.freenetInterface = freenetInterface; + this.database = database; this.soneId = soneId; this.soneModificationDetector = soneModificationDetector; this.delay = delay; @@ -236,7 +238,7 @@ public class SoneInserter extends AbstractService { /* if so, bail out, don’t change anything. */ break; } - sone.setTime(insertTime); + database.updateSoneTime(sone, insertTime); sone.setLatestEdition(finalUri.getEdition()); core.touchConfiguration(); success = true; diff --git a/src/main/java/net/pterodactylus/sone/data/Sone.java b/src/main/java/net/pterodactylus/sone/data/Sone.java index 04c5f39..45909d5 100644 --- a/src/main/java/net/pterodactylus/sone/data/Sone.java +++ b/src/main/java/net/pterodactylus/sone/data/Sone.java @@ -259,15 +259,6 @@ public interface Sone extends Identified, Fingerprintable, Comparable { long getTime(); /** - * Sets the time of the last inserted update of this Sone. - * - * @param time - * The time of the update (in milliseconds since Jan 1, 1970 UTC) - * @return This Sone (for method chaining) - */ - Sone setTime(long time); - - /** * Returns the status of this Sone. * * @return The status of this Sone diff --git a/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java b/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java index 0ef220b..8875f9a 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/IdOnlySone.java @@ -72,11 +72,6 @@ public class IdOnlySone implements Sone { } @Override - public Sone setTime(long time) { - return null; - } - - @Override public SoneStatus getStatus() { return null; } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java index 9b46988..eccce4b 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/SoneImpl.java @@ -240,17 +240,6 @@ public class SoneImpl implements LocalSone { } /** - * Sets the time of the last inserted update of this Sone. - * - * @param time - * The time of the update (in milliseconds since Jan 1, 1970 UTC) - * @return This Sone (for method chaining) - */ - public Sone setTime(long time) { - return this; - } - - /** * Returns the status of this Sone. * * @return The status of this Sone diff --git a/src/main/java/net/pterodactylus/sone/database/SoneDatabase.java b/src/main/java/net/pterodactylus/sone/database/SoneDatabase.java index b81271f..a3df63a 100644 --- a/src/main/java/net/pterodactylus/sone/database/SoneDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/SoneDatabase.java @@ -13,4 +13,6 @@ public interface SoneDatabase extends SoneProvider, SoneBuilderFactory, SoneStor boolean isSoneKnown(Sone sone); void setSoneKnown(Sone sone); + void updateSoneTime(Sone sone, long soneTime); + } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 2d8d1be..d5d5396 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -275,7 +275,7 @@ public class MemoryDatabase extends AbstractService implements Database { /* if we’re still here, Sone was loaded successfully. */ lock.writeLock().lock(); try { - sone.setTime(soneTime); + updateSoneTime(sone, soneTime); sone.setProfile(profile); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); @@ -517,6 +517,11 @@ public class MemoryDatabase extends AbstractService implements Database { soneDatabase.setSoneKnown(sone.getId()); } + @Override + public void updateSoneTime(Sone sone, long soneTime) { + soneDatabase.updateSoneTime(sone.getId(), soneTime); + } + private void storePosts(String soneId, Collection posts) { postDatabase.storePosts(soneId, posts); } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneDatabase.java index 67f3425..8435959 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneDatabase.java @@ -1,6 +1,8 @@ package net.pterodactylus.sone.database.memory; +import java.util.HashMap; import java.util.HashSet; +import java.util.Map; import java.util.Set; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; @@ -17,6 +19,7 @@ public class MemorySoneDatabase { private final ReadWriteLock lock = new ReentrantReadWriteLock(); private final ConfigurationLoader configurationLoader; private final Set knownSones = new HashSet(); + private final Map soneUpdateTimes = new HashMap(); public MemorySoneDatabase(ConfigurationLoader configurationLoader) { this.configurationLoader = configurationLoader; @@ -68,4 +71,13 @@ public class MemorySoneDatabase { } } + void updateSoneTime(String soneId, long soneTime) { + lock.writeLock().lock(); + try { + soneUpdateTimes.put(soneId, soneTime); + } finally { + lock.writeLock().unlock(); + } + } + } diff --git a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java index 8a4bd52..53eda56 100644 --- a/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java +++ b/src/test/java/net/pterodactylus/sone/core/SoneInserterTest.java @@ -26,7 +26,6 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; -import net.pterodactylus.sone.core.SoneInserter.InsertInformation; import net.pterodactylus.sone.core.SoneInserter.ManifestCreator; import net.pterodactylus.sone.core.event.InsertionDelayChangedEvent; import net.pterodactylus.sone.core.event.SoneEvent; @@ -36,6 +35,7 @@ import net.pterodactylus.sone.core.event.SoneInsertingEvent; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.LocalSone; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.database.Database; import net.pterodactylus.sone.main.SonePlugin; import freenet.client.async.ManifestElement; @@ -61,6 +61,7 @@ public class SoneInserterTest { private final Core core = mock(Core.class); private final EventBus eventBus = mock(EventBus.class); private final FreenetInterface freenetInterface = mock(FreenetInterface.class); + private final Database database = mock(Database.class); @Before public void setupCore() { @@ -73,7 +74,7 @@ public class SoneInserterTest { @Test public void insertionDelayIsForwardedToSoneInserter() { EventBus eventBus = new AsyncEventBus(sameThreadExecutor()); - eventBus.register(new SoneInserter(core, eventBus, freenetInterface, "SoneId")); + eventBus.register(new SoneInserter(core, eventBus, freenetInterface, database, "SoneId")); eventBus.post(new InsertionDelayChangedEvent(15)); assertThat(SoneInserter.getInsertionDelay().get(), is(15)); } @@ -92,27 +93,27 @@ public class SoneInserterTest { public void isModifiedIsTrueIfModificationDetectorSaysSo() { SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class); when(soneModificationDetector.isModified()).thenReturn(true); - SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1); + SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, database, "SoneId", soneModificationDetector, 1); assertThat(soneInserter.isModified(), is(true)); } @Test public void isModifiedIsFalseIfModificationDetectorSaysSo() { SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class); - SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1); + SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, database, "SoneId", soneModificationDetector, 1); assertThat(soneInserter.isModified(), is(false)); } @Test public void lastFingerprintIsStoredCorrectly() { - SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId"); + SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, database, "SoneId"); soneInserter.setLastInsertFingerprint("last-fingerprint"); assertThat(soneInserter.getLastInsertFingerprint(), is("last-fingerprint")); } @Test public void soneInserterStopsWhenItShould() { - SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId"); + SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, database, "SoneId"); soneInserter.stop(); soneInserter.serviceRun(); } @@ -126,7 +127,7 @@ public class SoneInserterTest { SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class); when(soneModificationDetector.isEligibleForInsert()).thenReturn(true); when(freenetInterface.insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"))).thenReturn(finalUri); - final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1); + final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, database, "SoneId", soneModificationDetector, 1); doAnswer(new Answer() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { @@ -152,7 +153,7 @@ public class SoneInserterTest { Sone sone = createSone(insertUri, fingerprint); SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class); when(soneModificationDetector.isEligibleForInsert()).thenReturn(true); - final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1); + final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, database, "SoneId", soneModificationDetector, 1); when(freenetInterface.insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"))).thenAnswer(new Answer() { @Override public FreenetURI answer(InvocationOnMock invocation) throws Throwable { @@ -177,7 +178,7 @@ public class SoneInserterTest { String fingerprint = "fingerprint"; Sone sone = createSone(insertUri, fingerprint); SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class); - final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1); + final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, database, "SoneId", soneModificationDetector, 1); new Thread(new Runnable() { @Override public void run() { @@ -201,7 +202,7 @@ public class SoneInserterTest { Sone sone = createSone(insertUri, fingerprint); SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class); when(soneModificationDetector.isEligibleForInsert()).thenReturn(true); - final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, "SoneId", soneModificationDetector, 1); + final SoneInserter soneInserter = new SoneInserter(core, eventBus, freenetInterface, database, "SoneId", soneModificationDetector, 1); final SoneException soneException = new SoneException(new Exception()); when(freenetInterface.insertDirectory(eq(insertUri), any(HashMap.class), eq("index.html"))).thenAnswer(new Answer() { @Override @@ -226,7 +227,7 @@ public class SoneInserterTest { SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class); SoneInserter soneInserter = - new SoneInserter(core, eventBus, freenetInterface, "SoneId", + new SoneInserter(core, eventBus, freenetInterface, database, "SoneId", soneModificationDetector, 1); when(soneModificationDetector.isEligibleForInsert()).thenReturn(true); when(core.getSone("SoneId")).thenReturn(Optional.absent()); @@ -238,7 +239,7 @@ public class SoneInserterTest { SoneModificationDetector soneModificationDetector = mock(SoneModificationDetector.class); final SoneInserter soneInserter = - new SoneInserter(core, eventBus, freenetInterface, "SoneId", + new SoneInserter(core, eventBus, freenetInterface, database, "SoneId", soneModificationDetector, 1); Answer> stopInserterAndThrowException = new Answer>() { diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java index 8da6498..ee576b2 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java @@ -64,6 +64,7 @@ import net.pterodactylus.util.config.Value; import com.google.common.base.Optional; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; @@ -475,4 +476,13 @@ public class MemoryDatabaseTest { assertThat(stringValues.get("SoneFollowingTimes/0/Sone").getValue(), nullValue()); } + @Test + @Ignore("enable once Sones are built by the database") + public void soneUpdateTimeIsRetained() { + memoryDatabase.storeSone(sone); + memoryDatabase.updateSoneTime(sone, 1000L); + Sone updatedSone = memoryDatabase.getSone(SONE_ID).get(); + assertThat(updatedSone.getTime(), is(1000L)); + } + }