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