From: David ‘Bombe’ Roden Date: Tue, 5 Nov 2013 19:20:45 +0000 (+0100) Subject: Move retrieval of post likes from Core to Post. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=4aec89e6f9c6eb0972be5e9a3e1363bb487a2898;p=Sone.git Move retrieval of post likes from Core to Post. --- diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 3ca5271..0caf03f 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -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 getLikes(Post post) { - Set sones = new HashSet(); - 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 diff --git a/src/main/java/net/pterodactylus/sone/data/Post.java b/src/main/java/net/pterodactylus/sone/data/Post.java index 269c2e4..474f9d1 100644 --- a/src/main/java/net/pterodactylus/sone/data/Post.java +++ b/src/main/java/net/pterodactylus/sone/data/Post.java @@ -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 getLikes(); + List getReplies(); } diff --git a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java index fa21e7a..587add9 100644 --- a/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java +++ b/src/main/java/net/pterodactylus/sone/data/impl/DefaultPost.java @@ -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 getLikes() { + return database.getLikes(this); + } + + @Override public List getReplies() { return from(database.getReplies(getId())).toSortedList(Reply.TIME_COMPARATOR); } diff --git a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java index 8bdca6c..a25f245 100644 --- a/src/main/java/net/pterodactylus/sone/database/PostDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/PostDatabase.java @@ -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 getLikes(Post post); } 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 c73513d..975b8ab 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -84,7 +84,8 @@ public class MemoryDatabase extends AbstractService implements Database { /** All posts by their Sones. */ private final Multimap sonePosts = HashMultimap.create(); - private final SetMultimap likedPosts = HashMultimap.create(); + private final SetMultimap likedPostsBySone = HashMultimap.create(); + private final SetMultimap postLikingSones = HashMultimap.create(); /** All posts by their recipient. */ private final Multimap 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 getLikes(Post post) { + lock.readLock().lock(); + try { + return from(postLikingSones.get(post.getId())).transform(getSone()).transformAndConcat(this.unwrap()).toSet(); + } finally { + lock.readLock().unlock(); + } + } + // // POSTSTORE METHODS // @@ -837,4 +850,13 @@ public class MemoryDatabase extends AbstractService implements Database { }; } + private static Function, Iterable> unwrap() { + return new Function, Iterable>() { + @Override + public Iterable apply(Optional input) { + return (input == null) ? Collections.emptyList() : input.asSet(); + } + }; + } + } diff --git a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java index 62d6a53..baa3129 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/AbstractSoneCommand.java @@ -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; } diff --git a/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java b/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java index d2acb4a..4a88ce1 100644 --- a/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java +++ b/src/main/java/net/pterodactylus/sone/fcp/LikePostCommand.java @@ -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()); } } diff --git a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java index 74c3d9f..c3f9de3 100644 --- a/src/main/java/net/pterodactylus/sone/template/PostAccessor.java +++ b/src/main/java/net/pterodactylus/sone/template/PostAccessor.java @@ -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())); diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java index 4838a4e..058b142 100644 --- a/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java +++ b/src/main/java/net/pterodactylus/sone/web/ajax/GetLikesAjaxPage.java @@ -68,7 +68,7 @@ public class GetLikesAjaxPage extends JsonPage { if (!post.isPresent()) { return createErrorJsonObject("invalid-post-id"); } - Set sones = webInterface.getCore().getLikes(post.get()); + Set sones = post.get().getLikes(); return createSuccessJsonObject().put("likes", sones.size()).put("sones", getSones(sones)); } else if ("reply".equals(type)) { Optional reply = webInterface.getCore().getDatabase().getPostReply(id);