Store the “known” status of a post in the database.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index bdc247e..4bf6643 100644 (file)
@@ -95,6 +95,8 @@ public class MemoryDatabase extends AbstractService implements Database {
 
        /** All post replies by their ID. */
        private final Map<String, PostReply> allPostReplies = new HashMap<String, PostReply>();
+       private final SetMultimap<String, String> likedPostRepliesBySone = HashMultimap.create();
+       private final SetMultimap<String, String> postReplyLikingSones = HashMultimap.create();
 
        /** Replies sorted by Sone. */
        private final SortedSetMultimap<String, PostReply> sonePostReplies = TreeMultimap.create(new Comparator<String>() {
@@ -176,6 +178,16 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
+       public void storeIdentity(Identity identitiy) {
+               lock.writeLock().lock();
+               try {
+                       identities.put(identitiy.getId(), identitiy);
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
+       @Override
        public Function<String, Optional<Sone>> getSone() {
                return new Function<String, Optional<Sone>>() {
                        @Override
@@ -226,6 +238,16 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
+       public void storeSone(Sone sone) {
+               lock.writeLock().lock();
+               try {
+                       sones.put(sone.getId(), sone);
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
+       @Override
        public SoneBuilder newSoneBuilder() {
                return new DefaultSoneBuilder(this) {
                        @Override
@@ -287,6 +309,39 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       /**
+        * Returns whether the given post is known.
+        *
+        * @param post
+        *              The post
+        * @return {@code true} if the post is known, {@code false} otherwise
+        */
+       @Override
+       public boolean isPostKnown(Post post) {
+               lock.readLock().lock();
+               try {
+                       return knownPosts.contains(post.getId());
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
+       /**
+        * Sets whether the given post is known.
+        *
+        * @param post
+        *              The post
+        */
+       @Override
+       public void setPostKnown(Post post) {
+               lock.writeLock().lock();
+               try {
+                       knownPosts.add(post.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
        @Override
        public void likePost(Post post, Sone localSone) {
                lock.writeLock().lock();
@@ -312,7 +367,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        public boolean isLiked(Post post, Sone sone) {
                lock.readLock().lock();
                try {
-                       return likedPostsBySone.containsEntry(sone, post);
+                       return likedPostsBySone.containsEntry(sone.getId(), post.getId());
                } finally {
                        lock.readLock().unlock();
                }
@@ -442,6 +497,48 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       @Override
+       public void likePostReply(PostReply postReply, Sone localSone) {
+               lock.writeLock().lock();
+               try {
+                       likedPostRepliesBySone.put(localSone.getId(), postReply.getId());
+                       postReplyLikingSones.put(postReply.getId(), localSone.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
+       @Override
+       public void unlikePostReply(PostReply postReply, Sone localSone) {
+               lock.writeLock().lock();
+               try {
+                       likedPostRepliesBySone.remove(localSone.getId(), postReply.getId());
+                       postReplyLikingSones.remove(postReply.getId(), localSone.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
+       @Override
+       public boolean isLiked(PostReply postReply, Sone sone) {
+               lock.readLock().lock();
+               try {
+                       return postReplyLikingSones.containsEntry(postReply.getId(), sone.getId());
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
+       @Override
+       public Set<Sone> getLikes(PostReply postReply) {
+               lock.readLock().lock();
+               try {
+                       return from(postReplyLikingSones.get(postReply.getId())).transform(getSone()).transformAndConcat(this.<Sone>unwrap()).toSet();
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
        //
        // POSTREPLYSTORE METHODS
        //
@@ -702,47 +799,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        //
-       // PACKAGE-PRIVATE METHODS
-       //
-
-       /**
-        * Returns whether the given post is known.
-        *
-        * @param post
-        *              The post
-        * @return {@code true} if the post is known, {@code false} otherwise
-        */
-       boolean isPostKnown(Post post) {
-               lock.readLock().lock();
-               try {
-                       return knownPosts.contains(post.getId());
-               } finally {
-                       lock.readLock().unlock();
-               }
-       }
-
-       /**
-        * Sets whether the given post is known.
-        *
-        * @param post
-        *              The post
-        * @param known
-        *              {@code true} if the post is known, {@code false} otherwise
-        */
-       void setPostKnown(Post post, boolean known) {
-               lock.writeLock().lock();
-               try {
-                       if (known) {
-                               knownPosts.add(post.getId());
-                       } else {
-                               knownPosts.remove(post.getId());
-                       }
-               } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
-       //
        // PRIVATE METHODS
        //