From 551444095937c85ad3ae7009c0979c5f433036d3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 4 Dec 2014 22:39:47 +0100 Subject: [PATCH] Move storage of known Sones into database. --- .../java/net/pterodactylus/sone/core/Core.java | 36 +------ .../pterodactylus/sone/database/SoneDatabase.java | 5 + .../sone/database/memory/ConfigurationLoader.java | 8 ++ .../sone/database/memory/MemoryDatabase.java | 14 +++ .../sone/database/memory/MemorySoneDatabase.java | 71 +++++++++++++ .../sone/database/memory/MemoryDatabaseTest.java | 117 ++++++++++++++------- 6 files changed, 183 insertions(+), 68 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/database/memory/MemorySoneDatabase.java diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 0680d30..90b6e2e 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -158,9 +158,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /* synchronize access on this on sones. */ private final Map soneRescuers = new HashMap(); - /** All known Sones. */ - private final Set knownSones = new HashSet(); - /** The post database. */ private final Database database; @@ -669,9 +666,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, Sone sone = !newSone ? existingSone.get() : database.newSoneBuilder().from(identity).build(); sone.setLatestEdition(latestEdition); if (newSone) { - synchronized (knownSones) { - newSone = !knownSones.contains(sone.getId()); - } + newSone = !database.isSoneKnown(sone); sone.setKnown(!newSone); if (newSone) { eventBus.post(new NewSoneFoundEvent(sone)); @@ -939,9 +934,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, public void markSoneKnown(Sone sone) { if (!sone.isKnown()) { sone.setKnown(true); - synchronized (knownSones) { - knownSones.add(sone.getId()); - } + database.setSoneKnown(sone); eventBus.post(new MarkSoneKnownEvent(sone)); touchConfiguration(); } @@ -1283,17 +1276,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, try { preferences.saveTo(configuration); - /* save known Sones. */ - int soneCounter = 0; - synchronized (knownSones) { - for (String knownSoneId : knownSones) { - configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId); - } - configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null); - } - /* save Sone following times. */ - soneCounter = 0; + int soneCounter = 0; synchronized (soneFollowingTimes) { for (Entry soneFollowingTime : soneFollowingTimes.entrySet()) { configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey()); @@ -1326,20 +1310,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, private void loadConfiguration() { new PreferencesLoader(preferences).loadFrom(configuration); - /* load known Sones. */ - int soneCounter = 0; - while (true) { - String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null); - if (knownSoneId == null) { - break; - } - synchronized (knownSones) { - knownSones.add(knownSoneId); - } - } - /* load Sone following times. */ - soneCounter = 0; + int soneCounter = 0; while (true) { String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null); if (soneId == null) { diff --git a/src/main/java/net/pterodactylus/sone/database/SoneDatabase.java b/src/main/java/net/pterodactylus/sone/database/SoneDatabase.java index f5c5cda..b81271f 100644 --- a/src/main/java/net/pterodactylus/sone/database/SoneDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/SoneDatabase.java @@ -1,5 +1,7 @@ package net.pterodactylus.sone.database; +import net.pterodactylus.sone.data.Sone; + /** * Combines a {@link SoneProvider} and a {@link SoneStore} into a Sone * database. @@ -8,4 +10,7 @@ package net.pterodactylus.sone.database; */ public interface SoneDatabase extends SoneProvider, SoneBuilderFactory, SoneStore { + boolean isSoneKnown(Sone sone); + void setSoneKnown(Sone sone); + } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java index e65cf48..fa8a362 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java @@ -35,6 +35,14 @@ public class ConfigurationLoader { saveIds("Sone/" + soneId + "/Friends", friends); } + public synchronized Set loadKnownSones() { + return loadIds("KnownSones"); + } + + public synchronized void saveKnownSones(Set knownSones) { + saveIds("KnownSones", knownSones); + } + public synchronized Set loadKnownPosts() { return loadIds("KnownPosts"); } 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 7ad40e0..7a548b9 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -132,6 +132,7 @@ public class MemoryDatabase extends AbstractService implements Database { private final Map allImages = new HashMap(); private final Multimap soneImages = HashMultimap.create(); + private final MemorySoneDatabase soneDatabase; private final MemoryPostDatabase postDatabase; private final MemoryBookmarkDatabase memoryBookmarkDatabase; private final MemoryFriendDatabase memoryFriendDatabase; @@ -149,6 +150,7 @@ public class MemoryDatabase extends AbstractService implements Database { this.soneProvider = soneProvider; this.configuration = configuration; this.configurationLoader = new ConfigurationLoader(configuration); + soneDatabase = new MemorySoneDatabase(configurationLoader); postDatabase = new MemoryPostDatabase(this, configurationLoader); memoryBookmarkDatabase = new MemoryBookmarkDatabase(this, configurationLoader); @@ -461,6 +463,7 @@ public class MemoryDatabase extends AbstractService implements Database { /** {@inheritDocs} */ @Override protected void doStart() { + soneDatabase.start(); postDatabase.start(); memoryBookmarkDatabase.start(); loadKnownPostReplies(); @@ -471,6 +474,7 @@ public class MemoryDatabase extends AbstractService implements Database { @Override protected void doStop() { try { + soneDatabase.stop(); postDatabase.stop(); memoryBookmarkDatabase.stop(); save(); @@ -501,6 +505,16 @@ public class MemoryDatabase extends AbstractService implements Database { } } + @Override + public boolean isSoneKnown(Sone sone) { + return soneDatabase.isKnownSone(sone.getId()); + } + + @Override + public void setSoneKnown(Sone sone) { + soneDatabase.setSoneKnown(sone.getId()); + } + 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 new file mode 100644 index 0000000..bef0224 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemorySoneDatabase.java @@ -0,0 +1,71 @@ +package net.pterodactylus.sone.database.memory; + +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import net.pterodactylus.sone.data.Sone; + +/** + * Groups {@link Sone}-related database functionality. + * + * @author David ‘Bombe’ Roden + */ +public class MemorySoneDatabase { + + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + private final Set knownSones = new HashSet(); + private final ConfigurationLoader configurationLoader; + + public MemorySoneDatabase(ConfigurationLoader configurationLoader) { + this.configurationLoader = configurationLoader; + } + + void start() { + loadKnownSones(); + } + + private void loadKnownSones() { + lock.writeLock().lock(); + try { + knownSones.clear(); + knownSones.addAll(configurationLoader.loadKnownSones()); + } finally { + lock.writeLock().unlock(); + } + } + + void stop() { + saveKnownSones(); + } + + boolean isKnownSone(String soneId) { + lock.readLock().lock(); + try { + return knownSones.contains(soneId); + } finally { + lock.readLock().unlock(); + } + } + + void setSoneKnown(String soneId) { + lock.writeLock().lock(); + try { + knownSones.add(soneId); + saveKnownSones(); + } finally { + lock.writeLock().unlock(); + } + } + + private void saveKnownSones() { + lock.readLock().lock(); + try { + configurationLoader.saveKnownSones(knownSones); + } finally { + lock.readLock().unlock(); + } + } + +} 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 7cba93b..ac53a5d 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java @@ -30,10 +30,12 @@ import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.emptyIterable; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; -import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -51,17 +53,19 @@ import net.pterodactylus.sone.TestPostBuilder; import net.pterodactylus.sone.TestPostReplyBuilder; import net.pterodactylus.sone.TestValue; import net.pterodactylus.sone.data.Album; -import net.pterodactylus.sone.data.impl.AlbumImpl; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Sone; +import net.pterodactylus.sone.data.impl.AlbumImpl; import net.pterodactylus.util.config.Configuration; +import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.config.Value; import com.google.common.base.Optional; import org.junit.Before; import org.junit.Test; +import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; @@ -77,6 +81,29 @@ public class MemoryDatabaseTest { private final Configuration configuration = mock(Configuration.class); private final MemoryDatabase memoryDatabase = new MemoryDatabase(null, configuration); private final Sone sone = mock(Sone.class); + private final Map> configurationValues = + new HashMap>(); + + @Before + public void prepareConfigurationValues() { + when(configuration.getStringValue(anyString())).thenAnswer(new Answer>() { + @Override + public Value answer(InvocationOnMock invocation) throws Throwable { + final String key = (String) invocation.getArguments()[0]; + if (!configurationValues.containsKey(key)) { + TestValue value = Mockito.spy(new TestValue(null) { + @Override + public void setValue(String newValue) throws ConfigurationException { + super.setValue(newValue); + configurationValues.put(key, this); + } + }); + configurationValues.put(key, value); + } + return configurationValues.get(key); + } + }); + } @Before public void setupSone() { @@ -216,7 +243,7 @@ public class MemoryDatabaseTest { @Test public void storedAndRemovedSoneIsNotAvailable() { - storedSoneIsMadeAvailable(); + storedSoneIsMadeAvailable(); memoryDatabase.removeSone(sone); assertThat(memoryDatabase.getSones(), empty()); } @@ -328,63 +355,81 @@ public class MemoryDatabaseTest { assertThat(memoryDatabase.isFriend(sone, "Friend1"), is(false)); } - private Map> prepareConfigurationValues() { - final Map> configurationValues = new HashMap>(); - when(configuration.getStringValue(anyString())).thenAnswer(new Answer>() { - @Override - public Value answer(InvocationOnMock invocation) throws Throwable { - Value stringValue = TestValue.from(null); - configurationValues.put((String) invocation.getArguments()[0], stringValue); - return stringValue; - } - }); - return configurationValues; - } - @Test - public void friendIsAddedCorrectlyToLocalSone() { - Map> configurationValues = prepareConfigurationValues(); + public void friendIsAddedCorrectlyToLocalSone() throws ConfigurationException { when(sone.isLocal()).thenReturn(true); memoryDatabase.addFriend(sone, "Friend1"); - assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID"), - is(TestValue.from("Friend1"))); - assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"), - is(TestValue.from(null))); + assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID").getValue(), + is("Friend1")); + assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID").getValue(), + nullValue()); } @Test - public void friendIsNotAddedToRemoteSone() { + public void friendIsNotAddedToRemoteSone() throws ConfigurationException { memoryDatabase.addFriend(sone, "Friend1"); - verify(configuration, never()).getStringValue(anyString()); + verify(configuration.getStringValue("Sone/" + SONE_ID + "/Friends/0/ID"), never()).setValue( + anyString()); } @Test - public void configurationIsWrittenOnceIfFriendIsAddedTwice() { - prepareConfigurationValues(); + public void configurationIsWrittenOnceIfFriendIsAddedTwice() throws ConfigurationException { when(sone.isLocal()).thenReturn(true); memoryDatabase.addFriend(sone, "Friend1"); memoryDatabase.addFriend(sone, "Friend1"); - verify(configuration, times(3)).getStringValue(anyString()); + verify(configuration.getStringValue("Sone/" + SONE_ID + "/Friends/0/ID")).setValue( + anyString()); } @Test - public void friendIsRemovedCorrectlyFromLocalSone() { - Map> configurationValues = prepareConfigurationValues(); + public void friendIsRemovedCorrectlyFromLocalSone() throws ConfigurationException { when(sone.isLocal()).thenReturn(true); memoryDatabase.addFriend(sone, "Friend1"); memoryDatabase.removeFriend(sone, "Friend1"); - assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID"), - is(TestValue.from(null))); - assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"), - is(TestValue.from(null))); + assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID").getValue(), + nullValue()); + assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID").getValue(), + nullValue()); } @Test - public void configurationIsNotWrittenWhenANonFriendIsRemoved() { - prepareConfigurationValues(); + public void configurationIsNotWrittenWhenANonFriendIsRemoved() throws ConfigurationException { when(sone.isLocal()).thenReturn(true); memoryDatabase.removeFriend(sone, "Friend1"); - verify(configuration).getStringValue(anyString()); + verify(configurationValues.get("Sone/" + SONE_ID + "/Friends/0/ID"), never()).setValue( + anyString()); + } + + @Test + public void newDatabaseKnowsNoSones() { + memoryDatabase.startAndWait(); + assertThat(memoryDatabase.isSoneKnown(sone), is(false)); + assertThat(configurationValues, hasKey("KnownSones/0/ID")); + assertThat(configurationValues, not(hasKey("KnownSones/1/ID"))); + } + + @Test + public void databaseLoadsKnownSonesCorrectly() { + configurationValues.put("KnownSones/0/ID", TestValue.from(SONE_ID)); + memoryDatabase.startAndWait(); + assertThat(memoryDatabase.isSoneKnown(sone), is(true)); + } + + @Test + public void databaseStoresKnownSonesCorrectly() throws ConfigurationException { + memoryDatabase.setSoneKnown(sone); + assertThat(configurationValues, hasKey("KnownSones/0/ID")); + assertThat(configurationValues.get("KnownSones/0/ID").getValue(), is(SONE_ID)); + assertThat(configurationValues, hasKey("KnownSones/1/ID")); + assertThat(configurationValues.get("KnownSones/1/ID").getValue(), nullValue()); + assertThat(configurationValues, not(hasKey("KnownSones/2/ID"))); + } + + @Test + public void stoppingTheDatabaseSavesTheKnownSones() throws ConfigurationException { + memoryDatabase.startAndWait(); + memoryDatabase.stopAndWait(); + verify(configurationValues.get("KnownSones/0/ID")).setValue(null); } } -- 2.7.4