X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneInserter.java;h=86ea92a41e5b10947495c678af0d43d4c45fad0e;hp=5b281f6cd52bf3923c948870659164a7a5e9b3ac;hb=4f686e5c5ddcf94ffdf074b953493c148fb2ab32;hpb=bce7561db29a00e3b18926aba073bf99b7790392 diff --git a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java index 5b281f6..86ea92a 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneInserter.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneInserter.java @@ -76,6 +76,12 @@ public class SoneInserter extends AbstractService { /** The Sone to insert. */ private final Sone sone; + /** Whether a modification has been detected. */ + private volatile boolean modified = false; + + /** The fingerprint of the last insert. */ + private volatile String lastInsertFingerprint; + /** * Creates a new Sone inserter. * @@ -108,6 +114,36 @@ public class SoneInserter extends AbstractService { SoneInserter.insertionDelay = insertionDelay; } + /** + * Returns the fingerprint of the last insert. + * + * @return The fingerprint of the last insert + */ + public String getLastInsertFingerprint() { + return lastInsertFingerprint; + } + + /** + * Sets the fingerprint of the last insert. + * + * @param lastInsertFingerprint + * The fingerprint of the last insert + */ + public void setLastInsertFingerprint(String lastInsertFingerprint) { + this.lastInsertFingerprint = lastInsertFingerprint; + } + + /** + * Returns whether the Sone inserter has detected a modification of the + * Sone. + * + * @return {@code true} if the Sone has been modified, {@code false} + * otherwise + */ + public boolean isModified() { + return modified; + } + // // SERVICE METHODS // @@ -117,21 +153,30 @@ public class SoneInserter extends AbstractService { */ @Override protected void serviceRun() { - long modificationCounter = 0; long lastModificationTime = 0; + String lastFingerprint = ""; while (!shouldStop()) { /* check every seconds. */ sleep(1000); InsertInformation insertInformation = null; synchronized (sone) { - 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 }); + String fingerprint = sone.getFingerprint(); + if (!fingerprint.equals(lastFingerprint)) { + if (fingerprint.equals(lastInsertFingerprint)) { + modified = false; + lastModificationTime = 0; + logger.log(Level.FINE, "Sone %s has been reverted to last insert state.", sone); + } else { + lastModificationTime = System.currentTimeMillis(); + modified = true; + sone.setTime(lastModificationTime); + logger.log(Level.FINE, "Sone %s has been modified, waiting %d seconds before inserting.", new Object[] { sone.getName(), insertionDelay }); + } + lastFingerprint = fingerprint; } - if ((lastModificationTime > 0) && ((System.currentTimeMillis() - lastModificationTime) > (insertionDelay * 1000))) { + if (modified && (lastModificationTime > 0) && ((System.currentTimeMillis() - lastModificationTime) > (insertionDelay * 1000))) { + lastInsertFingerprint = fingerprint; insertInformation = new InsertInformation(sone); } } @@ -163,12 +208,11 @@ public class SoneInserter extends AbstractService { */ if (success) { synchronized (sone) { - if (sone.getModificationCounter() == modificationCounter) { + if (lastInsertFingerprint.equals(sone.getFingerprint())) { logger.log(Level.FINE, "Sone “%s” was not modified further, resetting counter…", new Object[] { sone }); - sone.setModificationCounter(0); core.saveSone(sone); - modificationCounter = 0; lastModificationTime = 0; + modified = false; } } }