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