Use the memory database to create all albums.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index d66e032..0f0b6d3 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;
@@ -93,8 +92,11 @@ import net.pterodactylus.util.thread.NamedThreadFactory;
 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.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;
@@ -183,10 +185,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        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>>());
-
-       /** All known albums. */
-       private final Map<String, Album> albums = new HashMap<String, Album>();
+       private final Multimap<OwnIdentity, Identity> trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.<OwnIdentity, Identity>create());
 
        /** All known images. */
        private final Map<String, Image> images = new HashMap<String, Image>();
@@ -332,7 +331,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @Override
        public Collection<Sone> getSones() {
                synchronized (sones) {
-                       return Collections.unmodifiableCollection(sones.values());
+                       return ImmutableSet.copyOf(sones.values());
                }
        }
 
@@ -358,13 +357,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @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();
                }
        }
 
@@ -399,13 +398,13 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        @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();
                }
        }
 
@@ -469,7 +468,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());
        }
 
        /**
@@ -601,7 +600,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());
                                }
                        }
@@ -634,14 +633,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         *         given ID exists and {@code create} is {@code false}
         */
        public Album getAlbum(String albumId, boolean create) {
-               synchronized (albums) {
-                       Album album = albums.get(albumId);
-                       if (create && (album == null)) {
-                               album = new Album(albumId);
-                               albums.put(albumId, album);
-                       }
-                       return album;
+               Optional<Album> album = database.getAlbum(albumId);
+               if (album.isPresent()) {
+                       return album.get();
+               }
+               if (!create) {
+                       return null;
                }
+               Album newAlbum = database.newAlbumBuilder().withId(albumId).build();
+               database.storeAlbum(newAlbum);
+               return newAlbum;
        }
 
        /**
@@ -739,6 +740,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 {
@@ -751,7 +753,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);
@@ -800,7 +801,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));
@@ -1027,24 +1032,28 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                                }
                        }
                        database.storePostReplies(sone, sone.getReplies());
-                       synchronized (albums) {
-                               synchronized (images) {
-                                       for (Album album : storedSone.get().getAlbums()) {
-                                               albums.remove(album.getId());
-                                               for (Image image : album.getImages()) {
-                                                       images.remove(image.getId());
-                                               }
+                       synchronized (images) {
+                               for (Album album : storedSone.get().getRootAlbum().getAlbums()) {
+                                       database.removeAlbum(album);
+                                       for (Image image : album.getImages()) {
+                                               images.remove(image.getId());
                                        }
-                                       for (Album album : sone.getAlbums()) {
-                                               albums.put(album.getId(), album);
-                                               for (Image image : album.getImages()) {
-                                                       images.put(image.getId(), image);
-                                               }
+                               }
+                               for (Album album : sone.getRootAlbum().getAlbums()) {
+                                       database.storeAlbum(album);
+                                       for (Image image : album.getImages()) {
+                                               images.put(image.getId(), image);
                                        }
                                }
                        }
                        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);
                        }
                }
@@ -1111,6 +1120,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));
@@ -1306,7 +1316,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) {
@@ -1322,6 +1337,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                for (PostReply reply : replies) {
                        reply.setKnown(true);
                }
+
+               logger.info(String.format("Sone loaded successfully: %s", sone));
        }
 
        /**
@@ -1569,7 +1586,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());
        }
 
        /**
@@ -1583,16 +1600,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
         * @return The new album
         */
        public Album createAlbum(Sone sone, Album parent) {
-               Album album = new Album();
-               synchronized (albums) {
-                       albums.put(album.getId(), album);
-               }
+               Album album = database.newAlbumBuilder().randomId().build();
+               database.storeAlbum(album);
                album.setSone(sone);
-               if (parent != null) {
-                       parent.addAlbum(album);
-               } else {
-                       sone.addAlbum(album);
-               }
+               parent.addAlbum(album);
                return album;
        }
 
@@ -1609,14 +1620,8 @@ 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);
-               }
-               synchronized (albums) {
-                       albums.remove(album.getId());
-               }
+               album.getParent().removeAlbum(album);
+               database.removeAlbum(album);
                touchConfiguration();
        }
 
@@ -1868,7 +1873,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 = FluentIterable.from(sone.getAlbums()).transformAndConcat(Album.FLATTENER).toList();
+                       List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
 
                        int albumCounter = 0;
                        for (Album album : albums) {
@@ -1876,7 +1881,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);
@@ -2123,7 +2128,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);
        }
 
        /**
@@ -2136,7 +2141,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);
        }
 
@@ -2155,6 +2160,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);
@@ -2173,9 +2181,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;
                        }