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;
private final Multimap<String, Post> sonePosts = HashMultimap.create();
/** All posts by their recipient. */
- private final Map<String, Collection<Post>> recipientPosts = new HashMap<String, Collection<Post>>();
+ private final Multimap<String, Post> recipientPosts = HashMultimap.create();
/** Whether posts are known. */
private final Set<String> knownPosts = new HashSet<String>();
}, 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 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;
+ }
+ };
}
//
allPosts.put(post.getId(), post);
sonePosts.put(post.getSone().getId(), post);
if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).add(post);
+ recipientPosts.put(post.getRecipientId().get(), post);
}
} finally {
lock.writeLock().unlock();
allPosts.remove(post.getId());
sonePosts.remove(post.getSone().getId(), post);
if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).remove(post);
+ recipientPosts.remove(post.getRecipientId().get(), post);
}
post.getSone().removePost(post);
} finally {
for (Post post : posts) {
allPosts.remove(post.getId());
if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).remove(post);
+ recipientPosts.remove(post.getRecipientId().get(), post);
}
}
for (Post post : posts) {
allPosts.put(post.getId(), post);
if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).add(post);
+ recipientPosts.put(post.getRecipientId().get(), post);
}
}
} finally {
for (Post post : sone.getPosts()) {
allPosts.remove(post.getId());
if (post.getRecipientId().isPresent()) {
- getPostsTo(post.getRecipientId().get()).remove(post);
+ recipientPosts.remove(post.getRecipientId().get(), post);
}
}
} finally {
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();
}
// PRIVATE METHODS
//
- /**
- * Gets all posts that are directed the given Sone, creating a new collection
- * if there is none yet.
- *
- * @param recipientId
- * The ID of the Sone to get the posts for
- * @return All posts
- */
- private Collection<Post> getPostsTo(String recipientId) {
- Collection<Post> posts = null;
- lock.readLock().lock();
- try {
- posts = recipientPosts.get(recipientId);
- } finally {
- lock.readLock().unlock();
- }
- if (posts != null) {
- return posts;
- }
-
- posts = new HashSet<Post>();
- lock.writeLock().lock();
- try {
- recipientPosts.put(recipientId, posts);
- } finally {
- lock.writeLock().unlock();
- }
-
- return posts;
- }
-
/** Loads the known posts. */
private void loadKnownPosts() {
lock.writeLock().lock();