Permalink
Browse files

Support (key,value)

  • Loading branch information...
1 parent 5961cc5 commit 67281b59f819a31481e0af3052a0fdfdcb5c2cfd Andy Seaborne committed Mar 13, 2012
@@ -18,10 +18,45 @@
package dev;
+import structure.radix.RadixIndex ;
+
+import com.hp.hpl.jena.tdb.base.file.FileSet ;
+import com.hp.hpl.jena.tdb.base.record.RecordFactory ;
+import com.hp.hpl.jena.tdb.index.Index ;
+import com.hp.hpl.jena.tdb.index.RangeIndex ;
+import com.hp.hpl.jena.tdb.setup.* ;
+
public class RunAFS
{
public static void main(String ...argv)
{
+ BlockMgrBuilder blockMgrBuilder = null ;
+ NodeTableBuilder nodeTableBuilder = null ;
+
+ new DatasetBuilderStd(blockMgrBuilder, nodeTableBuilder) ;
+
+ }
+
+ static class RangeIndexBuilderR implements RangeIndexBuilder
+ {
+
+ @Override
+ public RangeIndex buildRangeIndex(FileSet fileSet, RecordFactory recordfactory)
+ {
+ return new RadixIndex(recordfactory) ;
+ }
+
+ }
+
+ static class IndexBuilderR implements IndexBuilder
+ {
+
+ @Override
+ public Index buildIndex(FileSet fileSet, RecordFactory recordfactory)
+ {
+ return new RadixIndex(recordfactory) ;
+ }
}
+
}
@@ -21,9 +21,6 @@
import java.util.Arrays ;
import java.util.Iterator ;
-import org.junit.runner.JUnitCore ;
-import org.junit.runner.Result ;
-import org.openjena.atlas.junit.TextListener2 ;
import org.openjena.atlas.logging.Log ;
public class MainRadix
@@ -48,13 +45,17 @@
// Insert - new root
static byte[] key6 = { 0 , 1 , 2 , 3 , 4 } ;
- static boolean print = false ;
+ static boolean print = true ;
public static void main(String ...argv)
{
+ Log.setLog4j() ;
+ RadixTree.logging = true ;
+ RadixTree.checking = true ;
+ Log.enable("structure.radix") ;
//runJUnit() ;
- if ( false )
+ if ( true )
{
byte[] k1 = { 1 , 2 , 3 } ;
byte[] k2 = { 1 , 2 } ;
@@ -111,24 +112,26 @@ static private RadixTree tree(byte[] ... keys)
static private RadixTree tree(RadixTree t, byte[] ... keys)
{
- for ( byte[]k : keys )
+ for ( int i = 0 ; i < keys.length ; i++ )
{
+ byte[] k = keys[i] ;
+ byte[] v = TestRadix.valFromKey(k) ;
if (print) System.out.println("Build: "+RLib.str(k)) ;
- t.insert(k) ;
+ t.insert(k,v) ;
if (print) t.print() ;
t.check() ;
if (print) System.out.println() ;
}
return t ;
}
- static private void runJUnit()
- {
- JUnitCore runner = new org.junit.runner.JUnitCore() ;
- runner.addListener(new TextListener2(System.out)) ;
- //TestRadix.beforeClass() ;
- Result result = runner.run(TestRadix.class) ;
- //TestRadix.afterClass() ;
- System.exit(0) ;
- }
+// static private void runJUnit()
+// {
+// JUnitCore runner = new org.junit.runner.JUnitCore() ;
+// runner.addListener(new TextListener2(System.out)) ;
+// //TestRadix.beforeClass() ;
+// Result result = runner.run(TestRadix.class) ;
+// //TestRadix.afterClass() ;
+// System.exit(0) ;
+// }
}
@@ -37,8 +37,6 @@
public RadixIndex(RecordFactory recordFactory)
{
this.recordFactory = recordFactory ;
- if ( recordFactory.hasValue() )
- throw new UnsupportedOperationException("Records with values") ;
}
@Override
@@ -58,7 +56,7 @@ public boolean contains(Record record)
@Override
public boolean add(Record record)
{
- return radix.insert(record.getKey()) ;
+ return radix.insert(record.getKey(), record.getValue()) ;
}
@Override
@@ -86,7 +86,7 @@
static ByteBuffer min(RadixNode node, ByteBuffer slot)
{
- while(!node.isValue())
+ while(!node.hasEntry())
{
// Copy as we go.
slot = appendBytes(node.prefix, 0, node.prefix.length, slot) ;
@@ -103,7 +103,7 @@ static ByteBuffer min(RadixNode node, ByteBuffer slot)
// TODO assumes bytebuffer large enough.
private static RadixNode downToMinNode(RadixNode node, ByteBuffer slot)
{
- while(!node.isValue())
+ while(!node.hasEntry())
{
// Copy as we go.
slot = appendBytes(node.prefix, 0, node.prefix.length, slot) ;
@@ -33,7 +33,10 @@
public final class RadixNode //extends PrintableBase implements Printable
{
- //TODO Clean and refasctor to allow for different implementations
+ //TODO Clean and refactor to allow for different implementations
+ // Nibble mode: array of first nibble.
+ // Flag to say if to look in first or second nibble.
+ // Interface for lenStart, lenFinish
// In-memory
// Old style list of subnodes.
// Here style 256-fan out
@@ -46,6 +49,8 @@
// Dirty flag.
// boolean nodeChanged = true ;
+
+ // Nibble flag
/*
* http://en.wikipedia.org/wiki/Radix_tree
@@ -68,36 +73,46 @@
//int maxNumChildren() { return FanOutSize+1 ; }
private RadixNode[] nodes = null ; // null -> leaf (and here is not null)
+
+ // The "key exists, no value" maker when used as just a key, no value index.
+ static private byte[] value0 = new byte[0] ;
+ // Null means no entry here.
+ private byte[] value = null ;
- // TODO Remove "here" and support (key,value)
- private boolean here = false ; // Does this node also record a value?
-// private byte[] value = null ;
-// /*package*/ void setValue(byte[] b) { value = b ; }
-// public byte[] getValue() { return value ; }
-
- boolean isValue() { return here ; }
- void setAsValue(boolean state) { here = state ; }
+ byte[] getValue() { return (value==value0)?null:value ; }
+ boolean hasEntry() { return value != null && value != value0 ; }
+ void setValue(byte[] value) { this.value = ((value==null) ? value0: value) ; }
+ void clearValue() { this.value = null ; }
private void setAsParent(RadixNode n)
{
if ( n != null )
{
- n.parent = this ;
- n.parentId = this.id ;
+ this.parent = n ;
+ this.parentId = n.id ;
}
}
// Get/set a slot
RadixNode get(int idx)
{
+ // Nodes -> long id ; long id -> RadixNode
+ //radixManager.get(idx) ;
return nodes[idx] ;
}
+
+ /** No longer in use. */
+ void release()
+ {
+ radixManager.release(this) ;
+ }
void set(int idx, RadixNode n)
{
nodes[idx] = n ;
- setAsParent(n) ;
+ if ( n != null )
+ n.setAsParent(this) ;
}
int nextIndex(int start)
@@ -121,9 +136,13 @@ int lastIndex()
}
+ // XXX rename.
void takeSubNodes(RadixNode n)
{
- this.here = n.here ;
+ if ( this.value != null )
+ error("takeSubNodes: Can't pull up - already got a value") ;
+
+ this.value = n.value ;
if ( n.nodes != null )
{
for ( int idx = 0 ; idx < n.nodes.length ; idx++ )
@@ -201,13 +220,14 @@ RadixNode convertToEmptyBranch()
if ( nodes == null )
nodes = new RadixNode[FanOutSize] ;
Arrays.fill(nodes, null) ;
+ value = null ;
return this ;
}
// XXX Version that always changes the node -- checking.
RadixNode convertToLeaf()
{
- setAsValue(true) ;
+ setValue(null) ;
if ( nodes == null )
return this ;
nodes = null ;
@@ -232,6 +252,7 @@ RadixNode convertToLeaf()
*
*/
+ static RadixNodeManager radixManager = null ;
static RadixNode allocBlank(RadixNode parent) { return new RadixNode(parent) ; }
static void dealloc(RadixNode node) { }
@@ -240,7 +261,7 @@ private RadixNode(RadixNode parent)
{
this.parent = parent ;
this.parentId = (parent==null)? -1 : parent.id ;
- setAsValue(false) ;
+ setValue(null) ;
}
// Space cost:
@@ -279,20 +300,26 @@ public String toString()
{
String prefixStr = Bytes.asHex(prefix) ;
if ( isLeaf() )
- return String.format("Leaf[%d/%d]: Length=(%d,%d) :: prefix = %s", id, parentId, lenStart, lenFinish, prefixStr) ;
+ {
+ String valStr = Bytes.asHex(value) ;
+ return String.format("Leaf[%d/%d]: Length=(%d,%d) :: prefix = %s : value = %s", id, parentId, lenStart, lenFinish, prefixStr, valStr) ;
+ }
- StringBuilder b = new StringBuilder() ;
- if ( isValue() )
- b.append(" [Value]") ;
+ String valStr = "" ;
+ if ( hasEntry() )
+ valStr = "["+Bytes.asHex(value)+"]" ;
+
+ StringBuilder b = new StringBuilder() ;
for ( RadixNode n : nodes )
{
if ( n == null )
continue ;
b.append(" ") ;
b.append(n.id+"") ;
}
- return String.format("Node[%d/%d]: Length=(%d,%d) :: prefix = %s -> Sub:%s", id, parentId, lenStart, lenFinish, prefixStr, b.toString() ) ;
+
+ return String.format("Node[%d/%d]: Length=(%d,%d) :: prefix = %s%s -> Sub:%s", id, parentId, lenStart, lenFinish, prefixStr, valStr, b.toString() ) ;
}
/*public*/ void output(final IndentedWriter out)
@@ -427,7 +454,7 @@ private void _check(int length, Set<Integer> seen)
if ( isLeaf() )
{
- if ( ! isValue() )
+ if ( ! hasEntry() )
error(this, "leaf but not a value") ;
return ;
}
@@ -437,13 +464,13 @@ private void _check(int length, Set<Integer> seen)
if ( c == 0 )
{
- if ( this.isValue() )
+ if ( this.hasEntry() )
error(this, "Branch should be a leaf.") ;
else
error(this, "Branch with no subnodes and no value") ;
}
- if ( c == 1 && ! this.isValue() )
+ if ( c == 1 && ! this.hasEntry() )
error(this, "One subnode but this is not a value") ;
// Legal?
@@ -516,7 +543,12 @@ public boolean isRoot()
visitor.after(this) ;
}
- private static void error(RadixNode node, String message, Object... args)
+ void error(String message, Object... args)
+ {
+ error(this, message, args) ;
+ }
+
+ /*package*/static void error(RadixNode node, String message, Object... args)
{
System.out.flush() ;
message = String.format(message, args) ;
@@ -29,5 +29,9 @@ public RadixNodeManager() {}
// TODO Finish!
RadixNode allocBlank(RadixNode parent) { return RadixNode.allocBlank(parent) ; }
void dealloc(RadixNode node) { RadixNode.dealloc(node) ; }
+
+ RadixNode get(long id) { return null ; }
+ void release(RadixNode node) {}
+
}
Oops, something went wrong.

0 comments on commit 67281b5

Please sign in to comment.