Check for correct objects.
[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         public boolean isLiked(Post post, Sone sone) {
313                 lock.readLock().lock();
314                 try {
315                         return likedPostsBySone.containsEntry(sone.getId(), post.getId());
316                 } finally {
317                         lock.readLock().unlock();
318                 }
319         }
320
321         @Override
322         public Set<Sone> getLikes(Post post) {
323                 lock.readLock().lock();
324                 try {
325                         return from(postLikingSones.get(post.getId())).transform(getSone()).transformAndConcat(this.<Sone>unwrap()).toSet();
326                 } finally {
327                         lock.readLock().unlock();
328                 }
329         }
330
331         //
332         // POSTSTORE METHODS
333         //
334
335         @Override
336         public void storePost(Post post) {
337                 checkNotNull(post, "post must not be null");
338                 lock.writeLock().lock();
339                 try {
340                         allPosts.put(post.getId(), post);
341                         sonePosts.put(post.getSone().getId(), post);
342                         if (post.getRecipientId().isPresent()) {
343                                 recipientPosts.put(post.getRecipientId().get(), post);
344                         }
345                 } finally {
346                         lock.writeLock().unlock();
347                 }
348         }
349
350         @Override
351         public void removePost(Post post) {
352                 checkNotNull(post, "post must not be null");
353                 lock.writeLock().lock();
354                 try {
355                         allPosts.remove(post.getId());
356                         sonePosts.remove(post.getSone().getId(), post);
357                         if (post.getRecipientId().isPresent()) {
358                                 recipientPosts.remove(post.getRecipientId().get(), post);
359                         }
360                         post.getSone().removePost(post);
361                 } finally {
362                         lock.writeLock().unlock();
363                 }
364         }
365
366         @Override
367         public void storePosts(Sone sone, Collection<Post> posts) throws IllegalArgumentException {
368                 checkNotNull(sone, "sone must not be null");
369                 /* verify that all posts are from the same Sone. */
370                 for (Post post : posts) {
371                         if (!sone.equals(post.getSone())) {
372                                 throw new IllegalArgumentException(String.format("Post from different Sone found: %s", post));
373                         }
374                 }
375
376                 lock.writeLock().lock();
377                 try {
378                         /* remove all posts by the Sone. */
379                         sonePosts.removeAll(sone.getId());
380                         for (Post post : posts) {
381                                 allPosts.remove(post.getId());
382                                 if (post.getRecipientId().isPresent()) {
383                                         recipientPosts.remove(post.getRecipientId().get(), post);
384                                 }
385                         }
386
387                         /* add new posts. */
388                         sonePosts.putAll(sone.getId(), posts);
389                         for (Post post : posts) {
390                                 allPosts.put(post.getId(), post);
391                                 if (post.getRecipientId().isPresent()) {
392                                         recipientPosts.put(post.getRecipientId().get(), post);
393                                 }
394                         }
395                 } finally {
396                         lock.writeLock().unlock();
397                 }
398         }
399
400         @Override
401         public void removePosts(Sone sone) {
402                 checkNotNull(sone, "sone must not be null");
403                 lock.writeLock().lock();
404                 try {
405                         /* remove all posts by the Sone. */
406                         sonePosts.removeAll(sone.getId());
407                         for (Post post : sone.getPosts()) {
408                                 allPosts.remove(post.getId());
409                                 if (post.getRecipientId().isPresent()) {
410                                         recipientPosts.remove(post.getRecipientId().get(), post);
411                                 }
412                         }
413                 } finally {
414                         lock.writeLock().unlock();
415                 }
416         }
417
418         //
419         // POSTREPLYPROVIDER METHODS
420         //
421
422         @Override
423         public Optional<PostReply> getPostReply(String id) {
424                 lock.readLock().lock();
425                 try {
426                         return fromNullable(allPostReplies.get(id));
427                 } finally {
428                         lock.readLock().unlock();
429                 }
430         }
431
432         @Override
433         public List<PostReply> getReplies(String postId) {
434                 lock.readLock().lock();
435                 try {
436                         if (!postReplies.containsKey(postId)) {
437                                 return emptyList();
438                         }
439                         return new ArrayList<PostReply>(postReplies.get(postId));
440                 } finally {
441                         lock.readLock().unlock();
442                 }
443         }
444
445         //
446         // POSTREPLYSTORE METHODS
447         //
448
449         /**
450          * Returns whether the given post reply is known.
451          *
452          * @param postReply
453          *              The post reply
454          * @return {@code true} if the given post reply is known, {@code false}
455          *         otherwise
456          */
457         public boolean isPostReplyKnown(PostReply postReply) {
458                 lock.readLock().lock();
459                 try {
460                         return knownPostReplies.contains(postReply.getId());
461                 } finally {
462                         lock.readLock().unlock();
463                 }
464         }
465
466         @Override
467         public void setPostReplyKnown(PostReply postReply) {
468                 lock.writeLock().lock();
469                 try {
470                         knownPostReplies.add(postReply.getId());
471                 } finally {
472                         lock.writeLock().unlock();
473                 }
474         }
475
476         @Override
477         public void storePostReply(PostReply postReply) {
478                 lock.writeLock().lock();
479                 try {
480                         allPostReplies.put(postReply.getId(), postReply);
481                         postReplies.put(postReply.getPostId(), postReply);
482                 } finally {
483                         lock.writeLock().unlock();
484                 }
485         }
486
487         @Override
488         public void storePostReplies(Sone sone, Collection<PostReply> postReplies) {
489                 checkNotNull(sone, "sone must not be null");
490                 /* verify that all posts are from the same Sone. */
491                 for (PostReply postReply : postReplies) {
492                         if (!sone.equals(postReply.getSone())) {
493                                 throw new IllegalArgumentException(String.format("PostReply from different Sone found: %s", postReply));
494                         }
495                 }
496
497                 lock.writeLock().lock();
498                 try {
499                         /* remove all post replies of the Sone. */
500                         for (PostReply postReply : getRepliesFrom(sone.getId())) {
501                                 removePostReply(postReply);
502                         }
503                         for (PostReply postReply : postReplies) {
504                                 allPostReplies.put(postReply.getId(), postReply);
505                                 sonePostReplies.put(postReply.getSone().getId(), postReply);
506                                 this.postReplies.put(postReply.getPostId(), postReply);
507                         }
508                 } finally {
509                         lock.writeLock().unlock();
510                 }
511         }
512
513         @Override
514         public void removePostReply(PostReply postReply) {
515                 lock.writeLock().lock();
516                 try {
517                         allPostReplies.remove(postReply.getId());
518                         postReplies.remove(postReply.getPostId(), postReply);
519                 } finally {
520                         lock.writeLock().unlock();
521                 }
522         }
523
524         @Override
525         public void removePostReplies(Sone sone) {
526                 checkNotNull(sone, "sone must not be null");
527
528                 lock.writeLock().lock();
529                 try {
530                         for (PostReply postReply : sone.getReplies()) {
531                                 removePostReply(postReply);
532                         }
533                 } finally {
534                         lock.writeLock().unlock();
535                 }
536         }
537
538         //
539         // ALBUMPROVDER METHODS
540         //
541
542         @Override
543         public Optional<Album> getAlbum(String albumId) {
544                 lock.readLock().lock();
545                 try {
546                         return fromNullable(allAlbums.get(albumId));
547                 } finally {
548                         lock.readLock().unlock();
549                 }
550         }
551
552         @Override
553         public List<Album> getAlbums(Album parent) {
554                 lock.readLock().lock();
555                 try {
556                         return from(albumChildren.get(parent.getId())).transformAndConcat(getAlbum()).toList();
557                 } finally {
558                         lock.readLock().unlock();
559                 }
560         }
561
562         @Override
563         public void moveUp(Album album) {
564                 lock.writeLock().lock();
565                 try {
566                         List<String> albums = albumChildren.get(album.getParent().getId());
567                         int currentIndex = albums.indexOf(album.getId());
568                         if (currentIndex == 0) {
569                                 return;
570                         }
571                         albums.remove(album.getId());
572                         albums.add(currentIndex - 1, album.getId());
573                 } finally {
574                         lock.writeLock().unlock();
575                 }
576         }
577
578         @Override
579         public void moveDown(Album album) {
580                 lock.writeLock().lock();
581                 try {
582                         List<String> albums = albumChildren.get(album.getParent().getId());
583                         int currentIndex = albums.indexOf(album.getId());
584                         if (currentIndex == (albums.size() - 1)) {
585                                 return;
586                         }
587                         albums.remove(album.getId());
588                         albums.add(currentIndex + 1, album.getId());
589                 } finally {
590                         lock.writeLock().unlock();
591                 }
592         }
593
594         //
595         // ALBUMSTORE METHODS
596         //
597
598         @Override
599         public void storeAlbum(Album album) {
600                 lock.writeLock().lock();
601                 try {
602                         allAlbums.put(album.getId(), album);
603                         if (!album.isRoot()) {
604                                 albumChildren.put(album.getParent().getId(), album.getId());
605                         }
606                 } finally {
607                         lock.writeLock().unlock();
608                 }
609         }
610
611         @Override
612         public void removeAlbum(Album album) {
613                 lock.writeLock().lock();
614                 try {
615                         allAlbums.remove(album.getId());
616                         albumChildren.remove(album.getParent().getId(), album.getId());
617                 } finally {
618                         lock.writeLock().unlock();
619                 }
620         }
621
622         //
623         // IMAGEPROVIDER METHODS
624         //
625
626         @Override
627         public Optional<Image> getImage(String imageId) {
628                 lock.readLock().lock();
629                 try {
630                         return fromNullable(allImages.get(imageId));
631                 } finally {
632                         lock.readLock().unlock();
633                 }
634         }
635
636         @Override
637         public List<Image> getImages(Album parent) {
638                 lock.readLock().lock();
639                 try {
640                         return from(albumImages.get(parent.getId())).transformAndConcat(getImage()).toList();
641                 } finally {
642                         lock.readLock().unlock();
643                 }
644         }
645
646         @Override
647         public void moveUp(Image image) {
648                 lock.writeLock().lock();
649                 try {
650                         List<String> images = albumImages.get(image.getAlbum().getId());
651                         int currentIndex = images.indexOf(image.getId());
652                         if (currentIndex == 0) {
653                                 return;
654                         }
655                         images.remove(image.getId());
656                         images.add(currentIndex - 1, image.getId());
657                 } finally {
658                         lock.writeLock().unlock();
659                 }
660         }
661
662         @Override
663         public void moveDown(Image image) {
664                 lock.writeLock().lock();
665                 try {
666                         List<String> images = albumChildren.get(image.getAlbum().getId());
667                         int currentIndex = images.indexOf(image.getId());
668                         if (currentIndex == (images.size() - 1)) {
669                                 return;
670                         }
671                         images.remove(image.getId());
672                         images.add(currentIndex + 1, image.getId());
673                 } finally {
674                         lock.writeLock().unlock();
675                 }
676         }
677
678         //
679         // IMAGESTORE METHODS
680         //
681
682         @Override
683         public void storeImage(Image image) {
684                 lock.writeLock().lock();
685                 try {
686                         allImages.put(image.getId(), image);
687                         albumImages.put(image.getAlbum().getId(), image.getId());
688                 } finally {
689                         lock.writeLock().unlock();
690                 }
691         }
692
693         @Override
694         public void removeImage(Image image) {
695                 lock.writeLock().lock();
696                 try {
697                         allImages.remove(image.getId());
698                         albumImages.remove(image.getAlbum().getId(), image.getId());
699                 } finally {
700                         lock.writeLock().unlock();
701                 }
702         }
703
704         //
705         // PACKAGE-PRIVATE METHODS
706         //
707
708         /**
709          * Returns whether the given post is known.
710          *
711          * @param post
712          *              The post
713          * @return {@code true} if the post is known, {@code false} otherwise
714          */
715         boolean isPostKnown(Post post) {
716                 lock.readLock().lock();
717                 try {
718                         return knownPosts.contains(post.getId());
719                 } finally {
720                         lock.readLock().unlock();
721                 }
722         }
723
724         /**
725          * Sets whether the given post is known.
726          *
727          * @param post
728          *              The post
729          * @param known
730          *              {@code true} if the post is known, {@code false} otherwise
731          */
732         void setPostKnown(Post post, boolean known) {
733                 lock.writeLock().lock();
734                 try {
735                         if (known) {
736                                 knownPosts.add(post.getId());
737                         } else {
738                                 knownPosts.remove(post.getId());
739                         }
740                 } finally {
741                         lock.writeLock().unlock();
742                 }
743         }
744
745         //
746         // PRIVATE METHODS
747         //
748
749         /** Loads the known posts. */
750         private void loadKnownPosts() {
751                 lock.writeLock().lock();
752                 try {
753                         int postCounter = 0;
754                         while (true) {
755                                 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
756                                 if (knownPostId == null) {
757                                         break;
758                                 }
759                                 knownPosts.add(knownPostId);
760                         }
761                 } finally {
762                         lock.writeLock().unlock();
763                 }
764         }
765
766         /**
767          * Saves the known posts to the configuration.
768          *
769          * @throws DatabaseException
770          *              if a configuration error occurs
771          */
772         private void saveKnownPosts() throws DatabaseException {
773                 lock.readLock().lock();
774                 try {
775                         int postCounter = 0;
776                         for (String knownPostId : knownPosts) {
777                                 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
778                         }
779                         configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
780                 } catch (ConfigurationException ce1) {
781                         throw new DatabaseException("Could not save database.", ce1);
782                 } finally {
783                         lock.readLock().unlock();
784                 }
785         }
786
787         /**
788          * Returns all replies by the given Sone.
789          *
790          * @param id
791          *              The ID of the Sone
792          * @return The post replies of the Sone, sorted by time (newest first)
793          */
794         private Collection<PostReply> getRepliesFrom(String id) {
795                 lock.readLock().lock();
796                 try {
797                         if (sonePostReplies.containsKey(id)) {
798                                 return Collections.unmodifiableCollection(sonePostReplies.get(id));
799                         }
800                         return Collections.emptySet();
801                 } finally {
802                         lock.readLock().unlock();
803                 }
804         }
805
806         /** Loads the known post replies. */
807         private void loadKnownPostReplies() {
808                 lock.writeLock().lock();
809                 try {
810                         int replyCounter = 0;
811                         while (true) {
812                                 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
813                                 if (knownReplyId == null) {
814                                         break;
815                                 }
816                                 knownPostReplies.add(knownReplyId);
817                         }
818                 } finally {
819                         lock.writeLock().unlock();
820                 }
821         }
822
823         /**
824          * Saves the known post replies to the configuration.
825          *
826          * @throws DatabaseException
827          *              if a configuration error occurs
828          */
829         private void saveKnownPostReplies() throws DatabaseException {
830                 lock.readLock().lock();
831                 try {
832                         int replyCounter = 0;
833                         for (String knownReplyId : knownPostReplies) {
834                                 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
835                         }
836                         configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
837                 } catch (ConfigurationException ce1) {
838                         throw new DatabaseException("Could not save database.", ce1);
839                 } finally {
840                         lock.readLock().unlock();
841                 }
842         }
843
844         private Function<String, Iterable<Album>> getAlbum() {
845                 return new Function<String, Iterable<Album>>() {
846                         @Override
847                         public Iterable<Album> apply(String input) {
848                                 return (input == null) ? Collections.<Album>emptyList() : getAlbum(input).asSet();
849                         }
850                 };
851         }
852
853         private Function<String, Iterable<Image>> getImage() {
854                 return new Function<String, Iterable<Image>>() {
855                         @Override
856                         public Iterable<Image> apply(String input) {
857                                 return (input == null) ? Collections.<Image>emptyList() : getImage(input).asSet();
858                         }
859                 };
860         }
861
862         private static <T> Function<Optional<T>, Iterable<T>> unwrap() {
863                 return new Function<Optional<T>, Iterable<T>>() {
864                         @Override
865                         public Iterable<T> apply(Optional<T> input) {
866                                 return (input == null) ? Collections.<T>emptyList() : input.asSet();
867                         }
868                 };
869         }
870
871 }