X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=18588a180fc5924acb1586229a93a4bce2a8c7a0;hb=438378deab1514f0f608d975ef65f5b7aea44ccb;hp=66677deb8fd390c4ecd053b52f44fea1db445886;hpb=ffd92ca2374c0b2218e583d02e0bdd24b8c110ae;p=Sone.git
diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java
index 66677de..18588a1 100644
--- a/src/main/java/net/pterodactylus/sone/core/Core.java
+++ b/src/main/java/net/pterodactylus/sone/core/Core.java
@@ -1,5 +1,5 @@
/*
- * Sone - Core.java - Copyright © 2010â2016 David Roden
+ * Sone - Core.java - Copyright © 2010â2020 David Roden
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -24,6 +24,7 @@ import static com.google.common.primitives.Longs.tryParse;
import static java.lang.String.format;
import static java.util.logging.Level.WARNING;
import static java.util.logging.Logger.getLogger;
+import static net.pterodactylus.sone.data.AlbumsKt.getAllImages;
import java.util.ArrayList;
import java.util.Collection;
@@ -37,31 +38,20 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import com.codahale.metrics.*;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
-import net.pterodactylus.sone.core.SoneChangeDetector.PostProcessor;
-import net.pterodactylus.sone.core.SoneChangeDetector.PostReplyProcessor;
-import net.pterodactylus.sone.core.event.ImageInsertFinishedEvent;
-import net.pterodactylus.sone.core.event.MarkPostKnownEvent;
-import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent;
-import net.pterodactylus.sone.core.event.MarkSoneKnownEvent;
-import net.pterodactylus.sone.core.event.NewPostFoundEvent;
-import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent;
-import net.pterodactylus.sone.core.event.NewSoneFoundEvent;
-import net.pterodactylus.sone.core.event.PostRemovedEvent;
-import net.pterodactylus.sone.core.event.PostReplyRemovedEvent;
-import net.pterodactylus.sone.core.event.SoneLockedEvent;
-import net.pterodactylus.sone.core.event.SoneRemovedEvent;
-import net.pterodactylus.sone.core.event.SoneUnlockedEvent;
+import net.pterodactylus.sone.core.event.*;
import net.pterodactylus.sone.data.Album;
import net.pterodactylus.sone.data.Client;
import net.pterodactylus.sone.data.Image;
@@ -99,8 +89,7 @@ import net.pterodactylus.util.service.AbstractService;
import net.pterodactylus.util.thread.NamedThreadFactory;
import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Function;
-import com.google.common.base.Optional;
+import com.google.common.base.Stopwatch;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
@@ -109,11 +98,10 @@ import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Inject;
import com.google.inject.Singleton;
+import kotlin.jvm.functions.Function1;
/**
* The Sone core.
- *
- * @author David âBombeâ Roden
*/
@Singleton
public class Core extends AbstractService implements SoneProvider, PostProvider, PostReplyProvider {
@@ -124,6 +112,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/** The start time. */
private final long startupTime = System.currentTimeMillis();
+ private final AtomicBoolean debug = new AtomicBoolean(false);
+
/** The preferences. */
private final Preferences preferences;
@@ -157,23 +147,20 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/** The trust updater. */
private final WebOfTrustUpdater webOfTrustUpdater;
- /** The times Sones were followed. */
- private final Map soneFollowingTimes = new HashMap();
-
/** Locked local Sones. */
/* synchronize on itself. */
- private final Set lockedSones = new HashSet();
+ private final Set lockedSones = new HashSet<>();
/** Sone inserters. */
/* synchronize access on this on sones. */
- private final Map soneInserters = new HashMap();
+ private final Map soneInserters = new HashMap<>();
/** Sone rescuers. */
/* synchronize access on this on sones. */
- private final Map soneRescuers = new HashMap();
+ private final Map soneRescuers = new HashMap<>();
/** All known Sones. */
- private final Set knownSones = new HashSet();
+ private final Set knownSones = new HashSet<>();
/** The post database. */
private final Database database;
@@ -182,7 +169,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
private final Multimap trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.create());
/** All temporary images. */
- private final Map temporaryImages = new HashMap();
+ private final Map temporaryImages = new HashMap<>();
/** Ticker for threads that mark own elements as known. */
private final ScheduledExecutorService localElementTicker = Executors.newScheduledThreadPool(1);
@@ -190,6 +177,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/** The time the configuration was last touched. */
private volatile long lastConfigurationUpdate;
+ private final MetricRegistry metricRegistry;
+ private final Histogram configurationSaveTimeHistogram;
+
/**
* Creates a new core.
*
@@ -207,22 +197,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* The database
*/
@Inject
- public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
- super("Sone Core");
- this.configuration = configuration;
- this.freenetInterface = freenetInterface;
- this.identityManager = identityManager;
- this.soneDownloader = new SoneDownloaderImpl(this, freenetInterface);
- this.imageInserter = new ImageInserter(freenetInterface, freenetInterface.new InsertTokenSupplier());
- this.updateChecker = updateChecker;
- this.webOfTrustUpdater = webOfTrustUpdater;
- this.eventBus = eventBus;
- this.database = database;
- preferences = new Preferences(eventBus);
- }
-
- @VisibleForTesting
- protected Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database) {
+ public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, SoneDownloader soneDownloader, ImageInserter imageInserter, UpdateChecker updateChecker, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus, Database database, MetricRegistry metricRegistry) {
super("Sone Core");
this.configuration = configuration;
this.freenetInterface = freenetInterface;
@@ -233,7 +208,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
this.webOfTrustUpdater = webOfTrustUpdater;
this.eventBus = eventBus;
this.database = database;
+ this.metricRegistry = metricRegistry;
preferences = new Preferences(eventBus);
+ this.configurationSaveTimeHistogram = metricRegistry.histogram("configuration.save.duration", () -> new Histogram(new ExponentiallyDecayingReservoir(3000, 0)));
}
//
@@ -249,6 +226,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return startupTime;
}
+ @Nonnull
+ public boolean getDebug() {
+ return debug.get();
+ }
+
+ public void setDebug() {
+ debug.set(true);
+ eventBus.post(new DebugActivatedEvent());
+ }
+
/**
* Returns the options used by the core.
*
@@ -323,9 +310,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return database.getSones();
}
+ @Nonnull
@Override
- public Function> soneLoader() {
- return database.soneLoader();
+ public Function1 getSoneLoader() {
+ return database.getSoneLoader();
}
/**
@@ -338,7 +326,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* Sone
*/
@Override
- public Optional getSone(String id) {
+ @Nullable
+ public Sone getSone(@Nonnull String id) {
return database.getSone(id);
}
@@ -358,9 +347,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return The Sone with the given ID, or {@code null}
*/
public Sone getLocalSone(String id) {
- Optional sone = database.getSone(id);
- if (sone.isPresent() && sone.get().isLocal()) {
- return sone.get();
+ Sone sone = database.getSone(id);
+ if ((sone != null) && sone.isLocal()) {
+ return sone;
}
return null;
}
@@ -382,7 +371,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return The Sone with the given ID
*/
public Sone getRemoteSone(String id) {
- return database.getSone(id).orNull();
+ return database.getSone(id);
}
/**
@@ -398,20 +387,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
/**
- * Returns the time when the given was first followed by any local Sone.
- *
- * @param sone
- * The Sone to get the time for
- * @return The time (in milliseconds since Jan 1, 1970) the Sone has first
- * been followed, or {@link Long#MAX_VALUE}
- */
- public long getSoneFollowingTime(Sone sone) {
- synchronized (soneFollowingTimes) {
- return Optional.fromNullable(soneFollowingTimes.get(sone.getId())).or(Long.MAX_VALUE);
- }
- }
-
- /**
* Returns a post builder.
*
* @return A new post builder
@@ -420,11 +395,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
return database.newPostBuilder();
}
- /**
- * {@inheritDoc}
- */
+ @Nullable
@Override
- public Optional getPost(String postId) {
+ public Post getPost(@Nonnull String postId) {
return database.getPost(postId);
}
@@ -457,8 +430,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
/**
* {@inheritDoc}
*/
+ @Nullable
@Override
- public Optional getPostReply(String replyId) {
+ public PostReply getPostReply(String replyId) {
return database.getPostReply(replyId);
}
@@ -478,7 +452,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return The Sones that like the given post
*/
public Set getLikes(Post post) {
- Set sones = new HashSet();
+ Set sones = new HashSet<>();
for (Sone sone : getSones()) {
if (sone.getLikedPostIds().contains(post.getId())) {
sones.add(sone);
@@ -495,7 +469,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* @return The Sones that like the given reply
*/
public Set getLikes(PostReply reply) {
- Set sones = new HashSet();
+ Set sones = new HashSet<>();
for (Sone sone : getSones()) {
if (sone.getLikedReplyIds().contains(reply.getId())) {
sones.add(sone);
@@ -540,7 +514,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*/
@Nullable
public Album getAlbum(@Nonnull String albumId) {
- return database.getAlbum(albumId).orNull();
+ return database.getAlbum(albumId);
}
public ImageBuilder imageBuilder() {
@@ -573,9 +547,9 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
*/
@Nullable
public Image getImage(String imageId, boolean create) {
- Optional image = database.getImage(imageId);
- if (image.isPresent()) {
- return image.get();
+ Image image = database.getImage(imageId);
+ if (image != null) {
+ return image;
}
if (!create) {
return null;
@@ -652,7 +626,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
sone.setLatestEdition(fromNullable(tryParse(property)).or(0L));
sone.setClient(new Client("Sone", SonePlugin.getPluginVersion()));
sone.setKnown(true);
- SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, ownIdentity.getId());
+ SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, metricRegistry, ownIdentity.getId());
+ soneInserter.insertionDelayChanged(new InsertionDelayChangedEvent(preferences.getInsertionDelay()));
eventBus.register(soneInserter);
synchronized (soneInserters) {
soneInserters.put(sone, soneInserter);
@@ -660,6 +635,11 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
loadSone(sone);
database.storeSone(sone);
sone.setStatus(SoneStatus.idle);
+ if (sone.getPosts().isEmpty() && sone.getReplies().isEmpty() && getAllImages(sone.getRootAlbum()).isEmpty()) {
+ // dirty hack
+ lockSone(sone);
+ eventBus.post(new SoneLockedOnStartup(sone));
+ }
soneInserter.start();
return sone;
}
@@ -697,12 +677,12 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
String property = fromNullable(identity.getProperty("Sone.LatestEdition")).or("0");
long latestEdition = fromNullable(tryParse(property)).or(0L);
- Optional existingSone = getSone(identity.getId());
- if (existingSone.isPresent() && existingSone.get().isLocal()) {
- return existingSone.get();
+ Sone existingSone = getSone(identity.getId());
+ if ((existingSone != null )&& existingSone.isLocal()) {
+ return existingSone;
}
- boolean newSone = !existingSone.isPresent();
- Sone sone = !newSone ? existingSone.get() : database.newSoneBuilder().from(identity).build();
+ boolean newSone = existingSone == null;
+ Sone sone = !newSone ? existingSone : database.newSoneBuilder().from(identity).build();
sone.setLatestEdition(latestEdition);
if (newSone) {
synchronized (knownSones) {
@@ -720,7 +700,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
}
database.storeSone(sone);
soneDownloader.addSone(sone);
- soneDownloaders.execute(soneDownloader.fetchSoneWithUriAction(sone));
+ soneDownloaders.execute(soneDownloader.fetchSoneAsUskAction(sone));
return sone;
}
@@ -736,24 +716,20 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
database.addFriend(sone, soneId);
- synchronized (soneFollowingTimes) {
- if (!soneFollowingTimes.containsKey(soneId)) {
- long now = System.currentTimeMillis();
- soneFollowingTimes.put(soneId, now);
- Optional followedSone = getSone(soneId);
- if (!followedSone.isPresent()) {
- return;
- }
- for (Post post : followedSone.get().getPosts()) {
- if (post.getTime() < now) {
- markPostKnown(post);
- }
- }
- for (PostReply reply : followedSone.get().getReplies()) {
- if (reply.getTime() < now) {
- markReplyKnown(reply);
- }
- }
+ @SuppressWarnings("ConstantConditions") // we just followed, this canât be null.
+ long now = database.getFollowingTime(soneId);
+ Sone followedSone = getSone(soneId);
+ if (followedSone == null) {
+ return;
+ }
+ for (Post post : followedSone.getPosts()) {
+ if (post.getTime() < now) {
+ markPostKnown(post);
+ }
+ }
+ for (PostReply reply : followedSone.getReplies()) {
+ if (reply.getTime() < now) {
+ markReplyKnown(reply);
}
}
touchConfiguration();
@@ -771,88 +747,10 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
checkNotNull(sone, "sone must not be null");
checkNotNull(soneId, "soneId must not be null");
database.removeFriend(sone, soneId);
- boolean unfollowedSoneStillFollowed = false;
- for (Sone localSone : getLocalSones()) {
- unfollowedSoneStillFollowed |= localSone.hasFriend(soneId);
- }
- if (!unfollowedSoneStillFollowed) {
- synchronized (soneFollowingTimes) {
- soneFollowingTimes.remove(soneId);
- }
- }
touchConfiguration();
}
/**
- * Sets the trust value of the given origin Sone for the target Sone.
- *
- * @param origin
- * The origin Sone
- * @param target
- * The target Sone
- * @param trustValue
- * The trust value (from {@code -100} to {@code 100})
- */
- public void setTrust(Sone origin, Sone target, int trustValue) {
- checkNotNull(origin, "origin must not be null");
- checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
- checkNotNull(target, "target must not be null");
- checkArgument((trustValue >= -100) && (trustValue <= 100), "trustValue must be within [-100, 100]");
- webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), trustValue, preferences.getTrustComment());
- }
-
- /**
- * Removes any trust assignment for the given target Sone.
- *
- * @param origin
- * The trust origin
- * @param target
- * The trust target
- */
- public void removeTrust(Sone origin, Sone target) {
- checkNotNull(origin, "origin must not be null");
- checkNotNull(target, "target must not be null");
- checkArgument(origin.getIdentity() instanceof OwnIdentity, "origin must be a local Sone");
- webOfTrustUpdater.setTrust((OwnIdentity) origin.getIdentity(), target.getIdentity(), null, null);
- }
-
- /**
- * Assigns the configured positive trust value for the given target.
- *
- * @param origin
- * The trust origin
- * @param target
- * The trust target
- */
- public void trustSone(Sone origin, Sone target) {
- setTrust(origin, target, preferences.getPositiveTrust());
- }
-
- /**
- * Assigns the configured negative trust value for the given target.
- *
- * @param origin
- * The trust origin
- * @param target
- * The trust target
- */
- public void distrustSone(Sone origin, Sone target) {
- setTrust(origin, target, preferences.getNegativeTrust());
- }
-
- /**
- * Removes the trust assignment for the given target.
- *
- * @param origin
- * The trust origin
- * @param target
- * The trust target
- */
- public void untrustSone(Sone origin, Sone target) {
- removeTrust(origin, target);
- }
-
- /**
* Updates the stored Sone with the given Sone.
*
* @param sone
@@ -874,20 +772,20 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
* of the age of the given Sone
*/
public void updateSone(final Sone sone, boolean soneRescueMode) {
- Optional storedSone = getSone(sone.getId());
- if (storedSone.isPresent()) {
- if (!soneRescueMode && !(sone.getTime() > storedSone.get().getTime())) {
+ Sone storedSone = getSone(sone.getId());
+ if (storedSone != null) {
+ if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
logger.log(Level.FINE, String.format("Downloaded Sone %s is not newer than stored Sone %s.", sone, storedSone));
return;
}
List