Updated storage to throw IOException when there is a problem

deserializing the data from disk
This commit is contained in:
nathan 2017-02-18 23:58:08 +01:00
parent 3b3a2e6d74
commit e9585ce5b7
3 changed files with 27 additions and 16 deletions

View File

@ -35,6 +35,7 @@ import dorkbox.util.bytes.ByteArrayWrapper;
* <p/> * <p/>
* Be wary of opening the database file in different JVM instances. Even with file-locks, you can corrupt the data. * Be wary of opening the database file in different JVM instances. Even with file-locks, you can corrupt the data.
*/ */
@SuppressWarnings({"Convert2Diamond", "Convert2Lambda"})
class DiskStorage implements Storage { class DiskStorage implements Storage {
private final DelayTimer timer; private final DelayTimer timer;
private final ByteArrayWrapper defaultKey; private final ByteArrayWrapper defaultKey;
@ -122,37 +123,45 @@ class DiskStorage implements Storage {
/** /**
* Reads a object using the default (blank) key, and casts it to the expected class * Reads a object using the default (blank) key, and casts it to the expected class
*
* @throws IOException if there is a problem deserializing data from disk
*/ */
@Override @Override
public final public final
<T> T get() { <T> T get() throws IOException {
return get0(this.defaultKey); return get0(this.defaultKey);
} }
/** /**
* Reads a object using the specific key, and casts it to the expected class * Reads a object using the specific key, and casts it to the expected class
*
* @throws IOException if there is a problem deserializing data from disk
*/ */
@Override @Override
public final public final
<T> T get(String key) { <T> T get(String key) throws IOException {
return get0(ByteArrayWrapper.wrap(key)); return get0(ByteArrayWrapper.wrap(key));
} }
/** /**
* Reads a object using the specific key, and casts it to the expected class * Reads a object using the specific key, and casts it to the expected class
*
* @throws IOException if there is a problem deserializing data from disk
*/ */
@Override @Override
public final public final
<T> T get(byte[] key) { <T> T get(byte[] key) throws IOException {
return get0(ByteArrayWrapper.wrap(key)); return get0(ByteArrayWrapper.wrap(key));
} }
/** /**
* Reads a object using the specific key, and casts it to the expected class * Reads a object using the specific key, and casts it to the expected class
*
* @throws IOException if there is a problem deserializing data from disk
*/ */
@Override @Override
public final public final
<T> T get(ByteArrayWrapper key) { <T> T get(ByteArrayWrapper key) throws IOException {
return get0(key); return get0(key);
} }
@ -223,9 +232,11 @@ class DiskStorage implements Storage {
/** /**
* Reads a object from pending or from storage * Reads a object from pending or from storage
*
* @throws IOException if there is a problem deserializing data from disk
*/ */
private private
<T> T get0(ByteArrayWrapper key) { <T> T get0(ByteArrayWrapper key) throws IOException {
if (!this.isOpen.get()) { if (!this.isOpen.get()) {
throw new RuntimeException("Unable to act on closed storage"); throw new RuntimeException("Unable to act on closed storage");
} }

View File

@ -38,22 +38,22 @@ interface Storage {
/** /**
* Reads a object using the DEFAULT key ("") key, and casts it to the expected class * Reads a object using the DEFAULT key ("") key, and casts it to the expected class
*/ */
<T> T get(); <T> T get() throws IOException;
/** /**
* Reads a object using the specific key, and casts it to the expected class * Reads a object using the specific key, and casts it to the expected class
*/ */
<T> T get(String key); <T> T get(String key) throws IOException;
/** /**
* Reads a object using the specific key, and casts it to the expected class * Reads a object using the specific key, and casts it to the expected class
*/ */
<T> T get(byte[] key); <T> T get(byte[] key) throws IOException;
/** /**
* Reads a object using the specific key, and casts it to the expected class * Reads a object using the specific key, and casts it to the expected class
*/ */
<T> T get(ByteArrayWrapper key); <T> T get(ByteArrayWrapper key) throws IOException;
/** /**
* Uses the DEFAULT key ("") to return saved data. * Uses the DEFAULT key ("") to return saved data.

View File

@ -33,9 +33,11 @@ import java.util.concurrent.locks.ReentrantLock;
import org.slf4j.Logger; import org.slf4j.Logger;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.io.Input; import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output; import com.esotericsoftware.kryo.io.Output;
import dorkbox.util.OS;
import dorkbox.util.SerializationManager; import dorkbox.util.SerializationManager;
import dorkbox.util.bytes.ByteArrayWrapper; import dorkbox.util.bytes.ByteArrayWrapper;
@ -251,7 +253,7 @@ class StorageBase {
* @return an object for a specified key form referenceCache FIRST, then from DISK * @return an object for a specified key form referenceCache FIRST, then from DISK
*/ */
final final
<T> T get(ByteArrayWrapper key) { <T> T get(ByteArrayWrapper key) throws IOException {
// NOT protected by lock // NOT protected by lock
Metadata meta = this.memoryIndex.get(key); Metadata meta = this.memoryIndex.get(key);
@ -298,13 +300,11 @@ class StorageBase {
return readRecordData; return readRecordData;
} catch (Exception e) { } catch (Exception e) {
if (this.logger != null) { if (e instanceof KryoException && e.getMessage().contains("(missing no-arg constructor)")) {
this.logger.error("Error while getting data from disk", e); throw new IOException("Cannot get data from disk: " + e.getMessage().substring(0, e.getMessage().indexOf(OS.LINE_SEPARATOR)));
} else {
throw new IOException("Cannot get data from disk", e);
} }
else {
e.printStackTrace();
}
return null;
} }
} }