From 70ac785b339120420da70a25e1651cd7b81aec0f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 1 Nov 2014 13:57:48 +0100 Subject: [PATCH] Add method to remove a Sone from the database. --- .../net/pterodactylus/sone/database/SoneStore.java | 1 + .../sone/database/memory/MemoryDatabase.java | 47 +++++++++++++--------- .../sone/database/memory/MemoryDatabaseTest.java | 8 ++++ 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/database/SoneStore.java b/src/main/java/net/pterodactylus/sone/database/SoneStore.java index 010fa60..3684d48 100644 --- a/src/main/java/net/pterodactylus/sone/database/SoneStore.java +++ b/src/main/java/net/pterodactylus/sone/database/SoneStore.java @@ -10,5 +10,6 @@ import net.pterodactylus.sone.data.Sone; public interface SoneStore { void storeSone(Sone sone); + void removeSone(Sone sone); } 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 5a8fb00..3c1dc74 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -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 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(); } 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 0753021..0df08ce 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java @@ -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); -- 2.7.4