From ebd842a2ffdd117073401ce0f47a7dfd598c7a61 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 17 Sep 2014 22:25:43 +0200 Subject: [PATCH] =?utf8?q?Don=E2=80=99t=20store=20post=20replies=20sorted?= =?utf8?q?=20by=20post.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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. --- .../sone/database/memory/MemoryDatabase.java | 27 ++++++-------------- .../sone/database/memory/MemoryDatabaseTest.java | 29 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 0b2c5a2..5a29285 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -100,15 +100,6 @@ public class MemoryDatabase extends AbstractService implements Database { } }, TIME_COMPARATOR); - /** Replies by post. */ - private final SortedSetMultimap postReplies = TreeMultimap.create(new Comparator() { - - @Override - public int compare(String leftString, String rightString) { - return leftString.compareTo(rightString); - } - }, TIME_COMPARATOR); - /** Whether post replies are known. */ private final Set knownPostReplies = new HashSet(); @@ -311,13 +302,16 @@ public class MemoryDatabase extends AbstractService implements Database { /** {@inheritDocs} */ @Override - public List getReplies(String postId) { + public List getReplies(final String postId) { lock.readLock().lock(); try { - if (!postReplies.containsKey(postId)) { - return Collections.emptyList(); - } - return new ArrayList(postReplies.get(postId)); + return from(allPostReplies.values()) + .filter(new Predicate() { + @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(); } diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java index 73e63dd..db18cd2 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java @@ -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.absent()); + PostReply firstPostFirstReply = createPostReply(firstPost, 1000L); + Post secondPost = createPost(Optional.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.absent())); -- 2.7.4