X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryDatabase.java;h=df3951110ea5e2a9f89a1fbab36ce831bc6fa852;hp=bdc247e08c4c0415e3459868d7a269860982f638;hb=90d8e2eacb9888735865d8f890d105885959ca65;hpb=6abff3e875d253a011c73574f2a6012a28af279a 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 bdc247e..df39511 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -95,6 +95,8 @@ public class MemoryDatabase extends AbstractService implements Database { /** All post replies by their ID. */ private final Map allPostReplies = new HashMap(); + private final SetMultimap likedPostRepliesBySone = HashMultimap.create(); + private final SetMultimap postReplyLikingSones = HashMultimap.create(); /** Replies sorted by Sone. */ private final SortedSetMultimap sonePostReplies = TreeMultimap.create(new Comparator() { @@ -176,6 +178,16 @@ public class MemoryDatabase extends AbstractService implements Database { } @Override + public void storeIdentity(Identity identitiy) { + lock.writeLock().lock(); + try { + identities.put(identitiy.getId(), identitiy); + } finally { + lock.writeLock().unlock(); + } + } + + @Override public Function> getSone() { return new Function>() { @Override @@ -312,7 +324,7 @@ public class MemoryDatabase extends AbstractService implements Database { public boolean isLiked(Post post, Sone sone) { lock.readLock().lock(); try { - return likedPostsBySone.containsEntry(sone, post); + return likedPostsBySone.containsEntry(sone.getId(), post.getId()); } finally { lock.readLock().unlock(); } @@ -442,6 +454,48 @@ public class MemoryDatabase extends AbstractService implements Database { } } + @Override + public void likePostReply(PostReply postReply, Sone localSone) { + lock.writeLock().lock(); + try { + likedPostRepliesBySone.put(localSone.getId(), postReply.getId()); + postReplyLikingSones.put(postReply.getId(), localSone.getId()); + } finally { + lock.writeLock().unlock(); + } + } + + @Override + public void unlikePostReply(PostReply postReply, Sone localSone) { + lock.writeLock().lock(); + try { + likedPostRepliesBySone.remove(localSone.getId(), postReply.getId()); + postReplyLikingSones.remove(postReply.getId(), localSone.getId()); + } finally { + lock.writeLock().unlock(); + } + } + + @Override + public boolean isLiked(PostReply postReply, Sone sone) { + lock.readLock().lock(); + try { + return postReplyLikingSones.containsEntry(postReply.getId(), sone.getId()); + } finally { + lock.readLock().unlock(); + } + } + + @Override + public Set getLikes(PostReply postReply) { + lock.readLock().lock(); + try { + return from(postReplyLikingSones.get(postReply.getId())).transform(getSone()).transformAndConcat(this.unwrap()).toSet(); + } finally { + lock.readLock().unlock(); + } + } + // // POSTREPLYSTORE METHODS //