import static com.google.common.base.Optional.fromNullable;
import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Predicates.not;
import static com.google.common.collect.FluentIterable.from;
+import static java.util.Collections.unmodifiableCollection;
import static net.pterodactylus.sone.data.Reply.TIME_COMPARATOR;
+import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
+import static net.pterodactylus.sone.data.Sone.toAllAlbums;
+import static net.pterodactylus.sone.data.Sone.toAllImages;
-import java.util.ArrayList;
import java.util.Collection;
-import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import net.pterodactylus.sone.database.PostBuilder;
import net.pterodactylus.sone.database.PostDatabase;
import net.pterodactylus.sone.database.PostReplyBuilder;
+import net.pterodactylus.sone.database.SoneBuilder;
import net.pterodactylus.sone.database.SoneProvider;
import net.pterodactylus.util.config.Configuration;
import net.pterodactylus.util.config.ConfigurationException;
/** The configuration. */
private final Configuration configuration;
+ private final Map<String, Sone> allSones = new HashMap<String, Sone>();
+
/** All posts by their ID. */
private final Map<String, Post> allPosts = new HashMap<String, Post>();
}
}, TIME_COMPARATOR);
- /** Replies by post. */
- private final SortedSetMultimap<String, PostReply> postReplies = TreeMultimap.create(new Comparator<String>() {
-
- @Override
- public int compare(String leftString, String rightString) {
- return leftString.compareTo(rightString);
- }
- }, TIME_COMPARATOR);
-
/** Whether post replies are known. */
private final Set<String> knownPostReplies = new HashSet<String>();
}
}
+ @Override
+ public SoneBuilder newSoneBuilder() {
+ return new MemorySoneBuilder();
+ }
+
+ @Override
+ public void storeSone(Sone sone) {
+ lock.writeLock().lock();
+ try {
+ Collection<Post> removedPosts = sonePosts.removeAll(sone.getId());
+ for (Post removedPost : removedPosts) {
+ allPosts.remove(removedPost.getId());
+ }
+ Collection<PostReply> removedPostReplies =
+ sonePostReplies.removeAll(sone.getId());
+ for (PostReply removedPostReply : removedPostReplies) {
+ allPostReplies.remove(removedPostReply.getId());
+ }
+ Collection<Album> removedAlbums =
+ soneAlbums.removeAll(sone.getId());
+ for (Album removedAlbum : removedAlbums) {
+ allAlbums.remove(removedAlbum.getId());
+ }
+ Collection<Image> removedImages =
+ soneImages.removeAll(sone.getId());
+ for (Image removedImage : removedImages) {
+ allImages.remove(removedImage.getId());
+ }
+
+ allSones.put(sone.getId(), sone);
+ sonePosts.putAll(sone.getId(), sone.getPosts());
+ for (Post post : sone.getPosts()) {
+ allPosts.put(post.getId(), post);
+ }
+ sonePostReplies.putAll(sone.getId(), sone.getReplies());
+ for (PostReply postReply : sone.getReplies()) {
+ allPostReplies.put(postReply.getId(), postReply);
+ }
+ soneAlbums.putAll(sone.getId(), toAllAlbums.apply(sone));
+ for (Album album : toAllAlbums.apply(sone)) {
+ allAlbums.put(album.getId(), album);
+ }
+ soneImages.putAll(sone.getId(), toAllImages.apply(sone));
+ for (Image image : toAllImages.apply(sone)) {
+ allImages.put(image.getId(), image);
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ @Override
+ public Optional<Sone> getSone(String soneId) {
+ lock.readLock().lock();
+ try {
+ return fromNullable(allSones.get(soneId));
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ @Override
+ public Collection<Sone> getSones() {
+ lock.readLock().lock();
+ try {
+ return unmodifiableCollection(allSones.values());
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ @Override
+ public Collection<Sone> getLocalSones() {
+ lock.readLock().lock();
+ try {
+ return from(allSones.values()).filter(LOCAL_SONE_FILTER).toSet();
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
+ @Override
+ public Collection<Sone> getRemoteSones() {
+ lock.readLock().lock();
+ try {
+ return from(allSones.values())
+ .filter(not(LOCAL_SONE_FILTER)) .toSet();
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
//
// POSTPROVIDER METHODS
//
/** {@inheritDocs} */
@Override
- public List<PostReply> getReplies(String postId) {
+ public List<PostReply> getReplies(final String postId) {
lock.readLock().lock();
try {
- if (!postReplies.containsKey(postId)) {
- return Collections.emptyList();
- }
- return new ArrayList<PostReply>(postReplies.get(postId));
+ return from(allPostReplies.values())
+ .filter(new Predicate<PostReply>() {
+ @Override
+ public boolean apply(PostReply postReply) {
+ return postReply.getPostId().equals(postId);
+ }
+ }).toSortedList(TIME_COMPARATOR);
} finally {
lock.readLock().unlock();
}
lock.writeLock().lock();
try {
allPostReplies.put(postReply.getId(), postReply);
- postReplies.put(postReply.getPostId(), postReply);
} finally {
lock.writeLock().unlock();
}
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();
lock.writeLock().lock();
try {
allPostReplies.remove(postReply.getId());
- if (postReplies.containsKey(postReply.getPostId())) {
- postReplies.get(postReply.getPostId()).remove(postReply);
- }
} finally {
lock.writeLock().unlock();
}
private Collection<PostReply> getRepliesFrom(String id) {
lock.readLock().lock();
try {
- return Collections.unmodifiableCollection(sonePostReplies.get(id));
+ return unmodifiableCollection(sonePostReplies.get(id));
} finally {
lock.readLock().unlock();
}