Store user specific local database information inside a single file

Closes #141
This commit is contained in:
Kai S. K. Engelbart 2020-06-13 18:46:41 +02:00
parent 2d92a3afcb
commit c87ab7c9e1
2 changed files with 30 additions and 55 deletions

View File

@ -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.<br>
* <br>
* 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.<br>
* {@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<Chat>) in.readObject();
messageCache = (Cache<Message>) in.readObject();
statusCache = (Cache<MessageStatusChangeEvent>) in.readObject();
}
}
/**
* {@inheritDoc}
*/
@Override
public void loadIDGenerator() {
try {

View File

@ -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 {
}