提交 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 { ...@@ -73,6 +73,11 @@ public class DataUtils {
*/ */
public static final int ERROR_CHUNK_NOT_FOUND = 9; 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. * The transaction store is corrupt.
*/ */
...@@ -781,7 +786,15 @@ public class DataUtils { ...@@ -781,7 +786,15 @@ public class DataUtils {
return e; 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) { Object... arguments) {
// convert arguments to strings, to avoid locale specific formatting // convert arguments to strings, to avoid locale specific formatting
for (int i = 0; i < arguments.length; i++) { for (int i = 0; i < arguments.length; i++) {
......
...@@ -344,7 +344,13 @@ public class StreamStore { ...@@ -344,7 +344,13 @@ public class StreamStore {
* @return the block * @return the block
*/ */
byte[] getBlock(long key) { 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 { ...@@ -367,7 +373,7 @@ public class StreamStore {
} }
@Override @Override
public int read() { public int read() throws IOException {
byte[] buffer = oneByteBuffer; byte[] buffer = oneByteBuffer;
if (buffer == null) { if (buffer == null) {
buffer = oneByteBuffer = new byte[1]; buffer = oneByteBuffer = new byte[1];
...@@ -405,13 +411,21 @@ public class StreamStore { ...@@ -405,13 +411,21 @@ public class StreamStore {
} }
@Override @Override
public int read(byte[] b, int off, int len) { public int read(byte[] b, int off, int len) throws IOException {
if (len <= 0) { if (len <= 0) {
return 0; return 0;
} }
while (true) { while (true) {
if (buffer == null) { if (buffer == null) {
try {
buffer = nextBuffer(); 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) { if (buffer == null) {
return -1; return -1;
} }
......
...@@ -14,6 +14,8 @@ import java.util.HashMap; ...@@ -14,6 +14,8 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.h2.mvstore.DataUtils;
import org.h2.mvstore.MVMap; import org.h2.mvstore.MVMap;
import org.h2.mvstore.MVStore; import org.h2.mvstore.MVStore;
import org.h2.mvstore.StreamStore; import org.h2.mvstore.StreamStore;
...@@ -41,6 +43,7 @@ public class TestStreamStore extends TestBase { ...@@ -41,6 +43,7 @@ public class TestStreamStore extends TestBase {
public void test() throws IOException { public void test() throws IOException {
FileUtils.deleteRecursive(getBaseDir(), true); FileUtils.deleteRecursive(getBaseDir(), true);
FileUtils.createDirectories(getBaseDir()); FileUtils.createDirectories(getBaseDir());
testIOException();
testSaveCount(); testSaveCount();
testExceptionDuringStore(); testExceptionDuringStore();
testReadCount(); testReadCount();
...@@ -53,6 +56,25 @@ public class TestStreamStore extends TestBase { ...@@ -53,6 +56,25 @@ public class TestStreamStore extends TestBase {
testLoop(); 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 { private void testSaveCount() throws IOException {
String fileName = getBaseDir() + "/testSaveCount.h3"; String fileName = getBaseDir() + "/testSaveCount.h3";
FileUtils.delete(fileName); FileUtils.delete(fileName);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论