Fixed classpaths. Changed storage to use deflate compression for disk storage
This commit is contained in:
parent
6a83e33644
commit
ca642c81ce
@ -5,7 +5,7 @@
|
|||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
|
||||||
<classpathentry exported="true" kind="lib" path="/Dependencies/logging/slf4j-api-1.7.5.jar" sourcepath="/Dependencies/logging/slf4j-api-1.7.5-sources.zip"/>
|
<classpathentry exported="true" kind="lib" path="/Dependencies/logging/slf4j-api-1.7.5.jar" sourcepath="/Dependencies/logging/slf4j-api-1.7.5-sources.zip"/>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
|
||||||
<classpathentry kind="lib" path="/Dependencies/kryo/kryo2-debug.jar" sourcepath="/Dependencies/kryo/kryo2-source.jar"/>
|
<classpathentry kind="lib" path="/Dependencies/kryo/kryo2-debug.jar" sourcepath="/Dependencies/kryo/kryo2-source.zip"/>
|
||||||
<classpathentry kind="lib" path="/Dependencies/kryo/objenesis-1.2.jar" sourcepath="/Dependencies/kryo/objenesis-1.2-sources.jar"/>
|
<classpathentry kind="lib" path="/Dependencies/kryo/objenesis-1.2.jar" sourcepath="/Dependencies/kryo/objenesis-1.2-sources.jar"/>
|
||||||
<classpathentry kind="lib" path="/Dependencies/logging/logback-classic-1.0.13.jar" sourcepath="/Dependencies/logging/logback-classic-1.0.13-sources.zip"/>
|
<classpathentry kind="lib" path="/Dependencies/logging/logback-classic-1.0.13.jar" sourcepath="/Dependencies/logging/logback-classic-1.0.13-sources.zip"/>
|
||||||
<classpathentry kind="lib" path="/Dependencies/logging/logback-core-1.0.13.jar" sourcepath="/Dependencies/logging/logback-core-1.0.13-sources.zip"/>
|
<classpathentry kind="lib" path="/Dependencies/logging/logback-core-1.0.13.jar" sourcepath="/Dependencies/logging/logback-core-1.0.13-sources.zip"/>
|
||||||
|
@ -60,7 +60,7 @@ public class DelayTimer {
|
|||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
DelayTimer.this.listener.execute();
|
DelayTimer.this.listener.execute();
|
||||||
cancel();
|
DelayTimer.this.cancel();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
this.timer.schedule(t, delay);
|
this.timer.schedule(t, delay);
|
||||||
|
@ -4,12 +4,15 @@ import java.io.File;
|
|||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.OutputStream;
|
||||||
import java.io.RandomAccessFile;
|
import java.io.RandomAccessFile;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.zip.DeflaterOutputStream;
|
||||||
|
import java.util.zip.InflaterInputStream;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -34,6 +37,8 @@ public class Storage {
|
|||||||
private DelayTimer timer;
|
private DelayTimer timer;
|
||||||
private WeakReference<?> objectReference;
|
private WeakReference<?> objectReference;
|
||||||
|
|
||||||
|
private Kryo kryo;
|
||||||
|
|
||||||
@SuppressWarnings({"rawtypes","unchecked"})
|
@SuppressWarnings({"rawtypes","unchecked"})
|
||||||
public static Storage load(File file, Object loadIntoObject) {
|
public static Storage load(File file, Object loadIntoObject) {
|
||||||
if (file == null) {
|
if (file == null) {
|
||||||
@ -58,7 +63,7 @@ public class Storage {
|
|||||||
// why load it from disk again? just copy out the values!
|
// why load it from disk again? just copy out the values!
|
||||||
synchronized (storage) {
|
synchronized (storage) {
|
||||||
// have to load from disk!
|
// have to load from disk!
|
||||||
Object source = load(file, loadIntoObject.getClass());
|
Object source = storage.load(file, loadIntoObject.getClass());
|
||||||
|
|
||||||
Object orig = storage.objectReference.get();
|
Object orig = storage.objectReference.get();
|
||||||
if (orig != null) {
|
if (orig != null) {
|
||||||
@ -81,7 +86,7 @@ public class Storage {
|
|||||||
storages.put(file, storage);
|
storages.put(file, storage);
|
||||||
|
|
||||||
// have to load from disk!
|
// have to load from disk!
|
||||||
Object source = load(file, loadIntoObject.getClass());
|
Object source = storage.load(file, loadIntoObject.getClass());
|
||||||
if (source != null) {
|
if (source != null) {
|
||||||
copyFields(source, loadIntoObject);
|
copyFields(source, loadIntoObject);
|
||||||
}
|
}
|
||||||
@ -106,6 +111,9 @@ public class Storage {
|
|||||||
parentFile.mkdirs();
|
parentFile.mkdirs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.kryo = new Kryo();
|
||||||
|
this.kryo.setRegistrationRequired(false);
|
||||||
|
|
||||||
this.objectReference = new WeakReference(loadIntoObject);
|
this.objectReference = new WeakReference(loadIntoObject);
|
||||||
this.timer = new DelayTimer("Storage Writer", new DelayTimer.Callback() {
|
this.timer = new DelayTimer("Storage Writer", new DelayTimer.Callback() {
|
||||||
@Override
|
@Override
|
||||||
@ -185,18 +193,20 @@ public class Storage {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Class<? extends Object> class1 = object.getClass();
|
||||||
|
|
||||||
RandomAccessFile raf = null;
|
RandomAccessFile raf = null;
|
||||||
Output output = null;
|
Output output = null;
|
||||||
try {
|
try {
|
||||||
raf = new RandomAccessFile(this.file, "rw");
|
raf = new RandomAccessFile(this.file, "rw");
|
||||||
FileOutputStream outputStream = new FileOutputStream(raf.getFD());
|
OutputStream outputStream = new DeflaterOutputStream(new FileOutputStream(raf.getFD()));
|
||||||
output = new Output(outputStream, 1024); // write 1024 at a time
|
output = new Output(outputStream, 1024); // write 1024 at a time
|
||||||
|
|
||||||
|
this.kryo.writeObject(output, object);
|
||||||
Kryo kryo = new Kryo();
|
|
||||||
kryo.setRegistrationRequired(false);
|
|
||||||
kryo.writeObject(output, object);
|
|
||||||
output.flush();
|
output.flush();
|
||||||
|
|
||||||
|
load(this.file, class1);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Storage.logger.error("Error saving the data!", e);
|
Storage.logger.error("Error saving the data!", e);
|
||||||
} finally {
|
} finally {
|
||||||
@ -214,7 +224,7 @@ public class Storage {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
private static <T> T load(File file, Class<? extends Object> clazz) {
|
private synchronized <T> T load(File file, Class<? extends Object> clazz) {
|
||||||
if (file.length() == 0) {
|
if (file.length() == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -223,11 +233,9 @@ public class Storage {
|
|||||||
Input input = null;
|
Input input = null;
|
||||||
try {
|
try {
|
||||||
raf = new RandomAccessFile(file, "r");
|
raf = new RandomAccessFile(file, "r");
|
||||||
input = new Input(new FileInputStream(raf.getFD()), 1024); // read 1024 at a time
|
input = new Input(new InflaterInputStream(new FileInputStream(raf.getFD())), 1024); // read 1024 at a time
|
||||||
|
|
||||||
Kryo kryo = new Kryo();
|
Object readObject = this.kryo.readObject(input, clazz);
|
||||||
kryo.setRegistrationRequired(false);
|
|
||||||
Object readObject = kryo.readObject(input, clazz);
|
|
||||||
return (T) readObject;
|
return (T) readObject;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.error("Error reading from '{}'! Perhaps the file is corrupt?", file.getAbsolutePath());
|
logger.error("Error reading from '{}'! Perhaps the file is corrupt?", file.getAbsolutePath());
|
||||||
|
Loading…
Reference in New Issue
Block a user