提交 54bbc678 authored 作者: Thomas Mueller's avatar Thomas Mueller

Memory mapped files: There was a bug in version 1.2.139 so that memory mapped…

Memory mapped files: There was a bug in version 1.2.139 so that memory mapped files could only be used together with split
上级 67123266
...@@ -47,42 +47,41 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -47,42 +47,41 @@ public class FileObjectDiskMapped implements FileObject {
} }
private void unMap() throws IOException { private void unMap() throws IOException {
if (mapped != null) { if (mapped == null) {
// first write all data return;
mapped.force(); }
// first write all data
// need to dispose old direct buffer, see bug mapped.force();
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
// need to dispose old direct buffer, see bug
boolean useSystemGc; // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4724038
if (SysProperties.NIO_CLEANER_HACK) {
try { boolean useSystemGc = true;
useSystemGc = false; if (SysProperties.NIO_CLEANER_HACK) {
Method cleanerMethod = mapped.getClass().getMethod("cleaner"); try {
cleanerMethod.setAccessible(true); Method cleanerMethod = mapped.getClass().getMethod("cleaner");
Object cleaner = cleanerMethod.invoke(mapped); cleanerMethod.setAccessible(true);
Method clearMethod = cleaner.getClass().getMethod("clear"); Object cleaner = cleanerMethod.invoke(mapped);
clearMethod.invoke(cleaner); Method clearMethod = cleaner.getClass().getMethod("clear");
} catch (Throwable e) { clearMethod.invoke(cleaner);
useSystemGc = true; useSystemGc = false;
} finally { } catch (Throwable e) {
mapped = null; // useSystemGc is already true
} } finally {
} else {
useSystemGc = true;
}
if (useSystemGc) {
WeakReference<MappedByteBuffer> bufferWeakRef = new WeakReference<MappedByteBuffer>(mapped);
mapped = null; mapped = null;
long start = System.currentTimeMillis(); }
while (bufferWeakRef.get() != null) { }
if (System.currentTimeMillis() - start > GC_TIMEOUT_MS) { if (useSystemGc) {
throw new IOException("Timeout (" + GC_TIMEOUT_MS WeakReference<MappedByteBuffer> bufferWeakRef = new WeakReference<MappedByteBuffer>(mapped);
+ " ms) reached while trying to GC mapped buffer"); mapped = null;
} long start = System.currentTimeMillis();
System.gc(); while (bufferWeakRef.get() != null) {
Thread.yield(); if (System.currentTimeMillis() - start > GC_TIMEOUT_MS) {
throw new IOException("Timeout (" + GC_TIMEOUT_MS
+ " ms) reached while trying to GC mapped buffer");
} }
System.gc();
Thread.yield();
} }
} }
} }
...@@ -95,7 +94,6 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -95,7 +94,6 @@ public class FileObjectDiskMapped implements FileObject {
int oldPos = 0; int oldPos = 0;
if (mapped != null) { if (mapped != null) {
oldPos = pos; oldPos = pos;
mapped.force();
unMap(); unMap();
} }
long length = file.length(); long length = file.length();
...@@ -141,6 +139,7 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -141,6 +139,7 @@ public class FileObjectDiskMapped implements FileObject {
try { try {
mapped.position(pos); mapped.position(pos);
mapped.get(b, off, len); mapped.get(b, off, len);
pos += len;
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
EOFException e2 = new EOFException("EOF"); EOFException e2 = new EOFException("EOF");
e2.initCause(e); e2.initCause(e);
...@@ -159,8 +158,11 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -159,8 +158,11 @@ public class FileObjectDiskMapped implements FileObject {
public void setFileLength(long newLength) throws IOException { public void setFileLength(long newLength) throws IOException {
checkFileSizeLimit(newLength); checkFileSizeLimit(newLength);
int oldPos = pos;
unMap();
IOUtils.setLength(file, newLength); IOUtils.setLength(file, newLength);
reMap(); reMap();
pos = (int) Math.min(newLength, oldPos);
} }
public void sync() throws IOException { public void sync() throws IOException {
...@@ -175,6 +177,7 @@ public class FileObjectDiskMapped implements FileObject { ...@@ -175,6 +177,7 @@ public class FileObjectDiskMapped implements FileObject {
} }
mapped.position(pos); mapped.position(pos);
mapped.put(b, off, len); mapped.put(b, off, len);
pos += len;
} }
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论