From c96777831ddacd8399f945a741145b814eef498b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 12 Jun 2014 07:10:00 +0200 Subject: [PATCH] Use Sone modification detector in Sone inserter. --- pom.xml | 5 +++ .../net/pterodactylus/sone/core/SoneInserter.java | 44 +++------------------- 2 files changed, 11 insertions(+), 38 deletions(-) diff --git a/pom.xml b/pom.xml index e96a971..8e57cc8 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,11 @@ test + org.hamcrest + hamcrest-all + 1.3 + + org.freenetproject fred 0.7.5.1405 diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index d9235fd..a511d14 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -17,8 +17,6 @@ package net.pterodactylus.sone.core; -import static com.google.common.base.Optional.absent; -import static com.google.common.base.Optional.of; import static com.google.common.base.Preconditions.checkArgument; import static java.lang.System.currentTimeMillis; import static net.pterodactylus.sone.data.Album.NOT_EMPTY; @@ -29,6 +27,7 @@ import java.nio.charset.Charset; import java.util.HashMap; import java.util.HashSet; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import java.util.logging.Level; import java.util.logging.Logger; @@ -54,7 +53,6 @@ import net.pterodactylus.util.template.TemplateException; import net.pterodactylus.util.template.TemplateParser; import net.pterodactylus.util.template.XmlFilter; -import com.google.common.base.Optional; import com.google.common.collect.FluentIterable; import com.google.common.collect.Ordering; import com.google.common.eventbus.EventBus; @@ -73,7 +71,7 @@ public class SoneInserter extends AbstractService { private static final Logger logger = Logging.getLogger(SoneInserter.class); /** The insertion delay (in seconds). */ - private static volatile int insertionDelay = 60; + private static final AtomicInteger insertionDelay = new AtomicInteger(60); /** The template factory used to create the templates. */ private static final TemplateContextFactory templateContextFactory = new TemplateContextFactory(); @@ -147,7 +145,7 @@ public class SoneInserter extends AbstractService { * The insertion delay (in seconds) */ public static void setInsertionDelay(int insertionDelay) { - SoneInserter.insertionDelay = insertionDelay; + SoneInserter.insertionDelay.set(insertionDelay); } /** @@ -189,43 +187,13 @@ public class SoneInserter extends AbstractService { */ @Override protected void serviceRun() { - Optional lastModificationTime = absent(); - String lastInsertedFingerprint = lastInsertFingerprint; - String lastFingerprint = ""; + SoneModificationDetector soneModificationDetector = new SoneModificationDetector(core, sone, insertionDelay); while (!shouldStop()) { try { /* check every second. */ sleep(1000); - /* don’t insert locked Sones. */ - Sone sone = this.sone; - if (core.isLocked(sone)) { - /* trigger redetection when the Sone is unlocked. */ - lastFingerprint = ""; - lastModificationTime = absent(); - continue; - } - - boolean insertSoneNow = false; - synchronized (sone) { - String fingerprint = sone.getFingerprint(); - if (!fingerprint.equals(lastFingerprint)) { - if (fingerprint.equals(lastInsertedFingerprint)) { - lastModificationTime = absent(); - logger.log(Level.FINE, String.format("Sone %s has been reverted to last insert state.", sone)); - } else { - lastModificationTime = of(currentTimeMillis()); - logger.log(Level.FINE, String.format("Sone %s has been modified, waiting %d seconds before inserting.", sone.getName(), insertionDelay)); - } - lastFingerprint = fingerprint; - } - if (lastModificationTime.isPresent() && ((currentTimeMillis() - lastModificationTime.get()) > (insertionDelay * 1000))) { - lastInsertedFingerprint = fingerprint; - insertSoneNow = true; - } - } - - if (insertSoneNow) { + if (soneModificationDetector.isEligibleForInsert()) { InsertInformation insertInformation = new InsertInformation(sone); logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName())); @@ -262,7 +230,7 @@ public class SoneInserter extends AbstractService { synchronized (sone) { if (insertInformation.getFingerprint().equals(sone.getFingerprint())) { logger.log(Level.FINE, String.format("Sone “%s” was not modified further, resetting counter…", sone)); - lastModificationTime = absent(); + soneModificationDetector.setFingerprint(insertInformation.getFingerprint()); lastInsertFingerprint = insertInformation.getFingerprint(); core.touchConfiguration(); } -- 2.7.4