@ -72,7 +72,7 @@ public final class kelondroStack extends kelondroRecords {
}
public kelondroStack ( File file , long buffersize , int [ ] columns , boolean exitOnFail ) {
// this creates a new tree
// this creates a new stack
super ( file , buffersize , thisOHBytes , thisOHHandles , columns , thisFHandles , columns . length /* txtProps */ , 80 /* txtPropWidth */ , exitOnFail ) ;
try {
setHandle ( root , null ) ; // define the root value
@ -85,15 +85,15 @@ public final class kelondroStack extends kelondroRecords {
}
public kelondroStack ( File file , long buffersize ) throws IOException {
// this opens a file with an existing tree
super ( file , buffersize ) ;
// this opens a file with an existing stack
super ( file , buffersize ) ;
if ( ( getHandle ( root ) = = null ) & & ( getHandle ( toor ) = = null ) ) clear ( ) ;
}
public void clear ( ) throws IOException {
super . clear ( ) ;
setHandle ( root , null ) ; // reset the root value
setHandle ( toor , null ) ; // reset the toor value
setHandle ( toor , null ) ; // reset the toor value
}
public static kelondroStack reset ( kelondroStack stack ) {
@ -111,118 +111,121 @@ public final class kelondroStack extends kelondroRecords {
}
public class Counter implements Iterator {
Handle nextHandle = null ;
public Counter ( ) {
nextHandle = getHandle ( root ) ;
}
public boolean hasNext ( ) {
return ( nextHandle ! = null ) ;
}
public Object next ( ) {
Handle ret = nextHandle ;
try {
nextHandle = getNode ( nextHandle , null , 0 ) . getOHHandle ( right ) ;
Handle nextHandle = null ;
public Counter ( ) {
nextHandle = getHandle ( root ) ;
}
public boolean hasNext ( ) {
return ( nextHandle ! = null ) ;
}
public Object next ( ) {
Handle ret = nextHandle ;
try {
nextHandle = getNode ( nextHandle , null , 0 ) . getOHHandle ( right ) ;
return getNode ( ret , null , 0 ) ;
} catch ( IOException e ) {
} catch ( IOException e ) {
throw new kelondroException ( filename , "IO error at Counter:next()" ) ;
}
}
public void remove ( ) {
throw new UnsupportedOperationException ( "no remove here.." ) ;
}
}
}
public void remove ( ) {
throw new UnsupportedOperationException ( "no remove here.." ) ;
}
}
public synchronized void push ( byte [ ] [ ] row ) throws IOException {
if ( row . length ! = columns ( ) ) throw new IllegalArgumentException ( "push: wrong row length " + row . length + "; must be " + columns ( ) ) ;
// check if there is already a stack
if ( getHandle ( toor ) = = null ) {
if ( getHandle ( root ) ! = null ) throw new RuntimeException ( "push: internal organisation of root and toor" ) ;
// create node
Node n = newNode ( ) ;
n . setValueCells ( row ) ;
n . setOHHandle ( left , null ) ;
public synchronized void push ( kelondroRow . Entry row ) throws IOException {
// check if there is already a stack
if ( getHandle ( toor ) = = null ) {
if ( getHandle ( root ) ! = null ) throw new RuntimeException ( "push: internal organisation of root and toor" ) ;
// create node
Node n = newNode ( ) ;
n . setValueRow ( row . bytes ( ) ) ;
n . setOHHandle ( left , null ) ;
n . setOHHandle ( right , null ) ;
n . commit ( CP_NONE ) ;
// assign handles
setHandle ( root , n . handle ( ) ) ;
setHandle ( toor , n . handle ( ) ) ;
// thats it
} else {
// expand the list at the end
Node n = newNode ( ) ;
n . setValue Cells( row ) ;
n . setOHHandle ( left , getHandle ( toor ) ) ;
n . commit ( CP_NONE ) ;
// assign handles
setHandle ( root , n . handle ( ) ) ;
setHandle ( toor , n . handle ( ) ) ;
// thats it
} else {
// expand the list at the end
Node n = newNode ( ) ;
n . setValue Row( row . bytes ( ) ) ;
n . setOHHandle ( left , getHandle ( toor ) ) ;
n . setOHHandle ( right , null ) ;
Node n1 = getNode ( getHandle ( toor ) , null , 0 ) ;
Node n1 = getNode ( getHandle ( toor ) , null , 0 ) ;
n1 . setOHHandle ( right , n . handle ( ) ) ;
n . commit ( CP_NONE ) ;
n1 . commit ( CP_NONE ) ;
// assign handles
setHandle ( toor , n . handle ( ) ) ;
// thats it
}
n . commit ( CP_NONE ) ;
n1 . commit ( CP_NONE ) ;
// assign handles
setHandle ( toor , n . handle ( ) ) ;
// thats it
}
}
public synchronized byte [ ] [ ] pop ( ) throws IOException {
// return row ontop of the stack and shrink stack by one
return pop ( 0 ) ;
public synchronized kelondroRow . Entry pop ( ) throws IOException {
// return row ontop of the stack and shrink stack by one
return pop ( 0 ) ;
}
public synchronized byte [ ] [ ] pop ( int dist ) throws IOException {
// return row relative to top of the stack and remove addressed element
Node n = topNode ( dist ) ;
public synchronized kelondroRow . Entry pop ( int dist ) throws IOException {
// return row relative to top of the stack and remove addressed element
Node n = topNode ( dist ) ;
if ( n = = null ) return null ;
byte [ ] [ ] ret = n . getValueCells ( ) ;
kelondroRow . Entry ret = row ( ) . newEntry ( n . getValueRow ( ) ) ;
// remove node
unlinkNode ( n ) ;
deleteNode ( n . handle ( ) ) ;
return ret ;
deleteNode ( n . handle ( ) ) ;
return ret ;
}
public synchronized byte [ ] [ ] top ( ) throws IOException {
// return row ontop of the stack
return top ( 0 ) ;
public synchronized kelondroRow . Entry top ( ) throws IOException {
// return row ontop of the stack
return top ( 0 ) ;
}
public synchronized byte [ ] [ ] top ( int dist ) throws IOException {
// return row ontop of the stack
// with dist == 0 this is the same function as with top()
public synchronized kelondroRow . Entry top ( int dist ) throws IOException {
// return row ontop of the stack
// with dist == 0 this is the same function as with top()
Node n = topNode ( dist ) ;
if ( n = = null ) return null ;
return n. getValueCells ( ) ;
return row( ) . newEntry ( n . getValueRow ( ) ) ;
}
public synchronized byte [ ] [ ] pot ( ) throws IOException {
// return row on the bottom of the stack and remove record
return pot ( 0 ) ;
public synchronized kelondroRow . Entry pot ( ) throws IOException {
// return row on the bottom of the stack and remove record
return pot ( 0 ) ;
}
public synchronized byte [ ] [ ] pot ( int dist ) throws IOException {
// return row relative to the bottom of the stack and remove addressed element
Node n = botNode ( dist ) ;
public synchronized kelondroRow . Entry pot ( int dist ) throws IOException {
// return row relative to the bottom of the stack and remove addressed element
Node n = botNode ( dist ) ;
if ( n = = null ) return null ;
byte [ ] [ ] ret = n . getValueCells ( ) ;
kelondroRow . Entry ret = row ( ) . newEntry ( n . getValueCells ( ) ) ;
// remove node
unlinkNode ( n ) ;
deleteNode ( n . handle ( ) ) ;
return ret ;
deleteNode ( n . handle ( ) ) ;
return ret ;
}
public synchronized byte [ ] [ ] bot ( ) throws IOException {
// return row on the bottom of the stack
return bot ( 0 ) ;
public synchronized kelondroRow . Entry bot ( ) throws IOException {
// return row on the bottom of the stack
return bot ( 0 ) ;
}
public synchronized byte [ ] [ ] bot ( int dist ) throws IOException {
// return row on bottom of the stack
// with dist == 0 this is the same function as with bot()
Node n = botNode ( dist ) ;
if ( n = = null ) return null ;
return n. getValueCells ( ) ;
public synchronized kelondroRow . Entry bot ( int dist ) throws IOException {
// return row on bottom of the stack
// with dist == 0 this is the same function as with bot()
Node n = botNode ( dist ) ;
if ( n = = null ) return null ;
return row( ) . newEntry ( n . getValueRow ( ) ) ;
}
public synchronized ArrayList botList ( int dist ) throws IOException {
@ -235,59 +238,51 @@ public final class kelondroStack extends kelondroRecords {
private void unlinkNode ( Node n ) throws IOException {
// join chaines over node
Handle l = n . getOHHandle ( left ) ;
Handle l = n . getOHHandle ( left ) ;
Handle r = n . getOHHandle ( right ) ;
// look left
if ( l = = null ) {
// reached the root on left side
setHandle ( root , r ) ;
} else {
// un-link the previous record
Node k = getNode ( l , null , 0 ) ;
k . setOHHandle ( left , k . getOHHandle ( left ) ) ;
if ( l = = null ) {
// reached the root on left side
setHandle ( root , r ) ;
} else {
// un-link the previous record
Node k = getNode ( l , null , 0 ) ;
k . setOHHandle ( left , k . getOHHandle ( left ) ) ;
k . setOHHandle ( right , r ) ;
k . commit ( CP_NONE ) ;
}
}
// look right
if ( r = = null ) {
// reached the root on right side
setHandle ( toor , l ) ;
} else {
// un-link the following record
Node k = getNode ( r , null , 0 ) ;
k . setOHHandle ( left , l ) ;
// reached the root on right side
setHandle ( toor , l ) ;
} else {
// un-link the following record
Node k = getNode ( r , null , 0 ) ;
k . setOHHandle ( left , l ) ;
k . setOHHandle ( right , k . getOHHandle ( right ) ) ;
k . commit ( CP_NONE ) ;
}
}
}
private Node topNode ( int dist ) throws IOException {
// return node ontop of the stack
// return node ontop of the stack
return queueNode ( dist , toor , left ) ;
}
private Node botNode ( int dist ) throws IOException {
// return node on bottom of the stack
// return node on bottom of the stack
return queueNode ( dist , root , right ) ;
}
private Node queueNode ( int dist , int side , int dir ) throws IOException {
// with dist == 0 this is the same function as with getNode(getHandle(side), null, 0)
Handle h = getHandle ( side ) ;
if ( h = = null ) return null ;
if ( dist > = size ( ) ) return null ; // that would exceed the stack
while ( dist - - > 0 ) h = getNode ( h ) . getOHHandle ( dir ) ; // track through elements
return getNode ( h ) ;
}
/ *
public synchronized byte [ ] [ ] seekPop ( byte [ ] key , long maxdepth ) throws IOException {
}
public synchronized byte [ ] [ ] seekPot ( byte [ ] key , long maxdepth ) throws IOException {
// with dist == 0 this is the same function as with
// getNode(getHandle(side), null, 0)
Handle h = getHandle ( side ) ;
if ( h = = null ) return null ;
if ( dist > = size ( ) ) return null ; // that would exceed the stack
while ( dist - - > 0 ) h = getNode ( h ) . getOHHandle ( dir ) ; // track through elements
return getNode ( h ) ;
}
* /
public Iterator iterator ( ) {
// iterates the elements in an ordered way. returns Node - type Objects
@ -302,7 +297,7 @@ public final class kelondroStack extends kelondroRecords {
String s ;
StringTokenizer st ;
int recs = 0 ;
byte [ ] [ ] buffer = new byte [ columns ( ) ] [ ] ;
kelondroRow . Entry buffer = row ( ) . newEntry ( ) ;
int c ;
int line = 0 ;
while ( ( s = f . readLine ( ) ) ! = null ) {
@ -313,7 +308,7 @@ public final class kelondroStack extends kelondroRecords {
// buffer the entry
c = 0 ;
while ( ( c < columns ( ) ) & & ( st . hasMoreTokens ( ) ) ) {
buffer [c + + ] = st . nextToken ( ) . trim ( ) . getBytes ( ) ;
buffer .setCol ( c + + , st . nextToken ( ) . trim ( ) . getBytes ( ) ) ;
}
if ( ( st . hasMoreTokens ( ) ) | | ( c ! = columns ( ) ) ) {
System . err . println ( "inapropriate number of entries in line " + line ) ;
@ -331,28 +326,34 @@ public final class kelondroStack extends kelondroRecords {
}
public String hp ( Handle h ) {
if ( h = = null ) return "NULL" ; else return h . toString ( ) ;
if ( h = = null )
return "NULL" ;
else
return h . toString ( ) ;
}
public void print ( ) throws IOException {
super . print ( false ) ;
Node n ;
try {
Iterator it = iterator ( ) ;
while ( it . hasNext ( ) ) {
n = ( Node ) it . next ( ) ;
//n = getNode(h, null, 0);
System . out . println ( "> NODE " + hp ( n . handle ( ) ) +
"; left " + hp ( n . getOHHandle ( left ) ) + ", right " + hp ( n . getOHHandle ( right ) ) ) ;
System . out . print ( " KEY:'" + ( new String ( n . getValueCells ( ) [ 0 ] ) ) . trim ( ) + "'" ) ;
for ( int j = 1 ; j < columns ( ) ; j + + )
System . out . print ( ", V[" + j + "]:'" + ( new String ( n . getValueCells ( ) [ j ] ) ) . trim ( ) + "'" ) ;
System . out . println ( ) ;
}
System . out . println ( ) ;
} catch ( IOException e ) {
System . out . println ( "File error: " + e . getMessage ( ) ) ;
}
super . print ( false ) ;
Node n ;
try {
Iterator it = iterator ( ) ;
while ( it . hasNext ( ) ) {
n = ( Node ) it . next ( ) ;
// n = getNode(h, null, 0);
System . out . println ( "> NODE " + hp ( n . handle ( ) ) + "; left "
+ hp ( n . getOHHandle ( left ) ) + ", right "
+ hp ( n . getOHHandle ( right ) ) ) ;
System . out . print ( " KEY:'"
+ ( new String ( n . getValueCells ( ) [ 0 ] ) ) . trim ( ) + "'" ) ;
for ( int j = 1 ; j < columns ( ) ; j + + )
System . out . print ( ", V[" + j + "]:'"
+ ( new String ( n . getValueCells ( ) [ j ] ) ) . trim ( ) + "'" ) ;
System . out . println ( ) ;
}
System . out . println ( ) ;
} catch ( IOException e ) {
System . out . println ( "File error: " + e . getMessage ( ) ) ;
}
}
private static void cmd ( String [ ] args ) {
@ -372,8 +373,8 @@ public final class kelondroStack extends kelondroRecords {
ret = null ;
} else if ( args [ 0 ] . equals ( "-g" ) ) {
fm = new kelondroStack ( new File ( args [ 1 ] ) , 0x100000 ) ;
byte [ ] [ ] ret2 = fm . pop ( ) ;
ret = ( ( ret2 = = null ) ? null : ret2 [1 ] ) ;
kelondroRow . Entry ret2 = fm . pop ( ) ;
ret = ( ( ret2 = = null ) ? null : ret2 .getColBytes ( 1 ) ) ;
fm . close ( ) ;
}
fm . close ( ) ;
@ -404,8 +405,8 @@ public final class kelondroStack extends kelondroRecords {
}
} else if ( args [ 0 ] . equals ( "-g" ) ) {
kelondroStack fm = new kelondroStack ( new File ( args [ 2 ] ) , 0x100000 ) ;
byte [ ] [ ] ret2 = fm . pop ( Integer . parseInt ( args [ 1 ] ) ) ;
ret = ( ( ret2 = = null ) ? null : ret2 [1 ] ) ;
kelondroRow . Entry ret2 = fm . pop ( Integer . parseInt ( args [ 1 ] ) ) ;
ret = ( ( ret2 = = null ) ? null : ret2 .getColBytes ( 1 ) ) ;
fm . close ( ) ;
}
} else if ( args . length = = 4 ) {
@ -420,7 +421,7 @@ public final class kelondroStack extends kelondroRecords {
fm . close ( ) ;
} else if ( args [ 0 ] . equals ( "-p" ) ) {
kelondroStack fm = new kelondroStack ( new File ( args [ 3 ] ) , 0x100000 ) ;
fm . push ( new byte [ ] [ ] { args [ 1 ] . getBytes ( ) , args [ 2 ] . getBytes ( ) } ) ;
fm . push ( fm . row ( ) . newEntry ( new byte [ ] [ ] { args [ 1 ] . getBytes ( ) , args [ 2 ] . getBytes ( ) } ) ) ;
fm . close ( ) ;
}
}