Merge pull request #146 from informatik-ag-ngl/f/single_file_local_db

Store user specific local database information inside a single file
This commit is contained in:
Kai S. K. Engelbart 2020-06-21 07:12:23 +00:00 committed by GitHub
commit c916c3c22c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 55 deletions

View File

@ -1,12 +1,12 @@
package envoy.client.data; package envoy.client.data;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import envoy.data.ConfigItem;
import envoy.data.IDGenerator; import envoy.data.IDGenerator;
import envoy.data.Message;
import envoy.event.MessageStatusChangeEvent;
import envoy.util.SerializationUtils; import envoy.util.SerializationUtils;
/** /**
@ -21,92 +21,67 @@ import envoy.util.SerializationUtils;
* @author Maximilian Käfer * @author Maximilian Käfer
* @since Envoy Client v0.1-alpha * @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 * Constructs an empty local database. To serialize any user-specific data to
* this instance cannot be saved to the file system.<br> * the file system, call {@link PersistentLocalDB#initializeUserStorage()} first
* <br> * and then {@link PersistentLocalDB#save()}.
* This constructor shall be used in conjunction with the {@code ignoreLocalDB}
* {@link ConfigItem}.
* *
* @since Envoy Client v0.3-alpha * @param dbDir the directory in which to persist data
*/ * @throws IOException if {@code dbDir} is a file (and not a directory)
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
* @since Envoy Client v0.1-alpha * @since Envoy Client v0.1-alpha
*/ */
public PersistentLocalDB(File localDBDir) throws IOException { public PersistentLocalDB(File dbDir) throws IOException {
this.localDBDir = localDBDir; this.dbDir = dbDir;
// Initialize local database directory // Test if the database directory is actually a directory
if (localDBDir.exists() && !localDBDir.isDirectory()) if (dbDir.exists() && !dbDir.isDirectory())
throw new IOException(String.format("LocalDBDir '%s' is not a directory!", localDBDir.getAbsolutePath())); throw new IOException(String.format("LocalDBDir '%s' is not a directory!", dbDir.getAbsolutePath()));
usersFile = new File(localDBDir, "users.db");
idGeneratorFile = new File(localDBDir, "id_generator.db"); // 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.<br> * Creates a database file for a user-specific list of chats.
* {@inheritDoc}
* *
* @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 * @since Envoy Client v0.1-alpha
*/ */
@Override @Override
public void initializeUserStorage() { public void initializeUserStorage() {
if (user == null) throw new NullPointerException("Client user is null"); if (user == null) throw new IllegalStateException("Client user is null, cannot initialize user storage");
localDBFile = new File(localDBDir, user.getID() + ".db"); userFile = new File(dbDir, user.getID() + ".db");
messageCacheFile = new File(localDBDir, user.getID() + "_message_cache.db");
statusCacheFile = new File(localDBDir, user.getID() + "_status_cache.db");
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void save() throws IOException { public void save() throws IOException {
// Save users // Save users
SerializationUtils.write(usersFile, users); SerializationUtils.write(usersFile, users);
// Save user data // Save user data
if (user != null) { if (user != null) SerializationUtils.write(userFile, chats, messageCache, statusCache);
SerializationUtils.write(localDBFile, chats);
SerializationUtils.write(messageCacheFile, messageCache);
SerializationUtils.write(statusCacheFile, statusCache);
}
// Save id generator // Save id generator
if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator); if (hasIDGenerator()) SerializationUtils.write(idGeneratorFile, idGenerator);
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); } public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); }
/**
* {@inheritDoc}
*/
@Override @Override
public void loadUserData() throws ClassNotFoundException, IOException { public void loadUserData() throws ClassNotFoundException, IOException {
chats = SerializationUtils.read(localDBFile, ArrayList.class); try (var in = new ObjectInputStream(new FileInputStream(userFile))) {
messageCache = SerializationUtils.read(messageCacheFile, Cache.class); chats = (ArrayList<Chat>) in.readObject();
statusCache = SerializationUtils.read(statusCacheFile, Cache.class); messageCache = (Cache<Message>) in.readObject();
statusCache = (Cache<MessageStatusChangeEvent>) in.readObject();
}
} }
/**
* {@inheritDoc}
*/
@Override @Override
public void loadIDGenerator() { public void loadIDGenerator() {
try { try {

View File

@ -11,5 +11,5 @@ package envoy.client.data;
* @author Kai S. K. Engelbart * @author Kai S. K. Engelbart
* @since Envoy Client v0.3-alpha * @since Envoy Client v0.3-alpha
*/ */
public class TransientLocalDB extends LocalDB { public final class TransientLocalDB extends LocalDB {
} }