Skip to content

Commit

Permalink
Support (key,value)
Browse files Browse the repository at this point in the history
  • Loading branch information
afs committed Mar 13, 2012
1 parent 5961cc5 commit 67281b5
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 155 deletions.
35 changes: 35 additions & 0 deletions src/main/java/dev/RunAFS.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,10 +18,45 @@


package dev; 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 class RunAFS
{ {
public static void main(String ...argv) 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) ;
}
} }

} }


35 changes: 19 additions & 16 deletions src/main/java/structure/radix/MainRadix.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import java.util.Arrays ; import java.util.Arrays ;
import java.util.Iterator ; 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 ; import org.openjena.atlas.logging.Log ;


public class MainRadix public class MainRadix
Expand All @@ -48,13 +45,17 @@ public class MainRadix
// Insert - new root // Insert - new root
static byte[] key6 = { 0 , 1 , 2 , 3 , 4 } ; static byte[] key6 = { 0 , 1 , 2 , 3 , 4 } ;


static boolean print = false ; static boolean print = true ;


public static void main(String ...argv) public static void main(String ...argv)
{ {
Log.setLog4j() ;
RadixTree.logging = true ;
RadixTree.checking = true ;
Log.enable("structure.radix") ;
//runJUnit() ; //runJUnit() ;


if ( false ) if ( true )
{ {
byte[] k1 = { 1 , 2 , 3 } ; byte[] k1 = { 1 , 2 , 3 } ;
byte[] k2 = { 1 , 2 } ; byte[] k2 = { 1 , 2 } ;
Expand Down Expand Up @@ -111,24 +112,26 @@ static private RadixTree tree(byte[] ... keys)


static private RadixTree tree(RadixTree t, 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)) ; if (print) System.out.println("Build: "+RLib.str(k)) ;
t.insert(k) ; t.insert(k,v) ;
if (print) t.print() ; if (print) t.print() ;
t.check() ; t.check() ;
if (print) System.out.println() ; if (print) System.out.println() ;
} }
return t ; return t ;
} }


static private void runJUnit() // static private void runJUnit()
{ // {
JUnitCore runner = new org.junit.runner.JUnitCore() ; // JUnitCore runner = new org.junit.runner.JUnitCore() ;
runner.addListener(new TextListener2(System.out)) ; // runner.addListener(new TextListener2(System.out)) ;
//TestRadix.beforeClass() ; // //TestRadix.beforeClass() ;
Result result = runner.run(TestRadix.class) ; // Result result = runner.run(TestRadix.class) ;
//TestRadix.afterClass() ; // //TestRadix.afterClass() ;
System.exit(0) ; // System.exit(0) ;
} // }
} }
4 changes: 1 addition & 3 deletions src/main/java/structure/radix/RadixIndex.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public class RadixIndex implements RangeIndex
public RadixIndex(RecordFactory recordFactory) public RadixIndex(RecordFactory recordFactory)
{ {
this.recordFactory = recordFactory ; this.recordFactory = recordFactory ;
if ( recordFactory.hasValue() )
throw new UnsupportedOperationException("Records with values") ;
} }


@Override @Override
Expand All @@ -58,7 +56,7 @@ public boolean contains(Record record)
@Override @Override
public boolean add(Record record) public boolean add(Record record)
{ {
return radix.insert(record.getKey()) ; return radix.insert(record.getKey(), record.getValue()) ;
} }


@Override @Override
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/structure/radix/RadixIterator.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class RadixIterator implements Iterator<ByteBuffer>


static ByteBuffer min(RadixNode node, ByteBuffer slot) static ByteBuffer min(RadixNode node, ByteBuffer slot)
{ {
while(!node.isValue()) while(!node.hasEntry())
{ {
// Copy as we go. // Copy as we go.
slot = appendBytes(node.prefix, 0, node.prefix.length, slot) ; slot = appendBytes(node.prefix, 0, node.prefix.length, slot) ;
Expand All @@ -103,7 +103,7 @@ static ByteBuffer min(RadixNode node, ByteBuffer slot)
// TODO assumes bytebuffer large enough. // TODO assumes bytebuffer large enough.
private static RadixNode downToMinNode(RadixNode node, ByteBuffer slot) private static RadixNode downToMinNode(RadixNode node, ByteBuffer slot)
{ {
while(!node.isValue()) while(!node.hasEntry())
{ {
// Copy as we go. // Copy as we go.
slot = appendBytes(node.prefix, 0, node.prefix.length, slot) ; slot = appendBytes(node.prefix, 0, node.prefix.length, slot) ;
Expand Down
80 changes: 56 additions & 24 deletions src/main/java/structure/radix/RadixNode.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
public final class RadixNode //extends PrintableBase implements Printable 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 // In-memory
// Old style list of subnodes. // Old style list of subnodes.
// Here style 256-fan out // Here style 256-fan out
Expand All @@ -46,6 +49,8 @@ public final class RadixNode //extends PrintableBase implements Printable


// Dirty flag. // Dirty flag.
// boolean nodeChanged = true ; // boolean nodeChanged = true ;

// Nibble flag


/* /*
* http://en.wikipedia.org/wiki/Radix_tree * http://en.wikipedia.org/wiki/Radix_tree
Expand All @@ -68,36 +73,46 @@ public final class RadixNode //extends PrintableBase implements Printable
//int maxNumChildren() { return FanOutSize+1 ; } //int maxNumChildren() { return FanOutSize+1 ; }


private RadixNode[] nodes = null ; // null -> leaf (and here is not null) 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) byte[] getValue() { return (value==value0)?null:value ; }
private boolean here = false ; // Does this node also record a value? boolean hasEntry() { return value != null && value != value0 ; }
// private byte[] value = null ; void setValue(byte[] value) { this.value = ((value==null) ? value0: value) ; }
// /*package*/ void setValue(byte[] b) { value = b ; } void clearValue() { this.value = null ; }
// public byte[] getValue() { return value ; }

boolean isValue() { return here ; }
void setAsValue(boolean state) { here = state ; }


private void setAsParent(RadixNode n) private void setAsParent(RadixNode n)
{ {
if ( n != null ) if ( n != null )
{ {
n.parent = this ; this.parent = n ;
n.parentId = this.id ; this.parentId = n.id ;
} }
} }


// Get/set a slot // Get/set a slot
RadixNode get(int idx) RadixNode get(int idx)
{ {
// Nodes -> long id ; long id -> RadixNode
//radixManager.get(idx) ;
return nodes[idx] ; return nodes[idx] ;
} }

/** No longer in use. */
void release()
{
radixManager.release(this) ;
}


void set(int idx, RadixNode n) void set(int idx, RadixNode n)
{ {
nodes[idx] = n ; nodes[idx] = n ;
setAsParent(n) ; if ( n != null )
n.setAsParent(this) ;
} }


int nextIndex(int start) int nextIndex(int start)
Expand All @@ -121,9 +136,13 @@ int lastIndex()


} }


// XXX rename.
void takeSubNodes(RadixNode n) 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 ) if ( n.nodes != null )
{ {
for ( int idx = 0 ; idx < n.nodes.length ; idx++ ) for ( int idx = 0 ; idx < n.nodes.length ; idx++ )
Expand Down Expand Up @@ -201,13 +220,14 @@ RadixNode convertToEmptyBranch()
if ( nodes == null ) if ( nodes == null )
nodes = new RadixNode[FanOutSize] ; nodes = new RadixNode[FanOutSize] ;
Arrays.fill(nodes, null) ; Arrays.fill(nodes, null) ;
value = null ;
return this ; return this ;
} }


// XXX Version that always changes the node -- checking. // XXX Version that always changes the node -- checking.
RadixNode convertToLeaf() RadixNode convertToLeaf()
{ {
setAsValue(true) ; setValue(null) ;
if ( nodes == null ) if ( nodes == null )
return this ; return this ;
nodes = null ; nodes = null ;
Expand All @@ -232,6 +252,7 @@ RadixNode convertToLeaf()
* *
*/ */


static RadixNodeManager radixManager = null ;


static RadixNode allocBlank(RadixNode parent) { return new RadixNode(parent) ; } static RadixNode allocBlank(RadixNode parent) { return new RadixNode(parent) ; }
static void dealloc(RadixNode node) { } static void dealloc(RadixNode node) { }
Expand All @@ -240,7 +261,7 @@ private RadixNode(RadixNode parent)
{ {
this.parent = parent ; this.parent = parent ;
this.parentId = (parent==null)? -1 : parent.id ; this.parentId = (parent==null)? -1 : parent.id ;
setAsValue(false) ; setValue(null) ;
} }


// Space cost: // Space cost:
Expand Down Expand Up @@ -279,20 +300,26 @@ public String toString()
{ {
String prefixStr = Bytes.asHex(prefix) ; String prefixStr = Bytes.asHex(prefix) ;
if ( isLeaf() ) 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() ; String valStr = "" ;
if ( isValue() )
b.append(" [Value]") ;


if ( hasEntry() )
valStr = "["+Bytes.asHex(value)+"]" ;

StringBuilder b = new StringBuilder() ;
for ( RadixNode n : nodes ) for ( RadixNode n : nodes )
{ {
if ( n == null ) if ( n == null )
continue ; continue ;
b.append(" ") ; b.append(" ") ;
b.append(n.id+"") ; 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) /*public*/ void output(final IndentedWriter out)
Expand Down Expand Up @@ -427,7 +454,7 @@ private void _check(int length, Set<Integer> seen)


if ( isLeaf() ) if ( isLeaf() )
{ {
if ( ! isValue() ) if ( ! hasEntry() )
error(this, "leaf but not a value") ; error(this, "leaf but not a value") ;
return ; return ;
} }
Expand All @@ -437,13 +464,13 @@ private void _check(int length, Set<Integer> seen)


if ( c == 0 ) if ( c == 0 )
{ {
if ( this.isValue() ) if ( this.hasEntry() )
error(this, "Branch should be a leaf.") ; error(this, "Branch should be a leaf.") ;
else else
error(this, "Branch with no subnodes and no value") ; 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") ; error(this, "One subnode but this is not a value") ;


// Legal? // Legal?
Expand Down Expand Up @@ -516,7 +543,12 @@ private <T> void _visit(RadixNodeVisitor<T> visitor, Set<RadixNode> seen)
visitor.after(this) ; 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() ; System.out.flush() ;
message = String.format(message, args) ; message = String.format(message, args) ;
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/structure/radix/RadixNodeManager.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,5 +29,9 @@ public RadixNodeManager() {}
// TODO Finish! // TODO Finish!
RadixNode allocBlank(RadixNode parent) { return RadixNode.allocBlank(parent) ; } RadixNode allocBlank(RadixNode parent) { return RadixNode.allocBlank(parent) ; }
void dealloc(RadixNode node) { RadixNode.dealloc(node) ; } void dealloc(RadixNode node) { RadixNode.dealloc(node) ; }

RadixNode get(long id) { return null ; }
void release(RadixNode node) {}

} }


Loading

0 comments on commit 67281b5

Please sign in to comment.