Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Support (key,value)

  • Loading branch information...
commit 67281b59f819a31481e0af3052a0fdfdcb5c2cfd 1 parent 5961cc5
Andy Seaborne authored
View
35 src/main/java/dev/RunAFS.java
@@ -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) ;
+ }
}
+
}
View
35 src/main/java/structure/radix/MainRadix.java
@@ -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,10 +112,12 @@ 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() ;
@@ -122,13 +125,13 @@ static private RadixTree tree(RadixTree t, byte[] ... keys)
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) ;
+// }
}
View
4 src/main/java/structure/radix/RadixIndex.java
@@ -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
View
4 src/main/java/structure/radix/RadixIterator.java
@@ -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) ;
View
80 src/main/java/structure/radix/RadixNode.java
@@ -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,12 +300,17 @@ 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 )
@@ -292,7 +318,8 @@ public String toString()
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) ;
View
4 src/main/java/structure/radix/RadixNodeManager.java
@@ -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) {}
+
}
View
205 src/main/java/structure/radix/RadixTree.java
@@ -31,7 +31,6 @@
import org.openjena.atlas.lib.Chars ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
-
/* http://en.wikipedia.org/wiki/Radix_tree */
public final class RadixTree
{
@@ -40,6 +39,7 @@
// Iteration
// Value?
// Architecture : action visitor pattern - find&do (overhead?)
+ // Add value
static public boolean logging = true ;
static public /*final*/ boolean checking = true ;
@@ -58,10 +58,10 @@
* <li>parent of the node </li>
* </ul>
*/
- private interface Action { RadixNode exec(byte[] key, RadixNode node, int N, RadixNode parentNode) ; }
+ private interface Action { RadixNode exec(byte[] key, byte[] value, RadixNode node, int N, RadixNode parentNode) ; }
/** Find the starting point - call the action */
- private static RadixNode applicator(RadixNode root, byte[] key, Action action)
+ private static RadixNode applicator(RadixNode root, byte[] key, byte[] value, Action action)
{
// A convoluted way to "return" with three results by calling on to a handler.
// Assumes root is not null.
@@ -75,24 +75,24 @@ private static RadixNode applicator(RadixNode root, byte[] key, Action action)
if ( N < node.prefix.length )
// Includes negative N
// Key ran out.
- return action.exec(key, node, N, nodePrev) ;
+ return action.exec(key, value, node, N, nodePrev) ;
// N == prefix
if ( node.isLeaf() )
// Whether it matches or not, this is the node where the action is.
- return action.exec(key, node, N, nodePrev) ;
+ return action.exec(key, value, node, N, nodePrev) ;
// Reached end; may end here, may not.
// Longer or same length key.
int j = node.locate(key, node.lenFinish) ;
if ( j < 0 ) //|| j == node.nodes.length )
// No match across subnodes - this node is the point of longest match.
- return action.exec(key, node, node.prefix.length, nodePrev) ;
+ return action.exec(key, value, node, node.prefix.length, nodePrev) ;
// There is a next node down to try.
nodePrev = node ;
RadixNode node1 = node.get(j) ;
// Nothign to go to
if ( node1 == null )
- return action.exec(key, node, node.prefix.length, nodePrev) ;
+ return action.exec(key, value, node, node.prefix.length, nodePrev) ;
node = node1 ;
}
// Does not happen
@@ -108,19 +108,19 @@ RadixNode find(byte[] key)
{
if ( root == null )
return null ;
- return applicator(root, key, findAction) ;
+ return applicator(root, key, null, findAction) ;
}
private static Action findAction = new Action() {
@Override
- public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode nodePrev)
+ public RadixNode exec(byte[] key, byte[] value, RadixNode node, int N, RadixNode nodePrev)
{
if ( N == node.prefix.length && node.lenFinish == key.length )
{
- if ( node.isValue() )
- // Exact match
+ if ( node.hasEntry() )
+ // Exact match of key.
return node ;
}
return null ;
@@ -130,11 +130,11 @@ public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode nodePrev)
private static Action searchAction = new Action() {
@Override
- public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode nodePrev)
+ public RadixNode exec(byte[] key, byte[] value, RadixNode node, int N, RadixNode nodePrev)
{
if ( N != node.prefix.length )
return null ;
- if ( node.isValue())
+ if ( node.hasEntry())
return node ;
return null ;
}
@@ -144,18 +144,18 @@ private RadixNode search(byte[] key)
{
if ( root == null )
return null ;
- return applicator(root, key, searchAction) ;
+ return applicator(root, key, null, searchAction) ;
}
// Return top changed node.
private static Action insertAction = new Action()
{
@Override
- public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode nodePrev)
+ public RadixNode exec(byte[] key, byte[] value, RadixNode node, int N, RadixNode nodePrev)
{
if (logging && log.isDebugEnabled() )
{
- log.debug("insert: "+RLib.str(key)) ;
+ log.debug("insert: ("+RLib.str(key)+", "+RLib.str(value)+")") ;
log.debug("insert: here => "+node) ;
log.debug("insert: prev => "+ ((nodePrev==null)?"null":nodePrev.toString()) ) ;
log.debug("insert: N = "+N) ;
@@ -178,7 +178,7 @@ public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode nodePrev)
* There is common processing.
*/
- // Key already present - we ended at a leaf.
+ // Key already present - we ended at leaf or branch exactly.
if ( N == node.prefix.length && node.lenFinish == key.length )
{
// Cases L1, B1 and B2
@@ -187,10 +187,12 @@ public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode nodePrev)
// L1 and B1
if (logging && log.isDebugEnabled() )
log.debug("insert: Already present") ;
+ node.setValue(value) ;
+ //XXX return node is different.
return null ;
}
// B2
- node.setAsValue(true) ;
+ node.setValue(value) ;
return node ;
}
@@ -209,7 +211,7 @@ public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode nodePrev)
if ( node.isLeaf() )
{
node = node.convertToEmptyBranch() ;
- node.setAsValue(true) ;
+ node.setValue(value) ;
}
RadixNode n = RadixNode.allocBlank(node) ;
n = n.convertToLeaf() ;
@@ -224,55 +226,81 @@ public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode nodePrev)
return node ;
}
+ // Key shorter or diverges in the middle of the prefix.
+
// Cases remaining. L3, B3, L4, B4
// Cases L3, B3 : Key is shorter than prefix.
// Split the prefix upto the end of the key.
// Make this node a value node (or leaf).
-
// Cases L4, B4.
- // Key diverges at this node at point N.
+ // Key diverges at this node at point N, not the full prefix.
// Split the prefix upto the point of diverence.
// Don't make this node a value node.
- byte[] prefixHere ;
- // Original data.
- byte[] prefixSub1 ;
- // New data.
- byte[] prefixSub2 ;
-
- boolean makeAValueNode = false ;
-
if ( N < 0 )
{
- // Key ran out.
+ // Key ran out. new node will be inserted.
+ // Cases L3, B3.
N = -(N+1) ;
- prefixHere = Bytes.copyOf(node.prefix, 0, N) ;
- // Remainder of original data.
- prefixSub1 = Bytes.copyOf(node.prefix, N) ;
- // New data. Keys end here so no more data.
- prefixSub2 = null ;
- makeAValueNode = true ;
- }
- else
- {
- // N >= 0
- // Key diverges at N
- if ( key.length <= node.lenStart )
- error("Incorrect key length: "+key.length+" ("+node.lenStart+","+node.lenFinish+")") ;
- // Case N = 0 only occurs at root (else a match to previous node).
- // Key shorter than this node and ends here.
- prefixHere = Bytes.copyOf(node.prefix, 0, N) ;
+ byte[] prefixHere = Bytes.copyOf(node.prefix, 0, N) ;
// Remainder of original data.
- prefixSub1 = Bytes.copyOf(node.prefix, N) ;
- // New data from key.
- prefixSub2 = Bytes.copyOf(key, N+node.lenStart) ;
- makeAValueNode = false ;
+ byte[] prefixSub = Bytes.copyOf(node.prefix, N) ;
+
+ if (logging && log.isDebugEnabled() )
+ {
+ log.debug("Key ends mid-way through this node") ;
+ log.debug(" Prefix here : "+Bytes.asHex(prefixHere)) ;
+ log.debug(" Prefix sub : "+Bytes.asHex(prefixSub)) ;
+ }
+
+ // New node to go under this one.
+ RadixNode node1 = RadixNode.allocBlank(node) ;
+ node1.prefix = prefixSub ;
+ node1.lenStart = node.lenStart+N ;
+ node1.lenFinish = node.lenFinish ;
+ if ( ! node.isLeaf() )
+ {
+ node1 = node1.convertToEmptyBranch() ;
+ node1.takeSubNodes(node) ;
+ }
+ else
+ node1.setValue(node.getValue()) ;
+
+ // Clear this node, making is a part of the prefix from before.
+ node = node.convertToEmptyBranch() ;
+ node.prefix = prefixHere ;
+ //node.lenStart = node.lenStart ;
+ node.lenFinish = node.lenStart+N ;
+ // Carry the inserted value.
+ node.setValue(value) ;
+
+ // Put in the neew subnode.
+ int idx1 = node.locate(prefixSub) ;
+ node.set(idx1, node1) ;
+ return node ;
}
+ // While there is some duplication of code fragement between this case
+ // and the last, it's easier to have simple, direct code without
+ // various "if"s.
+
+ // N >= 0
+ // Key diverges at N
+ // Cases L4, B4
+
-
- // Key shorter than this node and ends here.
+ if ( key.length <= node.lenStart )
+ error("Incorrect key length: "+key.length+" ("+node.lenStart+","+node.lenFinish+")") ;
+ // Case N = 0 only occurs at root (else a match to previous node).
+ // Key shorter than this node and ends here.
+
+ // New prefix to the split point
+ byte[] prefixHere = Bytes.copyOf(node.prefix, 0, N) ;
+ // Remainder of original data.
+ byte[] prefixSub1 = Bytes.copyOf(node.prefix, N) ;
+ // New data from key.
+ byte[] prefixSub2 = Bytes.copyOf(key, N+node.lenStart) ;
if (logging && log.isDebugEnabled() )
{
@@ -294,43 +322,42 @@ public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode nodePrev)
node1 = node1.convertToEmptyBranch() ;
node1.takeSubNodes(node) ;
}
- node1.setAsValue(node.isValue()) ;
-
- // The new leaf for the new data if key longer.
- RadixNode node2 = null ;
- if ( prefixSub2 != null )
- {
- node2 = RadixNode.allocBlank(node) ;
- node2.prefix = prefixSub2 ;
- node2.lenStart = node.lenStart+N ;
- node2.lenFinish = key.length ;
- node2.setAsValue(true) ;
- }
- else
- node.setAsValue(true) ;
-
- // Now make node a two way (or one way if it is now the value)
+ node1.setValue(node.getValue()) ;
+
+ // The new leaf for the new data
+ RadixNode node2 = RadixNode.allocBlank(node) ;
+ node2.prefix = prefixSub2 ;
+ node2.lenStart = node.lenStart+N ;
+ node2.lenFinish = key.length ;
+ node2.setValue(value) ;
+
+ // Now make node a two way in-place.
+
node = node.convertToEmptyBranch() ;
node.prefix = prefixHere ;
//node.lenStart
node.lenFinish = node.lenStart+N ;
- node.setAsValue(makeAValueNode) ;
+ node.setValue(null) ;
int idx1 = node.locate(prefixSub1) ;
node.set(idx1, node1) ;
- if ( node2 != null )
- {
- int idx2 = node.locate(prefixSub2) ;
- node.set(idx2, node2) ;
- }
+ int idx2 = node.locate(prefixSub2) ;
+ node.set(idx2, node2) ;
return node ;
}
} ;
- public boolean insert(byte[] key)
+ public boolean insert(byte[] key, byte[] value)
{
if (logging && log.isDebugEnabled() )
- log.debug("** Insert : "+Bytes.asHex(key)) ;
+ {
+ String v = (value==null)?"null":Bytes.asHex(value) ;
+ log.debug("** Insert : ("+Bytes.asHex(key)+","+v+")") ;
+ }
+
+ if ( value == null )
+ // XXX Value is never null.
+ value = key ;
if ( root == null )
{
@@ -338,17 +365,17 @@ public boolean insert(byte[] key)
root.prefix = key ;
root.lenStart = 0 ;
root.lenFinish = key.length ;
- root.setAsValue(true) ;
+ root.setValue(value) ;
return true ;
}
- return applicator(root, key, insertAction) != null ;
+ return applicator(root, key, value, insertAction) != null ;
}
private static Action deleteAction = new Action()
{
@Override
- public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode prevNode)
+ public RadixNode exec(byte[] key, byte[] value, RadixNode node, int N, RadixNode prevNode)
{
if (logging && log.isDebugEnabled() )
{
@@ -396,15 +423,15 @@ public RadixNode exec(byte[] key, RadixNode node, int N, RadixNode prevNode)
else
{
// Root.
- node.setAsValue(false) ;
+ node.clearValue() ;
return node ;
}
}
else
{
// Branch. Delete is a value branch.
- if ( node.isValue() && node.isValue() )
- node.setAsValue(false) ;
+ if ( node.hasEntry() && node.hasEntry() )
+ node.setValue(null) ;
// Drop though to fixup.
else
// Didn't match after all.
@@ -429,11 +456,11 @@ public boolean delete(byte[] key)
{
if ( root == null )
return false ;
- RadixNode n = applicator(root, key, deleteAction) ;
+ RadixNode n = applicator(root, key, null, deleteAction) ;
// Fixup root.
// If the root changed and now has no-subnodes and no value, free it.
- if ( n != null && n.isRoot() && (n.countSubNodes() == 0 && ! n.isValue() ) )
+ if ( n != null && n.isRoot() && (n.countSubNodes() == 0 && ! n.hasEntry() ) )
{
RadixNode.dealloc(n) ;
root = null ;
@@ -454,7 +481,7 @@ protected static RadixNode fixup(RadixNode node)
int c = node.countSubNodes() ;
if ( c == 0 )
{
- if ( node.isValue() )
+ if ( node.hasEntry() )
node = node.convertToLeaf() ;
// else; should not happen - a non-value branch which had one leaf subnode
else
@@ -465,7 +492,7 @@ protected static RadixNode fixup(RadixNode node)
if ( c != 1 )
return null ;
- if ( node.isValue() )
+ if ( node.hasEntry() )
return null ;
// Find exactly one subnode.
RadixNode sub = node.oneSubNode() ;
@@ -482,7 +509,7 @@ protected static RadixNode fixup(RadixNode node)
}
// We're a value if the subnode was a value.
- node.setAsValue(sub.isValue()) ;
+ node.setValue(sub.getValue()) ;
// Combined prefix.
int len1 = node.prefix.length + sub.prefix.length ;
@@ -583,7 +610,7 @@ public long size()
@Override
public void before(RadixNode node)
{
- if (node.isValue())
+ if (node.hasEntry())
count++ ;
}
@@ -653,7 +680,7 @@ public void before(RadixNode node)
{
for ( byte b : node.prefix )
x.addLast(b) ;
- if ( node.isValue() )
+ if ( node.hasEntry() )
{
System.out.print("[") ;
System.out.print(Iter.iter(x.iterator()).map(hex).asString(", ")) ;
View
2  src/test/java/structure/radix/RadixRun.java
@@ -213,7 +213,7 @@ static void check(RadixTree trie, List<byte[]> keys)
static void insertAndCheck(RadixTree trie, byte[] key)
{
boolean b2 = ! trie.contains(key) ;
- boolean b = trie.insert(key) ;
+ boolean b = trie.insert(key, key) ;
if ( b != b2 )
{
System.out.flush() ;
View
50 src/test/java/structure/radix/TestRadix.java
@@ -190,7 +190,7 @@ static RadixTree tree(RadixTree t, byte[] ... keys)
for ( byte[]k : keys )
{
if (print) System.out.println("Build: "+RLib.str(k)) ;
- t.insert(k) ;
+ t.insert(k, k) ;
if (print) t.print() ;
t.check() ;
if (print) System.out.println() ;
@@ -206,9 +206,11 @@ static void test(byte[]... keys)
static void test(RadixTree t, byte[]... keys)
{
+
for ( byte[]k : keys )
{
- t.insert(k) ;
+ byte[] v = valFromKey(k) ;
+ t.insert(k, v) ;
t.check() ;
assertTrue(t.contains(k)) ;
}
@@ -225,30 +227,38 @@ static void test(RadixTree t, byte[]... keys)
assertTrue(t.isEmpty()) ;
}
+ public static byte[] valFromKey(byte[] k)
+ {
+ byte[] v = new byte[k.length] ;
+ for ( int j = 0 ; j < v.length ; j++ )
+ v[j] = (byte)(k[j]+10) ;
+ return v ;
+ }
+
static void check(RadixTree t, byte[] ... keys)
{
for ( byte[]k : keys )
assertTrue("Not found: Key: "+RLib.str(k), t.contains(k)) ;
}
- private static void insert(RadixTree t, byte[] key)
- {
- t.insert(key) ;
- t.check();
- RadixNode n = t.find(key) ;
- assertNotNull(n) ;
- assertEquals(key.length, n.lenFinish) ;
- }
-
- private static void delete(RadixTree trie, byte[] key)
- {
- boolean b2 = ( trie.find(key) != null ) ;
- boolean b = trie.delete(key) ;
- System.out.flush() ;
- if ( b != b2 )
- System.err.println("Inconsistent (delete)") ;
- trie.check() ;
- }
+// private static void insert(RadixTree t, byte[] key, byte[] value)
+// {
+// t.insert(key, value) ;
+// t.check();
+// RadixNode n = t.find(key) ;
+// assertNotNull(n) ;
+// assertEquals(key.length, n.lenFinish) ;
+// }
+//
+// private static void delete(RadixTree trie, byte[] key)
+// {
+// boolean b2 = ( trie.find(key) != null ) ;
+// boolean b = trie.delete(key) ;
+// System.out.flush() ;
+// if ( b != b2 )
+// System.err.println("Inconsistent (delete)") ;
+// trie.check() ;
+// }
static void count(RadixTree t, int size)
{
Please sign in to comment.
Something went wrong with that request. Please try again.