[Mulgara-svn] r864 - projects/xa2/object-pool/src
andrae at mulgara.org
andrae at mulgara.org
Tue Apr 29 03:32:49 UTC 2008
Author: andrae
Date: 2008-04-28 20:32:48 -0700 (Mon, 28 Apr 2008)
New Revision: 864
Modified:
projects/xa2/object-pool/src/CompBlockTrie.java
projects/xa2/object-pool/src/CompMemTrie.java
Log:
Migrated individual node write() code out of the memory-trie and into the block-trie.
Modified: projects/xa2/object-pool/src/CompBlockTrie.java
===================================================================
--- projects/xa2/object-pool/src/CompBlockTrie.java 2008-04-29 03:03:56 UTC (rev 863)
+++ projects/xa2/object-pool/src/CompBlockTrie.java 2008-04-29 03:32:48 UTC (rev 864)
@@ -9,6 +9,7 @@
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.HashMap;
+import java.util.Map;
/**
* Extends CompMemTrie to guarantee the trie will fit within a single block.
@@ -145,7 +146,7 @@
index.putInt(MAGIC);
if (root != null) {
- root.write(index, data);
+ writeTrieNode(root, index, data);
}
index.put(TYPE_ROOT_LOC);
index.put((byte)0xFF);
@@ -156,4 +157,68 @@
// Reset the limit to its initial value.
index.limit(limit);
}
+
+ private static Map<Class, TrieNodeWriter> writerMap = new HashMap<Class, TrieNodeWriter>();
+ static {
+ writerMap.put(TrieBranch.class, new TrieBranchWriter());
+ writerMap.put(TrieLeaf.class, new TrieLeafWriter());
+ }
+
+ protected static void writeTrieNode(TrieNode node, ByteBuffer index, FileChannel data) throws IOException {
+ writerMap.get(node.getClass()).write(node, index, data);
+ }
+
+ private static interface TrieNodeWriter {
+ /**
+ * This method *must* update location with the position in the index buffer it was written to.
+ * This allows parents to obtain a physical reference to this node in their write methods.
+ * The index buffer is the target of the Trie index itself.
+ * The data buffer is the target of the byte[]/value pairs stored in the leaves. We want to keep these
+ * separate for caching purposes.
+ */
+ public void write(TrieNode node, ByteBuffer index, FileChannel data) throws IOException;
+ }
+
+ private static class TrieBranchWriter implements TrieNodeWriter {
+ public void write(TrieNode node, ByteBuffer index, FileChannel data) throws IOException {
+ TrieBranch branch = (TrieBranch)node;
+ if (branch.term != null) {
+ writeTrieNode(branch.term, index, data);
+ }
+ for (TrieNode child : branch.children) {
+ writeTrieNode(child, index, data);
+ }
+
+ branch.location = (short)index.position();
+
+ index.put((branch.term == null) ? TYPE_BRANCH_NOTERM : TYPE_BRANCH_TERM);
+ index.put((byte)0xFF); // Padding to keep things short-aligned.
+ index.putInt(branch.offset);
+ if (branch.term != null) {
+ index.putShort(branch.term.location);
+ }
+ branch.children.writeHeader(index);
+ for (TrieNode child : branch.children) {
+ index.putShort(child.location);
+ }
+ }
+ }
+
+ private static class TrieLeafWriter implements TrieNodeWriter {
+ public void write(TrieNode node, ByteBuffer index, FileChannel data) throws IOException {
+ TrieLeaf leaf = (TrieLeaf)node;
+
+ ByteBuffer dataBuf = ByteBuffer.allocateDirect(8 + 4 + leaf.key.length);
+ leaf.keyLocation = data.position();
+ dataBuf.putLong(leaf.value);
+ dataBuf.putInt(leaf.key.length);
+ dataBuf.put(leaf.key);
+ data.write((ByteBuffer)dataBuf.flip());
+
+ leaf.location = (short)index.position();
+ index.put(TYPE_LEAF);
+ index.put((byte)0xFF); // Pad to 16-bit aligned.
+ index.putLong(leaf.keyLocation);
+ }
+ }
}
Modified: projects/xa2/object-pool/src/CompMemTrie.java
===================================================================
--- projects/xa2/object-pool/src/CompMemTrie.java 2008-04-29 03:03:56 UTC (rev 863)
+++ projects/xa2/object-pool/src/CompMemTrie.java 2008-04-29 03:32:48 UTC (rev 864)
@@ -83,14 +83,6 @@
return lookup(key, 0, valid);
}
- /**
- * This method *must* update location with the position in the index buffer it was written to.
- * This allows parents to obtain a physical reference to this node in their write methods.
- * The index buffer is the target of the Trie index itself.
- * The data buffer is the target of the byte[]/value pairs stored in the leaves. We want to keep these
- * separate for caching purposes.
- */
- protected abstract void write(ByteBuffer index, FileChannel data) throws IOException;
protected abstract int totalIndexSize();
protected abstract int totalDataSize();
protected abstract boolean insert(TrieLeaf node, int parentLcd);
@@ -112,9 +104,9 @@
}
protected static class TrieBranch extends TrieNode {
- private int offset;
- private ByteMap<TrieNode> children;
- private TrieLeaf term;
+ protected int offset;
+ protected ByteMap<TrieNode> children;
+ protected TrieLeaf term;
public TrieBranch(byte[] key, long value) {
super();
@@ -178,28 +170,6 @@
// aLeaf = (term == null) ? ((TrieNode)(children.data[0])).aLeaf : term;
}
- protected void write(ByteBuffer index, FileChannel data) throws IOException {
- if (term != null) {
- term.write(index, data);
- }
- for (TrieNode child : children) {
- child.write(index, data);
- }
-
- location = (short)index.position();
-
- index.put((term == null) ? TYPE_BRANCH_NOTERM : TYPE_BRANCH_TERM);
- index.put((byte)0xFF); // Padding to keep things short-aligned.
- index.putInt(offset);
- if (term != null) {
- index.putShort(term.location);
- }
- children.writeHeader(index);
- for (TrieNode child : children) {
- index.putShort(child.location);
- }
- }
-
private TrieBranch(TrieNode oldNode, TrieLeaf newNode) {
super();
assert oldNode != null;
@@ -314,7 +284,7 @@
final byte[] key;
final long value;
- private long keyLocation = 0;
+ protected long keyLocation = 0;
public TrieLeaf(byte[] key, long value) {
super();
@@ -365,20 +335,6 @@
return 8 + 4 + key.length;
}
- protected void write(ByteBuffer index, FileChannel data) throws IOException {
- ByteBuffer dataBuf = ByteBuffer.allocateDirect(8 + 4 + key.length);
- keyLocation = data.position();
- dataBuf.putLong(value);
- dataBuf.putInt(key.length);
- dataBuf.put(key);
- data.write((ByteBuffer)dataBuf.flip());
-
- location = (short)index.position();
- index.put(TYPE_LEAF);
- index.put((byte)0xFF); // Pad to 16-bit aligned.
- index.putLong(keyLocation);
- }
-
public String toString() {
return "Trie-LEAF: " + Arrays.toString(key) + " -> " + value;
}
More information about the Mulgara-svn
mailing list