Add method to remove a Sone from the database.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 1 Nov 2014 12:57:48 +0000 (13:57 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 1 Nov 2014 12:57:48 +0000 (13:57 +0100)
src/main/java/net/pterodactylus/sone/database/SoneStore.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java

index 010fa60..3684d48 100644 (file)
@@ -10,5 +10,6 @@ import net.pterodactylus.sone.data.Sone;
 public interface SoneStore {
 
        void storeSone(Sone sone);
+       void removeSone(Sone sone);
 
 }
index 5a8fb00..3c1dc74 100644 (file)
@@ -180,6 +180,35 @@ public class MemoryDatabase extends AbstractService implements Database {
        public void storeSone(Sone sone) {
                lock.writeLock().lock();
                try {
+                       removeSone(sone);
+
+                       allSones.put(sone.getId(), sone);
+                       sonePosts.putAll(sone.getId(), sone.getPosts());
+                       for (Post post : sone.getPosts()) {
+                               allPosts.put(post.getId(), post);
+                       }
+                       sonePostReplies.putAll(sone.getId(), sone.getReplies());
+                       for (PostReply postReply : sone.getReplies()) {
+                               allPostReplies.put(postReply.getId(), postReply);
+                       }
+                       soneAlbums.putAll(sone.getId(), toAllAlbums.apply(sone));
+                       for (Album album : toAllAlbums.apply(sone)) {
+                               allAlbums.put(album.getId(), album);
+                       }
+                       soneImages.putAll(sone.getId(), toAllImages.apply(sone));
+                       for (Image image : toAllImages.apply(sone)) {
+                               allImages.put(image.getId(), image);
+                       }
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
+       @Override
+       public void removeSone(Sone sone) {
+               lock.writeLock().lock();
+               try {
+                       allSones.remove(sone.getId());
                        Collection<Post> removedPosts = sonePosts.removeAll(sone.getId());
                        for (Post removedPost : removedPosts) {
                                allPosts.remove(removedPost.getId());
@@ -199,24 +228,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                        for (Image removedImage : removedImages) {
                                allImages.remove(removedImage.getId());
                        }
-
-                       allSones.put(sone.getId(), sone);
-                       sonePosts.putAll(sone.getId(), sone.getPosts());
-                       for (Post post : sone.getPosts()) {
-                               allPosts.put(post.getId(), post);
-                       }
-                       sonePostReplies.putAll(sone.getId(), sone.getReplies());
-                       for (PostReply postReply : sone.getReplies()) {
-                               allPostReplies.put(postReply.getId(), postReply);
-                       }
-                       soneAlbums.putAll(sone.getId(), toAllAlbums.apply(sone));
-                       for (Album album : toAllAlbums.apply(sone)) {
-                               allAlbums.put(album.getId(), album);
-                       }
-                       soneImages.putAll(sone.getId(), toAllImages.apply(sone));
-                       for (Image image : toAllImages.apply(sone)) {
-                               allImages.put(image.getId(), image);
-                       }
                } finally {
                        lock.writeLock().unlock();
                }
index 0753021..0df08ce 100644 (file)
@@ -27,6 +27,7 @@ import static net.pterodactylus.sone.Matchers.isPostReply;
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.empty;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -198,6 +199,13 @@ public class MemoryDatabaseTest {
        }
 
        @Test
+       public void storedAndRemovedSoneIsNotAvailable() {
+           storedSoneIsMadeAvailable();
+               memoryDatabase.removeSone(sone);
+               assertThat(memoryDatabase.getSones(), empty());
+       }
+
+       @Test
        public void postRecipientsAreDetectedCorrectly() {
                Post postWithRecipient = createPost(of(RECIPIENT_ID));
                memoryDatabase.storePost(postWithRecipient);