Insert a root album into all Sones to get rid of album manipulation in the Sone.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index b2188e0..5103b7d 100644 (file)
@@ -23,7 +23,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
 import java.net.MalformedURLException;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.List;
@@ -63,14 +62,14 @@ import net.pterodactylus.sone.data.Reply;
 import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
 import net.pterodactylus.sone.data.Sone.SoneStatus;
+import net.pterodactylus.sone.data.TemporaryImage;
+import net.pterodactylus.sone.database.Database;
+import net.pterodactylus.sone.database.DatabaseException;
 import net.pterodactylus.sone.database.PostBuilder;
-import net.pterodactylus.sone.database.PostBuilderFactory;
 import net.pterodactylus.sone.database.PostProvider;
 import net.pterodactylus.sone.database.PostReplyBuilder;
-import net.pterodactylus.sone.database.PostReplyBuilderFactory;
 import net.pterodactylus.sone.database.PostReplyProvider;
 import net.pterodactylus.sone.database.SoneProvider;
-import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.sone.fcp.FcpInterface;
 import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired;
 import net.pterodactylus.sone.freenet.wot.Identity;
@@ -90,13 +89,14 @@ import net.pterodactylus.util.number.Numbers;
 import net.pterodactylus.util.service.AbstractService;
 import net.pterodactylus.util.thread.NamedThreadFactory;
 
-import com.google.common.base.Function;
 import com.google.common.base.Optional;
 import com.google.common.base.Predicate;
 import com.google.common.base.Predicates;
-import com.google.common.collect.Collections2;
 import com.google.common.collect.FluentIterable;
-import com.google.common.collect.Ordering;
+import com.google.common.collect.HashMultimap;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Multimaps;
 import com.google.common.eventbus.EventBus;
 import com.google.common.eventbus.Subscribe;
 import com.google.inject.Inject;
@@ -177,30 +177,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /** All known Sones. */
        private final Set<String> knownSones = new HashSet<String>();
 
-       /** The post builder. */
-       private final PostBuilderFactory postBuilderFactory;
-
-       /** All posts. */
-       private final Map<String, Post> posts = new HashMap<String, Post>();
-
-       /** All known posts. */
-       private final Set<String> knownPosts = new HashSet<String>();
-
-       /** The post reply builder factory. */
-       private final PostReplyBuilderFactory postReplyBuilderFactory;
-
-       /** All replies. */
-       private final Map<String, PostReply> replies = new HashMap<String, PostReply>();
-
-       /** All known replies. */
-       private final Set<String> knownReplies = new HashSet<String>();
+       /** The post database. */
+       private final Database database;
 
        /** All bookmarked posts. */
        /* synchronize access on itself. */
        private final Set<String> bookmarkedPosts = new HashSet<String>();
 
        /** Trusted identities, sorted by own identities. */
-       private final Map<OwnIdentity, Set<Identity>> trustedIdentities = Collections.synchronizedMap(new HashMap<OwnIdentity, Set<Identity>>());
+       private final Multimap<OwnIdentity, Identity> trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.<OwnIdentity, Identity>create());
 
        /** All known albums. */
        private final Map<String, Album> albums = new HashMap<String, Album>();
@@ -230,13 +215,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            The WebOfTrust updater
         * @param eventBus
         *            The event bus
-        * @param postBuilderFactory
-        *            The post builder
-        * @param postReplyBuilderFactory
-        *            The post reply builder factory
+        * @param database
+        *            The database
         */
        @Inject
-       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, PostBuilderFactory postBuilderFactory, PostReplyBuilderFactory postReplyBuilderFactory) {
+       public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
                super("Sone Core");
                this.configuration = configuration;
                this.freenetInterface = freenetInterface;
@@ -246,8 +229,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                this.updateChecker = new UpdateChecker(eventBus, freenetInterface);
                this.webOfTrustUpdater = webOfTrustUpdater;
                this.eventBus = eventBus;
-               this.postBuilderFactory = postBuilderFactory;
-               this.postReplyBuilderFactory = postReplyBuilderFactory;
+               this.database = database;
        }
 
        //
@@ -347,12 +329,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Returns all Sones, remote and local.
-        *
-        * @return All Sones
+        * {@inheritDocs}
         */
-       public Set<Sone> getSones() {
-               return new HashSet<Sone>(sones.values());
+       @Override
+       public Collection<Sone> getSones() {
+               synchronized (sones) {
+                       return ImmutableSet.copyOf(sones.values());
+               }
        }
 
        /**
@@ -372,33 +355,18 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Checks whether the core knows a Sone with the given ID.
-        *
-        * @param id
-        *            The ID of the Sone
-        * @return {@code true} if there is a Sone with the given ID, {@code false}
-        *         otherwise
-        */
-       public boolean hasSone(String id) {
-               synchronized (sones) {
-                       return sones.containsKey(id);
-               }
-       }
-
-       /**
-        * Returns all local Sones.
-        *
-        * @return All local Sones
+        * {@inheritDocs}
         */
+       @Override
        public Collection<Sone> getLocalSones() {
                synchronized (sones) {
-                       return Collections2.filter(sones.values(), new Predicate<Sone>() {
+                       return FluentIterable.from(sones.values()).filter(new Predicate<Sone>() {
 
                                @Override
                                public boolean apply(Sone sone) {
                                        return sone.isLocal();
                                }
-                       });
+                       }).toSet();
                }
        }
 
@@ -428,19 +396,18 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        }
 
        /**
-        * Returns all remote Sones.
-        *
-        * @return All remote Sones
+        * {@inheritDocs}
         */
+       @Override
        public Collection<Sone> getRemoteSones() {
                synchronized (sones) {
-                       return Collections2.filter(sones.values(), new Predicate<Sone>() {
+                       return FluentIterable.from(sones.values()).filter(new Predicate<Sone>() {
 
                                @Override
                                public boolean apply(Sone sone) {
                                        return !sone.isLocal();
                                }
-                       });
+                       }).toSet();
                }
        }
 
@@ -504,7 +471,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                checkNotNull(origin, "origin must not be null");
                checkNotNull(target, "target must not be null");
                checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin’s identity must be an OwnIdentity");
-               return trustedIdentities.containsKey(origin.getIdentity()) && trustedIdentities.get(origin.getIdentity()).contains(target.getIdentity());
+               return trustedIdentities.containsEntry(origin.getIdentity(), target.getIdentity());
        }
 
        /**
@@ -513,7 +480,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return A new post builder
         */
        public PostBuilder postBuilder() {
-               return postBuilderFactory.newPostBuilder();
+               return database.newPostBuilder();
        }
 
        /**
@@ -521,9 +488,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        @Override
        public Optional<Post> getPost(String postId) {
-               synchronized (posts) {
-                       return Optional.fromNullable(posts.get(postId));
-               }
+               return database.getPost(postId);
+       }
+
+       /**
+        * {@inheritDocs}
+        */
+       @Override
+       public Collection<Post> getPosts(String soneId) {
+               return database.getPosts(soneId);
        }
 
        /**
@@ -532,15 +505,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @Override
        public Collection<Post> getDirectedPosts(final String recipientId) {
                checkNotNull(recipientId, "recipient must not be null");
-               synchronized (posts) {
-                       return Collections2.filter(posts.values(), new Predicate<Post>() {
-
-                               @Override
-                               public boolean apply(Post post) {
-                                       return recipientId.equals(post.getRecipientId().orNull());
-                               }
-                       });
-               }
+               return database.getDirectedPosts(recipientId);
        }
 
        /**
@@ -549,7 +514,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return A new post reply builder
         */
        public PostReplyBuilder postReplyBuilder() {
-               return postReplyBuilderFactory.newPostReplyBuilder();
+               return database.newPostReplyBuilder();
        }
 
        /**
@@ -557,29 +522,15 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        @Override
        public Optional<PostReply> getPostReply(String replyId) {
-               synchronized (replies) {
-                       return Optional.fromNullable(replies.get(replyId));
-               }
+               return database.getPostReply(replyId);
        }
 
        /**
         * {@inheritDoc}
         */
        @Override
-       public List<PostReply> getReplies(final Post post) {
-               return Ordering.from(Reply.TIME_COMPARATOR).sortedCopy(FluentIterable.from(getSones()).transformAndConcat(new Function<Sone, Iterable<PostReply>>() {
-
-                       @Override
-                       public Iterable<PostReply> apply(Sone sone) {
-                               return sone.getReplies();
-                       }
-               }).filter(new Predicate<PostReply>() {
-
-                       @Override
-                       public boolean apply(PostReply reply) {
-                               return post.getId().equals(reply.getPostId());
-                       }
-               }));
+       public List<PostReply> getReplies(final String postId) {
+               return database.getReplies(postId);
        }
 
        /**
@@ -652,7 +603,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                synchronized (bookmarkedPosts) {
                        for (String bookmarkedPostId : bookmarkedPosts) {
                                Optional<Post> post = getPost(bookmarkedPostId);
-                               if (!post.isPresent()) {
+                               if (post.isPresent()) {
                                        posts.add(post.get());
                                }
                        }
@@ -790,6 +741,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.WARNING, "Given OwnIdentity is null!");
                        return null;
                }
+               logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity));
                synchronized (sones) {
                        final Sone sone;
                        try {
@@ -802,7 +754,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
                        sone.setKnown(true);
                        /* TODO - load posts ’n stuff */
-                       trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet<Identity>()));
                        sones.put(ownIdentity.getId(), sone);
                        final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone);
                        soneInserters.put(sone, soneInserter);
@@ -851,7 +802,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        return null;
                }
                synchronized (sones) {
-                       final Sone sone = getRemoteSone(identity.getId(), true).setIdentity(identity);
+                       final Sone sone = getRemoteSone(identity.getId(), true);
+                       if (sone.isLocal()) {
+                               return sone;
+                       }
+                       sone.setIdentity(identity);
                        boolean newSone = sone.getRequestUri() == null;
                        sone.setRequestUri(SoneUri.create(identity.getRequestUri()));
                        sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
@@ -1039,65 +994,54 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
                                return;
                        }
-                       synchronized (posts) {
-                               if (!soneRescueMode) {
-                                       for (Post post : storedSone.get().getPosts()) {
-                                               posts.remove(post.getId());
-                                               if (!sone.getPosts().contains(post)) {
-                                                       eventBus.post(new PostRemovedEvent(post));
-                                               }
-                                       }
+                       /* find removed posts. */
+                       Collection<Post> existingPosts = database.getPosts(sone.getId());
+                       for (Post oldPost : existingPosts) {
+                               if (!sone.getPosts().contains(oldPost)) {
+                                       eventBus.post(new PostRemovedEvent(oldPost));
                                }
-                               List<Post> storedPosts = storedSone.get().getPosts();
-                               synchronized (knownPosts) {
-                                       for (Post post : sone.getPosts()) {
-                                               post.setKnown(knownPosts.contains(post.getId()));
-                                               if (!storedPosts.contains(post)) {
-                                                       if (post.getTime() < getSoneFollowingTime(sone)) {
-                                                               knownPosts.add(post.getId());
-                                                               post.setKnown(true);
-                                                       } else if (!knownPosts.contains(post.getId())) {
-                                                               eventBus.post(new NewPostFoundEvent(post));
-                                                       }
-                                               }
-                                               posts.put(post.getId(), post);
-                                       }
+                       }
+                       /* find new posts. */
+                       for (Post newPost : sone.getPosts()) {
+                               if (existingPosts.contains(newPost)) {
+                                       continue;
+                               }
+                               if (newPost.getTime() < getSoneFollowingTime(sone)) {
+                                       newPost.setKnown(true);
+                               } else if (!newPost.isKnown()) {
+                                       eventBus.post(new NewPostFoundEvent(newPost));
                                }
                        }
-                       synchronized (replies) {
-                               if (!soneRescueMode) {
-                                       for (PostReply reply : storedSone.get().getReplies()) {
-                                               replies.remove(reply.getId());
-                                               if (!sone.getReplies().contains(reply)) {
-                                                       eventBus.post(new PostReplyRemovedEvent(reply));
-                                               }
+                       /* store posts. */
+                       database.storePosts(sone, sone.getPosts());
+                       if (!soneRescueMode) {
+                               for (PostReply reply : storedSone.get().getReplies()) {
+                                       if (!sone.getReplies().contains(reply)) {
+                                               eventBus.post(new PostReplyRemovedEvent(reply));
                                        }
                                }
-                               Set<PostReply> storedReplies = storedSone.get().getReplies();
-                               synchronized (knownReplies) {
-                                       for (PostReply reply : sone.getReplies()) {
-                                               reply.setKnown(knownReplies.contains(reply.getId()));
-                                               if (!storedReplies.contains(reply)) {
-                                                       if (reply.getTime() < getSoneFollowingTime(sone)) {
-                                                               knownReplies.add(reply.getId());
-                                                               reply.setKnown(true);
-                                                       } else if (!knownReplies.contains(reply.getId())) {
-                                                               eventBus.post(new NewPostReplyFoundEvent(reply));
-                                                       }
-                                               }
-                                               replies.put(reply.getId(), reply);
-                                       }
+                       }
+                       Set<PostReply> storedReplies = storedSone.get().getReplies();
+                       for (PostReply reply : sone.getReplies()) {
+                               if (storedReplies.contains(reply)) {
+                                       continue;
+                               }
+                               if (reply.getTime() < getSoneFollowingTime(sone)) {
+                                       reply.setKnown(true);
+                               } else if (!reply.isKnown()) {
+                                       eventBus.post(new NewPostReplyFoundEvent(reply));
                                }
                        }
+                       database.storePostReplies(sone, sone.getReplies());
                        synchronized (albums) {
                                synchronized (images) {
-                                       for (Album album : storedSone.get().getAlbums()) {
+                                       for (Album album : storedSone.get().getRootAlbum().getAlbums()) {
                                                albums.remove(album.getId());
                                                for (Image image : album.getImages()) {
                                                        images.remove(image.getId());
                                                }
                                        }
-                                       for (Album album : sone.getAlbums()) {
+                                       for (Album album : sone.getRootAlbum().getAlbums()) {
                                                albums.put(album.getId(), album);
                                                for (Image image : album.getImages()) {
                                                        images.put(image.getId(), image);
@@ -1106,6 +1050,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                }
                        }
                        synchronized (sones) {
+                               sone.setOptions(storedSone.get().getOptions());
+                               sone.setKnown(storedSone.get().isKnown());
+                               sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
+                               if (sone.isLocal()) {
+                                       soneInserters.get(storedSone.get()).setSone(sone);
+                                       touchConfiguration();
+                               }
                                sones.put(sone.getId(), sone);
                        }
                }
@@ -1172,6 +1123,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.FINE, String.format("Tried to load non-local Sone: %s", sone));
                        return;
                }
+               logger.info(String.format("Loading local Sone: %s", sone));
 
                /* initialize options. */
                sone.getOptions().addBooleanOption("AutoFollow", new DefaultOption<Boolean>(false));
@@ -1247,8 +1199,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                logger.log(Level.WARNING, "Invalid reply found, aborting load!");
                                return;
                        }
-                       PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder();
-                       postReplyBuilder.withId(replyId).from(sone.getId()).to(postId).withTime(replyTime).withText(replyText);
+                       PostReplyBuilder postReplyBuilder = postReplyBuilder().withId(replyId).from(sone.getId()).to(postId).withTime(replyTime).withText(replyText);
                        replies.add(postReplyBuilder.build());
                }
 
@@ -1368,7 +1319,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        for (String friendId : friends) {
                                followSone(sone, friendId);
                        }
-                       sone.setAlbums(topLevelAlbums);
+                       for (Album album : sone.getRootAlbum().getAlbums()) {
+                               sone.getRootAlbum().removeAlbum(album);
+                       }
+                       for (Album album : topLevelAlbums) {
+                               sone.getRootAlbum().addAlbum(album);
+                       }
                        soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
                }
                synchronized (knownSones) {
@@ -1376,26 +1332,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                knownSones.add(friend);
                        }
                }
-               synchronized (this.posts) {
-                       for (Post post : posts) {
-                               this.posts.put(post.getId(), post);
-                       }
-               }
-               synchronized (knownPosts) {
-                       for (Post post : posts) {
-                               knownPosts.add(post.getId());
-                       }
+               database.storePosts(sone, posts);
+               for (Post post : posts) {
+                       post.setKnown(true);
                }
-               synchronized (this.replies) {
-                       for (PostReply postReply : replies) {
-                               this.replies.put(postReply.getId(), postReply);
-                       }
-               }
-               synchronized (knownReplies) {
-                       for (PostReply reply : replies) {
-                               knownReplies.add(reply.getId());
-                       }
+               database.storePostReplies(sone, replies);
+               for (PostReply reply : replies) {
+                       reply.setKnown(true);
                }
+
+               logger.info(String.format("Sone loaded successfully: %s", sone));
        }
 
        /**
@@ -1463,15 +1409,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.FINE, String.format("Tried to create post for non-local Sone: %s", sone));
                        return null;
                }
-               PostBuilder postBuilder = postBuilderFactory.newPostBuilder();
+               PostBuilder postBuilder = database.newPostBuilder();
                postBuilder.from(sone.getId()).randomId().withTime(time).withText(text.trim());
                if (recipient.isPresent()) {
                        postBuilder.to(recipient.get().getId());
                }
                final Post post = postBuilder.build();
-               synchronized (posts) {
-                       posts.put(post.getId(), post);
-               }
+               database.storePost(post);
                eventBus.post(new NewPostFoundEvent(post));
                sone.addPost(post);
                touchConfiguration();
@@ -1499,10 +1443,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.WARNING, String.format("Tried to delete post of non-local Sone: %s", post.getSone()));
                        return;
                }
-               post.getSone().removePost(post);
-               synchronized (posts) {
-                       posts.remove(post.getId());
-               }
+               database.removePost(post);
                eventBus.post(new PostRemovedEvent(post));
                markPostKnown(post);
                touchConfiguration();
@@ -1517,13 +1458,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         */
        public void markPostKnown(Post post) {
                post.setKnown(true);
-               synchronized (knownPosts) {
-                       eventBus.post(new MarkPostKnownEvent(post));
-                       if (knownPosts.add(post.getId())) {
-                               touchConfiguration();
-                       }
-               }
-               for (PostReply reply : getReplies(post)) {
+               eventBus.post(new MarkPostKnownEvent(post));
+               touchConfiguration();
+               for (PostReply reply : getReplies(post.getId())) {
                        markReplyKnown(reply);
                }
        }
@@ -1590,15 +1527,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.FINE, String.format("Tried to create reply for non-local Sone: %s", sone));
                        return null;
                }
-               PostReplyBuilder postReplyBuilder = postReplyBuilderFactory.newPostReplyBuilder();
+               PostReplyBuilder postReplyBuilder = postReplyBuilder();
                postReplyBuilder.randomId().from(sone.getId()).to(post.getId()).currentTime().withText(text.trim());
                final PostReply reply = postReplyBuilder.build();
-               synchronized (replies) {
-                       replies.put(reply.getId(), reply);
-               }
-               synchronized (knownReplies) {
-                       eventBus.post(new NewPostReplyFoundEvent(reply));
-               }
+               database.storePostReply(reply);
+               eventBus.post(new NewPostReplyFoundEvent(reply));
                sone.addReply(reply);
                touchConfiguration();
                localElementTicker.schedule(new Runnable() {
@@ -1626,13 +1559,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        logger.log(Level.FINE, String.format("Tried to delete non-local reply: %s", reply));
                        return;
                }
-               synchronized (replies) {
-                       replies.remove(reply.getId());
-               }
-               synchronized (knownReplies) {
-                       markReplyKnown(reply);
-                       knownReplies.remove(reply.getId());
-               }
+               database.removePostReply(reply);
+               markReplyKnown(reply);
                sone.removeReply(reply);
                touchConfiguration();
        }
@@ -1645,12 +1573,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *            The reply to mark as known
         */
        public void markReplyKnown(PostReply reply) {
+               boolean previouslyKnown = reply.isKnown();
                reply.setKnown(true);
-               synchronized (knownReplies) {
-                       eventBus.post(new MarkPostReplyKnownEvent(reply));
-                       if (knownReplies.add(reply.getId())) {
-                               touchConfiguration();
-                       }
+               eventBus.post(new MarkPostReplyKnownEvent(reply));
+               if (!previouslyKnown) {
+                       touchConfiguration();
                }
        }
 
@@ -1662,7 +1589,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return The new album
         */
        public Album createAlbum(Sone sone) {
-               return createAlbum(sone, null);
+               return createAlbum(sone, sone.getRootAlbum());
        }
 
        /**
@@ -1681,11 +1608,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        albums.put(album.getId(), album);
                }
                album.setSone(sone);
-               if (parent != null) {
-                       parent.addAlbum(album);
-               } else {
-                       sone.addAlbum(album);
-               }
+               parent.addAlbum(album);
                return album;
        }
 
@@ -1702,11 +1625,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                if (!album.isEmpty()) {
                        return;
                }
-               if (album.getParent() == null) {
-                       album.getSone().removeAlbum(album);
-               } else {
-                       album.getParent().removeAlbum(album);
-               }
+               album.getParent().removeAlbum(album);
                synchronized (albums) {
                        albums.remove(album.getId());
                }
@@ -1827,6 +1746,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                identityManager.start();
                webOfTrustUpdater.init();
                webOfTrustUpdater.start();
+               database.start();
        }
 
        /**
@@ -1861,6 +1781,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        }
                }
                saveConfiguration();
+               database.stop();
                webOfTrustUpdater.stop();
                updateChecker.stop();
                soneDownloader.stop();
@@ -1959,7 +1880,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
 
                        /* save albums. first, collect in a flat structure, top-level first. */
-                       List<Album> albums = sone.getAllAlbums();
+                       List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
 
                        int albumCounter = 0;
                        for (Album album : albums) {
@@ -1967,7 +1888,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
                                configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
                                configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
-                               configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent() == null ? null : album.getParent().getId());
+                               configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
                                configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage() == null ? null : album.getAlbumImage().getId());
                        }
                        configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
@@ -2058,22 +1979,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        }
 
                        /* save known posts. */
-                       int postCounter = 0;
-                       synchronized (knownPosts) {
-                               for (String knownPostId : knownPosts) {
-                                       configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
-                               }
-                               configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
-                       }
-
-                       /* save known replies. */
-                       int replyCounter = 0;
-                       synchronized (knownReplies) {
-                               for (String knownReplyId : knownReplies) {
-                                       configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
-                               }
-                               configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
-                       }
+                       database.save();
 
                        /* save bookmarked posts. */
                        int bookmarkedPostCounter = 0;
@@ -2089,6 +1995,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
 
                } catch (ConfigurationException ce1) {
                        logger.log(Level.SEVERE, "Could not store configuration!", ce1);
+               } catch (DatabaseException de1) {
+                       logger.log(Level.SEVERE, "Could not save database!", de1);
                } finally {
                        synchronized (configuration) {
                                storingConfiguration = false;
@@ -2173,30 +2081,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        ++soneCounter;
                }
 
-               /* load known posts. */
-               int postCounter = 0;
-               while (true) {
-                       String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
-                       if (knownPostId == null) {
-                               break;
-                       }
-                       synchronized (knownPosts) {
-                               knownPosts.add(knownPostId);
-                       }
-               }
-
-               /* load known replies. */
-               int replyCounter = 0;
-               while (true) {
-                       String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
-                       if (knownReplyId == null) {
-                               break;
-                       }
-                       synchronized (knownReplies) {
-                               knownReplies.add(knownReplyId);
-                       }
-               }
-
                /* load bookmarked posts. */
                int bookmarkedPostCounter = 0;
                while (true) {
@@ -2251,7 +2135,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void ownIdentityRemoved(OwnIdentityRemovedEvent ownIdentityRemovedEvent) {
                OwnIdentity ownIdentity = ownIdentityRemovedEvent.ownIdentity();
                logger.log(Level.FINEST, String.format("Removing OwnIdentity: %s", ownIdentity));
-               trustedIdentities.remove(ownIdentity);
+               trustedIdentities.removeAll(ownIdentity);
        }
 
        /**
@@ -2264,7 +2148,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void identityAdded(IdentityAddedEvent identityAddedEvent) {
                Identity identity = identityAddedEvent.identity();
                logger.log(Level.FINEST, String.format("Adding Identity: %s", identity));
-               trustedIdentities.get(identityAddedEvent.ownIdentity()).add(identity);
+               trustedIdentities.put(identityAddedEvent.ownIdentity(), identity);
                addRemoteSone(identity);
        }
 
@@ -2283,6 +2167,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        @SuppressWarnings("synthetic-access")
                        public void run() {
                                Sone sone = getRemoteSone(identity.getId(), false);
+                               if (sone.isLocal()) {
+                                       return;
+                               }
                                sone.setIdentity(identity);
                                sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), sone.getLatestEdition()));
                                soneDownloader.addSone(sone);
@@ -2301,9 +2188,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void identityRemoved(IdentityRemovedEvent identityRemovedEvent) {
                OwnIdentity ownIdentity = identityRemovedEvent.ownIdentity();
                Identity identity = identityRemovedEvent.identity();
-               trustedIdentities.get(ownIdentity).remove(identity);
+               trustedIdentities.remove(ownIdentity, identity);
                boolean foundIdentity = false;
-               for (Entry<OwnIdentity, Set<Identity>> trustedIdentity : trustedIdentities.entrySet()) {
+               for (Entry<OwnIdentity, Collection<Identity>> trustedIdentity : trustedIdentities.asMap().entrySet()) {
                        if (trustedIdentity.getKey().equals(ownIdentity)) {
                                continue;
                        }
@@ -2320,21 +2207,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                        /* TODO - we don’t have the Sone anymore. should this happen? */
                        return;
                }
-               synchronized (posts) {
-                       synchronized (knownPosts) {
-                               for (Post post : sone.get().getPosts()) {
-                                       posts.remove(post.getId());
-                                       eventBus.post(new PostRemovedEvent(post));
-                               }
-                       }
+               database.removePosts(sone.get());
+               for (Post post : sone.get().getPosts()) {
+                       eventBus.post(new PostRemovedEvent(post));
                }
-               synchronized (replies) {
-                       synchronized (knownReplies) {
-                               for (PostReply reply : sone.get().getReplies()) {
-                                       replies.remove(reply.getId());
-                                       eventBus.post(new PostReplyRemovedEvent(reply));
-                               }
-                       }
+               database.removePostReplies(sone.get());
+               for (PostReply reply : sone.get().getReplies()) {
+                       eventBus.post(new PostReplyRemovedEvent(reply));
                }
                synchronized (sones) {
                        sones.remove(identity.getId());