From deff9d642b076552534b8df4ac0f235907d73438 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 13 Jun 2020 18:46:41 +0200 Subject: [PATCH] Store user specific local database information inside a single file Closes #141 --- .../envoy/client/data/PersistentLocalDB.java | 83 +++++++------------ .../envoy/client/data/TransientLocalDB.java | 2 +- 2 files changed, 30 insertions(+), 55 deletions(-) diff --git a/src/main/java/envoy/client/data/PersistentLocalDB.java b/src/main/java/envoy/client/data/PersistentLocalDB.java index 9548516..35e77c3 100644 --- a/src/main/java/envoy/client/data/PersistentLocalDB.java +++ b/src/main/java/envoy/client/data/PersistentLocalDB.java @@ -1,12 +1,12 @@ package envoy.client.data; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.util.ArrayList; import java.util.HashMap; -import envoy.data.ConfigItem; import envoy.data.IDGenerator; +import envoy.data.Message; +import envoy.event.MessageStatusChangeEvent; import envoy.util.SerializationUtils; /** @@ -21,92 +21,67 @@ import envoy.util.SerializationUtils; * @author Maximilian Käfer * @since Envoy Client v0.1-alpha */ -public class PersistentLocalDB extends LocalDB { +public final class PersistentLocalDB extends LocalDB { - private File localDBDir, localDBFile, usersFile, idGeneratorFile, messageCacheFile, statusCacheFile; + private File dbDir, userFile, idGeneratorFile, usersFile; /** - * Initializes an empty local database without a directory. All changes made to - * this instance cannot be saved to the file system.
- *
- * This constructor shall be used in conjunction with the {@code ignoreLocalDB} - * {@link ConfigItem}. + * Constructs an empty local database. To serialize any user-specific data to + * the file system, call {@link PersistentLocalDB#initializeUserStorage()} first + * and then {@link PersistentLocalDB#save()}. * - * @since Envoy Client v0.3-alpha - */ - public PersistentLocalDB() {} - - /** - * Constructs an empty local database. To serialize any chats to the file - * system, call {@link PersistentLocalDB#initializeUserStorage()}. - * - * @param localDBDir the directory in which to store users and chats - * @throws IOException if the PersistentLocalDB could not be initialized + * @param dbDir the directory in which to persist data + * @throws IOException if {@code dbDir} is a file (and not a directory) * @since Envoy Client v0.1-alpha */ - public PersistentLocalDB(File localDBDir) throws IOException { - this.localDBDir = localDBDir; + public PersistentLocalDB(File dbDir) throws IOException { + this.dbDir = dbDir; - // Initialize local database directory - if (localDBDir.exists() && !localDBDir.isDirectory()) - throw new IOException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); - usersFile = new File(localDBDir, "users.db"); - idGeneratorFile = new File(localDBDir, "id_generator.db"); + // Test if the database directory is actually a directory + if (dbDir.exists() && !dbDir.isDirectory()) + throw new IOException(String.format("LocalDBDir '%s' is not a directory!", dbDir.getAbsolutePath())); + + // Initialize global files + idGeneratorFile = new File(dbDir, "id_gen.db"); + usersFile = new File(dbDir, "users.db"); } /** - * Creates a database file for a user-specific list of chats.
- * {@inheritDoc} + * Creates a database file for a user-specific list of chats. * - * @throws NullPointerException if the client user is not yet specified + * @throws IllegalStateException if the client user is not specified * @since Envoy Client v0.1-alpha */ @Override public void initializeUserStorage() { - if (user == null) throw new NullPointerException("Client user is null"); - localDBFile = new File(localDBDir, user.getID() + ".db"); - messageCacheFile = new File(localDBDir, user.getID() + "_message_cache.db"); - statusCacheFile = new File(localDBDir, user.getID() + "_status_cache.db"); + if (user == null) throw new IllegalStateException("Client user is null, cannot initialize user storage"); + userFile = new File(dbDir, user.getID() + ".db"); } - /** - * {@inheritDoc} - */ @Override public void save() throws IOException { // Save users SerializationUtils.write(usersFile, users); // Save user data - if (user != null) { - SerializationUtils.write(localDBFile, chats); - SerializationUtils.write(messageCacheFile, messageCache); - SerializationUtils.write(statusCacheFile, statusCache); - } + if (user != null) SerializationUtils.write(userFile, chats, messageCache, statusCache); // Save id generator if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator); } - /** - * {@inheritDoc} - */ @Override public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); } - /** - * {@inheritDoc} - */ @Override public void loadUserData() throws ClassNotFoundException, IOException { - chats = SerializationUtils.read(localDBFile, ArrayList.class); - messageCache = SerializationUtils.read(messageCacheFile, Cache.class); - statusCache = SerializationUtils.read(statusCacheFile, Cache.class); + try (var in = new ObjectInputStream(new FileInputStream(userFile))) { + chats = (ArrayList) in.readObject(); + messageCache = (Cache) in.readObject(); + statusCache = (Cache) in.readObject(); + } } - /** - * {@inheritDoc} - */ @Override public void loadIDGenerator() { try { diff --git a/src/main/java/envoy/client/data/TransientLocalDB.java b/src/main/java/envoy/client/data/TransientLocalDB.java index 1dcb0c4..ef592fb 100644 --- a/src/main/java/envoy/client/data/TransientLocalDB.java +++ b/src/main/java/envoy/client/data/TransientLocalDB.java @@ -11,5 +11,5 @@ package envoy.client.data; * @author Kai S. K. Engelbart * @since Envoy Client v0.3-alpha */ -public class TransientLocalDB extends LocalDB { +public final class TransientLocalDB extends LocalDB { }