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;
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.DefaultSoneBuilder;
import net.pterodactylus.sone.database.Database;
import net.pterodactylus.sone.database.DatabaseException;
import net.pterodactylus.sone.database.PostDatabase;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
+import com.google.common.collect.SetMultimap;
import com.google.common.collect.SortedSetMultimap;
import com.google.common.collect.TreeMultimap;
import com.google.common.util.concurrent.AbstractService;
/** All posts by their Sones. */
private final Multimap<String, Post> sonePosts = HashMultimap.create();
+ private final SetMultimap<String, String> likedPosts = HashMultimap.create();
/** All posts by their recipient. */
private final Multimap<String, Post> recipientPosts = HashMultimap.create();
}, PostReply.TIME_COMPARATOR);
/** Replies by post. */
- private final Map<String, SortedSet<PostReply>> postReplies = new HashMap<String, SortedSet<PostReply>>();
+ private final SortedSetMultimap<String, PostReply> postReplies = TreeMultimap.create(new Comparator<String>() {
+
+ @Override
+ public int compare(String leftString, String rightString) {
+ return leftString.compareTo(rightString);
+ }
+ }, PostReply.TIME_COMPARATOR);
/** Whether post replies are known. */
private final Set<String> knownPostReplies = new HashSet<String>();
}
@Override
+ public Function<String, Optional<Sone>> getSone() {
+ return new Function<String, Optional<Sone>>() {
+ @Override
+ public Optional<Sone> apply(String soneId) {
+ return (soneId == null) ? Optional.<Sone>absent() : getSone(soneId);
+ }
+ };
+ }
+
+ @Override
public Optional<Sone> getSone(String soneId) {
lock.readLock().lock();
try {
@Override
public SoneBuilder newSoneBuilder() {
- return null;
+ return new DefaultSoneBuilder(this) {
+ @Override
+ public Sone build(Optional<SoneCreated> soneCreated) throws IllegalStateException {
+ Sone sone = super.build(soneCreated);
+ lock.writeLock().lock();
+ try {
+ sones.put(sone.getId(), sone);
+ } finally {
+ lock.writeLock().unlock();
+ }
+ return sone;
+ }
+ };
}
//
//
@Override
+ public Function<String, Optional<Post>> getPost() {
+ return new Function<String, Optional<Post>>() {
+ @Override
+ public Optional<Post> apply(String postId) {
+ return (postId == null) ? Optional.<Post>absent() : getPost(postId);
+ }
+ };
+ }
+
+ @Override
public Optional<Post> getPost(String postId) {
lock.readLock().lock();
try {
}
}
+ @Override
+ public void likePost(Post post, Sone localSone) {
+ lock.writeLock().lock();
+ try {
+ likedPosts.put(localSone.getId(), post.getId());
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ @Override
+ public void unlikePost(Post post, Sone localSone) {
+ lock.writeLock().lock();
+ try {
+ likedPosts.remove(localSone.getId(), post.getId());
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
//
// POSTSTORE METHODS
//
lock.writeLock().lock();
try {
allPostReplies.put(postReply.getId(), postReply);
- if (postReplies.containsKey(postReply.getPostId())) {
- postReplies.get(postReply.getPostId()).add(postReply);
- } else {
- TreeSet<PostReply> replies = new TreeSet<PostReply>(Reply.TIME_COMPARATOR);
- replies.add(postReply);
- postReplies.put(postReply.getPostId(), replies);
- }
+ postReplies.put(postReply.getPostId(), postReply);
} finally {
lock.writeLock().unlock();
}
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<PostReply> replies = new TreeSet<PostReply>(Reply.TIME_COMPARATOR);
- replies.add(postReply);
- this.postReplies.put(postReply.getPostId(), replies);
- }
+ this.postReplies.put(postReply.getPostId(), postReply);
}
} finally {
lock.writeLock().unlock();
lock.writeLock().lock();
try {
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());
- }
- }
+ postReplies.remove(postReply.getPostId(), postReply);
} finally {
lock.writeLock().unlock();
}
lock.writeLock().lock();
try {
allAlbums.put(album.getId(), album);
- albumChildren.put(album.getParent().getId(), album.getId());
+ if (!album.isRoot()) {
+ albumChildren.put(album.getParent().getId(), album.getId());
+ }
} finally {
lock.writeLock().unlock();
}