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