Store identities in database.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index c73513d..df39511 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();
@@ -94,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>() {
@@ -175,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
@@ -290,7 +303,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 +314,32 @@ 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();
                }
        }
 
+       public boolean isLiked(Post post, Sone sone) {
+               lock.readLock().lock();
+               try {
+                       return likedPostsBySone.containsEntry(sone.getId(), post.getId());
+               } finally {
+                       lock.readLock().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
        //
@@ -420,6 +454,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
        //
@@ -837,4 +913,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();
+                       }
+               };
+       }
+
 }