Fix synchronisation.
[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                         albumChildren.put(album.getParent().getId(), album.getId());
540                 } finally {
541                         lock.writeLock().unlock();
542                 }
543         }
544
545         @Override
546         public void removeAlbum(Album album) {
547                 lock.writeLock().lock();
548                 try {
549                         allAlbums.remove(album.getId());
550                         albumChildren.remove(album.getParent().getId(), album.getId());
551                 } finally {
552                         lock.writeLock().unlock();
553                 }
554         }
555
556         //
557         // IMAGEPROVIDER METHODS
558         //
559
560         @Override
561         public Optional<Image> getImage(String imageId) {
562                 lock.readLock().lock();
563                 try {
564                         return fromNullable(allImages.get(imageId));
565                 } finally {
566                         lock.readLock().unlock();
567                 }
568         }
569
570         @Override
571         public List<Image> getImages(Album parent) {
572                 lock.readLock().lock();
573                 try {
574                         return from(albumImages.get(parent.getId())).transformAndConcat(getImage()).toList();
575                 } finally {
576                         lock.readLock().unlock();
577                 }
578         }
579
580         @Override
581         public void moveUp(Image image) {
582                 lock.writeLock().lock();
583                 try {
584                         List<String> images = albumImages.get(image.getAlbum().getId());
585                         int currentIndex = images.indexOf(image.getId());
586                         if (currentIndex == 0) {
587                                 return;
588                         }
589                         images.remove(image.getId());
590                         images.add(currentIndex - 1, image.getId());
591                 } finally {
592                         lock.writeLock().unlock();
593                 }
594         }
595
596         @Override
597         public void moveDown(Image image) {
598                 lock.writeLock().lock();
599                 try {
600                         List<String> images = albumChildren.get(image.getAlbum().getId());
601                         int currentIndex = images.indexOf(image.getId());
602                         if (currentIndex == (images.size() - 1)) {
603                                 return;
604                         }
605                         images.remove(image.getId());
606                         images.add(currentIndex + 1, image.getId());
607                 } finally {
608                         lock.writeLock().unlock();
609                 }
610         }
611
612         //
613         // IMAGESTORE METHODS
614         //
615
616         @Override
617         public void storeImage(Image image) {
618                 lock.writeLock().lock();
619                 try {
620                         allImages.put(image.getId(), image);
621                         albumImages.put(image.getAlbum().getId(), image.getId());
622                 } finally {
623                         lock.writeLock().unlock();
624                 }
625         }
626
627         @Override
628         public void removeImage(Image image) {
629                 lock.writeLock().lock();
630                 try {
631                         allImages.remove(image.getId());
632                         albumImages.remove(image.getAlbum().getId(), image.getId());
633                 } finally {
634                         lock.writeLock().unlock();
635                 }
636         }
637
638         //
639         // PACKAGE-PRIVATE METHODS
640         //
641
642         /**
643          * Returns whether the given post is known.
644          *
645          * @param post
646          *              The post
647          * @return {@code true} if the post is known, {@code false} otherwise
648          */
649         boolean isPostKnown(Post post) {
650                 lock.readLock().lock();
651                 try {
652                         return knownPosts.contains(post.getId());
653                 } finally {
654                         lock.readLock().unlock();
655                 }
656         }
657
658         /**
659          * Sets whether the given post is known.
660          *
661          * @param post
662          *              The post
663          * @param known
664          *              {@code true} if the post is known, {@code false} otherwise
665          */
666         void setPostKnown(Post post, boolean known) {
667                 lock.writeLock().lock();
668                 try {
669                         if (known) {
670                                 knownPosts.add(post.getId());
671                         } else {
672                                 knownPosts.remove(post.getId());
673                         }
674                 } finally {
675                         lock.writeLock().unlock();
676                 }
677         }
678
679         //
680         // PRIVATE METHODS
681         //
682
683         /** Loads the known posts. */
684         private void loadKnownPosts() {
685                 lock.writeLock().lock();
686                 try {
687                         int postCounter = 0;
688                         while (true) {
689                                 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
690                                 if (knownPostId == null) {
691                                         break;
692                                 }
693                                 knownPosts.add(knownPostId);
694                         }
695                 } finally {
696                         lock.writeLock().unlock();
697                 }
698         }
699
700         /**
701          * Saves the known posts to the configuration.
702          *
703          * @throws DatabaseException
704          *              if a configuration error occurs
705          */
706         private void saveKnownPosts() throws DatabaseException {
707                 lock.readLock().lock();
708                 try {
709                         int postCounter = 0;
710                         for (String knownPostId : knownPosts) {
711                                 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
712                         }
713                         configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
714                 } catch (ConfigurationException ce1) {
715                         throw new DatabaseException("Could not save database.", ce1);
716                 } finally {
717                         lock.readLock().unlock();
718                 }
719         }
720
721         /**
722          * Returns all replies by the given Sone.
723          *
724          * @param id
725          *              The ID of the Sone
726          * @return The post replies of the Sone, sorted by time (newest first)
727          */
728         private Collection<PostReply> getRepliesFrom(String id) {
729                 lock.readLock().lock();
730                 try {
731                         if (sonePostReplies.containsKey(id)) {
732                                 return Collections.unmodifiableCollection(sonePostReplies.get(id));
733                         }
734                         return Collections.emptySet();
735                 } finally {
736                         lock.readLock().unlock();
737                 }
738         }
739
740         /** Loads the known post replies. */
741         private void loadKnownPostReplies() {
742                 lock.writeLock().lock();
743                 try {
744                         int replyCounter = 0;
745                         while (true) {
746                                 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
747                                 if (knownReplyId == null) {
748                                         break;
749                                 }
750                                 knownPostReplies.add(knownReplyId);
751                         }
752                 } finally {
753                         lock.writeLock().unlock();
754                 }
755         }
756
757         /**
758          * Saves the known post replies to the configuration.
759          *
760          * @throws DatabaseException
761          *              if a configuration error occurs
762          */
763         private void saveKnownPostReplies() throws DatabaseException {
764                 lock.readLock().lock();
765                 try {
766                         int replyCounter = 0;
767                         for (String knownReplyId : knownPostReplies) {
768                                 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
769                         }
770                         configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
771                 } catch (ConfigurationException ce1) {
772                         throw new DatabaseException("Could not save database.", ce1);
773                 } finally {
774                         lock.readLock().unlock();
775                 }
776         }
777
778         private Function<String, Iterable<Album>> getAlbum() {
779                 return new Function<String, Iterable<Album>>() {
780                         @Override
781                         public Iterable<Album> apply(String input) {
782                                 return (input == null) ? Collections.<Album>emptyList() : getAlbum(input).asSet();
783                         }
784                 };
785         }
786
787         private Function<String, Iterable<Image>> getImage() {
788                 return new Function<String, Iterable<Image>>() {
789                         @Override
790                         public Iterable<Image> apply(String input) {
791                                 return (input == null) ? Collections.<Image>emptyList() : getImage(input).asSet();
792                         }
793                 };
794         }
795
796 }