diff options
Diffstat (limited to 'src/main/java')
8 files changed, 174 insertions, 18 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/ConfigInitializer.java b/src/main/java/com/keuin/kbackupfabric/ConfigInitializer.java new file mode 100644 index 0000000..770c1cd --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/ConfigInitializer.java @@ -0,0 +1,8 @@ +package com.keuin.kbackupfabric; + +/** + * Initialize classes using config from disk. + */ +public class ConfigInitializer { + +} diff --git a/src/main/java/com/keuin/kbackupfabric/KBConfiguration.java b/src/main/java/com/keuin/kbackupfabric/KBConfiguration.java new file mode 100644 index 0000000..3744430 --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/KBConfiguration.java @@ -0,0 +1,33 @@ +package com.keuin.kbackupfabric; + +/** + * Global plugin configuration. + */ +public class KBConfiguration { + // auto backup interval in seconds. Set this to a negative value to disable auto backup. + private final int autoBackupIntervalSeconds; + // name of backup created automatically. By default it is `auto-backup` + private final String autoBackupName; + // if no player has logged in since previous backup, we skip this backup + private final boolean skipAutoBackupIfNoPlayerLoggedIn; + + public KBConfiguration() { + autoBackupIntervalSeconds = -1; // disabled by default + autoBackupName = "auto-backup"; + skipAutoBackupIfNoPlayerLoggedIn = false; + } + + public KBConfiguration(int autoBackupIntervalSeconds, String autoBackupName, boolean skipAutoBackupIfNoPlayerLoggedIn) { + this.autoBackupIntervalSeconds = autoBackupIntervalSeconds; + this.autoBackupName = autoBackupName; + this.skipAutoBackupIfNoPlayerLoggedIn = skipAutoBackupIfNoPlayerLoggedIn; + } + + public int getAutoBackupIntervalSeconds() { + return autoBackupIntervalSeconds; + } + + public String getAutoBackupName() { + return autoBackupName; + } +} diff --git a/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java new file mode 100644 index 0000000..c35a35d --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/autobackup/AutoBackupScheduler.java @@ -0,0 +1,50 @@ +package com.keuin.kbackupfabric.autobackup; + +import java.util.Optional; +import java.util.Timer; +import java.util.TimerTask; +import java.util.logging.Logger; + +public class AutoBackupScheduler { + + private Timer timer = null; + private final Logger logger = Logger.getLogger(AutoBackupScheduler.class.getName()); + private boolean skipIfNoPlayerLoggedIn; + private final PlayerActivityTracker playerActivityTracker; + + public AutoBackupScheduler(int intervalSeconds, boolean skipIfNoPlayerLoggedIn, PlayerActivityTracker playerActivityTracker) { + this.skipIfNoPlayerLoggedIn = skipIfNoPlayerLoggedIn; + this.playerActivityTracker = playerActivityTracker; + if (intervalSeconds > 0) + setInterval(intervalSeconds); + } + + public synchronized void setInterval(int intervalSeconds) { + Optional.ofNullable(timer).ifPresent(Timer::cancel); + Timer newTimer = new Timer("AutoBackupTimer"); + newTimer.schedule(new TimerTask() { + @Override + public void run() { + toggleBackup(); + } + }, 0L, intervalSeconds * 1000L); + timer = newTimer; + } + + public void setSkipIfNoPlayerLoggedIn(boolean skipIfNoPlayerLoggedIn) { + this.skipIfNoPlayerLoggedIn = skipIfNoPlayerLoggedIn; + } + + public synchronized void stop() { + timer.cancel(); + timer = null; + } + + private void toggleBackup() { + if (playerActivityTracker.getCheckpoint() || !skipIfNoPlayerLoggedIn) { + logger.info("Interval backup event is triggered."); + // TODO: perform a backup + } + } + +} diff --git a/src/main/java/com/keuin/kbackupfabric/autobackup/PlayerActivityTracker.java b/src/main/java/com/keuin/kbackupfabric/autobackup/PlayerActivityTracker.java new file mode 100644 index 0000000..ef2b85e --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/autobackup/PlayerActivityTracker.java @@ -0,0 +1,15 @@ +package com.keuin.kbackupfabric.autobackup; + +public interface PlayerActivityTracker { + /** + * Update the checkpoint, return accumulated result. + * + * @return if there is at least one player logged in since last checkpoint. + */ + boolean getCheckpoint(); + + /** + * Mark dirty. In the next checkpoint, the backup will be performed. + */ + void setCheckpoint(); +} diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionIterator.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionIterator.java index 248d36d..7e523bc 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionIterator.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/ObjectCollectionIterator.java @@ -6,9 +6,6 @@ import java.util.List; import java.util.NoSuchElementException; public class ObjectCollectionIterator implements Iterator<ObjectElement> { - - // TODO: test this - private Iterator<ObjectElement> currentIterator; private final List<ObjectCollection2> cols = new LinkedList<>(); diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/SingleHashIdentifier.java index 9fd61c8..b3afa7f 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/SingleHashIdentifier.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/identifier/SingleHashIdentifier.java @@ -37,11 +37,11 @@ public abstract class SingleHashIdentifier implements ObjectIdentifier { } @Override - public boolean equals(Object obj) { - if (!(obj instanceof SingleHashIdentifier)) { - return false; - } - return Arrays.equals(hash, ((SingleHashIdentifier) obj).hash); + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + SingleHashIdentifier that = (SingleHashIdentifier) o; + return Arrays.equals(hash, that.hash) && type.equals(that.type); } @Override diff --git a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java index 0b15a84..18e0e58 100644 --- a/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java +++ b/src/main/java/com/keuin/kbackupfabric/backup/incremental/manager/IncrementalBackupStorageManager.java @@ -3,8 +3,10 @@ package com.keuin.kbackupfabric.backup.incremental.manager; import com.keuin.kbackupfabric.backup.incremental.ObjectCollection2; import com.keuin.kbackupfabric.backup.incremental.ObjectCollectionIterator; import com.keuin.kbackupfabric.backup.incremental.ObjectElement; +import com.keuin.kbackupfabric.backup.incremental.identifier.ObjectIdentifier; import com.keuin.kbackupfabric.util.FilesystemUtil; import com.keuin.kbackupfabric.util.PrintUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; @@ -31,7 +33,34 @@ public class IncrementalBackupStorageManager { } /** - * Add a object collection to storage base. + * Check whether the storage contains a copy of file with given identifier. + * + * @param identifier the identifier. + * @return whether the file exists. + */ + public boolean contains(@NotNull ObjectIdentifier identifier) { + Objects.requireNonNull(identifier); + return new File(backupStorageBase.toFile(), identifier.getIdentification()).isFile(); + } + + /** + * Check whether the storage contains all files in the given collection. + * + * @param collection the collection. + * @return whether all files exists. + */ + public boolean contains(@NotNull ObjectCollection2 collection) { + Objects.requireNonNull(collection); + for (ObjectCollectionIterator it = new ObjectCollectionIterator(collection); it.hasNext(); ) { + ObjectElement ele = it.next(); + if (!contains(ele.getIdentifier())) + return false; + } + return true; + } + + /** + * Add a object collection to storage base and copy files to the storage. * * @param collection the collection. * @return objects copied to the base. @@ -93,20 +122,22 @@ public class IncrementalBackupStorageManager { Iterable<ObjectCollection2> otherExistingCollections) { // TODO: test this Iterator<ObjectElement> iter = new ObjectCollectionIterator(collection); - Set<ObjectElement> unusedElementSet = new HashSet<>(); - iter.forEachRemaining(unusedElementSet::add); - otherExistingCollections.forEach(col -> new ObjectCollectionIterator(col).forEachRemaining(unusedElementSet::remove)); - AtomicInteger deleteCount = new AtomicInteger(); - unusedElementSet.forEach(ele -> { - File file = new File(backupStorageBase.toFile(), ele.getIdentifier().getIdentification()); + Set<ObjectIdentifier> identifierSet = new HashSet<>(); + iter.forEachRemaining(ele -> identifierSet.add(ele.getIdentifier())); + otherExistingCollections.forEach(col -> new ObjectCollectionIterator(col) + .forEachRemaining(ele -> identifierSet.remove(ele.getIdentifier()))); + int deleteCount = 0; + for (ObjectIdentifier id : identifierSet) { + Objects.requireNonNull(id); + File file = new File(backupStorageBase.toFile(), id.getIdentification()); if (file.exists()) { if (file.delete()) - deleteCount.incrementAndGet(); + ++deleteCount; else LOGGER.warning("Failed to delete unused file " + file.getName()); } - }); - return deleteCount.get(); + } + return deleteCount; } /** diff --git a/src/main/java/com/keuin/kbackupfabric/event/handler/ConcretePlayerConnectEventHandler.java b/src/main/java/com/keuin/kbackupfabric/event/handler/ConcretePlayerConnectEventHandler.java new file mode 100644 index 0000000..171d80d --- /dev/null +++ b/src/main/java/com/keuin/kbackupfabric/event/handler/ConcretePlayerConnectEventHandler.java @@ -0,0 +1,22 @@ +package com.keuin.kbackupfabric.event.handler; + +import com.keuin.kbackupfabric.autobackup.PlayerActivityTracker; +import com.keuin.kbackupfabric.event.OnPlayerConnect; +import com.keuin.kbackupfabric.notification.DistinctNotifiable; +import com.keuin.kbackupfabric.notification.NotificationManager; +import net.minecraft.network.ClientConnection; +import net.minecraft.server.network.ServerPlayerEntity; + +public class ConcretePlayerConnectEventHandler implements OnPlayerConnect.PlayerConnectEventCallback { + private final PlayerActivityTracker playerActivityTracker; + + public ConcretePlayerConnectEventHandler(PlayerActivityTracker playerActivityTracker) { + this.playerActivityTracker = playerActivityTracker; + } + + @Override + public void onPlayerConnect(ClientConnection connection, ServerPlayerEntity player) { + playerActivityTracker.setCheckpoint(); + NotificationManager.INSTANCE.notifyPlayer(DistinctNotifiable.fromServerPlayerEntity(player)); + } +} |