提交 5f3ca200 authored 作者: Thomas Mueller's avatar Thomas Mueller

Documentation

上级 62710f4f
#Translation
#Fri Jul 06 10:47:50 KST 2012
.translator=Dylan
a.help=도움말
a.language=한국어
......
/*
* 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.dev.store.btree;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Properties;
/**
* A block of data.
*/
class Block {
/**
* The block id.
*/
int id;
/**
* The start position within the file.
*/
long start;
/**
* The length in bytes.
*/
long length;
/**
* The entry count.
*/
int entryCount;
/**
* The number of life (non-garbage) objects.
*/
int liveCount;
/**
* The garbage collection priority.
*/
int collectPriority;
Block(int id) {
this.id = id;
}
/**
* Build a block from the given string.
*
* @param s the string
* @return the block
*/
static Block fromString(String s) {
Block b = new Block(0);
Properties prop = new Properties();
try {
prop.load(new ByteArrayInputStream(s.getBytes("UTF-8")));
b.id = Integer.parseInt(prop.get("id").toString());
b.start = Long.parseLong(prop.get("start").toString());
b.length = Long.parseLong(prop.get("length").toString());
b.entryCount = Integer.parseInt(prop.get("entryCount").toString());
b.liveCount = Integer.parseInt(prop.get("liveCount").toString());
return b;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public int getFillRate() {
return entryCount == 0 ? 0 : 100 * liveCount / entryCount;
}
public int hashCode() {
return id;
}
public boolean equals(Object o) {
return o instanceof Block && ((Block) o).id == id;
}
public String toString() {
return
"id:" + id + "\n" +
"start:" + start + "\n" +
"length:" + length + "\n" +
"entryCount:" + entryCount + "\n" +
"liveCount:" + liveCount + "\n";
}
}
......@@ -6,8 +6,6 @@
*/
package org.h2.dev.store.btree;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
/**
......@@ -20,8 +18,8 @@ public class BtreeMap<K, V> {
private final BtreeMapStore store;
private final String name;
private final KeyType keyType;
private final ValueType valueType;
private final DataType keyType;
private final DataType valueType;
private Page root;
private BtreeMap(BtreeMapStore store, String name, Class<K> keyClass, Class<V> valueClass) {
......@@ -136,59 +134,6 @@ public class BtreeMap<K, V> {
return root != null && root.getId() < 0;
}
/**
* A value type.
*/
static interface ValueType {
/**
* Get the length in bytes.
*
* @param obj the object
* @return the length
*/
int length(Object obj);
/**
* Write the object.
*
* @param buff the target buffer
* @param x the value
*/
void write(ByteBuffer buff, Object x);
/**
* Read an object.
*
* @param buff the source buffer
* @return the object
*/
Object read(ByteBuffer buff);
/**
* Get the tag name of the class.
*
* @return the tag name
*/
String getName();
}
/**
* A key type.
*/
static interface KeyType extends ValueType {
/**
* Compare two keys.
*
* @param a the first key
* @param b the second key
* @return -1 if the first key is smaller, 1 if larger, and 0 if equal
*/
int compare(Object a, Object b);
}
/**
* Compare two keys.
*
......@@ -200,84 +145,12 @@ public class BtreeMap<K, V> {
return keyType.compare(a, b);
}
/**
* An integer type.
*/
static class IntegerType implements KeyType {
public int compare(Object a, Object b) {
return ((Integer) a).compareTo((Integer) b);
}
public int length(Object obj) {
return getVarIntLen((Integer) obj);
}
public Integer read(ByteBuffer buff) {
return readVarInt(buff);
}
public void write(ByteBuffer buff, Object x) {
writeVarInt(buff, (Integer) x);
}
public String getName() {
return "i";
}
}
/**
* A string type.
*/
static class StringType implements KeyType {
public int compare(Object a, Object b) {
return a.toString().compareTo(b.toString());
}
public int length(Object obj) {
try {
byte[] bytes = obj.toString().getBytes("UTF-8");
return getVarIntLen(bytes.length) + bytes.length;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String read(ByteBuffer buff) {
int len = readVarInt(buff);
byte[] bytes = new byte[len];
buff.get(bytes);
try {
return new String(bytes, "UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void write(ByteBuffer buff, Object x) {
try {
byte[] bytes = x.toString().getBytes("UTF-8");
writeVarInt(buff, bytes.length);
buff.put(bytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String getName() {
return "s";
}
}
/**
* Get the key type.
*
* @return the key type
*/
KeyType getKeyType() {
DataType getKeyType() {
return keyType;
}
......@@ -286,7 +159,7 @@ public class BtreeMap<K, V> {
*
* @return the value type
*/
ValueType getValueType() {
DataType getValueType() {
return valueType;
}
......@@ -339,42 +212,7 @@ public class BtreeMap<K, V> {
* @return the iterator
*/
public Iterator<K> keyIterator(K from) {
return new Cursor(root, from);
}
/**
* A cursor to iterate over elements in ascending order.
*/
class Cursor implements Iterator<K> {
private ArrayList<Page.CursorPos> parents = new ArrayList<Page.CursorPos>();
private K current;
Cursor(Page root, K from) {
Page.min(root, parents, from);
fetchNext();
}
public K next() {
K c = current;
if (c != null) {
fetchNext();
}
return c == null ? null : c;
}
@SuppressWarnings("unchecked")
private void fetchNext() {
current = (K) Page.nextKey(parents);
}
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
return new Cursor<K>(root, from);
}
/**
......@@ -395,124 +233,4 @@ public class BtreeMap<K, V> {
return name;
}
/**
* Read a variable size long.
*
* @param buff the source buffer
* @return the value
*/
static long readVarLong(ByteBuffer buff) {
long x = buff.get();
if (x >= 0) {
return x;
}
x &= 0x7f;
for (int s = 7;; s += 7) {
long b = buff.get();
x |= (b & 0x7f) << s;
if (b >= 0) {
return x;
}
}
}
/**
* Read a variable size int.
*
* @param buff the source buffer
* @return the value
*/
static int readVarInt(ByteBuffer buff) {
int b = buff.get();
if (b >= 0) {
return b;
}
// a separate function so that this one can be inlined
return readVarIntRest(buff, b);
}
private static int readVarIntRest(ByteBuffer buff, int b) {
int x = b & 0x7f;
b = buff.get();
if (b >= 0) {
return x | (b << 7);
}
x |= (b & 0x7f) << 7;
b = buff.get();
if (b >= 0) {
return x | (b << 14);
}
x |= (b & 0x7f) << 14;
b = buff.get();
if (b >= 0) {
return x | b << 21;
}
x |= ((b & 0x7f) << 21) | (buff.get() << 28);
return x;
}
/**
* Get the length of the variable size int.
*
* @param x the value
* @return the length in bytes
*/
static int getVarIntLen(int x) {
if ((x & (-1 << 7)) == 0) {
return 1;
} else if ((x & (-1 << 14)) == 0) {
return 2;
} else if ((x & (-1 << 21)) == 0) {
return 3;
} else if ((x & (-1 << 28)) == 0) {
return 4;
}
return 5;
}
/**
* Get the length of the variable size long.
*
* @param x the value
* @return the length in bytes
*/
static int getVarLongLen(long x) {
int i = 1;
while (true) {
x >>>= 7;
if (x == 0) {
return i;
}
i++;
}
}
/**
* Write a variable size int.
*
* @param buff the target buffer
* @param x the value
*/
static void writeVarInt(ByteBuffer buff, int x) {
while ((x & ~0x7f) != 0) {
buff.put((byte) (0x80 | (x & 0x7f)));
x >>>= 7;
}
buff.put((byte) x);
}
/**
* Write a variable size int.
*
* @param buff the target buffer
* @param x the value
*/
static void writeVarLong(ByteBuffer buff, long x) {
while ((x & ~0x7f) != 0) {
buff.put((byte) (0x80 | (x & 0x7f)));
x >>>= 7;
}
buff.put((byte) x);
}
}
......@@ -605,90 +605,6 @@ public class BtreeMapStore {
return blocks.get(getBlockId(pageId));
}
/**
* A block of data.
*/
static class Block {
/**
* The block id.
*/
int id;
/**
* The start position within the file.
*/
long start;
/**
* The length in bytes.
*/
long length;
/**
* The entry count.
*/
int entryCount;
/**
* The number of life (non-garbage) objects.
*/
int liveCount;
/**
* The garbage collection priority.
*/
int collectPriority;
Block(int id) {
this.id = id;
}
/**
* Build a block from the given string.
*
* @param s the string
* @return the block
*/
static Block fromString(String s) {
Block b = new Block(0);
Properties prop = new Properties();
try {
prop.load(new ByteArrayInputStream(s.getBytes("UTF-8")));
b.id = Integer.parseInt(prop.get("id").toString());
b.start = Long.parseLong(prop.get("start").toString());
b.length = Long.parseLong(prop.get("length").toString());
b.entryCount = Integer.parseInt(prop.get("entryCount").toString());
b.liveCount = Integer.parseInt(prop.get("liveCount").toString());
return b;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public int getFillRate() {
return entryCount == 0 ? 0 : 100 * liveCount / entryCount;
}
public int hashCode() {
return id;
}
public boolean equals(Object o) {
return o instanceof Block && ((Block) o).id == id;
}
public String toString() {
return
"id:" + id + "\n" +
"start:" + start + "\n" +
"length:" + length + "\n" +
"entryCount:" + entryCount + "\n" +
"liveCount:" + liveCount + "\n";
}
}
/**
* Log the string, if logging is enabled.
*
......
/*
* 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.dev.store.btree;
import java.util.ArrayList;
import java.util.Iterator;
/**
* A cursor to iterate over elements in ascending order.
*/
class Cursor<K> implements Iterator<K> {
private ArrayList<CursorPos> parents = new ArrayList<CursorPos>();
private K current;
Cursor(Page root, K from) {
Page.min(root, parents, from);
fetchNext();
}
public K next() {
K c = current;
if (c != null) {
fetchNext();
}
return c == null ? null : c;
}
@SuppressWarnings("unchecked")
private void fetchNext() {
current = (K) Page.nextKey(parents);
}
public boolean hasNext() {
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
}
/*
* 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.dev.store.btree;
/**
* A position in a cursor
*/
class CursorPos {
/**
* The current page.
*/
Page page;
/**
* The current index.
*/
int index;
}
/*
* 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.dev.store.btree;
import java.nio.ByteBuffer;
/**
* A value type.
*/
interface DataType {
/**
* Compare two keys.
*
* @param a the first key
* @param b the second key
* @return -1 if the first key is smaller, 1 if larger, and 0 if equal
*/
int compare(Object a, Object b);
/**
* Get the length in bytes.
*
* @param obj the object
* @return the length
*/
int length(Object obj);
/**
* Write the object.
*
* @param buff the target buffer
* @param x the value
*/
void write(ByteBuffer buff, Object x);
/**
* Read an object.
*
* @param buff the source buffer
* @return the object
*/
Object read(ByteBuffer buff);
/**
* Get the tag name of the class.
*
* @return the tag name
*/
String getName();
}
/*
* 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.dev.store.btree;
import java.nio.ByteBuffer;
/**
* Utility methods
*/
public class DataUtils {
/**
* Get the length of the variable size int.
*
* @param x the value
* @return the length in bytes
*/
static int getVarIntLen(int x) {
if ((x & (-1 << 7)) == 0) {
return 1;
} else if ((x & (-1 << 14)) == 0) {
return 2;
} else if ((x & (-1 << 21)) == 0) {
return 3;
} else if ((x & (-1 << 28)) == 0) {
return 4;
}
return 5;
}
/**
* Get the length of the variable size long.
*
* @param x the value
* @return the length in bytes
*/
static int getVarLongLen(long x) {
int i = 1;
while (true) {
x >>>= 7;
if (x == 0) {
return i;
}
i++;
}
}
/**
* Read a variable size int.
*
* @param buff the source buffer
* @return the value
*/
static int readVarInt(ByteBuffer buff) {
int b = buff.get();
if (b >= 0) {
return b;
}
// a separate function so that this one can be inlined
return readVarIntRest(buff, b);
}
private static int readVarIntRest(ByteBuffer buff, int b) {
int x = b & 0x7f;
b = buff.get();
if (b >= 0) {
return x | (b << 7);
}
x |= (b & 0x7f) << 7;
b = buff.get();
if (b >= 0) {
return x | (b << 14);
}
x |= (b & 0x7f) << 14;
b = buff.get();
if (b >= 0) {
return x | b << 21;
}
x |= ((b & 0x7f) << 21) | (buff.get() << 28);
return x;
}
/**
* Read a variable size long.
*
* @param buff the source buffer
* @return the value
*/
static long readVarLong(ByteBuffer buff) {
long x = buff.get();
if (x >= 0) {
return x;
}
x &= 0x7f;
for (int s = 7;; s += 7) {
long b = buff.get();
x |= (b & 0x7f) << s;
if (b >= 0) {
return x;
}
}
}
/**
* Write a variable size int.
*
* @param buff the target buffer
* @param x the value
*/
static void writeVarInt(ByteBuffer buff, int x) {
while ((x & ~0x7f) != 0) {
buff.put((byte) (0x80 | (x & 0x7f)));
x >>>= 7;
}
buff.put((byte) x);
}
/**
* Write a variable size int.
*
* @param buff the target buffer
* @param x the value
*/
static void writeVarLong(ByteBuffer buff, long x) {
while ((x & ~0x7f) != 0) {
buff.put((byte) (0x80 | (x & 0x7f)));
x >>>= 7;
}
buff.put((byte) x);
}
}
/*
* 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.dev.store.btree;
import java.nio.ByteBuffer;
/**
* An integer type.
*/
class IntegerType implements DataType {
public int compare(Object a, Object b) {
return ((Integer) a).compareTo((Integer) b);
}
public int length(Object obj) {
return DataUtils.getVarIntLen((Integer) obj);
}
public Integer read(ByteBuffer buff) {
return DataUtils.readVarInt(buff);
}
public void write(ByteBuffer buff, Object x) {
DataUtils.writeVarInt(buff, (Integer) x);
}
public String getName() {
return "i";
}
}
......@@ -167,22 +167,6 @@ class Page {
return -(low + 1);
}
/**
* A position in a cursor
*/
static class CursorPos {
/**
* The current page.
*/
Page page;
/**
* The current index.
*/
int index;
}
/**
* Go to the first element for the given key.
*
......@@ -453,7 +437,7 @@ class Page {
private void read(ByteBuffer buff) {
boolean node = buff.get() == 1;
if (node) {
int len = BtreeMap.readVarInt(buff);
int len = DataUtils.readVarInt(buff);
children = new long[len];
keys = new Object[len - 1];
for (int i = 0; i < len; i++) {
......@@ -463,7 +447,7 @@ class Page {
}
}
} else {
int len = BtreeMap.readVarInt(buff);
int len = DataUtils.readVarInt(buff);
keys = new Object[len];
values = new Object[len];
for (int i = 0; i < len; i++) {
......@@ -482,7 +466,7 @@ class Page {
if (children != null) {
buff.put((byte) 1);
int len = children.length;
BtreeMap.writeVarInt(buff, len);
DataUtils.writeVarInt(buff, len);
for (int i = 0; i < len; i++) {
long c = map.readPage(children[i]).storedId;
buff.putLong(c);
......@@ -493,7 +477,7 @@ class Page {
} else {
buff.put((byte) 0);
int len = keys.length;
BtreeMap.writeVarInt(buff, len);
DataUtils.writeVarInt(buff, len);
for (int i = 0; i < len; i++) {
map.getKeyType().write(buff, keys[i]);
map.getValueType().write(buff, values[i]);
......@@ -590,7 +574,7 @@ class Page {
int byteCount = 1;
if (children != null) {
int len = children.length;
byteCount += BtreeMap.getVarIntLen(len);
byteCount += DataUtils.getVarIntLen(len);
for (int i = 0; i < len; i++) {
byteCount += 8;
if (i < keys.length) {
......@@ -599,7 +583,7 @@ class Page {
}
} else {
int len = keys.length;
byteCount += BtreeMap.getVarIntLen(len);
byteCount += DataUtils.getVarIntLen(len);
for (int i = 0; i < len; i++) {
byteCount += map.getKeyType().length(keys[i]);
byteCount += map.getValueType().length(values[i]);
......
/*
* 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.dev.store.btree;
import java.nio.ByteBuffer;
/**
* A string type.
*/
class StringType implements DataType {
public int compare(Object a, Object b) {
return a.toString().compareTo(b.toString());
}
public int length(Object obj) {
try {
byte[] bytes = obj.toString().getBytes("UTF-8");
return DataUtils.getVarIntLen(bytes.length) + bytes.length;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String read(ByteBuffer buff) {
int len = DataUtils.readVarInt(buff);
byte[] bytes = new byte[len];
buff.get(bytes);
try {
return new String(bytes, "UTF-8");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void write(ByteBuffer buff, Object x) {
try {
byte[] bytes = x.toString().getBytes("UTF-8");
DataUtils.writeVarInt(buff, bytes.length);
buff.put(bytes);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String getName() {
return "s";
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论