提交 46f4961f authored 作者: Thomas Mueller's avatar Thomas Mueller

Fix the write buffer

上级 696b949a
...@@ -13,14 +13,14 @@ import java.nio.ByteBuffer; ...@@ -13,14 +13,14 @@ import java.nio.ByteBuffer;
*/ */
public class WriteBuffer { public class WriteBuffer {
private static final int MAX_REUSE_LIMIT = 4 * 1024 * 1024; private static final int MAX_REUSE_CAPACITY = 4 * 1024 * 1024;
/** /**
* The maximum byte to grow a buffer at a time. * The minimum number of bytes to grow a buffer at a time.
*/ */
private static final int MAX_GROW = 1024 * 1024; private static final int MIN_GROW = 1024 * 1024;
private ByteBuffer reuse = ByteBuffer.allocate(512 * 1024); private ByteBuffer reuse = ByteBuffer.allocate(MIN_GROW);
private ByteBuffer buff = reuse; private ByteBuffer buff = reuse;
...@@ -273,7 +273,7 @@ public class WriteBuffer { ...@@ -273,7 +273,7 @@ public class WriteBuffer {
* @return this * @return this
*/ */
public WriteBuffer clear() { public WriteBuffer clear() {
if (buff.limit() > MAX_REUSE_LIMIT) { if (buff.limit() > MAX_REUSE_CAPACITY) {
buff = reuse; buff = reuse;
} else if (buff != reuse) { } else if (buff != reuse) {
reuse = buff; reuse = buff;
...@@ -300,13 +300,12 @@ public class WriteBuffer { ...@@ -300,13 +300,12 @@ public class WriteBuffer {
private void grow(int len) { private void grow(int len) {
ByteBuffer temp = buff; ByteBuffer temp = buff;
len = temp.remaining() + len; int needed = len - temp.remaining();
int capacity = temp.capacity(); int newCapacity = temp.capacity() + Math.max(needed, MIN_GROW);
len = Math.max(len, Math.min(capacity + MAX_GROW, capacity * 2)); buff = ByteBuffer.allocate(newCapacity);
buff = ByteBuffer.allocate(len);
temp.flip(); temp.flip();
buff.put(temp); buff.put(temp);
if (len <= MAX_REUSE_LIMIT) { if (newCapacity <= MAX_REUSE_CAPACITY) {
reuse = buff; reuse = buff;
} }
} }
......
...@@ -13,6 +13,7 @@ import java.util.HashMap; ...@@ -13,6 +13,7 @@ import java.util.HashMap;
import java.util.Random; import java.util.Random;
import org.h2.mvstore.DataUtils; import org.h2.mvstore.DataUtils;
import org.h2.mvstore.WriteBuffer;
import org.h2.test.TestBase; import org.h2.test.TestBase;
/** /**
...@@ -31,6 +32,7 @@ public class TestDataUtils extends TestBase { ...@@ -31,6 +32,7 @@ public class TestDataUtils extends TestBase {
@Override @Override
public void test() { public void test() {
testWriteBuffer();
testEncodeLength(); testEncodeLength();
testFletcher(); testFletcher();
testMap(); testMap();
...@@ -40,6 +42,12 @@ public class TestDataUtils extends TestBase { ...@@ -40,6 +42,12 @@ public class TestDataUtils extends TestBase {
testCheckValue(); testCheckValue();
testPagePos(); testPagePos();
} }
private static void testWriteBuffer() {
WriteBuffer buff = new WriteBuffer();
buff.put(new byte[1500000]);
buff.put(new byte[1900000]);
}
private void testFletcher() { private void testFletcher() {
byte[] data = new byte[10000]; byte[] data = new byte[10000];
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论