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.
This commit is contained in:
Kai S. K. Engelbart 2020-09-22 16:37:43 +02:00
parent 5b4f2762e5
commit f36f330c81
Signed by: kske
GPG Key ID: 8BEB13EC5DF7EF13
4 changed files with 25 additions and 3 deletions

View File

@ -35,6 +35,7 @@ public final class ClientConfig extends Config {
put("server", "s", identity()); put("server", "s", identity());
put("port", "p", Integer::parseInt); put("port", "p", Integer::parseInt);
put("localDB", "db", File::new); 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 * @since Envoy Client v0.1-alpha
*/ */
public File getLocalDB() { return (File) items.get("localDB").get(); } 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(); }
} }

View File

@ -160,6 +160,20 @@ public final class LocalDB implements EventListener {
.forEach(chats::add); .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 * 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. * as well. The message id generator will also be saved if present.

View File

@ -226,5 +226,8 @@ public final class Startup extends Application {
else trayIcon.hide(); else trayIcon.hide();
}); });
} }
// Start auto save thread
localDB.initAutoSave();
} }
} }

View File

@ -1,8 +1,6 @@
server=localhost server=localhost
port=8080 port=8080
localDB=localDB localDB=localDB
password= localDBSaveInterval=2
user=
ignoreLocalDB=false
consoleLevelBarrier=FINER consoleLevelBarrier=FINER
fileLevelBarrier=OFF fileLevelBarrier=OFF