提交 76f8ac82 authored 作者: noelgrandin's avatar noelgrandin

avoid copying data from ByteBuffer to another ByteBuffer if possible, specially…

avoid copying data from ByteBuffer to another ByteBuffer if possible,  specially for the OffHeapStore
上级 7974fd2a
......@@ -46,9 +46,11 @@ public class FileStore {
return fileName;
}
public void readFully(long pos, ByteBuffer dst) {
public ByteBuffer readFully(long pos, int len) {
readCount++;
ByteBuffer dst = ByteBuffer.allocate(len);
DataUtils.readFully(file, pos, dst);
return dst;
}
public void writeFully(long pos, ByteBuffer src) {
......
......@@ -112,8 +112,6 @@ MVStore:
- simple rollback method (rollback to last committed version)
- MVMap to implement SortedMap, then NavigableMap
- Test with OSGi
- avoid copying data from ByteBuffer to another ByteBuffer if possible,
specially for the OffHeapStore
- storage that splits database into multiple files,
to speed up compact and allow using trim
(by truncating / deleting empty files)
......@@ -555,12 +553,11 @@ public class MVStore {
// we don't know which chunk is the newest
long newestChunk = -1;
// read the last block of the file, and then the two first blocks
ByteBuffer buffLastBlock = fileStore.readFully(fileStore.size() - BLOCK_SIZE, BLOCK_SIZE);
ByteBuffer buffFirst2Blocks = fileStore.readFully(0, BLOCK_SIZE * 2);
ByteBuffer buff = ByteBuffer.allocate(3 * BLOCK_SIZE);
buff.limit(BLOCK_SIZE);
fileStore.readFully(fileStore.size() - BLOCK_SIZE, buff);
buff.limit(3 * BLOCK_SIZE);
buff.position(BLOCK_SIZE);
fileStore.readFully(0, buff);
buff.put(buffLastBlock);
buff.put(buffFirst2Blocks);
for (int i = 0; i < 3 * BLOCK_SIZE; i += BLOCK_SIZE) {
String s = new String(buff.array(), i, BLOCK_SIZE, DataUtils.UTF8)
.trim();
......@@ -1112,9 +1109,7 @@ public class MVStore {
}
private Chunk readChunkHeader(long start) {
ByteBuffer buff = ByteBuffer.allocate(40);
fileStore.readFully(start, buff);
buff.rewind();
ByteBuffer buff = fileStore.readFully(start, 40);
return Chunk.fromHeader(buff, start);
}
......@@ -1164,7 +1159,8 @@ public class MVStore {
int length = MathUtils.roundUpInt(c.length, BLOCK_SIZE) + BLOCK_SIZE;
buff = DataUtils.ensureCapacity(buff, length);
buff.limit(length);
fileStore.readFully(c.start, buff);
ByteBuffer buff2 = fileStore.readFully(c.start, length);
buff.put(buff2);
long end = getEndPosition();
fileStore.markUsed(end, length);
fileStore.free(c.start, length);
......@@ -1196,7 +1192,8 @@ public class MVStore {
int length = MathUtils.roundUpInt(c.length, BLOCK_SIZE) + BLOCK_SIZE;
buff = DataUtils.ensureCapacity(buff, length);
buff.limit(length);
fileStore.readFully(c.start, buff);
ByteBuffer buff2 = fileStore.readFully(c.start, length);
buff.put(buff2);
long pos = fileStore.allocate(length);
fileStore.free(c.start, length);
buff.position(0);
......@@ -1325,8 +1322,7 @@ public class MVStore {
}
private void copyLive(Chunk chunk, ArrayList<Chunk> old) {
ByteBuffer buff = ByteBuffer.allocate(chunk.length);
fileStore.readFully(chunk.start, buff);
ByteBuffer buff = fileStore.readFully(chunk.start, chunk.length);
Chunk.fromHeader(buff, chunk.start);
int chunkLength = chunk.length;
markMetaChanged();
......
......@@ -30,20 +30,22 @@ public class OffHeapStore extends FileStore {
}
@Override
public void readFully(long pos, ByteBuffer dst) {
Entry<Long, ByteBuffer> mem = memory.floorEntry(pos);
if (mem == null) {
public ByteBuffer readFully(long pos, int len) {
Entry<Long, ByteBuffer> memEntry = memory.floorEntry(pos);
if (memEntry == null) {
throw DataUtils.newIllegalStateException(DataUtils.ERROR_READING_FAILED,
"Could not read from position {0}", pos);
}
readCount++;
ByteBuffer buff = mem.getValue();
ByteBuffer read = buff.duplicate();
int offset = (int) (pos - mem.getKey());
read.position(offset);
read.limit(dst.remaining() + offset);
dst.put(read);
dst.rewind();
ByteBuffer buff = memEntry.getValue();
int oldLimit = buff.limit();
int offset = (int) (pos - memEntry.getKey());
buff.position(offset);
buff.limit(len + offset);
ByteBuffer read = buff.slice();
buff.position(0);
buff.limit(oldLimit);
return read;
}
@Override
......
......@@ -169,13 +169,11 @@ public class Page {
maxLength = (int) Math.min(fileSize - filePos, maxLength);
int length = maxLength;
if (maxLength == Integer.MAX_VALUE) {
buff = ByteBuffer.allocate(128);
fileStore.readFully(filePos, buff);
buff = fileStore.readFully(filePos, 128);
maxLength = buff.getInt();
// read the first bytes again
}
buff = ByteBuffer.allocate(length);
fileStore.readFully(filePos, buff);
buff = fileStore.readFully(filePos, length);
Page p = new Page(map, 0);
p.pos = pos;
int chunkId = DataUtils.getPageChunkId(pos);
......
......@@ -87,8 +87,7 @@ public class FilePathCache extends FilePathWrapper {
}
}
len = Math.min(len, dst.remaining());
System.arraycopy(buff.array(), off, dst.array(), dst.position(), len);
dst.position(dst.position() + len);
dst.put(buff.array(), off, len);
return len;
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论