From: David ‘Bombe’ Roden Date: Sat, 19 Oct 2013 23:48:27 +0000 (+0200) Subject: Use a multimap to store the posts directed at a Sone. X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=ade604ddc1b54c33c8f6b48b593eec9b948151bb Use a multimap to store the posts directed at a 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 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();