Use a multimap to store posts with recipienets.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index 61eb9be..d7e9f58 100644 (file)
@@ -84,7 +84,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        private final Multimap<String, Post> sonePosts = HashMultimap.create();
 
        /** All posts by their recipient. */
-       private final Map<String, Collection<Post>> recipientPosts = new HashMap<String, Collection<Post>>();
+       private final Multimap<String, Post> recipientPosts = HashMultimap.create();
 
        /** Whether posts are known. */
        private final Set<String> knownPosts = new HashSet<String>();
@@ -258,8 +258,8 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        /* remove all posts by the Sone. */
-                       getPostsFrom(sone.getId()).clear();
-                       for (Post post : posts) {
+                       Collection<Post> oldPosts = getPostsFrom(sone.getId());
+                       for (Post post : oldPosts) {
                                allPosts.remove(post.getId());
                                if (post.getRecipientId().isPresent()) {
                                        getPostsTo(post.getRecipientId().get()).remove(post);
@@ -627,26 +627,12 @@ public class MemoryDatabase extends AbstractService implements Database {
         * @return All posts
         */
        private Collection<Post> getPostsTo(String recipientId) {
-               Collection<Post> posts = null;
                lock.readLock().lock();
                try {
-                       posts = recipientPosts.get(recipientId);
+                       return recipientPosts.get(recipientId);
                } finally {
                        lock.readLock().unlock();
                }
-               if (posts != null) {
-                       return posts;
-               }
-
-               posts = new HashSet<Post>();
-               lock.writeLock().lock();
-               try {
-                       recipientPosts.put(recipientId, posts);
-               } finally {
-                       lock.writeLock().unlock();
-               }
-
-               return posts;
        }
 
        /** Loads the known posts. */