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

Improved compatibility with the Java 7 FileSystem abstraction.

上级 abc9cec3
......@@ -54,6 +54,7 @@ public class FileStore {
private String mode;
private TempFileDeleter tempFileDeleter;
private boolean textMode;
private java.nio.channels.FileLock lock;
/**
* Create a new file using the given settings.
......@@ -492,16 +493,27 @@ public class FileStore {
*
* @return true if successful
*/
public boolean tryLock() {
return file.tryLock();
public synchronized boolean tryLock() {
try {
lock = file.tryLock();
return true;
} catch (IOException e) {
// ignore
return false;
}
}
/**
* Release the file lock.
*/
public void releaseLock() {
if (file != null) {
file.releaseLock();
public synchronized void releaseLock() {
if (file != null && lock != null) {
try {
lock.release();
} catch (IOException e) {
// ignore
}
lock = null;
}
}
......
......@@ -7,6 +7,7 @@
package org.h2.store.fs;
import java.io.IOException;
import java.nio.channels.FileLock;
/**
* This interface represents a random access file.
......@@ -71,13 +72,8 @@ public interface FileObject {
/**
* Try to lock the file exclusively.
*
* @return true if locking was successful
* @return a lock object if successful, or null if not
*/
boolean tryLock();
/**
* Release the file lock.
*/
void releaseLock();
FileLock tryLock() throws IOException;
}
......@@ -19,7 +19,6 @@ public class FileObjectDisk implements FileObject {
private final RandomAccessFile file;
private final String name;
private FileLock lock;
FileObjectDisk(String fileName, String mode) throws FileNotFoundException {
this.file = new RandomAccessFile(fileName, mode);
......@@ -48,27 +47,8 @@ public class FileObjectDisk implements FileObject {
}
}
public synchronized boolean tryLock() {
if (lock == null) {
try {
lock = file.getChannel().tryLock();
} catch (Exception e) {
// could not lock (OverlappingFileLockException)
}
return lock != null;
}
return false;
}
public synchronized void releaseLock() {
if (lock != null) {
try {
lock.release();
} catch (IOException e) {
// ignore
}
lock = null;
}
public synchronized FileLock tryLock() throws IOException {
return file.getChannel().tryLock();
}
public void close() throws IOException {
......
......@@ -7,6 +7,7 @@
package org.h2.store.fs;
import java.io.IOException;
import java.nio.channels.FileLock;
/**
* This class represents an in-memory file.
......@@ -60,12 +61,8 @@ public class FileObjectMem implements FileObject {
// do nothing
}
public boolean tryLock() {
return data.tryLock();
}
public void releaseLock() {
data.releaseLock();
public FileLock tryLock() {
return null;
}
}
......@@ -22,7 +22,6 @@ public class FileObjectNio implements FileObject {
private final String name;
private final RandomAccessFile file;
private final FileChannel channel;
private FileLock lock;
private long length;
FileObjectNio(String fileName, String mode) throws IOException {
......@@ -94,27 +93,8 @@ public class FileObjectNio implements FileObject {
}
}
public synchronized boolean tryLock() {
if (lock == null) {
try {
lock = channel.tryLock();
} catch (IOException e) {
// could not lock
}
return lock != null;
}
return false;
}
public synchronized void releaseLock() {
if (lock != null) {
try {
lock.release();
} catch (IOException e) {
// ignore
}
lock = null;
}
public synchronized FileLock tryLock() throws IOException {
return channel.tryLock();
}
public String toString() {
......
......@@ -28,7 +28,6 @@ public class FileObjectNioMapped implements FileObject {
private final MapMode mode;
private RandomAccessFile file;
private MappedByteBuffer mapped;
private FileLock lock;
/**
* The position within the file. Can't use the position of the mapped buffer
......@@ -202,27 +201,8 @@ public class FileObjectNioMapped implements FileObject {
pos += len;
}
public synchronized boolean tryLock() {
if (lock == null) {
try {
lock = file.getChannel().tryLock();
} catch (IOException e) {
// could not lock
}
return lock != null;
}
return false;
}
public synchronized void releaseLock() {
if (lock != null) {
try {
lock.release();
} catch (IOException e) {
// ignore
}
lock = null;
}
public synchronized FileLock tryLock() throws IOException {
return file.getChannel().tryLock();
}
}
......@@ -7,6 +7,7 @@
package org.h2.store.fs;
import java.io.IOException;
import java.nio.channels.FileLock;
/**
* A file object that records all write operations and can re-play them.
......@@ -62,12 +63,8 @@ public class FileObjectRec implements FileObject {
fs.log(Recorder.WRITE, name, buff, file.position());
}
public boolean tryLock() {
public FileLock tryLock() throws IOException {
return file.tryLock();
}
public void releaseLock() {
file.releaseLock();
}
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ package org.h2.store.fs;
import java.io.EOFException;
import java.io.IOException;
import java.nio.channels.FileLock;
import org.h2.message.DbException;
/**
......@@ -153,12 +154,8 @@ public class FileObjectSplit implements FileObject {
return l;
}
public boolean tryLock() {
public FileLock tryLock() throws IOException {
return list[0].tryLock();
}
public void releaseLock() {
list[0].releaseLock();
}
}
......@@ -9,6 +9,7 @@ package org.h2.store.fs;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileLock;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
......@@ -103,12 +104,8 @@ public class FileObjectZip implements FileObject {
throw new IOException("File is read-only");
}
public boolean tryLock() {
return false;
}
public void releaseLock() {
// ignore
public FileLock tryLock() {
return null;
}
}
......@@ -29,7 +29,7 @@ public abstract class FilePath {
private static Map<String, FilePath> providers;
/**
* The prefix for temporary files. See also TestClearReferences.
* The prefix for temporary files.
*/
private static String tempRandom;
private static long tempSequence;
......
......@@ -15,7 +15,7 @@ import java.io.IOException;
public class FilePathNio extends FilePathWrapper {
public FileObject openFileObject(String mode) throws IOException {
return new FileObjectNio(name, mode);
return new FileObjectNio(name.substring(getScheme().length() + 1), mode);
}
public String getScheme() {
......
......@@ -15,7 +15,7 @@ import java.io.IOException;
public class FilePathNioMapped extends FilePathNio {
public FileObject openFileObject(String mode) throws IOException {
return new FileObjectNioMapped(name, mode);
return new FileObjectNioMapped(name.substring(getScheme().length() + 1), mode);
}
public String getScheme() {
......
......@@ -12,6 +12,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
......@@ -112,9 +113,11 @@ public class TestFileSystem extends TestBase {
FileObject o = FileUtils.openFileObject(f, "rw");
o.position(4000);
o.write(new byte[1], 0, 1);
o.tryLock();
FileLock lock = o.tryLock();
o.truncate(0);
o.releaseLock();
if (lock != null) {
lock.release();
}
o.close();
}
......@@ -245,8 +248,10 @@ public class TestFileSystem extends TestBase {
can = FilePath.get(fsBase).getCanonicalPath().toString();
String can2 = FileUtils.getCanonicalPath(FileUtils.getParent(path));
assertEquals(can, can2);
fo.tryLock();
fo.releaseLock();
FileLock lock = fo.tryLock();
if (lock != null) {
lock.release();
}
assertEquals(10000, fo.size());
fo.close();
assertEquals(10000, FileUtils.size(fsBase + "/test"));
......
......@@ -7,6 +7,7 @@
package org.h2.test.utils;
import java.io.IOException;
import java.nio.channels.FileLock;
import org.h2.store.fs.FileObject;
/**
......@@ -83,14 +84,9 @@ public class FileDebug implements FileObject {
}
}
public boolean tryLock() {
public FileLock tryLock() throws IOException {
debug("tryLock");
return file.tryLock();
}
public void releaseLock() {
debug("releaseLock");
file.releaseLock();
}
}
\ No newline at end of file
......@@ -8,6 +8,7 @@ package org.h2.dev.fs;
import java.io.EOFException;
import java.io.IOException;
import java.nio.channels.FileLock;
import org.h2.engine.Constants;
import org.h2.security.BlockCipher;
import org.h2.security.CipherFactory;
......@@ -80,10 +81,6 @@ public class FileObjectCrypt implements FileObject {
return Math.max(0, file.size() - HEADER_LENGTH - BLOCK_SIZE);
}
public void releaseLock() {
file.releaseLock();
}
public void position(long pos) throws IOException {
file.position(pos + HEADER_LENGTH);
}
......@@ -92,7 +89,7 @@ public class FileObjectCrypt implements FileObject {
file.sync();
}
public boolean tryLock() {
public FileLock tryLock() throws IOException {
return file.tryLock();
}
......
......@@ -9,6 +9,7 @@ package org.h2.dev.fs;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileLock;
import java.util.zip.ZipInputStream;
import org.h2.store.fs.FileObject;
......@@ -109,12 +110,8 @@ public class FileObjectZip2 implements FileObject {
throw new IOException("File is read-only");
}
public boolean tryLock() {
return false;
}
public void releaseLock() {
// ignore
public FileLock tryLock() {
return null;
}
public String toString() {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论