Added check to ODBs to make sure they are loaded before saving them

This commit is contained in:
Obique PSWG
2015-07-12 16:57:03 -05:00
parent 7e96ee02ca
commit bde7ea802b
2 changed files with 16 additions and 2 deletions
@@ -41,10 +41,12 @@ import java.util.Map.Entry;
public class CachedObjectDatabase<V extends Serializable> extends ObjectDatabase<V> {
private final Map <Long, V> objects;
private boolean loaded;
public CachedObjectDatabase(String filename) {
super(filename);
objects = new HashMap<Long, V>();
loaded = false;
}
public synchronized V put(String key, V value) {
@@ -94,6 +96,10 @@ public class CachedObjectDatabase<V extends Serializable> extends ObjectDatabase
}
public synchronized boolean save() {
if (!loaded) {
System.err.println("Not saving '" + getFile() + "', file not loaded yet!");
return false;
}
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream(getFile()));
@@ -141,6 +147,7 @@ public class CachedObjectDatabase<V extends Serializable> extends ObjectDatabase
objects.put(key, val);
}
}
loaded = true;
} catch (EOFException e) {
} catch (IOException | ClassNotFoundException | ClassCastException e) {
@@ -45,13 +45,15 @@ import java.util.Map.Entry;
public class UncachedObjectDatabase<V extends Serializable> extends ObjectDatabase<V> {
private Map<Long, Long> objectIndex;
private Map<Long, V> objects;
private final Map<Long, Long> objectIndex;
private final Map<Long, V> objects;
private boolean loaded;
public UncachedObjectDatabase(String filename) {
super(filename);
objectIndex = new HashMap<Long, Long>();
objects = new HashMap<Long, V>();
loaded = false;
loadPointers();
}
@@ -106,6 +108,10 @@ public class UncachedObjectDatabase<V extends Serializable> extends ObjectDataba
}
public synchronized boolean save() {
if (!loaded) {
System.err.println("Not saving '" + getFile() + "', file not loaded yet!");
return false;
}
DataOutputStream dos = null;
try {
File file = getFile();
@@ -322,6 +328,7 @@ public class UncachedObjectDatabase<V extends Serializable> extends ObjectDataba
index = loadObject(dis, index);
}
dis.close();
loaded = true;
} catch (IOException e) {
return false;
} finally {