Store album relationships.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
1 /*
2  * Sone - MemoryDatabase.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.database.memory;
19
20 import static com.google.common.base.Optional.fromNullable;
21 import static com.google.common.base.Preconditions.checkNotNull;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.SortedSet;
33 import java.util.TreeSet;
34 import java.util.concurrent.locks.ReadWriteLock;
35 import java.util.concurrent.locks.ReentrantReadWriteLock;
36
37 import net.pterodactylus.sone.data.Album;
38 import net.pterodactylus.sone.data.Image;
39 import net.pterodactylus.sone.data.Post;
40 import net.pterodactylus.sone.data.PostReply;
41 import net.pterodactylus.sone.data.Reply;
42 import net.pterodactylus.sone.data.Sone;
43 import net.pterodactylus.sone.database.Database;
44 import net.pterodactylus.sone.database.DatabaseException;
45 import net.pterodactylus.sone.database.PostBuilder;
46 import net.pterodactylus.sone.database.PostDatabase;
47 import net.pterodactylus.sone.database.PostReplyBuilder;
48 import net.pterodactylus.sone.database.SoneProvider;
49 import net.pterodactylus.util.config.Configuration;
50 import net.pterodactylus.util.config.ConfigurationException;
51
52 import com.google.common.base.Optional;
53 import com.google.common.collect.ArrayListMultimap;
54 import com.google.common.collect.ListMultimap;
55 import com.google.common.collect.SortedSetMultimap;
56 import com.google.common.collect.TreeMultimap;
57 import com.google.common.util.concurrent.AbstractService;
58 import com.google.inject.Inject;
59
60 /**
61  * Memory-based {@link PostDatabase} implementation.
62  *
63  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
64  */
65 public class MemoryDatabase extends AbstractService implements Database {
66
67         /** The lock. */
68         private final ReadWriteLock lock = new ReentrantReadWriteLock();
69
70         /** The Sone provider. */
71         private final SoneProvider soneProvider;
72
73         /** The configuration. */
74         private final Configuration configuration;
75
76         /** All posts by their ID. */
77         private final Map<String, Post> allPosts = new HashMap<String, Post>();
78
79         /** All posts by their Sones. */
80         private final Map<String, Collection<Post>> sonePosts = new HashMap<String, Collection<Post>>();
81
82         /** All posts by their recipient. */
83         private final Map<String, Collection<Post>> recipientPosts = new HashMap<String, Collection<Post>>();
84
85         /** Whether posts are known. */
86         private final Set<String> knownPosts = new HashSet<String>();
87
88         /** All post replies by their ID. */
89         private final Map<String, PostReply> allPostReplies = new HashMap<String, PostReply>();
90
91         /** Replies sorted by Sone. */
92         private final SortedSetMultimap<String, PostReply> sonePostReplies = TreeMultimap.create(new Comparator<String>() {
93
94                 @Override
95                 public int compare(String leftString, String rightString) {
96                         return leftString.compareTo(rightString);
97                 }
98         }, PostReply.TIME_COMPARATOR);
99
100         /** Replies by post. */
101         private final Map<String, SortedSet<PostReply>> postReplies = new HashMap<String, SortedSet<PostReply>>();
102
103         /** Whether post replies are known. */
104         private final Set<String> knownPostReplies = new HashSet<String>();
105
106         private final Map<String, Album> allAlbums = new HashMap<String, Album>();
107         private final ListMultimap<String, String> albumChildren = ArrayListMultimap.create();
108         private final ListMultimap<String, String> albumImages = ArrayListMultimap.create();
109
110         private final Map<String, Image> allImages = new HashMap<String, Image>();
111
112         /**
113          * Creates a new memory database.
114          *
115          * @param soneProvider
116          *              The Sone provider
117          * @param configuration
118          *              The configuration for loading and saving elements
119          */
120         @Inject
121         public MemoryDatabase(SoneProvider soneProvider, Configuration configuration) {
122                 this.soneProvider = soneProvider;
123                 this.configuration = configuration;
124         }
125
126         //
127         // DATABASE METHODS
128         //
129
130         /**
131          * Saves the database.
132          *
133          * @throws DatabaseException
134          *              if an error occurs while saving
135          */
136         @Override
137         public void save() throws DatabaseException {
138                 saveKnownPosts();
139                 saveKnownPostReplies();
140         }
141
142         //
143         // SERVICE METHODS
144         //
145
146         /** {@inheritDocs} */
147         @Override
148         protected void doStart() {
149                 loadKnownPosts();
150                 loadKnownPostReplies();
151                 notifyStarted();
152         }
153
154         /** {@inheritDocs} */
155         @Override
156         protected void doStop() {
157                 try {
158                         save();
159                         notifyStopped();
160                 } catch (DatabaseException de1) {
161                         notifyFailed(de1);
162                 }
163         }
164
165         //
166         // POSTPROVIDER METHODS
167         //
168
169         /** {@inheritDocs} */
170         @Override
171         public Optional<Post> getPost(String postId) {
172                 lock.readLock().lock();
173                 try {
174                         return fromNullable(allPosts.get(postId));
175                 } finally {
176                         lock.readLock().unlock();
177                 }
178         }
179
180         /** {@inheritDocs} */
181         @Override
182         public Collection<Post> getPosts(String soneId) {
183                 return new HashSet<Post>(getPostsFrom(soneId));
184         }
185
186         /** {@inheritDocs} */
187         @Override
188         public Collection<Post> getDirectedPosts(String recipientId) {
189                 lock.readLock().lock();
190                 try {
191                         Collection<Post> posts = recipientPosts.get(recipientId);
192                         return (posts == null) ? Collections.<Post>emptySet() : new HashSet<Post>(posts);
193                 } finally {
194                         lock.readLock().unlock();
195                 }
196         }
197
198         //
199         // POSTBUILDERFACTORY METHODS
200         //
201
202         /** {@inheritDocs} */
203         @Override
204         public PostBuilder newPostBuilder() {
205                 return new MemoryPostBuilder(this, soneProvider);
206         }
207
208         //
209         // POSTSTORE METHODS
210         //
211
212         /** {@inheritDocs} */
213         @Override
214         public void storePost(Post post) {
215                 checkNotNull(post, "post must not be null");
216                 lock.writeLock().lock();
217                 try {
218                         allPosts.put(post.getId(), post);
219                         getPostsFrom(post.getSone().getId()).add(post);
220                         if (post.getRecipientId().isPresent()) {
221                                 getPostsTo(post.getRecipientId().get()).add(post);
222                         }
223                 } finally {
224                         lock.writeLock().unlock();
225                 }
226         }
227
228         /** {@inheritDocs} */
229         @Override
230         public void removePost(Post post) {
231                 checkNotNull(post, "post must not be null");
232                 lock.writeLock().lock();
233                 try {
234                         allPosts.remove(post.getId());
235                         getPostsFrom(post.getSone().getId()).remove(post);
236                         if (post.getRecipientId().isPresent()) {
237                                 getPostsTo(post.getRecipientId().get()).remove(post);
238                         }
239                         post.getSone().removePost(post);
240                 } finally {
241                         lock.writeLock().unlock();
242                 }
243         }
244
245         /** {@inheritDocs} */
246         @Override
247         public void storePosts(Sone sone, Collection<Post> posts) throws IllegalArgumentException {
248                 checkNotNull(sone, "sone must not be null");
249                 /* verify that all posts are from the same Sone. */
250                 for (Post post : posts) {
251                         if (!sone.equals(post.getSone())) {
252                                 throw new IllegalArgumentException(String.format("Post from different Sone found: %s", post));
253                         }
254                 }
255
256                 lock.writeLock().lock();
257                 try {
258                         /* remove all posts by the Sone. */
259                         getPostsFrom(sone.getId()).clear();
260                         for (Post post : posts) {
261                                 allPosts.remove(post.getId());
262                                 if (post.getRecipientId().isPresent()) {
263                                         getPostsTo(post.getRecipientId().get()).remove(post);
264                                 }
265                         }
266
267                         /* add new posts. */
268                         getPostsFrom(sone.getId()).addAll(posts);
269                         for (Post post : posts) {
270                                 allPosts.put(post.getId(), post);
271                                 if (post.getRecipientId().isPresent()) {
272                                         getPostsTo(post.getRecipientId().get()).add(post);
273                                 }
274                         }
275                 } finally {
276                         lock.writeLock().unlock();
277                 }
278         }
279
280         /** {@inheritDocs} */
281         @Override
282         public void removePosts(Sone sone) {
283                 checkNotNull(sone, "sone must not be null");
284                 lock.writeLock().lock();
285                 try {
286                         /* remove all posts by the Sone. */
287                         getPostsFrom(sone.getId()).clear();
288                         for (Post post : sone.getPosts()) {
289                                 allPosts.remove(post.getId());
290                                 if (post.getRecipientId().isPresent()) {
291                                         getPostsTo(post.getRecipientId().get()).remove(post);
292                                 }
293                         }
294                 } finally {
295                         lock.writeLock().unlock();
296                 }
297         }
298
299         //
300         // POSTREPLYPROVIDER METHODS
301         //
302
303         /** {@inheritDocs} */
304         @Override
305         public Optional<PostReply> getPostReply(String id) {
306                 lock.readLock().lock();
307                 try {
308                         return fromNullable(allPostReplies.get(id));
309                 } finally {
310                         lock.readLock().unlock();
311                 }
312         }
313
314         /** {@inheritDocs} */
315         @Override
316         public List<PostReply> getReplies(String postId) {
317                 lock.readLock().lock();
318                 try {
319                         if (!postReplies.containsKey(postId)) {
320                                 return Collections.emptyList();
321                         }
322                         return new ArrayList<PostReply>(postReplies.get(postId));
323                 } finally {
324                         lock.readLock().unlock();
325                 }
326         }
327
328         //
329         // POSTREPLYBUILDERFACTORY METHODS
330         //
331
332         /** {@inheritDocs} */
333         @Override
334         public PostReplyBuilder newPostReplyBuilder() {
335                 return new MemoryPostReplyBuilder(this, soneProvider);
336         }
337
338         //
339         // POSTREPLYSTORE METHODS
340         //
341
342         /** {@inheritDocs} */
343         @Override
344         public void storePostReply(PostReply postReply) {
345                 lock.writeLock().lock();
346                 try {
347                         allPostReplies.put(postReply.getId(), postReply);
348                         if (postReplies.containsKey(postReply.getPostId())) {
349                                 postReplies.get(postReply.getPostId()).add(postReply);
350                         } else {
351                                 TreeSet<PostReply> replies = new TreeSet<PostReply>(Reply.TIME_COMPARATOR);
352                                 replies.add(postReply);
353                                 postReplies.put(postReply.getPostId(), replies);
354                         }
355                 } finally {
356                         lock.writeLock().unlock();
357                 }
358         }
359
360         /** {@inheritDocs} */
361         @Override
362         public void storePostReplies(Sone sone, Collection<PostReply> postReplies) {
363                 checkNotNull(sone, "sone must not be null");
364                 /* verify that all posts are from the same Sone. */
365                 for (PostReply postReply : postReplies) {
366                         if (!sone.equals(postReply.getSone())) {
367                                 throw new IllegalArgumentException(String.format("PostReply from different Sone found: %s", postReply));
368                         }
369                 }
370
371                 lock.writeLock().lock();
372                 try {
373                         /* remove all post replies of the Sone. */
374                         for (PostReply postReply : getRepliesFrom(sone.getId())) {
375                                 removePostReply(postReply);
376                         }
377                         for (PostReply postReply : postReplies) {
378                                 allPostReplies.put(postReply.getId(), postReply);
379                                 sonePostReplies.put(postReply.getSone().getId(), postReply);
380                                 if (this.postReplies.containsKey(postReply.getPostId())) {
381                                         this.postReplies.get(postReply.getPostId()).add(postReply);
382                                 } else {
383                                         TreeSet<PostReply> replies = new TreeSet<PostReply>(Reply.TIME_COMPARATOR);
384                                         replies.add(postReply);
385                                         this.postReplies.put(postReply.getPostId(), replies);
386                                 }
387                         }
388                 } finally {
389                         lock.writeLock().unlock();
390                 }
391         }
392
393         /** {@inheritDocs} */
394         @Override
395         public void removePostReply(PostReply postReply) {
396                 lock.writeLock().lock();
397                 try {
398                         allPostReplies.remove(postReply.getId());
399                         if (postReplies.containsKey(postReply.getPostId())) {
400                                 postReplies.get(postReply.getPostId()).remove(postReply);
401                                 if (postReplies.get(postReply.getPostId()).isEmpty()) {
402                                         postReplies.remove(postReply.getPostId());
403                                 }
404                         }
405                 } finally {
406                         lock.writeLock().unlock();
407                 }
408         }
409
410         /** {@inheritDocs} */
411         @Override
412         public void removePostReplies(Sone sone) {
413                 checkNotNull(sone, "sone must not be null");
414
415                 lock.writeLock().lock();
416                 try {
417                         for (PostReply postReply : sone.getReplies()) {
418                                 removePostReply(postReply);
419                         }
420                 } finally {
421                         lock.writeLock().unlock();
422                 }
423         }
424
425         //
426         // ALBUMPROVDER METHODS
427         //
428
429         @Override
430         public Optional<Album> getAlbum(String albumId) {
431                 lock.readLock().lock();
432                 try {
433                         return fromNullable(allAlbums.get(albumId));
434                 } finally {
435                         lock.readLock().unlock();
436                 }
437         }
438
439         //
440         // ALBUMSTORE METHODS
441         //
442
443         @Override
444         public void storeAlbum(Album album) {
445                 lock.writeLock().lock();
446                 try {
447                         allAlbums.put(album.getId(), album);
448                         albumChildren.put(album.getParent().getId(), album.getId());
449                 } finally {
450                         lock.writeLock().unlock();
451                 }
452         }
453
454         @Override
455         public void removeAlbum(Album album) {
456                 lock.writeLock().lock();
457                 try {
458                         allAlbums.remove(album.getId());
459                         albumChildren.remove(album.getParent().getId(), album.getId());
460                 } finally {
461                         lock.writeLock().unlock();
462                 }
463         }
464
465         //
466         // IMAGEPROVIDER METHODS
467         //
468
469         @Override
470         public Optional<Image> getImage(String imageId) {
471                 lock.readLock().lock();
472                 try {
473                         return fromNullable(allImages.get(imageId));
474                 } finally {
475                         lock.readLock().unlock();
476                 }
477         }
478
479         //
480         // IMAGESTORE METHODS
481         //
482
483         @Override
484         public void storeImage(Image image) {
485                 lock.writeLock().lock();
486                 try {
487                         allImages.put(image.getId(), image);
488                         albumImages.put(image.getAlbum().getId(), image.getId());
489                 } finally {
490                         lock.writeLock().unlock();
491                 }
492         }
493
494         @Override
495         public void removeImage(Image image) {
496                 lock.writeLock().lock();
497                 try {
498                         allImages.remove(image.getId());
499                         albumImages.remove(image.getAlbum().getId(), image.getId());
500                 } finally {
501                         lock.writeLock().unlock();
502                 }
503         }
504
505         //
506         // PACKAGE-PRIVATE METHODS
507         //
508
509         /**
510          * Returns whether the given post is known.
511          *
512          * @param post
513          *              The post
514          * @return {@code true} if the post is known, {@code false} otherwise
515          */
516         boolean isPostKnown(Post post) {
517                 lock.readLock().lock();
518                 try {
519                         return knownPosts.contains(post.getId());
520                 } finally {
521                         lock.readLock().unlock();
522                 }
523         }
524
525         /**
526          * Sets whether the given post is known.
527          *
528          * @param post
529          *              The post
530          * @param known
531          *              {@code true} if the post is known, {@code false} otherwise
532          */
533         void setPostKnown(Post post, boolean known) {
534                 lock.writeLock().lock();
535                 try {
536                         if (known) {
537                                 knownPosts.add(post.getId());
538                         } else {
539                                 knownPosts.remove(post.getId());
540                         }
541                 } finally {
542                         lock.writeLock().unlock();
543                 }
544         }
545
546         /**
547          * Returns whether the given post reply is known.
548          *
549          * @param postReply
550          *              The post reply
551          * @return {@code true} if the given post reply is known, {@code false}
552          *         otherwise
553          */
554         boolean isPostReplyKnown(PostReply postReply) {
555                 lock.readLock().lock();
556                 try {
557                         return knownPostReplies.contains(postReply.getId());
558                 } finally {
559                         lock.readLock().unlock();
560                 }
561         }
562
563         /**
564          * Sets whether the given post reply is known.
565          *
566          * @param postReply
567          *              The post reply
568          * @param known
569          *              {@code true} if the post reply is known, {@code false} otherwise
570          */
571         void setPostReplyKnown(PostReply postReply, boolean known) {
572                 lock.writeLock().lock();
573                 try {
574                         if (known) {
575                                 knownPostReplies.add(postReply.getId());
576                         } else {
577                                 knownPostReplies.remove(postReply.getId());
578                         }
579                 } finally {
580                         lock.writeLock().unlock();
581                 }
582         }
583
584         //
585         // PRIVATE METHODS
586         //
587
588         /**
589          * Gets all posts for the given Sone, creating a new collection if there is
590          * none yet.
591          *
592          * @param soneId
593          *              The ID of the Sone to get the posts for
594          * @return All posts
595          */
596         private Collection<Post> getPostsFrom(String soneId) {
597                 Collection<Post> posts = null;
598                 lock.readLock().lock();
599                 try {
600                         posts = sonePosts.get(soneId);
601                 } finally {
602                         lock.readLock().unlock();
603                 }
604                 if (posts != null) {
605                         return posts;
606                 }
607
608                 posts = new HashSet<Post>();
609                 lock.writeLock().lock();
610                 try {
611                         sonePosts.put(soneId, posts);
612                 } finally {
613                         lock.writeLock().unlock();
614                 }
615
616                 return posts;
617         }
618
619         /**
620          * Gets all posts that are directed the given Sone, creating a new collection
621          * if there is none yet.
622          *
623          * @param recipientId
624          *              The ID of the Sone to get the posts for
625          * @return All posts
626          */
627         private Collection<Post> getPostsTo(String recipientId) {
628                 Collection<Post> posts = null;
629                 lock.readLock().lock();
630                 try {
631                         posts = recipientPosts.get(recipientId);
632                 } finally {
633                         lock.readLock().unlock();
634                 }
635                 if (posts != null) {
636                         return posts;
637                 }
638
639                 posts = new HashSet<Post>();
640                 lock.writeLock().lock();
641                 try {
642                         recipientPosts.put(recipientId, posts);
643                 } finally {
644                         lock.writeLock().unlock();
645                 }
646
647                 return posts;
648         }
649
650         /** Loads the known posts. */
651         private void loadKnownPosts() {
652                 lock.writeLock().lock();
653                 try {
654                         int postCounter = 0;
655                         while (true) {
656                                 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
657                                 if (knownPostId == null) {
658                                         break;
659                                 }
660                                 knownPosts.add(knownPostId);
661                         }
662                 } finally {
663                         lock.writeLock().unlock();
664                 }
665         }
666
667         /**
668          * Saves the known posts to the configuration.
669          *
670          * @throws DatabaseException
671          *              if a configuration error occurs
672          */
673         private void saveKnownPosts() throws DatabaseException {
674                 lock.readLock().lock();
675                 try {
676                         int postCounter = 0;
677                         for (String knownPostId : knownPosts) {
678                                 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
679                         }
680                         configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
681                 } catch (ConfigurationException ce1) {
682                         throw new DatabaseException("Could not save database.", ce1);
683                 } finally {
684                         lock.readLock().unlock();
685                 }
686         }
687
688         /**
689          * Returns all replies by the given Sone.
690          *
691          * @param id
692          *              The ID of the Sone
693          * @return The post replies of the Sone, sorted by time (newest first)
694          */
695         private Collection<PostReply> getRepliesFrom(String id) {
696                 lock.readLock().lock();
697                 try {
698                         if (sonePostReplies.containsKey(id)) {
699                                 return Collections.unmodifiableCollection(sonePostReplies.get(id));
700                         }
701                         return Collections.emptySet();
702                 } finally {
703                         lock.readLock().unlock();
704                 }
705         }
706
707         /** Loads the known post replies. */
708         private void loadKnownPostReplies() {
709                 lock.writeLock().lock();
710                 try {
711                         int replyCounter = 0;
712                         while (true) {
713                                 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
714                                 if (knownReplyId == null) {
715                                         break;
716                                 }
717                                 knownPostReplies.add(knownReplyId);
718                         }
719                 } finally {
720                         lock.writeLock().unlock();
721                 }
722         }
723
724         /**
725          * Saves the known post replies to the configuration.
726          *
727          * @throws DatabaseException
728          *              if a configuration error occurs
729          */
730         private void saveKnownPostReplies() throws DatabaseException {
731                 lock.readLock().lock();
732                 try {
733                         int replyCounter = 0;
734                         for (String knownReplyId : knownPostReplies) {
735                                 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
736                         }
737                         configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
738                 } catch (ConfigurationException ce1) {
739                         throw new DatabaseException("Could not save database.", ce1);
740                 } finally {
741                         lock.readLock().unlock();
742                 }
743         }
744
745 }