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

A persistent tree map (work in progress).

上级 4a0120b8
...@@ -103,6 +103,8 @@ import org.h2.test.server.TestAutoServer; ...@@ -103,6 +103,8 @@ import org.h2.test.server.TestAutoServer;
import org.h2.test.server.TestNestedLoop; import org.h2.test.server.TestNestedLoop;
import org.h2.test.server.TestWeb; import org.h2.test.server.TestWeb;
import org.h2.test.server.TestInit; import org.h2.test.server.TestInit;
import org.h2.test.store.TestCacheLIRS;
import org.h2.test.store.TestDataUtils;
import org.h2.test.store.TestTreeMapStore; import org.h2.test.store.TestTreeMapStore;
import org.h2.test.synth.TestBtreeIndex; import org.h2.test.synth.TestBtreeIndex;
import org.h2.test.synth.TestCrashAPI; import org.h2.test.synth.TestCrashAPI;
...@@ -662,8 +664,9 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1` ...@@ -662,8 +664,9 @@ kill -9 `jps -l | grep "org.h2.test." | cut -d " " -f 1`
private void testUnit() { private void testUnit() {
// store // store
new TestCacheLIRS().runTest(this);
new TestTreeMapStore().runTest(this); new TestTreeMapStore().runTest(this);
new TestCache().runTest(this); new TestDataUtils().runTest(this);
// unit // unit
new TestAutoReconnect().runTest(this); new TestAutoReconnect().runTest(this);
......
...@@ -12,13 +12,12 @@ import java.util.Map.Entry; ...@@ -12,13 +12,12 @@ import java.util.Map.Entry;
import java.util.Random; import java.util.Random;
import org.h2.dev.store.btree.CacheLIRS; import org.h2.dev.store.btree.CacheLIRS;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.upgrade.v1_1.util.Profiler;
import org.h2.util.New; import org.h2.util.New;
/** /**
* Tests the cache algorithm. * Tests the cache algorithm.
*/ */
public class TestCache extends TestBase { public class TestCacheLIRS extends TestBase {
/** /**
* Run just this test. * Run just this test.
...@@ -30,8 +29,6 @@ public class TestCache extends TestBase { ...@@ -30,8 +29,6 @@ public class TestCache extends TestBase {
} }
public void test() throws Exception { public void test() throws Exception {
Profiler p = new Profiler();
p.startCollecting();
testEdgeCases(); testEdgeCases();
testSize(); testSize();
testClear(); testClear();
...@@ -42,7 +39,6 @@ public class TestCache extends TestBase { ...@@ -42,7 +39,6 @@ public class TestCache extends TestBase {
testBadHashMethod(); testBadHashMethod();
testScanResistance(); testScanResistance();
testRandomOperations(); testRandomOperations();
System.out.println(p.getTop(5));
} }
private void testEdgeCases() { private void testEdgeCases() {
......
/*
* Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License, Version
* 1.0, and under the Eclipse Public License, Version 1.0
* (http://h2database.com/html/license.html). Initial Developer: H2 Group
*/
package org.h2.test.store;
import org.h2.dev.store.btree.Page;
import org.h2.test.TestBase;
/**
* Test utility classes.
*/
public class TestDataUtils extends TestBase {
/**
* Run just this test.
*
* @param a ignored
*/
public static void main(String... a) throws Exception {
TestBase.createCaller().init().test();
}
public void test() throws Exception {
testPagePos();
}
private void testPagePos() {
int lastCode = 0;
assertEquals(0, Page.encodeLength(32));
assertEquals(1, Page.encodeLength(33));
assertEquals(1, Page.encodeLength(48));
assertEquals(2, Page.encodeLength(49));
assertEquals(30, Page.encodeLength(1024 * 1024));
assertEquals(31, Page.encodeLength(1024 * 1024 + 1));
for (int i = 1024 * 1024 + 1; i < 100 * 1024 * 1024; i += 1024) {
int code = Page.encodeLength(i);
assertEquals(31, code);
}
for (int i = 0; i < 1024 * 1024; i++) {
int code = Page.encodeLength(i);
assertTrue(code <= 31 && code >= 0);
assertTrue(code >= lastCode);
if (code > lastCode) {
lastCode = code;
}
int max = Page.getMaxLength(code);
assertTrue(max >= i && max >= 32);
}
}
}
...@@ -17,13 +17,13 @@ import java.util.Iterator; ...@@ -17,13 +17,13 @@ import java.util.Iterator;
public class BtreeMap<K, V> { public class BtreeMap<K, V> {
private final BtreeMapStore store; private final BtreeMapStore store;
private final long id; private final int id;
private final String name; private final String name;
private final DataType keyType; private final DataType keyType;
private final DataType valueType; private final DataType valueType;
private Page root; private Page root;
private BtreeMap(BtreeMapStore store, long id, String name, DataType keyType, DataType valueType) { private BtreeMap(BtreeMapStore store, int id, String name, DataType keyType, DataType valueType) {
this.store = store; this.store = store;
this.id = id; this.id = id;
this.name = name; this.name = name;
...@@ -43,7 +43,7 @@ public class BtreeMap<K, V> { ...@@ -43,7 +43,7 @@ public class BtreeMap<K, V> {
* @param valueClass the value class * @param valueClass the value class
* @return the map * @return the map
*/ */
static <K, V> BtreeMap<K, V> open(BtreeMapStore store, long id, String name, DataType keyType, DataType valueType) { static <K, V> BtreeMap<K, V> open(BtreeMapStore store, int id, String name, DataType keyType, DataType valueType) {
return new BtreeMap<K, V>(store, id, name, keyType, valueType); return new BtreeMap<K, V>(store, id, name, keyType, valueType);
} }
...@@ -96,6 +96,14 @@ public class BtreeMap<K, V> { ...@@ -96,6 +96,14 @@ public class BtreeMap<K, V> {
} }
} }
/**
* Remove all entries, and remove the map.
*/
public void remove() {
clear();
store.removeMap(id);
}
/** /**
* Remove a key-value pair. * Remove a key-value pair.
* *
...@@ -114,7 +122,7 @@ public class BtreeMap<K, V> { ...@@ -114,7 +122,7 @@ public class BtreeMap<K, V> {
* @return true if yes * @return true if yes
*/ */
boolean isChanged() { boolean isChanged() {
return root != null && root.getId() < 0; return root != null && root.getPos() < 0;
} }
private void markChanged() { private void markChanged() {
...@@ -167,22 +175,22 @@ public class BtreeMap<K, V> { ...@@ -167,22 +175,22 @@ public class BtreeMap<K, V> {
} }
/** /**
* Read a node. * Read a page.
* *
* @param id the node id * @param pos the position of the page
* @return the node * @return the page
*/ */
Page readPage(long id) { Page readPage(long pos) {
return store.readPage(this, id); return store.readPage(this, pos);
} }
/** /**
* Remove a node. * Remove a page.
* *
* @param id the node id * @param pos the position of the page
*/ */
void removePage(long id) { void removePage(long pos) {
store.removePage(id); store.removePage(pos);
} }
/** /**
...@@ -190,7 +198,7 @@ public class BtreeMap<K, V> { ...@@ -190,7 +198,7 @@ public class BtreeMap<K, V> {
* *
* @param rootPos the position * @param rootPos the position
*/ */
void setRoot(long rootPos) { void setRootPos(long rootPos) {
root = readPage(rootPos); root = readPage(rootPos);
} }
...@@ -205,9 +213,9 @@ public class BtreeMap<K, V> { ...@@ -205,9 +213,9 @@ public class BtreeMap<K, V> {
} }
/** /**
* Get the root node. * Get the root page.
* *
* @return the root node * @return the root page
*/ */
Page getRoot() { Page getRoot() {
return root; return root;
...@@ -226,7 +234,7 @@ public class BtreeMap<K, V> { ...@@ -226,7 +234,7 @@ public class BtreeMap<K, V> {
return store.getMaxPageSize(); return store.getMaxPageSize();
} }
long getId() { int getId() {
return id; return id;
} }
......
...@@ -46,9 +46,9 @@ class Chunk { ...@@ -46,9 +46,9 @@ class Chunk {
int collectPriority; int collectPriority;
/** /**
* The offset of the meta root. * The position of the meta root.
*/ */
int metaRootOffset; long metaRootPos;
Chunk(int id) { Chunk(int id) {
this.id = id; this.id = id;
...@@ -70,7 +70,7 @@ class Chunk { ...@@ -70,7 +70,7 @@ class Chunk {
c.length = Long.parseLong(prop.get("length").toString()); c.length = Long.parseLong(prop.get("length").toString());
c.entryCount = Integer.parseInt(prop.get("entryCount").toString()); c.entryCount = Integer.parseInt(prop.get("entryCount").toString());
c.liveCount = Integer.parseInt(prop.get("liveCount").toString()); c.liveCount = Integer.parseInt(prop.get("liveCount").toString());
c.metaRootOffset = Integer.parseInt(prop.get("metaRoot").toString()); c.metaRootPos = Long.parseLong(prop.get("metaRoot").toString());
return c; return c;
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -96,7 +96,7 @@ class Chunk { ...@@ -96,7 +96,7 @@ class Chunk {
"length:" + length + "\n" + "length:" + length + "\n" +
"entryCount:" + entryCount + "\n" + "entryCount:" + entryCount + "\n" +
"liveCount:" + liveCount + "\n" + "liveCount:" + liveCount + "\n" +
"metaRoot:" + metaRootOffset + "\n"; "metaRoot:" + metaRootPos + "\n";
} }
} }
......
...@@ -6,7 +6,9 @@ ...@@ -6,7 +6,9 @@
*/ */
package org.h2.dev.store.btree; package org.h2.dev.store.btree;
import java.io.IOException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/** /**
* Utility methods * Utility methods
...@@ -172,4 +174,14 @@ public class DataUtils { ...@@ -172,4 +174,14 @@ public class DataUtils {
} }
} }
static void readFully(FileChannel file, ByteBuffer buff) throws IOException {
do {
int len = file.read(buff);
if (len < 0) {
break;
}
} while (buff.remaining() > 0);
buff.rewind();
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论