Persisting cache in PersistentLocalDb
This commit is contained in:
parent
4afe073e79
commit
74715bbf82
@ -1,5 +1,6 @@
|
|||||||
package envoy.client.data;
|
package envoy.client.data;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
@ -18,12 +19,13 @@ import envoy.client.util.EnvoyLog;
|
|||||||
* @author Kai S. K. Engelbart
|
* @author Kai S. K. Engelbart
|
||||||
* @since Envoy v0.3-alpha
|
* @since Envoy v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public class Cache<T> implements Consumer<T> {
|
public class Cache<T> implements Consumer<T>, Serializable {
|
||||||
|
|
||||||
private final Queue<T> elements = new LinkedList<>();
|
private final Queue<T> elements = new LinkedList<>();
|
||||||
private transient Consumer<T> processor;
|
private transient Consumer<T> processor;
|
||||||
|
|
||||||
private static final Logger logger = EnvoyLog.getLogger(Cache.class.getSimpleName());
|
private static final Logger logger = EnvoyLog.getLogger(Cache.class.getSimpleName());
|
||||||
|
private static final long serialVersionUID = 7343544675545545076L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an element to the cache.
|
* Adds an element to the cache.
|
||||||
|
@ -24,8 +24,8 @@ public abstract class LocalDb {
|
|||||||
protected Map<String, User> users = new HashMap<>();
|
protected Map<String, User> users = new HashMap<>();
|
||||||
protected List<Chat> chats = new ArrayList<>();
|
protected List<Chat> chats = new ArrayList<>();
|
||||||
protected IdGenerator idGenerator;
|
protected IdGenerator idGenerator;
|
||||||
protected Cache<Message> messageCache;
|
protected Cache<Message> messageCache = new Cache<>();
|
||||||
protected Cache<MessageStatusChangeEvent> statusCache;
|
protected Cache<MessageStatusChangeEvent> statusCache = new Cache<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a storage space for a user-specific list of chats.
|
* Initializes a storage space for a user-specific list of chats.
|
||||||
@ -52,12 +52,12 @@ public abstract class LocalDb {
|
|||||||
public void loadUsers() throws Exception {}
|
public void loadUsers() throws Exception {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads all chat data of the client user.
|
* Loads all data of the client user.
|
||||||
*
|
*
|
||||||
* @throws Exception if the loading process failed
|
* @throws Exception if the loading process failed
|
||||||
* @since Envoy v0.3-alpha
|
* @since Envoy v0.3-alpha
|
||||||
*/
|
*/
|
||||||
public void loadChats() throws Exception {}
|
public void loadUserData() throws Exception {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads the ID generator. Any exception thrown during this process is ignored.
|
* Loads the ID generator. Any exception thrown during this process is ignored.
|
||||||
|
@ -23,7 +23,7 @@ import envoy.util.SerializationUtils;
|
|||||||
*/
|
*/
|
||||||
public class PersistentLocalDb extends LocalDb {
|
public class PersistentLocalDb extends LocalDb {
|
||||||
|
|
||||||
private File localDBDir, localDBFile, usersFile, idGeneratorFile;
|
private File localDBDir, localDBFile, usersFile, idGeneratorFile, messageCacheFile, statusCacheFile;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes an empty local database without a directory. All changes made to
|
* Initializes an empty local database without a directory. All changes made to
|
||||||
@ -63,7 +63,9 @@ public class PersistentLocalDb extends LocalDb {
|
|||||||
@Override
|
@Override
|
||||||
public void initializeUserStorage() {
|
public void initializeUserStorage() {
|
||||||
if (user == null) throw new NullPointerException("Client user is null");
|
if (user == null) throw new NullPointerException("Client user is null");
|
||||||
localDBFile = new File(localDBDir, user.getId() + ".db");
|
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -71,8 +73,12 @@ public class PersistentLocalDb extends LocalDb {
|
|||||||
// Save users
|
// Save users
|
||||||
SerializationUtils.write(usersFile, users);
|
SerializationUtils.write(usersFile, users);
|
||||||
|
|
||||||
// Save chats
|
// Save user data
|
||||||
if (user != null) SerializationUtils.write(localDBFile, chats);
|
if (user != null) {
|
||||||
|
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);
|
||||||
@ -82,7 +88,11 @@ public class PersistentLocalDb extends LocalDb {
|
|||||||
public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); }
|
public void loadUsers() throws ClassNotFoundException, IOException { users = SerializationUtils.read(usersFile, HashMap.class); }
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void loadChats() throws ClassNotFoundException, IOException { chats = SerializationUtils.read(localDBFile, ArrayList.class); }
|
public void loadUserData() throws ClassNotFoundException, IOException {
|
||||||
|
chats = SerializationUtils.read(localDBFile, ArrayList.class);
|
||||||
|
messageCache = SerializationUtils.read(messageCacheFile, Cache.class);
|
||||||
|
statusCache = SerializationUtils.read(statusCacheFile, Cache.class);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void loadIdGenerator() {
|
public void loadIdGenerator() {
|
||||||
|
@ -138,7 +138,7 @@ public class Startup {
|
|||||||
// Initialize chats in local database
|
// Initialize chats in local database
|
||||||
try {
|
try {
|
||||||
localDb.initializeUserStorage();
|
localDb.initializeUserStorage();
|
||||||
localDb.loadChats();
|
localDb.loadUserData();
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
// The local database file has not yet been created, probably first login
|
// The local database file has not yet been created, probably first login
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
Reference in New Issue
Block a user