Move retrieval of post likes from Core to Post.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 5 Nov 2013 19:20:45 +0000 (20:20 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:53 +0000 (22:25 +0100)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/data/Post.java
src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java
src/main/java/net/pterodactylus/sone/database/PostDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java
src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java
src/main/java/net/pterodactylus/sone/template/PostAccessor.java
src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java

index 3ca5271..0caf03f 100644 (file)
@@ -413,23 +413,6 @@ public class Core extends AbstractService implements SoneProvider {
        }
 
        /**
-        * Returns all Sones that have liked the given post.
-        *
-        * @param post
-        *              The post to get the liking Sones for
-        * @return The Sones that like the given post
-        */
-       public Set<Sone> getLikes(Post post) {
-               Set<Sone> sones = new HashSet<Sone>();
-               for (Sone sone : getSones()) {
-                       if (sone.getLikedPostIds().contains(post.getId())) {
-                               sones.add(sone);
-                       }
-               }
-               return sones;
-       }
-
-       /**
         * Returns all Sones that have liked the given reply.
         *
         * @param reply
index 269c2e4..474f9d1 100644 (file)
@@ -20,6 +20,7 @@ package net.pterodactylus.sone.data;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.List;
+import java.util.Set;
 
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
@@ -127,6 +128,8 @@ public interface Post extends Identified {
        public void like(Sone localSone);
        public void unlike(Sone localSone);
 
+       Set<Sone> getLikes();
+
        List<PostReply> getReplies();
 
 }
index fa21e7a..587add9 100644 (file)
@@ -20,6 +20,7 @@ package net.pterodactylus.sone.data.impl;
 import static com.google.common.collect.FluentIterable.from;
 
 import java.util.List;
+import java.util.Set;
 
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
@@ -138,6 +139,11 @@ public class DefaultPost implements Post {
        }
 
        @Override
+       public Set<Sone> getLikes() {
+               return database.getLikes(this);
+       }
+
+       @Override
        public List<PostReply> getReplies() {
                return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR);
        }
index 8bdca6c..a25f245 100644 (file)
@@ -18,6 +18,7 @@
 package net.pterodactylus.sone.database;
 
 import java.util.Collection;
+import java.util.Set;
 
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
@@ -101,5 +102,6 @@ public interface PostDatabase {
 
        void likePost(Post post, Sone localSone);
        void unlikePost(Post post, Sone localSone);
+       Set<Sone> getLikes(Post post);
 
 }
index c73513d..975b8ab 100644 (file)
@@ -84,7 +84,8 @@ public class MemoryDatabase extends AbstractService implements Database {
 
        /** All posts by their Sones. */
        private final Multimap<String, Post> sonePosts = HashMultimap.create();
-       private final SetMultimap<String, String> likedPosts = HashMultimap.create();
+       private final SetMultimap<String, String> likedPostsBySone = HashMultimap.create();
+       private final SetMultimap<String, String> postLikingSones = HashMultimap.create();
 
        /** All posts by their recipient. */
        private final Multimap<String, Post> recipientPosts = HashMultimap.create();
@@ -290,7 +291,8 @@ public class MemoryDatabase extends AbstractService implements Database {
        public void likePost(Post post, Sone localSone) {
                lock.writeLock().lock();
                try {
-                       likedPosts.put(localSone.getId(), post.getId());
+                       likedPostsBySone.put(localSone.getId(), post.getId());
+                       postLikingSones.put(post.getId(), localSone.getId());
                } finally {
                        lock.writeLock().unlock();
                }
@@ -300,12 +302,23 @@ public class MemoryDatabase extends AbstractService implements Database {
        public void unlikePost(Post post, Sone localSone) {
                lock.writeLock().lock();
                try {
-                       likedPosts.remove(localSone.getId(), post.getId());
+                       likedPostsBySone.remove(localSone.getId(), post.getId());
+                       postLikingSones.remove(post.getId(), localSone.getId());
                } finally {
                        lock.writeLock().unlock();
                }
        }
 
+       @Override
+       public Set<Sone> getLikes(Post post) {
+               lock.readLock().lock();
+               try {
+                       return from(postLikingSones.get(post.getId())).transform(getSone()).transformAndConcat(this.<Sone>unwrap()).toSet();
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
        //
        // POSTSTORE METHODS
        //
@@ -837,4 +850,13 @@ public class MemoryDatabase extends AbstractService implements Database {
                };
        }
 
+       private static <T> Function<Optional<T>, Iterable<T>> unwrap() {
+               return new Function<Optional<T>, Iterable<T>>() {
+                       @Override
+                       public Iterable<T> apply(Optional<T> input) {
+                               return (input == null) ? Collections.<T>emptyList() : input.asSet();
+                       }
+               };
+       }
+
 }
index 62d6a53..baa3129 100644 (file)
@@ -289,7 +289,7 @@ public abstract class AbstractSoneCommand extends AbstractCommand {
                }
                postBuilder.put(prefix + "Time", post.getTime());
                postBuilder.put(prefix + "Text", encodeString(post.getText()));
-               postBuilder.put(encodeLikes(core.getLikes(post), prefix + "Likes."));
+               postBuilder.put(encodeLikes(post.getLikes(), prefix + "Likes."));
 
                return postBuilder;
        }
index d2acb4a..4a88ce1 100644 (file)
@@ -47,7 +47,7 @@ public class LikePostCommand extends AbstractSoneCommand {
                Post post = getPost(parameters, "Post");
                Sone sone = getMandatoryLocalSone(parameters, "Sone");
                post.like(sone);
-               return new Response("PostLiked", new SimpleFieldSetBuilder().put("LikeCount", getCore().getLikes(post).size()).get());
+               return new Response("PostLiked", new SimpleFieldSetBuilder().put("LikeCount", post.getLikes().size()).get());
        }
 
 }
index 74c3d9f..c3f9de3 100644 (file)
@@ -56,7 +56,7 @@ public class PostAccessor extends ReflectionAccessor {
                if ("replies".equals(member)) {
                        return from(post.getReplies()).filter(Reply.FUTURE_REPLY_FILTER).toList();
                } else if (member.equals("likes")) {
-                       return core.getLikes(post);
+                       return post.getLikes();
                } else if (member.equals("liked")) {
                        Sone currentSone = (Sone) templateContext.get("currentSone");
                        return (currentSone != null) && (currentSone.isLikedPostId(post.getId()));
index 4838a4e..058b142 100644 (file)
@@ -68,7 +68,7 @@ public class GetLikesAjaxPage extends JsonPage {
                        if (!post.isPresent()) {
                                return createErrorJsonObject("invalid-post-id");
                        }
-                       Set<Sone> sones = webInterface.getCore().getLikes(post.get());
+                       Set<Sone> sones = post.get().getLikes();
                        return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones));
                } else if ("reply".equals(type)) {
                        Optional<PostReply> reply = webInterface.getCore().getDatabase().getPostReply(id);