2 * Sone - MemoryPostDatabase.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.database.memory;
20 import static com.google.common.base.Preconditions.checkNotNull;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.HashSet;
28 import java.util.UUID;
29 import java.util.concurrent.locks.ReadWriteLock;
30 import java.util.concurrent.locks.ReentrantReadWriteLock;
32 import net.pterodactylus.sone.data.Post;
33 import net.pterodactylus.sone.data.Sone;
34 import net.pterodactylus.sone.data.impl.AbstractPostBuilder;
35 import net.pterodactylus.sone.database.PostBuilder;
36 import net.pterodactylus.sone.database.PostDatabase;
37 import net.pterodactylus.sone.database.SoneProvider;
38 import net.pterodactylus.util.config.Configuration;
39 import net.pterodactylus.util.config.ConfigurationException;
41 import com.google.common.base.Optional;
42 import com.google.inject.Inject;
45 * Memory-based {@link PostDatabase} implementation.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class MemoryPostDatabase implements PostDatabase {
52 private final ReadWriteLock lock = new ReentrantReadWriteLock();
54 /** The Sone provider. */
55 private final SoneProvider soneProvider;
57 /** All posts by their ID. */
58 private final Map<String, Post> allPosts = new HashMap<String, Post>();
60 /** All posts by their Sones. */
61 private final Map<String, Collection<Post>> sonePosts = new HashMap<String, Collection<Post>>();
63 /** All posts by their recipient. */
64 private final Map<String, Collection<Post>> recipientPosts = new HashMap<String, Collection<Post>>();
66 /** Whether posts are known. */
67 private final Set<String> knownPosts = new HashSet<String>();
70 * Creates a new memory database.
76 public MemoryPostDatabase(SoneProvider soneProvider) {
77 this.soneProvider = soneProvider;
81 // POSTPROVIDER METHODS
88 public Optional<Post> getPost(String postId) {
89 lock.readLock().lock();
91 return Optional.fromNullable(allPosts.get(postId));
93 lock.readLock().unlock();
101 public Collection<Post> getPosts(String soneId) {
102 return new HashSet<Post>(getPostsFrom(soneId));
109 public Collection<Post> getDirectedPosts(String recipientId) {
110 lock.readLock().lock();
112 Collection<Post> posts = recipientPosts.get(recipientId);
113 return (posts == null) ? Collections.<Post> emptySet() : new HashSet<Post>(posts);
115 lock.readLock().unlock();
120 // POSTBUILDERFACTORY METHODS
127 public PostBuilder newPostBuilder() {
128 return new MemoryPostBuilder(soneProvider);
139 public void storePost(Post post) {
140 checkNotNull(post, "post must not be null");
141 lock.writeLock().lock();
143 allPosts.put(post.getId(), post);
144 getPostsFrom(post.getSone().getId()).add(post);
145 if (post.getRecipientId().isPresent()) {
146 getPostsTo(post.getRecipientId().get()).add(post);
149 lock.writeLock().unlock();
157 public void removePost(Post post) {
158 checkNotNull(post, "post must not be null");
159 lock.writeLock().lock();
161 allPosts.remove(post.getId());
162 getPostsFrom(post.getSone().getId()).remove(post);
163 if (post.getRecipientId().isPresent()) {
164 getPostsTo(post.getRecipientId().get()).remove(post);
166 post.getSone().removePost(post);
168 lock.writeLock().unlock();
176 public void storePosts(Sone sone, Collection<Post> posts) throws IllegalArgumentException {
177 checkNotNull(sone, "sone must not be null");
178 /* verify that all posts are from the same Sone. */
179 for (Post post : posts) {
180 if (!sone.equals(post.getSone())) {
181 throw new IllegalArgumentException(String.format("Post from different Sone found: %s", post));
185 lock.writeLock().lock();
187 /* remove all posts by the Sone. */
188 getPostsFrom(sone.getId()).clear();
189 for (Post post : posts) {
190 allPosts.remove(post.getId());
191 if (post.getRecipientId().isPresent()) {
192 getPostsTo(post.getRecipientId().get()).remove(post);
197 getPostsFrom(sone.getId()).addAll(posts);
198 for (Post post : posts) {
199 allPosts.put(post.getId(), post);
200 if (post.getRecipientId().isPresent()) {
201 getPostsTo(post.getRecipientId().get()).add(post);
205 lock.writeLock().unlock();
213 public void removePosts(Sone sone) {
214 checkNotNull(sone, "sone must not be null");
215 lock.writeLock().lock();
217 /* remove all posts by the Sone. */
218 getPostsFrom(sone.getId()).clear();
219 for (Post post : sone.getPosts()) {
220 allPosts.remove(post.getId());
221 if (post.getRecipientId().isPresent()) {
222 getPostsTo(post.getRecipientId().get()).remove(post);
226 lock.writeLock().unlock();
231 // POSTDATABASE METHODS
238 public void loadKnownPosts(Configuration configuration, String prefix) {
239 lock.writeLock().lock();
243 String knownPostId = configuration.getStringValue(prefix + postCounter++ + "/ID").getValue(null);
244 if (knownPostId == null) {
247 knownPosts.add(knownPostId);
250 lock.writeLock().unlock();
258 public void saveKnownPosts(Configuration configuration, String prefix) throws ConfigurationException {
259 lock.readLock().lock();
262 for (String knownPostId : knownPosts) {
263 configuration.getStringValue(prefix + postCounter++ + "/ID").setValue(knownPostId);
265 configuration.getStringValue(prefix + postCounter + "/ID").setValue(null);
267 lock.readLock().unlock();
272 // PACKAGE-PRIVATE METHODS
276 * Returns whether the given post is known.
280 * @return {@code true} if the post is known, {@code false} otherwise
282 boolean isPostKnown(Post post) {
283 lock.readLock().lock();
285 return knownPosts.contains(post.getId());
287 lock.readLock().unlock();
292 * Sets whether the given post is known.
297 * {@code true} if the post is known, {@code false} otherwise
299 void setPostKnown(Post post, boolean known) {
300 lock.writeLock().lock();
303 knownPosts.add(post.getId());
305 knownPosts.remove(post.getId());
308 lock.writeLock().unlock();
317 * Gets all posts for the given Sone, creating a new collection if there is
321 * The ID of the Sone to get the posts for
324 private Collection<Post> getPostsFrom(String soneId) {
325 Collection<Post> posts = null;
326 lock.readLock().lock();
328 posts = sonePosts.get(soneId);
330 lock.readLock().unlock();
336 posts = new HashSet<Post>();
337 lock.writeLock().lock();
339 sonePosts.put(soneId, posts);
341 lock.writeLock().unlock();
348 * Gets all posts that are directed the given Sone, creating a new
349 * collection if there is none yet.
352 * The ID of the Sone to get the posts for
355 private Collection<Post> getPostsTo(String recipientId) {
356 Collection<Post> posts = null;
357 lock.readLock().lock();
359 posts = recipientPosts.get(recipientId);
361 lock.readLock().unlock();
367 posts = new HashSet<Post>();
368 lock.writeLock().lock();
370 recipientPosts.put(recipientId, posts);
372 lock.writeLock().unlock();
379 * {@link PostBuilder} implementation that creates a {@link MemoryPost}.
381 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
383 private class MemoryPostBuilder extends AbstractPostBuilder {
386 * Creates a new memory post builder.
388 * @param soneProvider
391 public MemoryPostBuilder(SoneProvider soneProvider) {
399 public Post build() throws IllegalStateException {
401 Post post = new MemoryPost(MemoryPostDatabase.this, soneProvider, randomId ? UUID.randomUUID().toString() : id, senderId, recipientId, currentTime ? System.currentTimeMillis() : time, text);
402 post.setKnown(isPostKnown(post));