提交 7040e52b authored 作者: Thomas Mueller's avatar Thomas Mueller

Reading from a StreamStore now throws an IOException if the underlying data doesn't exist.

上级 85c83388
......@@ -73,6 +73,11 @@ public class DataUtils {
*/
public static final int ERROR_CHUNK_NOT_FOUND = 9;
/**
* The block in the stream store was not found.
*/
public static final int ERROR_BLOCK_NOT_FOUND = 50;
/**
* The transaction store is corrupt.
*/
......@@ -781,7 +786,15 @@ public class DataUtils {
return e;
}
private static String formatMessage(int errorCode, String message,
/**
* Format an error message.
*
* @param errorCode the error code
* @param message the message
* @param arguments the arguments
* @return the formatted message
*/
public static String formatMessage(int errorCode, String message,
Object... arguments) {
// convert arguments to strings, to avoid locale specific formatting
for (int i = 0; i < arguments.length; i++) {
......
......@@ -344,7 +344,13 @@ public class StreamStore {
* @return the block
*/
byte[] getBlock(long key) {
return map.get(key);
byte[] data = map.get(key);
if (data == null) {
throw DataUtils.newIllegalStateException(
DataUtils.ERROR_BLOCK_NOT_FOUND,
"Block {0} not found", key);
}
return data;
}
/**
......@@ -367,7 +373,7 @@ public class StreamStore {
}
@Override
public int read() {
public int read() throws IOException {
byte[] buffer = oneByteBuffer;
if (buffer == null) {
buffer = oneByteBuffer = new byte[1];
......@@ -405,13 +411,21 @@ public class StreamStore {
}
@Override
public int read(byte[] b, int off, int len) {
public int read(byte[] b, int off, int len) throws IOException {
if (len <= 0) {
return 0;
}
while (true) {
if (buffer == null) {
try {
buffer = nextBuffer();
} catch (IllegalStateException e) {
String msg = DataUtils.formatMessage(
DataUtils.ERROR_BLOCK_NOT_FOUND,
"Block not found in id {0}",
Arrays.toString(idBuffer.array()));
throw new IOException(msg, e);
}
if (buffer == null) {
return -1;
}
......
......@@ -14,6 +14,8 @@ import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import org.h2.mvstore.DataUtils;
import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore;
import org.h2.mvstore.StreamStore;
......@@ -41,6 +43,7 @@ public class TestStreamStore extends TestBase {
public void test() throws IOException {
FileUtils.deleteRecursive(getBaseDir(), true);
FileUtils.createDirectories(getBaseDir());
testIOException();
testSaveCount();
testExceptionDuringStore();
testReadCount();
......@@ -53,6 +56,25 @@ public class TestStreamStore extends TestBase {
testLoop();
}
private void testIOException() throws IOException {
HashMap<Long, byte[]> map = New.hashMap();
StreamStore s = new StreamStore(map);
byte[] id = s.put(new ByteArrayInputStream(new byte[1024 * 1024]));
InputStream in = s.get(id);
map.clear();
try {
while (true) {
if (in.read() < 0) {
break;
}
}
fail();
} catch (IOException e) {
assertEquals(DataUtils.ERROR_BLOCK_NOT_FOUND,
DataUtils.getErrorCode(e.getMessage()));
}
}
private void testSaveCount() throws IOException {
String fileName = getBaseDir() + "/testSaveCount.h3";
FileUtils.delete(fileName);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论