Store identities in database.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index 8cff1be..df39511 100644 (file)
@@ -33,8 +33,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.SortedSet;
-import java.util.TreeSet;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
@@ -42,19 +40,24 @@ import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.Image;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.PostReply;
-import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
+import net.pterodactylus.sone.data.impl.DefaultSoneBuilder;
 import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.DatabaseException;
 import net.pterodactylus.sone.database.PostDatabase;
 import net.pterodactylus.sone.database.SoneBuilder;
+import net.pterodactylus.sone.freenet.wot.Identity;
 import net.pterodactylus.util.config.Configuration;
 import net.pterodactylus.util.config.ConfigurationException;
 
 import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.HashMultimap;
 import com.google.common.collect.ListMultimap;
+import com.google.common.collect.Maps;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.SetMultimap;
 import com.google.common.collect.SortedSetMultimap;
 import com.google.common.collect.TreeMultimap;
 import com.google.common.util.concurrent.AbstractService;
@@ -73,22 +76,27 @@ public class MemoryDatabase extends AbstractService implements Database {
        /** The configuration. */
        private final Configuration configuration;
 
+       private final Map<String, Identity> identities = Maps.newHashMap();
        private final Map<String, Sone> sones = new HashMap<String, Sone>();
 
        /** All posts by their ID. */
        private final Map<String, Post> allPosts = new HashMap<String, Post>();
 
        /** All posts by their Sones. */
-       private final Map<String, Collection<Post>> sonePosts = new HashMap<String, Collection<Post>>();
+       private final Multimap<String, Post> sonePosts = 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 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>();
 
        /** 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>() {
@@ -100,7 +108,13 @@ public class MemoryDatabase extends AbstractService implements Database {
        }, PostReply.TIME_COMPARATOR);
 
        /** Replies by post. */
-       private final Map<String, SortedSet<PostReply>> postReplies = new HashMap<String, SortedSet<PostReply>>();
+       private final SortedSetMultimap<String, PostReply> postReplies = TreeMultimap.create(new Comparator<String>() {
+
+               @Override
+               public int compare(String leftString, String rightString) {
+                       return leftString.compareTo(rightString);
+               }
+       }, PostReply.TIME_COMPARATOR);
 
        /** Whether post replies are known. */
        private final Set<String> knownPostReplies = new HashSet<String>();
@@ -154,6 +168,36 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
+       public Optional<Identity> getIdentity(String identityId) {
+               lock.readLock().lock();
+               try {
+                       return fromNullable(identities.get(identityId));
+               } finally {
+                       lock.readLock().unlock();
+               }
+       }
+
+       @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
+                       public Optional<Sone> apply(String soneId) {
+                               return (soneId == null) ? Optional.<Sone>absent() : getSone(soneId);
+                       }
+               };
+       }
+
+       @Override
        public Optional<Sone> getSone(String soneId) {
                lock.readLock().lock();
                try {
@@ -195,7 +239,19 @@ public class MemoryDatabase extends AbstractService implements Database {
 
        @Override
        public SoneBuilder newSoneBuilder() {
-               return null;
+               return new DefaultSoneBuilder(this) {
+                       @Override
+                       public Sone build(Optional<SoneCreated> soneCreated) throws IllegalStateException {
+                               Sone sone = super.build(soneCreated);
+                               lock.writeLock().lock();
+                               try {
+                                       sones.put(sone.getId(), sone);
+                               } finally {
+                                       lock.writeLock().unlock();
+                               }
+                               return sone;
+                       }
+               };
        }
 
        //
@@ -203,6 +259,16 @@ public class MemoryDatabase extends AbstractService implements Database {
        //
 
        @Override
+       public Function<String, Optional<Post>> getPost() {
+               return new Function<String, Optional<Post>>() {
+                       @Override
+                       public Optional<Post> apply(String postId) {
+                               return (postId == null) ? Optional.<Post>absent() : getPost(postId);
+                       }
+               };
+       }
+
+       @Override
        public Optional<Post> getPost(String postId) {
                lock.readLock().lock();
                try {
@@ -214,7 +280,12 @@ public class MemoryDatabase extends AbstractService implements Database {
 
        @Override
        public Collection<Post> getPosts(String soneId) {
-               return new HashSet<Post>(getPostsFrom(soneId));
+               lock.readLock().lock();
+               try {
+                       return new HashSet<Post>(sonePosts.get(soneId));
+               } finally {
+                       lock.readLock().unlock();
+               }
        }
 
        @Override
@@ -228,6 +299,47 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }
 
+       @Override
+       public void likePost(Post post, Sone localSone) {
+               lock.writeLock().lock();
+               try {
+                       likedPostsBySone.put(localSone.getId(), post.getId());
+                       postLikingSones.put(post.getId(), localSone.getId());
+               } finally {
+                       lock.writeLock().unlock();
+               }
+       }
+
+       @Override
+       public void unlikePost(Post post, Sone localSone) {
+               lock.writeLock().lock();
+               try {
+                       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
        //
@@ -238,9 +350,9 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        allPosts.put(post.getId(), post);
-                       getPostsFrom(post.getSone().getId()).add(post);
+                       sonePosts.put(post.getSone().getId(), post);
                        if (post.getRecipientId().isPresent()) {
-                               getPostsTo(post.getRecipientId().get()).add(post);
+                               recipientPosts.put(post.getRecipientId().get(), post);
                        }
                } finally {
                        lock.writeLock().unlock();
@@ -253,9 +365,9 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        allPosts.remove(post.getId());
-                       getPostsFrom(post.getSone().getId()).remove(post);
+                       sonePosts.remove(post.getSone().getId(), post);
                        if (post.getRecipientId().isPresent()) {
-                               getPostsTo(post.getRecipientId().get()).remove(post);
+                               recipientPosts.remove(post.getRecipientId().get(), post);
                        }
                        post.getSone().removePost(post);
                } finally {
@@ -276,20 +388,20 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        /* remove all posts by the Sone. */
-                       getPostsFrom(sone.getId()).clear();
+                       sonePosts.removeAll(sone.getId());
                        for (Post post : posts) {
                                allPosts.remove(post.getId());
                                if (post.getRecipientId().isPresent()) {
-                                       getPostsTo(post.getRecipientId().get()).remove(post);
+                                       recipientPosts.remove(post.getRecipientId().get(), post);
                                }
                        }
 
                        /* add new posts. */
-                       getPostsFrom(sone.getId()).addAll(posts);
+                       sonePosts.putAll(sone.getId(), posts);
                        for (Post post : posts) {
                                allPosts.put(post.getId(), post);
                                if (post.getRecipientId().isPresent()) {
-                                       getPostsTo(post.getRecipientId().get()).add(post);
+                                       recipientPosts.put(post.getRecipientId().get(), post);
                                }
                        }
                } finally {
@@ -303,11 +415,11 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        /* remove all posts by the Sone. */
-                       getPostsFrom(sone.getId()).clear();
+                       sonePosts.removeAll(sone.getId());
                        for (Post post : sone.getPosts()) {
                                allPosts.remove(post.getId());
                                if (post.getRecipientId().isPresent()) {
-                                       getPostsTo(post.getRecipientId().get()).remove(post);
+                                       recipientPosts.remove(post.getRecipientId().get(), post);
                                }
                        }
                } finally {
@@ -342,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
        //
@@ -378,13 +532,7 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        allPostReplies.put(postReply.getId(), postReply);
-                       if (postReplies.containsKey(postReply.getPostId())) {
-                               postReplies.get(postReply.getPostId()).add(postReply);
-                       } else {
-                               TreeSet<PostReply> replies = new TreeSet<PostReply>(Reply.TIME_COMPARATOR);
-                               replies.add(postReply);
-                               postReplies.put(postReply.getPostId(), replies);
-                       }
+                       postReplies.put(postReply.getPostId(), postReply);
                } finally {
                        lock.writeLock().unlock();
                }
@@ -409,13 +557,7 @@ public class MemoryDatabase extends AbstractService implements Database {
                        for (PostReply postReply : postReplies) {
                                allPostReplies.put(postReply.getId(), postReply);
                                sonePostReplies.put(postReply.getSone().getId(), postReply);
-                               if (this.postReplies.containsKey(postReply.getPostId())) {
-                                       this.postReplies.get(postReply.getPostId()).add(postReply);
-                               } else {
-                                       TreeSet<PostReply> replies = new TreeSet<PostReply>(Reply.TIME_COMPARATOR);
-                                       replies.add(postReply);
-                                       this.postReplies.put(postReply.getPostId(), replies);
-                               }
+                               this.postReplies.put(postReply.getPostId(), postReply);
                        }
                } finally {
                        lock.writeLock().unlock();
@@ -427,12 +569,7 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        allPostReplies.remove(postReply.getId());
-                       if (postReplies.containsKey(postReply.getPostId())) {
-                               postReplies.get(postReply.getPostId()).remove(postReply);
-                               if (postReplies.get(postReply.getPostId()).isEmpty()) {
-                                       postReplies.remove(postReply.getPostId());
-                               }
-                       }
+                       postReplies.remove(postReply.getPostId(), postReply);
                } finally {
                        lock.writeLock().unlock();
                }
@@ -517,7 +654,9 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        allAlbums.put(album.getId(), album);
-                       albumChildren.put(album.getParent().getId(), album.getId());
+                       if (!album.isRoot()) {
+                               albumChildren.put(album.getParent().getId(), album.getId());
+                       }
                } finally {
                        lock.writeLock().unlock();
                }
@@ -661,68 +800,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        // PRIVATE METHODS
        //
 
-       /**
-        * Gets all posts for the given Sone, creating a new collection if there is
-        * none yet.
-        *
-        * @param soneId
-        *              The ID of the Sone to get the posts for
-        * @return All posts
-        */
-       private Collection<Post> getPostsFrom(String soneId) {
-               Collection<Post> posts = null;
-               lock.readLock().lock();
-               try {
-                       posts = sonePosts.get(soneId);
-               } finally {
-                       lock.readLock().unlock();
-               }
-               if (posts != null) {
-                       return posts;
-               }
-
-               posts = new HashSet<Post>();
-               lock.writeLock().lock();
-               try {
-                       sonePosts.put(soneId, posts);
-               } finally {
-                       lock.writeLock().unlock();
-               }
-
-               return posts;
-       }
-
-       /**
-        * Gets all posts that are directed the given Sone, creating a new collection
-        * if there is none yet.
-        *
-        * @param recipientId
-        *              The ID of the Sone to get the posts for
-        * @return All posts
-        */
-       private Collection<Post> getPostsTo(String recipientId) {
-               Collection<Post> posts = null;
-               lock.readLock().lock();
-               try {
-                       posts = 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. */
        private void loadKnownPosts() {
                lock.writeLock().lock();
@@ -836,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();
+                       }
+               };
+       }
+
 }