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