/* synchronize access on this on sones. */
private final Map<LocalSone, SoneRescuer> soneRescuers = new HashMap<LocalSone, SoneRescuer>();
- /** All known Sones. */
- private final Set<String> knownSones = new HashSet<String>();
-
/** The post database. */
private final Database database;
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));
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();
}
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<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
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) {
--- /dev/null
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class MemorySoneDatabase {
+
+ private final ReadWriteLock lock = new ReentrantReadWriteLock();
+ private final Set<String> knownSones = new HashSet<String>();
+ 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();
+ }
+ }
+
+}
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;
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;
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<String, Value<String>> configurationValues =
+ new HashMap<String, Value<String>>();
+
+ @Before
+ public void prepareConfigurationValues() {
+ when(configuration.getStringValue(anyString())).thenAnswer(new Answer<Value<String>>() {
+ @Override
+ public Value<String> answer(InvocationOnMock invocation) throws Throwable {
+ final String key = (String) invocation.getArguments()[0];
+ if (!configurationValues.containsKey(key)) {
+ TestValue<String> value = Mockito.spy(new TestValue<String>(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() {
@Test
public void storedAndRemovedSoneIsNotAvailable() {
- storedSoneIsMadeAvailable();
+ storedSoneIsMadeAvailable();
memoryDatabase.removeSone(sone);
assertThat(memoryDatabase.getSones(), empty());
}
assertThat(memoryDatabase.isFriend(sone, "Friend1"), is(false));
}
- private Map<String, Value<String>> prepareConfigurationValues() {
- final Map<String, Value<String>> configurationValues = new HashMap<String, Value<String>>();
- when(configuration.getStringValue(anyString())).thenAnswer(new Answer<Value<String>>() {
- @Override
- public Value<String> answer(InvocationOnMock invocation) throws Throwable {
- Value<String> stringValue = TestValue.from(null);
- configurationValues.put((String) invocation.getArguments()[0], stringValue);
- return stringValue;
- }
- });
- return configurationValues;
- }
-
@Test
- public void friendIsAddedCorrectlyToLocalSone() {
- Map<String, Value<String>> 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.<String>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<String, Value<String>> 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.<String>from(null)));
- assertThat(configurationValues.get("Sone/" + SONE_ID + "/Friends/1/ID"),
- is(TestValue.<String>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);
}
}