X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryDatabase.java;h=813dc422a561d96dd268ece2171721c7c0a9780b;hb=b282ae4881da1f8329fded3369bc08d6aaee6e7b;hp=83a886f361ed200897ca65bd020ca33ef285149a;hpb=da82d77fe096ef89404a8323adced3b75fefaa87;p=Sone.git 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 83a886f..813dc42 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -110,15 +110,6 @@ public class MemoryDatabase extends AbstractService implements Database { private final Map allSones = new HashMap(); private final Map lastInsertFingerprints = new HashMap(); - /** All posts by their ID. */ - private final Map allPosts = new HashMap(); - - /** All posts by their Sones. */ - private final Multimap sonePosts = HashMultimap.create(); - - /** Whether posts are known. */ - private final Set knownPosts = new HashSet(); - /** All post replies by their ID. */ private final Map allPostReplies = new HashMap(); @@ -140,6 +131,7 @@ public class MemoryDatabase extends AbstractService implements Database { private final Map allImages = new HashMap(); private final Multimap soneImages = HashMultimap.create(); + private final MemoryPostDatabase postDatabase; private final MemoryBookmarkDatabase memoryBookmarkDatabase; private final MemoryFriendDatabase memoryFriendDatabase; @@ -156,6 +148,7 @@ public class MemoryDatabase extends AbstractService implements Database { this.soneProvider = soneProvider; this.configuration = configuration; this.configurationLoader = new ConfigurationLoader(configuration); + postDatabase = new MemoryPostDatabase(this, configurationLoader); memoryBookmarkDatabase = new MemoryBookmarkDatabase(this, configurationLoader); memoryFriendDatabase = new MemoryFriendDatabase(configurationLoader); @@ -326,7 +319,6 @@ public class MemoryDatabase extends AbstractService implements Database { public void save() throws DatabaseException { lock.writeLock().lock(); try { - saveKnownPosts(); saveKnownPostReplies(); for (Sone localSone : from(localSones).transform(soneLoader()).transform(Optionals.get())) { saveSone(localSone); @@ -456,8 +448,8 @@ public class MemoryDatabase extends AbstractService implements Database { /** {@inheritDocs} */ @Override protected void doStart() { + postDatabase.start(); memoryBookmarkDatabase.start(); - loadKnownPosts(); loadKnownPostReplies(); notifyStarted(); } @@ -466,6 +458,7 @@ public class MemoryDatabase extends AbstractService implements Database { @Override protected void doStop() { try { + postDatabase.stop(); memoryBookmarkDatabase.stop(); save(); notifyStopped(); @@ -496,10 +489,7 @@ public class MemoryDatabase extends AbstractService implements Database { } private void storePosts(String soneId, Collection posts) { - sonePosts.putAll(soneId, posts); - for (Post post : posts) { - allPosts.put(post.getId(), post); - } + postDatabase.storePosts(soneId, posts); } private void storePostReplies(String soneId, Collection postReplies) { @@ -528,10 +518,7 @@ public class MemoryDatabase extends AbstractService implements Database { lock.writeLock().lock(); try { allSones.remove(sone.getId()); - Collection removedPosts = sonePosts.removeAll(sone.getId()); - for (Post removedPost : removedPosts) { - allPosts.remove(removedPost.getId()); - } + postDatabase.removePostsFor(sone.getId()); Collection removedPostReplies = sonePostReplies.removeAll(sone.getId()); for (PostReply removedPostReply : removedPostReplies) { @@ -642,12 +629,7 @@ public class MemoryDatabase extends AbstractService implements Database { /** {@inheritDocs} */ @Override public Optional getPost(String postId) { - lock.readLock().lock(); - try { - return fromNullable(allPosts.get(postId)); - } finally { - lock.readLock().unlock(); - } + return postDatabase.getPost(postId); } /** {@inheritDocs} */ @@ -659,17 +641,7 @@ public class MemoryDatabase extends AbstractService implements Database { /** {@inheritDocs} */ @Override public Collection getDirectedPosts(final String recipientId) { - lock.readLock().lock(); - try { - return from(sonePosts.values()).filter(new Predicate() { - @Override - public boolean apply(Post post) { - return post.getRecipientId().asSet().contains(recipientId); - } - }).toSet(); - } finally { - lock.readLock().unlock(); - } + return postDatabase.getDirectedPosts(recipientId); } // @@ -690,27 +662,14 @@ public class MemoryDatabase extends AbstractService implements Database { @Override public void storePost(Post post) { checkNotNull(post, "post must not be null"); - lock.writeLock().lock(); - try { - allPosts.put(post.getId(), post); - getPostsFrom(post.getSone().getId()).add(post); - } finally { - lock.writeLock().unlock(); - } + postDatabase.storePost(post); } /** {@inheritDocs} */ @Override public void removePost(Post post) { checkNotNull(post, "post must not be null"); - lock.writeLock().lock(); - try { - allPosts.remove(post.getId()); - getPostsFrom(post.getSone().getId()).remove(post); - post.getSone().removePost(post); - } finally { - lock.writeLock().unlock(); - } + postDatabase.removePost(post.getId()); } // @@ -911,12 +870,7 @@ public class MemoryDatabase extends AbstractService implements Database { * @return {@code true} if the post is known, {@code false} otherwise */ boolean isPostKnown(Post post) { - lock.readLock().lock(); - try { - return knownPosts.contains(post.getId()); - } finally { - lock.readLock().unlock(); - } + return postDatabase.isPostKnown(post.getId()); } /** @@ -928,16 +882,7 @@ public class MemoryDatabase extends AbstractService implements Database { * {@code true} if the post is known, {@code false} otherwise */ void setPostKnown(Post post, boolean known) { - lock.writeLock().lock(); - try { - if (known) { - knownPosts.add(post.getId()); - } else { - knownPosts.remove(post.getId()); - } - } finally { - lock.writeLock().unlock(); - } + postDatabase.setPostKnown(post.getId(), known); } /** @@ -991,46 +936,7 @@ public class MemoryDatabase extends AbstractService implements Database { * @return All posts */ private Collection getPostsFrom(String soneId) { - lock.readLock().lock(); - try { - return sonePosts.get(soneId); - } finally { - lock.readLock().unlock(); - } - } - - /** Loads the known posts. */ - private void loadKnownPosts() { - Set knownPosts = configurationLoader.loadKnownPosts(); - lock.writeLock().lock(); - try { - this.knownPosts.clear(); - this.knownPosts.addAll(knownPosts); - } finally { - lock.writeLock().unlock(); - } - } - - /** - * Saves the known posts to the configuration. - * - * @throws DatabaseException - * if a configuration error occurs - */ - private void saveKnownPosts() throws DatabaseException { - lock.readLock().lock(); - try { - int postCounter = 0; - for (String knownPostId : knownPosts) { - configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue( - knownPostId); - } - configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null); - } catch (ConfigurationException ce1) { - throw new DatabaseException("Could not save database.", ce1); - } finally { - lock.readLock().unlock(); - } + return postDatabase.getPostsFrom(soneId); } /** Loads the known post replies. */