From f36f330c8177dc4f5d15c5074b4a774beb8934f1 Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 22 Sep 2020 16:37:43 +0200 Subject: [PATCH] Add a LocalDB auto save mechanism During startup, a timer is initialized inside the LocalDB which saves it after 500 milliseconds during startup and then in intervals of 2 minutes, which can be configured in the ClientConfig. --- .../main/java/envoy/client/data/ClientConfig.java | 7 +++++++ .../src/main/java/envoy/client/data/LocalDB.java | 14 ++++++++++++++ client/src/main/java/envoy/client/ui/Startup.java | 3 +++ client/src/main/resources/client.properties | 4 +--- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/client/src/main/java/envoy/client/data/ClientConfig.java b/client/src/main/java/envoy/client/data/ClientConfig.java index 1d6498e..f0789e2 100644 --- a/client/src/main/java/envoy/client/data/ClientConfig.java +++ b/client/src/main/java/envoy/client/data/ClientConfig.java @@ -35,6 +35,7 @@ public final class ClientConfig extends Config { put("server", "s", identity()); put("port", "p", Integer::parseInt); put("localDB", "db", File::new); + put("localDBSaveInterval", "db-si", Integer::parseInt); } /** @@ -54,4 +55,10 @@ public final class ClientConfig extends Config { * @since Envoy Client v0.1-alpha */ public File getLocalDB() { return (File) items.get("localDB").get(); } + + /** + * @return the amount of minutes after which the local database should be saved + * @since Envoy Client v0.2-beta + */ + public Integer getLocalDBSaveInterval() { return (Integer) items.get("localDBSaveInterval").get(); } } diff --git a/client/src/main/java/envoy/client/data/LocalDB.java b/client/src/main/java/envoy/client/data/LocalDB.java index 4d02a31..61e9988 100644 --- a/client/src/main/java/envoy/client/data/LocalDB.java +++ b/client/src/main/java/envoy/client/data/LocalDB.java @@ -160,6 +160,20 @@ public final class LocalDB implements EventListener { .forEach(chats::add); } + /** + * Initializes a timer that automatically saves this local database after a + * period of time specified in the settings. + * + * @since Envoy Client v0.2-beta + */ + public void initAutoSave() { + new Timer("LocalDB Autosave", true).schedule(new TimerTask() { + + @Override + public void run() { save(); } + }, 2000, ClientConfig.getInstance().getLocalDBSaveInterval() * 60000); + } + /** * Stores all users. If the client user is specified, their chats will be stored * as well. The message id generator will also be saved if present. diff --git a/client/src/main/java/envoy/client/ui/Startup.java b/client/src/main/java/envoy/client/ui/Startup.java index 84bbba4..012cc00 100644 --- a/client/src/main/java/envoy/client/ui/Startup.java +++ b/client/src/main/java/envoy/client/ui/Startup.java @@ -226,5 +226,8 @@ public final class Startup extends Application { else trayIcon.hide(); }); } + + // Start auto save thread + localDB.initAutoSave(); } } diff --git a/client/src/main/resources/client.properties b/client/src/main/resources/client.properties index 8b0e4dc..2e97815 100644 --- a/client/src/main/resources/client.properties +++ b/client/src/main/resources/client.properties @@ -1,8 +1,6 @@ server=localhost port=8080 localDB=localDB -password= -user= -ignoreLocalDB=false +localDBSaveInterval=2 consoleLevelBarrier=FINER fileLevelBarrier=OFF