提交 ac65d7c1 authored 作者: Thomas Mueller's avatar Thomas Mueller

FileSystem improvements.

上级 d497e7d0
...@@ -213,7 +213,7 @@ public abstract class FilePath { ...@@ -213,7 +213,7 @@ public abstract class FilePath {
* truncated first * truncated first
* @return the output stream * @return the output stream
*/ */
public abstract OutputStream newOutputStream(boolean append); public abstract OutputStream newOutputStream(boolean append) throws IOException;
/** /**
* Open a random access file object. * Open a random access file object.
......
...@@ -247,7 +247,7 @@ public class FilePathDisk extends FilePath { ...@@ -247,7 +247,7 @@ public class FilePathDisk extends FilePath {
throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, name); throw DbException.get(ErrorCode.FILE_CREATION_FAILED_1, name);
} }
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) throws IOException {
try { try {
File file = new File(name); File file = new File(name);
File parent = file.getParentFile(); File parent = file.getParentFile();
...@@ -259,11 +259,7 @@ public class FilePathDisk extends FilePath { ...@@ -259,11 +259,7 @@ public class FilePathDisk extends FilePath {
return out; return out;
} catch (IOException e) { } catch (IOException e) {
freeMemoryAndFinalize(); freeMemoryAndFinalize();
try {
return new FileOutputStream(name); return new FileOutputStream(name);
} catch (IOException e2) {
throw DbException.convertIOException(e, name);
}
} }
} }
...@@ -398,7 +394,6 @@ class FileDisk extends FileBase { ...@@ -398,7 +394,6 @@ class FileDisk extends FileBase {
public FileChannel truncate(long newLength) throws IOException { public FileChannel truncate(long newLength) throws IOException {
if (newLength < file.length()) { if (newLength < file.length()) {
// some implementations actually only support truncate
file.setLength(newLength); file.setLength(newLength);
} }
return this; return this;
......
...@@ -136,14 +136,10 @@ public class FilePathMem extends FilePath { ...@@ -136,14 +136,10 @@ public class FilePathMem extends FilePath {
// TODO directories are not really supported // TODO directories are not really supported
} }
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) throws IOException {
try {
FileMemData obj = getMemoryFile(); FileMemData obj = getMemoryFile();
FileMem m = new FileMem(obj, false); FileMem m = new FileMem(obj, false);
return new FileChannelOutputStream(m, append); return new FileChannelOutputStream(m, append);
} catch (IOException e) {
throw DbException.convertIOException(e, name);
}
} }
public InputStream newInputStream() { public InputStream newInputStream() {
......
...@@ -59,7 +59,7 @@ public class FilePathRec extends FilePathWrapper { ...@@ -59,7 +59,7 @@ public class FilePathRec extends FilePathWrapper {
return new FileRec(this, super.open(mode), name); return new FileRec(this, super.open(mode), name);
} }
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) throws IOException {
log(Recorder.OPEN_OUTPUT_STREAM, name); log(Recorder.OPEN_OUTPUT_STREAM, name);
return super.newOutputStream(append); return super.newOutputStream(append);
} }
......
...@@ -166,12 +166,8 @@ public class FilePathSplit extends FilePathWrapper { ...@@ -166,12 +166,8 @@ public class FilePathSplit extends FilePathWrapper {
throw new IOException(message); throw new IOException(message);
} }
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) throws IOException {
try {
return new FileChannelOutputStream(open("rw"), append); return new FileChannelOutputStream(open("rw"), append);
} catch (IOException e) {
throw DbException.convertIOException(e, name);
}
} }
public void moveTo(FilePath path) { public void moveTo(FilePath path) {
......
...@@ -124,7 +124,7 @@ public abstract class FilePathWrapper extends FilePath { ...@@ -124,7 +124,7 @@ public abstract class FilePathWrapper extends FilePath {
return base.newInputStream(); return base.newInputStream();
} }
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) throws IOException {
return base.newOutputStream(append); return base.newOutputStream(append);
} }
......
...@@ -168,8 +168,8 @@ public class FilePathZip extends FilePath { ...@@ -168,8 +168,8 @@ public class FilePathZip extends FilePath {
return new FileZip(file, entry); return new FileZip(file, entry);
} }
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) throws IOException {
throw DbException.getUnsupportedException("write"); throw new IOException("write");
} }
public void moveTo(FilePath newName) { public void moveTo(FilePath newName) {
......
...@@ -219,7 +219,7 @@ public class FileUtils { ...@@ -219,7 +219,7 @@ public class FileUtils {
* truncated first * truncated first
* @return the output stream * @return the output stream
*/ */
public static OutputStream newOutputStream(String fileName, boolean append) { public static OutputStream newOutputStream(String fileName, boolean append) throws IOException {
return FilePath.get(fileName).newOutputStream(append); return FilePath.get(fileName).newOutputStream(append);
} }
......
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
*/ */
package org.h2.test.unit; package org.h2.test.unit;
import java.io.EOFException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
...@@ -25,6 +24,7 @@ import java.util.List; ...@@ -25,6 +24,7 @@ import java.util.List;
import java.util.Random; import java.util.Random;
import org.h2.message.DbException; import org.h2.message.DbException;
import org.h2.store.fs.FilePath; import org.h2.store.fs.FilePath;
import org.h2.store.fs.FilePathCrypt;
import org.h2.store.fs.FileUtils; import org.h2.store.fs.FileUtils;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.test.utils.AssertThrows; import org.h2.test.utils.AssertThrows;
...@@ -58,8 +58,8 @@ public class TestFileSystem extends TestBase { ...@@ -58,8 +58,8 @@ public class TestFileSystem extends TestBase {
testMemFsDir(); testMemFsDir();
testClasspath(); testClasspath();
FilePathDebug.register().setTrace(true); FilePathDebug.register().setTrace(true);
// testFileSystem("crypt:007:" + getBaseDir() + "/fs"); FilePathCrypt.register();
testFileSystem("crypt:0007:" + getBaseDir() + "/fs");
testSimpleExpandTruncateSize(); testSimpleExpandTruncateSize();
testSplitDatabaseInZip(); testSplitDatabaseInZip();
testDatabaseInMemFileSys(); testDatabaseInMemFileSys();
...@@ -72,7 +72,6 @@ public class TestFileSystem extends TestBase { ...@@ -72,7 +72,6 @@ public class TestFileSystem extends TestBase {
testFileSystem("memLZF:"); testFileSystem("memLZF:");
testUserHome(); testUserHome();
try { try {
// testFileSystem("crypt:007:" + getBaseDir() + "/fs");
testFileSystem("nio:" + getBaseDir() + "/fs"); testFileSystem("nio:" + getBaseDir() + "/fs");
testFileSystem("nioMapped:" + getBaseDir() + "/fs"); testFileSystem("nioMapped:" + getBaseDir() + "/fs");
if (!config.splitFileSystem) { if (!config.splitFileSystem) {
...@@ -230,7 +229,7 @@ public class TestFileSystem extends TestBase { ...@@ -230,7 +229,7 @@ public class TestFileSystem extends TestBase {
} }
private void testReadOnly(final String f) throws IOException { private void testReadOnly(final String f) throws IOException {
new AssertThrows(DbException.class) { public void test() { new AssertThrows(IOException.class) { public void test() throws IOException {
FileUtils.newOutputStream(f, false); FileUtils.newOutputStream(f, false);
}}; }};
new AssertThrows(DbException.class) { public void test() { new AssertThrows(DbException.class) { public void test() {
...@@ -500,6 +499,7 @@ public class TestFileSystem extends TestBase { ...@@ -500,6 +499,7 @@ public class TestFileSystem extends TestBase {
} }
case 2: { case 2: {
trace("truncate " + pos); trace("truncate " + pos);
buff.append("truncate " + pos + "\n");
f.truncate(pos); f.truncate(pos);
if (pos < ra.length()) { if (pos < ra.length()) {
// truncate is supposed to have no effect if the // truncate is supposed to have no effect if the
...@@ -507,7 +507,6 @@ public class TestFileSystem extends TestBase { ...@@ -507,7 +507,6 @@ public class TestFileSystem extends TestBase {
ra.setLength(pos); ra.setLength(pos);
} }
assertEquals(ra.getFilePointer(), f.position()); assertEquals(ra.getFilePointer(), f.position());
buff.append("truncate " + pos + "\n");
break; break;
} }
case 3: { case 3: {
...@@ -516,13 +515,9 @@ public class TestFileSystem extends TestBase { ...@@ -516,13 +515,9 @@ public class TestFileSystem extends TestBase {
byte[] b1 = new byte[len]; byte[] b1 = new byte[len];
byte[] b2 = new byte[len]; byte[] b2 = new byte[len];
trace("readFully " + len); trace("readFully " + len);
buff.append("readFully " + len + "\n");
ra.readFully(b1, 0, len); ra.readFully(b1, 0, len);
try {
FileUtils.readFully(f, ByteBuffer.wrap(b2, 0, len)); FileUtils.readFully(f, ByteBuffer.wrap(b2, 0, len));
} catch (EOFException e) {
e.printStackTrace();
}
buff.append("readFully " + len + "\n");
assertEquals(b1, b2); assertEquals(b1, b2);
break; break;
} }
......
...@@ -132,7 +132,7 @@ public class FilePathDebug extends FilePathWrapper { ...@@ -132,7 +132,7 @@ public class FilePathDebug extends FilePathWrapper {
public InputStream newInputStream() throws IOException { public InputStream newInputStream() throws IOException {
trace(name, "newInputStream"); trace(name, "newInputStream");
InputStream in = super.newInputStream(); InputStream in = super.newInputStream();
if (!trace) { if (!isTrace()) {
return in; return in;
} }
final String fileName = name; final String fileName = name;
...@@ -159,7 +159,7 @@ public class FilePathDebug extends FilePathWrapper { ...@@ -159,7 +159,7 @@ public class FilePathDebug extends FilePathWrapper {
return new FileDebug(this, super.open(mode), name); return new FileDebug(this, super.open(mode), name);
} }
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) throws IOException {
trace(name, "newOutputStream", append); trace(name, "newOutputStream", append);
return super.newOutputStream(append); return super.newOutputStream(append);
} }
...@@ -183,7 +183,7 @@ public class FilePathDebug extends FilePathWrapper { ...@@ -183,7 +183,7 @@ public class FilePathDebug extends FilePathWrapper {
* @param params parameters if any * @param params parameters if any
*/ */
void trace(String fileName, String method, Object... params) { void trace(String fileName, String method, Object... params) {
if (trace) { if (isTrace()) {
StringBuilder buff = new StringBuilder(" "); StringBuilder buff = new StringBuilder(" ");
buff.append(unwrap(fileName)).append(' ').append(method); buff.append(unwrap(fileName)).append(' ').append(method);
for (Object s : params) { for (Object s : params) {
...@@ -202,11 +202,11 @@ public class FilePathDebug extends FilePathWrapper { ...@@ -202,11 +202,11 @@ public class FilePathDebug extends FilePathWrapper {
} }
public boolean isTrace() { public boolean isTrace() {
return trace; return INSTANCE.trace;
} }
public void setTrace(boolean trace) { public void setTrace(boolean trace) {
this.trace = trace; INSTANCE.trace = trace;
} }
public String getScheme() { public String getScheme() {
...@@ -227,7 +227,7 @@ class FileDebug extends FileBase { ...@@ -227,7 +227,7 @@ class FileDebug extends FileBase {
FileDebug(FilePathDebug debug, FileChannel channel, String name) { FileDebug(FilePathDebug debug, FileChannel channel, String name) {
this.debug = debug; this.debug = debug;
this.channel = channel; this.channel = channel;
this.name = debug.getScheme() + ":" + name; this.name = name;
} }
public void implCloseChannel() throws IOException { public void implCloseChannel() throws IOException {
...@@ -296,4 +296,8 @@ class FileDebug extends FileBase { ...@@ -296,4 +296,8 @@ class FileDebug extends FileBase {
return channel.tryLock(position, size, shared); return channel.tryLock(position, size, shared);
} }
public String toString() {
return name;
}
} }
...@@ -122,7 +122,7 @@ public class FilePathUnstable extends FilePathWrapper { ...@@ -122,7 +122,7 @@ public class FilePathUnstable extends FilePathWrapper {
return new FileUnstable(this, super.open(mode)); return new FileUnstable(this, super.open(mode));
} }
public OutputStream newOutputStream(boolean append) { public OutputStream newOutputStream(boolean append) throws IOException {
return super.newOutputStream(append); return super.newOutputStream(append);
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论