From: David ‘Bombe’ Roden Date: Mon, 2 Jun 2014 04:48:52 +0000 (+0200) Subject: Use a multimap for storing replies. X-Git-Tag: 0.9-rc1^2~3^2~255 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=7fdd5f77272f3f1305d65caf36606bd783b5bfb0;p=Sone.git Use a multimap for storing replies. --- 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 d7e9f58..67ea671 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -19,6 +19,7 @@ package net.pterodactylus.sone.database.memory; import static com.google.common.base.Optional.fromNullable; import static com.google.common.base.Preconditions.checkNotNull; +import static net.pterodactylus.sone.data.Reply.TIME_COMPARATOR; import java.util.ArrayList; import java.util.Collection; @@ -29,8 +30,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; @@ -38,7 +37,6 @@ 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.AlbumBuilderImpl; import net.pterodactylus.sone.data.impl.ImageBuilderImpl; @@ -99,10 +97,16 @@ public class MemoryDatabase extends AbstractService implements Database { public int compare(String leftString, String rightString) { return leftString.compareTo(rightString); } - }, PostReply.TIME_COMPARATOR); + }, TIME_COMPARATOR); /** Replies by post. */ - private final Map> postReplies = new HashMap>(); + 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(); @@ -347,13 +351,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 replies = new TreeSet(Reply.TIME_COMPARATOR); - replies.add(postReply); - postReplies.put(postReply.getPostId(), replies); - } + postReplies.put(postReply.getPostId(), postReply); } finally { lock.writeLock().unlock(); } @@ -379,13 +377,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 replies = new TreeSet(Reply.TIME_COMPARATOR); - replies.add(postReply); - this.postReplies.put(postReply.getPostId(), replies); - } + this.postReplies.put(postReply.getPostId(), postReply); } } finally { lock.writeLock().unlock(); @@ -400,9 +392,6 @@ public class MemoryDatabase extends AbstractService implements Database { 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()); - } } } finally { lock.writeLock().unlock(); @@ -683,10 +672,7 @@ public class MemoryDatabase extends AbstractService implements Database { private Collection getRepliesFrom(String id) { lock.readLock().lock(); try { - if (sonePostReplies.containsKey(id)) { - return Collections.unmodifiableCollection(sonePostReplies.get(id)); - } - return Collections.emptySet(); + return Collections.unmodifiableCollection(sonePostReplies.get(id)); } finally { lock.readLock().unlock(); }