X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserter.java;h=05e36008a11ae686b69bf43dcd1b9f783202031d;hb=b29cf0908e6dfd2b55220a3a7e44200f2fe5b19e;hp=314f1bd683fffbdbc2e4417b619b9cd27616116f;hpb=c69b92eb053a01cb651bce19c6f8656a679ff796;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 314f1bd..05e3600 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -25,6 +25,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -56,6 +57,9 @@ public class SoneInserter extends AbstractService { /** The logger. */ private static final Logger logger = Logging.getLogger(SoneInserter.class); + /** The insertion delay (in seconds). */ + private static volatile int insertionDelay = 60; + /** The template factory used to create the templates. */ private static final DefaultTemplateFactory templateFactory = new DefaultTemplateFactory(); @@ -87,13 +91,28 @@ public class SoneInserter extends AbstractService { * The Sone to insert */ public SoneInserter(Core core, FreenetInterface freenetInterface, Sone sone) { - super("Sone Inserter for “" + sone.getName() + "”"); + super("Sone Inserter for “" + sone.getName() + "”", false); this.core = core; this.freenetInterface = freenetInterface; this.sone = sone; } // + // ACCESSORS + // + + /** + * Changes the insertion delay, i.e. the time the Sone inserter waits after + * it has noticed a Sone modification before it starts the insert. + * + * @param insertionDelay + * The insertion delay (in seconds) + */ + public static void setInsertionDelay(int insertionDelay) { + SoneInserter.insertionDelay = insertionDelay; + } + + // // SERVICE METHODS // @@ -103,29 +122,32 @@ public class SoneInserter extends AbstractService { @Override protected void serviceRun() { long modificationCounter = 0; - boolean restartNow = true; + long lastModificationTime = 0; while (!shouldStop()) { - if (!restartNow) { - logger.log(Level.FINEST, "Waiting 60 seconds before checking Sone “" + sone.getName() + "”."); - sleep(60 * 1000); - } - restartNow = false; + /* check every seconds. */ + sleep(1000); + InsertInformation insertInformation = null; synchronized (sone) { - modificationCounter = sone.getModificationCounter(); - if (modificationCounter > 0) { - sone.setTime(System.currentTimeMillis()); + if (sone.getModificationCounter() > modificationCounter) { + modificationCounter = sone.getModificationCounter(); + lastModificationTime = System.currentTimeMillis(); + sone.setTime(lastModificationTime); + logger.log(Level.FINE, "Sone %s has been modified, waiting %d seconds before inserting.", new Object[] { sone.getName(), insertionDelay }); + } + if ((lastModificationTime > 0) && ((System.currentTimeMillis() - lastModificationTime) > (insertionDelay * 1000))) { insertInformation = new InsertInformation(sone); } } + if (insertInformation != null) { logger.log(Level.INFO, "Inserting Sone “%s”…", new Object[] { sone.getName() }); boolean success = false; try { core.setSoneStatus(sone, SoneStatus.inserting); - FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setDocName("Sone-" + sone.getName()).setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html"); - sone.updateUris(finalUri); + FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html"); + sone.updateUris(finalUri.getEdition()); success = true; logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri }); } catch (SoneException se1) { @@ -143,9 +165,8 @@ public class SoneInserter extends AbstractService { if (sone.getModificationCounter() == modificationCounter) { logger.log(Level.FINE, "Sone “%s” was not modified further, resetting counter…", new Object[] { sone }); sone.setModificationCounter(0); - } else { - logger.log(Level.FINE, "Sone “%s” was modified since the insert started, starting another insert…", new Object[] { sone }); - restartNow = true; + modificationCounter = 0; + lastModificationTime = 0; } } } @@ -180,8 +201,9 @@ public class SoneInserter extends AbstractService { soneProperties.put("profile", sone.getProfile()); soneProperties.put("posts", new ArrayList(sone.getPosts())); soneProperties.put("replies", new HashSet(sone.getReplies())); - soneProperties.put("friends", new HashSet(sone.getFriends())); soneProperties.put("blockedSoneIds", new HashSet(sone.getBlockedSoneIds())); + soneProperties.put("likedPostIds", new HashSet(sone.getLikedPostIds())); + soneProperties.put("likeReplyIds", new HashSet(sone.getLikedReplyIds())); } // @@ -245,6 +267,8 @@ public class SoneInserter extends AbstractService { } finally { Closer.close(templateInputStreamReader); } + @SuppressWarnings("unchecked") + final Set blockedSoneIds = (Set) soneProperties.get("blockedSoneIds"); Collection knownSones = Filters.filteredCollection(core.getKnownSones(), new Filter() { /** @@ -252,7 +276,7 @@ public class SoneInserter extends AbstractService { */ @Override public boolean filterObject(Sone object) { - return !soneProperties.containsKey(object.getId()) && !object.getId().equals(soneProperties.get("id")); + return !blockedSoneIds.contains(object.getId()) && !object.getId().equals(soneProperties.get("id")); } });