From ade604ddc1b54c33c8f6b48b593eec9b948151bb Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 20 Oct 2013 01:48:27 +0200 Subject: [PATCH] Use a multimap to store the posts directed at a Sone. --- .../sone/database/memory/MemoryDatabase.java | 43 +++------------------- 1 file changed, 6 insertions(+), 37 deletions(-) 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 a19290e..92f603f 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -87,7 +87,7 @@ public class MemoryDatabase extends AbstractService implements Database { private final Multimap sonePosts = HashMultimap.create(); /** All posts by their recipient. */ - private final Map> recipientPosts = new HashMap>(); + private final Multimap recipientPosts = HashMultimap.create(); /** Whether posts are known. */ private final Set knownPosts = new HashSet(); @@ -260,7 +260,7 @@ public class MemoryDatabase extends AbstractService implements Database { allPosts.put(post.getId(), post); sonePosts.put(post.getSone().getId(), post); if (post.getRecipientId().isPresent()) { - getPostsTo(post.getRecipientId().get()).add(post); + recipientPosts.put(post.getRecipientId().get(), post); } } finally { lock.writeLock().unlock(); @@ -275,7 +275,7 @@ public class MemoryDatabase extends AbstractService implements Database { allPosts.remove(post.getId()); sonePosts.remove(post.getSone().getId(), post); if (post.getRecipientId().isPresent()) { - getPostsTo(post.getRecipientId().get()).remove(post); + recipientPosts.remove(post.getRecipientId().get(), post); } post.getSone().removePost(post); } finally { @@ -300,7 +300,7 @@ public class MemoryDatabase extends AbstractService implements Database { for (Post post : posts) { allPosts.remove(post.getId()); if (post.getRecipientId().isPresent()) { - getPostsTo(post.getRecipientId().get()).remove(post); + recipientPosts.remove(post.getRecipientId().get(), post); } } @@ -309,7 +309,7 @@ public class MemoryDatabase extends AbstractService implements Database { for (Post post : posts) { allPosts.put(post.getId(), post); if (post.getRecipientId().isPresent()) { - getPostsTo(post.getRecipientId().get()).add(post); + recipientPosts.put(post.getRecipientId().get(), post); } } } finally { @@ -327,7 +327,7 @@ public class MemoryDatabase extends AbstractService implements Database { for (Post post : sone.getPosts()) { allPosts.remove(post.getId()); if (post.getRecipientId().isPresent()) { - getPostsTo(post.getRecipientId().get()).remove(post); + recipientPosts.remove(post.getRecipientId().get(), post); } } } finally { @@ -681,37 +681,6 @@ public class MemoryDatabase extends AbstractService implements Database { // PRIVATE METHODS // - /** - * Gets all posts that are directed the given Sone, creating a new collection - * if there is none yet. - * - * @param recipientId - * The ID of the Sone to get the posts for - * @return All posts - */ - private Collection getPostsTo(String recipientId) { - Collection posts = null; - lock.readLock().lock(); - try { - posts = recipientPosts.get(recipientId); - } finally { - lock.readLock().unlock(); - } - if (posts != null) { - return posts; - } - - posts = new HashSet(); - lock.writeLock().lock(); - try { - recipientPosts.put(recipientId, posts); - } finally { - lock.writeLock().unlock(); - } - - return posts; - } - /** Loads the known posts. */ private void loadKnownPosts() { lock.writeLock().lock(); -- 2.7.4