Don’t store post replies sorted by post.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 17 Sep 2014 20:25:43 +0000 (22:25 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Wed, 17 Sep 2014 20:25:43 +0000 (22:25 +0200)
Yes, this will also reduce performance in the short run, which will
magically fix itself once a database retrieves all the data which will
then be accessed via an index.

src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java

index 0b2c5a2..5a29285 100644 (file)
@@ -100,15 +100,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                }
        }, TIME_COMPARATOR);
 
-       /** Replies by post. */
-       private final SortedSetMultimap<String, PostReply> postReplies = TreeMultimap.create(new Comparator<String>() {
-
-               @Override
-               public int compare(String leftString, String rightString) {
-                       return leftString.compareTo(rightString);
-               }
-       }, TIME_COMPARATOR);
-
        /** Whether post replies are known. */
        private final Set<String> knownPostReplies = new HashSet<String>();
 
@@ -311,13 +302,16 @@ public class MemoryDatabase extends AbstractService implements Database {
 
        /** {@inheritDocs} */
        @Override
-       public List<PostReply> getReplies(String postId) {
+       public List<PostReply> getReplies(final String postId) {
                lock.readLock().lock();
                try {
-                       if (!postReplies.containsKey(postId)) {
-                               return Collections.emptyList();
-                       }
-                       return new ArrayList<PostReply>(postReplies.get(postId));
+                       return from(allPostReplies.values())
+                                       .filter(new Predicate<PostReply>() {
+                                               @Override
+                                               public boolean apply(PostReply postReply) {
+                                                       return postReply.getPostId().equals(postId);
+                                               }
+                                       }).toSortedList(TIME_COMPARATOR);
                } finally {
                        lock.readLock().unlock();
                }
@@ -343,7 +337,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        allPostReplies.put(postReply.getId(), postReply);
-                       postReplies.put(postReply.getPostId(), postReply);
                } finally {
                        lock.writeLock().unlock();
                }
@@ -369,7 +362,6 @@ public class MemoryDatabase extends AbstractService implements Database {
                        for (PostReply postReply : postReplies) {
                                allPostReplies.put(postReply.getId(), postReply);
                                sonePostReplies.put(postReply.getSone().getId(), postReply);
-                               this.postReplies.put(postReply.getPostId(), postReply);
                        }
                } finally {
                        lock.writeLock().unlock();
@@ -382,9 +374,6 @@ 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);
-                       }
                } finally {
                        lock.writeLock().unlock();
                }
index 73e63dd..db18cd2 100644 (file)
@@ -28,6 +28,7 @@ import static org.mockito.Mockito.when;
 import net.pterodactylus.sone.data.Album;
 import net.pterodactylus.sone.data.AlbumImpl;
 import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
 
 import com.google.common.base.Optional;
@@ -70,6 +71,34 @@ public class MemoryDatabaseTest {
        }
 
        @Test
+       public void postRepliesAreManagedCorrectly() {
+               Post firstPost = createPost(Optional.<String>absent());
+               PostReply firstPostFirstReply = createPostReply(firstPost, 1000L);
+               Post secondPost = createPost(Optional.<String>absent());
+               PostReply secondPostFirstReply = createPostReply(secondPost, 1000L);
+               PostReply secondPostSecondReply = createPostReply(secondPost, 2000L);
+               memoryDatabase.storePost(firstPost);
+               memoryDatabase.storePost(secondPost);
+               memoryDatabase.storePostReply(firstPostFirstReply);
+               memoryDatabase.storePostReply(secondPostFirstReply);
+               memoryDatabase.storePostReply(secondPostSecondReply);
+               assertThat(memoryDatabase.getReplies(firstPost.getId()),
+                               contains(firstPostFirstReply));
+               assertThat(memoryDatabase.getReplies(secondPost.getId()),
+                               contains(secondPostFirstReply, secondPostSecondReply));
+       }
+
+       private PostReply createPostReply(Post post, long time) {
+               PostReply postReply = mock(PostReply.class);
+               when(postReply.getId()).thenReturn(randomUUID().toString());
+               when(postReply.getTime()).thenReturn(time);
+               when(postReply.getPost()).thenReturn(of(post));
+               final String postId = post.getId();
+               when(postReply.getPostId()).thenReturn(postId);
+               return postReply;
+       }
+
+       @Test
        public void testBasicAlbumFunctionality() {
                Album newAlbum = new AlbumImpl(mock(Sone.class));
                assertThat(memoryDatabase.getAlbum(newAlbum.getId()), is(Optional.<Album>absent()));