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