Extract memory-backed post database into its own class.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryPostDatabase.java
index eed00ea..a16a36b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - MemoryPostDatabase.java - Copyright © 2013 David Roden
+ * Sone - MemoryPostDatabase.java - Copyright © 2014 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 
 package net.pterodactylus.sone.database.memory;
 
+import static com.google.common.base.Optional.fromNullable;
 import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.collect.FluentIterable.from;
+import static com.google.common.collect.HashMultimap.create;
 
 import java.util.Collection;
 import java.util.Collections;
@@ -25,21 +28,19 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-import java.util.UUID;
 import java.util.concurrent.locks.ReadWriteLock;
-import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.data.impl.AbstractPostBuilder;
-import net.pterodactylus.sone.database.PostBuilder;
+import net.pterodactylus.sone.database.DatabaseException;
 import net.pterodactylus.sone.database.PostDatabase;
-import net.pterodactylus.sone.database.SoneProvider;
 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.inject.Inject;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.SetMultimap;
 
 /**
  * Memory-based {@link PostDatabase} implementation.
@@ -48,130 +49,168 @@ import com.google.inject.Inject;
  */
 public class MemoryPostDatabase implements PostDatabase {
 
-       /** The lock. */
-       private final ReadWriteLock lock = new ReentrantReadWriteLock();
-
-       /** The Sone provider. */
-       private final SoneProvider soneProvider;
-
-       /** All posts by their ID. */
+       private final MemoryDatabase memoryDatabase;
+       private final ReadWriteLock readWriteLock;
+       private final Configuration configuration;
        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>>();
-
-       /** All posts by their recipient. */
-       private final Map<String, Collection<Post>> recipientPosts = new HashMap<String, Collection<Post>>();
-
-       /** Whether posts are known. */
+       private final Multimap<String, Post> sonePosts = create();
+       private final SetMultimap<String, String> likedPostsBySone = create();
+       private final SetMultimap<String, String> postLikingSones = create();
+       private final Multimap<String, Post> recipientPosts = create();
        private final Set<String> knownPosts = new HashSet<String>();
 
-       /**
-        * Creates a new memory database.
-        *
-        * @param soneProvider
-        *            The Sone provider
-        */
-       @Inject
-       public MemoryPostDatabase(SoneProvider soneProvider) {
-               this.soneProvider = soneProvider;
+       public MemoryPostDatabase(MemoryDatabase memoryDatabase, ReadWriteLock readWriteLock, Configuration configuration) {
+               this.memoryDatabase = memoryDatabase;
+               this.readWriteLock = readWriteLock;
+               this.configuration = configuration;
        }
 
-       //
-       // POSTPROVIDER METHODS
-       //
+       @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);
+                       }
+               };
+       }
 
-       /**
-        * {@inheritDocs}
-        */
        @Override
        public Optional<Post> getPost(String postId) {
-               lock.readLock().lock();
+               readWriteLock.readLock().lock();
                try {
-                       return Optional.fromNullable(allPosts.get(postId));
+                       return fromNullable(allPosts.get(postId));
                } finally {
-                       lock.readLock().unlock();
+                       readWriteLock.readLock().unlock();
                }
        }
 
-       /**
-        * {@inheritDocs}
-        */
        @Override
        public Collection<Post> getPosts(String soneId) {
-               return new HashSet<Post>(getPostsFrom(soneId));
+               readWriteLock.readLock().lock();
+               try {
+                       return new HashSet<Post>(sonePosts.get(soneId));
+               } finally {
+                       readWriteLock.readLock().unlock();
+               }
        }
 
-       /**
-        * {@inheritDocs}
-        */
        @Override
        public Collection<Post> getDirectedPosts(String recipientId) {
-               lock.readLock().lock();
+               readWriteLock.readLock().lock();
                try {
                        Collection<Post> posts = recipientPosts.get(recipientId);
-                       return (posts == null) ? Collections.<Post> emptySet() : new HashSet<Post>(posts);
+                       return (posts == null) ? Collections.<Post>emptySet() : new HashSet<Post>(posts);
                } finally {
-                       lock.readLock().unlock();
+                       readWriteLock.readLock().unlock();
                }
        }
 
-       //
-       // POSTBUILDERFACTORY METHODS
-       //
-
        /**
-        * {@inheritDocs}
+        * Returns whether the given post is known.
+        *
+        * @param post
+        *              The post
+        * @return {@code true} if the post is known, {@code false} otherwise
         */
        @Override
-       public PostBuilder newPostBuilder() {
-               return new MemoryPostBuilder(soneProvider);
+       public boolean isPostKnown(Post post) {
+               readWriteLock.readLock().lock();
+               try {
+                       return knownPosts.contains(post.getId());
+               } finally {
+                       readWriteLock.readLock().unlock();
+               }
        }
 
-       //
-       // POSTSTORE METHODS
-       //
-
        /**
-        * {@inheritDocs}
+        * Sets whether the given post is known.
+        *
+        * @param post
+        *              The post
         */
        @Override
+       public void setPostKnown(Post post) {
+               readWriteLock.writeLock().lock();
+               try {
+                       knownPosts.add(post.getId());
+               } finally {
+                       readWriteLock.writeLock().unlock();
+               }
+       }
+
+       @Override
+       public void likePost(Post post, Sone localSone) {
+               readWriteLock.writeLock().lock();
+               try {
+                       likedPostsBySone.put(localSone.getId(), post.getId());
+                       postLikingSones.put(post.getId(), localSone.getId());
+               } finally {
+                       readWriteLock.writeLock().unlock();
+               }
+       }
+
+       @Override
+       public void unlikePost(Post post, Sone localSone) {
+               readWriteLock.writeLock().lock();
+               try {
+                       likedPostsBySone.remove(localSone.getId(), post.getId());
+                       postLikingSones.remove(post.getId(), localSone.getId());
+               } finally {
+                       readWriteLock.writeLock().unlock();
+               }
+       }
+
+       public boolean isLiked(Post post, Sone sone) {
+               readWriteLock.readLock().lock();
+               try {
+                       return likedPostsBySone.containsEntry(sone.getId(), post.getId());
+               } finally {
+                       readWriteLock.readLock().unlock();
+               }
+       }
+
+       @Override
+       public Set<Sone> getLikes(Post post) {
+               readWriteLock.readLock().lock();
+               try {
+                       return from(postLikingSones.get(post.getId())).transform(memoryDatabase.getSone()).transformAndConcat(MemoryDatabase.<Sone>unwrap()).toSet();
+               } finally {
+                       readWriteLock.readLock().unlock();
+               }
+       }
+
+       @Override
        public void storePost(Post post) {
                checkNotNull(post, "post must not be null");
-               lock.writeLock().lock();
+               readWriteLock.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();
+                       readWriteLock.writeLock().unlock();
                }
        }
 
-       /**
-        * {@inheritDocs}
-        */
        @Override
        public void removePost(Post post) {
                checkNotNull(post, "post must not be null");
-               lock.writeLock().lock();
+               readWriteLock.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 {
-                       lock.writeLock().unlock();
+                       readWriteLock.writeLock().unlock();
                }
        }
 
-       /**
-        * {@inheritDocs}
-        */
        @Override
        public void storePosts(Sone sone, Collection<Post> posts) throws IllegalArgumentException {
                checkNotNull(sone, "sone must not be null");
@@ -182,227 +221,81 @@ public class MemoryPostDatabase implements PostDatabase {
                        }
                }
 
-               lock.writeLock().lock();
+               readWriteLock.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 {
-                       lock.writeLock().unlock();
+                       readWriteLock.writeLock().unlock();
                }
        }
 
-       /**
-        * {@inheritDocs}
-        */
        @Override
        public void removePosts(Sone sone) {
                checkNotNull(sone, "sone must not be null");
-               lock.writeLock().lock();
+               readWriteLock.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 {
-                       lock.writeLock().unlock();
+                       readWriteLock.writeLock().unlock();
                }
        }
 
-       //
-       // POSTDATABASE METHODS
-       //
-
-       /**
-        * {@inheritDocs}
-        */
-       @Override
-       public void loadKnownPosts(Configuration configuration, String prefix) {
-               lock.writeLock().lock();
+       public void start() {
+               readWriteLock.writeLock().lock();
                try {
                        int postCounter = 0;
                        while (true) {
-                               String knownPostId = configuration.getStringValue(prefix + postCounter++ + "/ID").getValue(null);
+                               String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
                                if (knownPostId == null) {
                                        break;
                                }
                                knownPosts.add(knownPostId);
                        }
                } finally {
-                       lock.writeLock().unlock();
+                       readWriteLock.writeLock().unlock();
                }
        }
 
-       /**
-        * {@inheritDocs}
-        */
-       @Override
-       public void saveKnownPosts(Configuration configuration, String prefix) throws ConfigurationException {
-               lock.readLock().lock();
-               try {
-                       int postCounter = 0;
-                       for (String knownPostId : knownPosts) {
-                               configuration.getStringValue(prefix + postCounter++ + "/ID").setValue(knownPostId);
-                       }
-                       configuration.getStringValue(prefix + postCounter + "/ID").setValue(null);
-               } finally {
-                       lock.readLock().unlock();
-               }
+       public void stop() throws DatabaseException {
+               save();
        }
 
-       //
-       // 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();
+       public void save() throws DatabaseException {
+               readWriteLock.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());
+                       int postCounter = 0;
+                       for (String knownPostId : knownPosts) {
+                               configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
                        }
+                       configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
+               } catch (ConfigurationException ce1) {
+                       throw new DatabaseException("Could not save database.", ce1);
                } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
-       //
-       // 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();
+                       readWriteLock.readLock().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;
-       }
-
-       /**
-        * {@link PostBuilder} implementation that creates a {@link MemoryPost}.
-        *
-        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
-        */
-       private class MemoryPostBuilder extends AbstractPostBuilder {
-
-               /**
-                * Creates a new memory post builder.
-                *
-                * @param soneProvider
-                *            The Sone provider
-                */
-               public MemoryPostBuilder(SoneProvider soneProvider) {
-                       super(soneProvider);
-               }
-
-               /**
-                * {@inheritDocs}
-                */
-               @Override
-               public Post build() throws IllegalStateException {
-                       validate();
-                       Post post = new MemoryPost(MemoryPostDatabase.this, soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);
-                       post.setKnown(isPostKnown(post));
-                       return post;
-               }
-
        }
 
 }