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

File system abstraction: if used directly, some file systems did not work…

File system abstraction: if used directly, some file systems did not work correctly with spliced byte buffers (the database engine doesn't use those).
上级 291face6
...@@ -73,20 +73,31 @@ public class FilePathCache extends FilePathWrapper { ...@@ -73,20 +73,31 @@ public class FilePathCache extends FilePathWrapper {
long cachePos = getCachePos(position); long cachePos = getCachePos(position);
int off = (int) (position - cachePos); int off = (int) (position - cachePos);
int len = CACHE_BLOCK_SIZE - off; int len = CACHE_BLOCK_SIZE - off;
len = Math.min(len, dst.remaining());
ByteBuffer buff = cache.get(cachePos); ByteBuffer buff = cache.get(cachePos);
if (buff == null) { if (buff == null) {
buff = ByteBuffer.allocate(CACHE_BLOCK_SIZE); buff = ByteBuffer.allocate(CACHE_BLOCK_SIZE);
int read = base.read(buff, cachePos); long pos = cachePos;
while (true) {
int read = base.read(buff, pos);
if (read < 0) {
break;
}
if (buff.remaining() == 0) {
break;
}
pos += read;
}
int read = buff.position();
if (read == CACHE_BLOCK_SIZE) { if (read == CACHE_BLOCK_SIZE) {
cache.put(cachePos, buff); cache.put(cachePos, buff);
} else { } else {
if (read < 0) { if (read < 0) {
return -1; return -1;
} }
len = Math.min(len, read); len = Math.min(len, read - off);
} }
} }
len = Math.min(len, dst.remaining());
dst.put(buff.array(), off, len); dst.put(buff.array(), off, len);
return len; return len;
} }
...@@ -109,6 +120,17 @@ public class FilePathCache extends FilePathWrapper { ...@@ -109,6 +120,17 @@ public class FilePathCache extends FilePathWrapper {
@Override @Override
public int write(ByteBuffer src, long position) throws IOException { public int write(ByteBuffer src, long position) throws IOException {
clearCache(src, position);
return base.write(src, position);
}
@Override
public int write(ByteBuffer src) throws IOException {
clearCache(src, position());
return base.write(src);
}
private void clearCache(ByteBuffer src, long position) {
if (cache.size() > 0) { if (cache.size() > 0) {
int len = src.remaining(); int len = src.remaining();
long p = getCachePos(position); long p = getCachePos(position);
...@@ -118,13 +140,6 @@ public class FilePathCache extends FilePathWrapper { ...@@ -118,13 +140,6 @@ public class FilePathCache extends FilePathWrapper {
len -= CACHE_BLOCK_SIZE; len -= CACHE_BLOCK_SIZE;
} }
} }
return base.write(src, position);
}
@Override
public int write(ByteBuffer src) throws IOException {
throw new UnsupportedOperationException();
// return base.write(src);
} }
@Override @Override
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论