Move post-related database functionality into its own class.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index 83a886f..813dc42 100644 (file)
@@ -110,15 +110,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        private final Map<String, Sone> allSones = new HashMap<String, Sone>();
        private final Map<String, String> lastInsertFingerprints = new HashMap<String, String>();
 
-       /** All posts by their ID. */
-       private final Map<String, Post> allPosts = new HashMap<String, Post>();
-
-       /** All posts by their Sones. */
-       private final Multimap<String, Post> sonePosts = 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>();
 
@@ -140,6 +131,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        private final Map<String, Image> allImages = new HashMap<String, Image>();
        private final Multimap<String, Image> soneImages = HashMultimap.create();
 
+       private final MemoryPostDatabase postDatabase;
        private final MemoryBookmarkDatabase memoryBookmarkDatabase;
        private final MemoryFriendDatabase memoryFriendDatabase;
 
@@ -156,6 +148,7 @@ public class MemoryDatabase extends AbstractService implements Database {
                this.soneProvider = soneProvider;
                this.configuration = configuration;
                this.configurationLoader = new ConfigurationLoader(configuration);
+               postDatabase = new MemoryPostDatabase(this, configurationLoader);
                memoryBookmarkDatabase =
                                new MemoryBookmarkDatabase(this, configurationLoader);
                memoryFriendDatabase = new MemoryFriendDatabase(configurationLoader);
@@ -326,7 +319,6 @@ public class MemoryDatabase extends AbstractService implements Database {
        public void save() throws DatabaseException {
                lock.writeLock().lock();
                try {
-                       saveKnownPosts();
                        saveKnownPostReplies();
                        for (Sone localSone : from(localSones).transform(soneLoader()).transform(Optionals.<Sone>get())) {
                                saveSone(localSone);
@@ -456,8 +448,8 @@ public class MemoryDatabase extends AbstractService implements Database {
        /** {@inheritDocs} */
        @Override
        protected void doStart() {
+               postDatabase.start();
                memoryBookmarkDatabase.start();
-               loadKnownPosts();
                loadKnownPostReplies();
                notifyStarted();
        }
@@ -466,6 +458,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        @Override
        protected void doStop() {
                try {
+                       postDatabase.stop();
                        memoryBookmarkDatabase.stop();
                        save();
                        notifyStopped();
@@ -496,10 +489,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        private void storePosts(String soneId, Collection<Post> posts) {
-               sonePosts.putAll(soneId, posts);
-               for (Post post : posts) {
-                       allPosts.put(post.getId(), post);
-               }
+               postDatabase.storePosts(soneId, posts);
        }
 
        private void storePostReplies(String soneId, Collection<PostReply> postReplies) {
@@ -528,10 +518,7 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        allSones.remove(sone.getId());
-                       Collection<Post> removedPosts = sonePosts.removeAll(sone.getId());
-                       for (Post removedPost : removedPosts) {
-                               allPosts.remove(removedPost.getId());
-                       }
+                       postDatabase.removePostsFor(sone.getId());
                        Collection<PostReply> removedPostReplies =
                                        sonePostReplies.removeAll(sone.getId());
                        for (PostReply removedPostReply : removedPostReplies) {
@@ -642,12 +629,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        /** {@inheritDocs} */
        @Override
        public Optional<Post> getPost(String postId) {
-               lock.readLock().lock();
-               try {
-                       return fromNullable(allPosts.get(postId));
-               } finally {
-                       lock.readLock().unlock();
-               }
+               return postDatabase.getPost(postId);
        }
 
        /** {@inheritDocs} */
@@ -659,17 +641,7 @@ public class MemoryDatabase extends AbstractService implements Database {
        /** {@inheritDocs} */
        @Override
        public Collection<Post> getDirectedPosts(final String recipientId) {
-               lock.readLock().lock();
-               try {
-                       return from(sonePosts.values()).filter(new Predicate<Post>() {
-                               @Override
-                               public boolean apply(Post post) {
-                                       return post.getRecipientId().asSet().contains(recipientId);
-                               }
-                       }).toSet();
-               } finally {
-                       lock.readLock().unlock();
-               }
+               return postDatabase.getDirectedPosts(recipientId);
        }
 
        //
@@ -690,27 +662,14 @@ public class MemoryDatabase extends AbstractService implements Database {
        @Override
        public void storePost(Post post) {
                checkNotNull(post, "post must not be null");
-               lock.writeLock().lock();
-               try {
-                       allPosts.put(post.getId(), post);
-                       getPostsFrom(post.getSone().getId()).add(post);
-               } finally {
-                       lock.writeLock().unlock();
-               }
+               postDatabase.storePost(post);
        }
 
        /** {@inheritDocs} */
        @Override
        public void removePost(Post post) {
                checkNotNull(post, "post must not be null");
-               lock.writeLock().lock();
-               try {
-                       allPosts.remove(post.getId());
-                       getPostsFrom(post.getSone().getId()).remove(post);
-                       post.getSone().removePost(post);
-               } finally {
-                       lock.writeLock().unlock();
-               }
+               postDatabase.removePost(post.getId());
        }
 
        //
@@ -911,12 +870,7 @@ public class MemoryDatabase extends AbstractService implements Database {
         * @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();
-               }
+               return postDatabase.isPostKnown(post.getId());
        }
 
        /**
@@ -928,16 +882,7 @@ public class MemoryDatabase extends AbstractService implements Database {
         *              {@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();
-               }
+               postDatabase.setPostKnown(post.getId(), known);
        }
 
        /**
@@ -991,46 +936,7 @@ public class MemoryDatabase extends AbstractService implements Database {
         * @return All posts
         */
        private Collection<Post> getPostsFrom(String soneId) {
-               lock.readLock().lock();
-               try {
-                       return sonePosts.get(soneId);
-               } finally {
-                       lock.readLock().unlock();
-               }
-       }
-
-       /** Loads the known posts. */
-       private void loadKnownPosts() {
-               Set<String> knownPosts = configurationLoader.loadKnownPosts();
-               lock.writeLock().lock();
-               try {
-                       this.knownPosts.clear();
-                       this.knownPosts.addAll(knownPosts);
-               } finally {
-                       lock.writeLock().unlock();
-               }
-       }
-
-       /**
-        * Saves the known posts to the configuration.
-        *
-        * @throws DatabaseException
-        *              if a configuration error occurs
-        */
-       private void saveKnownPosts() throws DatabaseException {
-               lock.readLock().lock();
-               try {
-                       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.readLock().unlock();
-               }
+               return postDatabase.getPostsFrom(soneId);
        }
 
        /** Loads the known post replies. */