2006-06-20 16:17:21 +02:00
// kelondroRowSet.java
// (C) 2006 by Michael Peter Christen; mc@anomic.de, Frankfurt a. M., Germany
// first published 20.06.2006 on http://www.anomic.de
//
// $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
// $LastChangedRevision: 1986 $
// $LastChangedBy: orbiter $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package de.anomic.kelondro ;
2008-04-06 22:31:16 +02:00
import java.io.DataInput ;
2008-04-02 15:18:23 +02:00
import java.io.DataInputStream ;
2008-03-31 00:58:42 +02:00
import java.io.IOException ;
2006-10-13 01:14:41 +02:00
import java.util.Date ;
2006-06-20 16:17:21 +02:00
import java.util.Iterator ;
2007-02-25 22:06:26 +01:00
import java.util.List ;
2006-06-20 16:17:21 +02:00
import java.util.Random ;
2006-12-01 02:30:05 +01:00
2007-04-19 15:37:02 +02:00
import de.anomic.server.logging.serverLog ;
2006-06-30 14:54:19 +02:00
public class kelondroRowSet extends kelondroRowCollection implements kelondroIndex {
2006-06-20 16:17:21 +02:00
2008-01-25 00:58:18 +01:00
private static final int collectionReSortLimit = 400 ;
2007-11-09 16:34:11 +01:00
2006-06-23 14:49:42 +02:00
private kelondroProfile profile ;
2006-08-05 01:04:03 +02:00
2006-08-05 21:18:33 +02:00
public kelondroRowSet ( kelondroRowSet rs ) {
super ( rs ) ;
this . profile = rs . profile ;
}
2006-11-05 03:10:40 +01:00
2007-05-09 19:59:36 +02:00
public kelondroRowSet ( kelondroRow rowdef , int objectCount , byte [ ] cache , int sortBound ) {
super ( rowdef , objectCount , cache , sortBound ) ;
assert rowdef . objectOrder ! = null ;
this . profile = new kelondroProfile ( ) ;
}
2006-06-20 16:17:21 +02:00
public kelondroRowSet ( kelondroRow rowdef , int objectCount ) {
super ( rowdef , objectCount ) ;
2007-05-09 19:59:36 +02:00
assert rowdef . objectOrder ! = null ;
2006-06-23 14:49:42 +02:00
this . profile = new kelondroProfile ( ) ;
2006-06-20 16:17:21 +02:00
}
2007-04-03 14:10:12 +02:00
public kelondroRowSet ( kelondroRow rowdef , kelondroRow . Entry exportedCollectionRowEnvironment , int columnInEnvironment ) {
super ( rowdef , exportedCollectionRowEnvironment , columnInEnvironment ) ;
2007-05-09 19:59:36 +02:00
assert rowdef . objectOrder ! = null ;
2006-08-05 01:04:03 +02:00
this . profile = new kelondroProfile ( ) ;
}
2007-04-05 12:14:48 +02:00
2008-01-11 01:12:01 +01:00
public void setOrdering ( kelondroByteOrder newOrder , int newColumn ) {
2007-05-09 19:59:36 +02:00
assert newOrder ! = null ;
if ( ( rowdef . objectOrder = = null ) | |
2007-11-07 23:38:09 +01:00
( ! ( rowdef . objectOrder . signature ( ) . equals ( newOrder . signature ( ) ) ) ) | |
( newColumn ! = rowdef . primaryKeyIndex ) ) {
2007-05-09 19:59:36 +02:00
rowdef . setOrdering ( newOrder , newColumn ) ;
this . sortBound = 0 ;
}
}
2008-04-06 22:31:16 +02:00
public static kelondroRowSet importRowSet ( DataInput is , kelondroRow rowdef ) throws IOException {
2008-03-31 00:58:42 +02:00
byte [ ] byte2 = new byte [ 2 ] ;
byte [ ] byte4 = new byte [ 4 ] ;
2008-04-02 15:18:23 +02:00
is . readFully ( byte4 ) ; int size = ( int ) kelondroNaturalOrder . decodeLong ( byte4 ) ;
is . readFully ( byte2 ) ; //short lastread = (short) kelondroNaturalOrder.decodeLong(byte2);
is . readFully ( byte2 ) ; //short lastwrote = (short) kelondroNaturalOrder.decodeLong(byte2);
is . readFully ( byte2 ) ; //String orderkey = new String(byte2);
is . readFully ( byte2 ) ; short ordercol = ( short ) kelondroNaturalOrder . decodeLong ( byte2 ) ;
is . readFully ( byte2 ) ; short orderbound = ( short ) kelondroNaturalOrder . decodeLong ( byte2 ) ;
2008-03-31 00:58:42 +02:00
assert rowdef . primaryKeyIndex = = ordercol ;
byte [ ] chunkcache = new byte [ size * rowdef . objectsize ] ;
2008-04-02 15:18:23 +02:00
is . readFully ( chunkcache ) ;
2008-03-31 00:58:42 +02:00
return new kelondroRowSet ( rowdef , size , chunkcache , orderbound ) ;
}
2008-04-02 15:18:23 +02:00
public static int skipNextRowSet ( DataInputStream is , kelondroRow rowdef ) throws IOException {
byte [ ] byte2 = new byte [ 2 ] ;
byte [ ] byte4 = new byte [ 4 ] ;
is . readFully ( byte4 ) ; int size = ( int ) kelondroNaturalOrder . decodeLong ( byte4 ) ;
is . readFully ( byte2 ) ; //short lastread = (short) kelondroNaturalOrder.decodeLong(byte2);
is . readFully ( byte2 ) ; //short lastwrote = (short) kelondroNaturalOrder.decodeLong(byte2);
is . readFully ( byte2 ) ; //String orderkey = new String(byte2);
is . readFully ( byte2 ) ; short ordercol = ( short ) kelondroNaturalOrder . decodeLong ( byte2 ) ;
is . readFully ( byte2 ) ;
assert rowdef . primaryKeyIndex = = ordercol ;
int skip = size * rowdef . objectsize ;
while ( skip > 0 ) skip - = is . skip ( skip ) ;
return size * rowdef . objectsize + 14 ;
}
2007-04-05 12:14:48 +02:00
public void reset ( ) {
super . reset ( ) ;
this . profile = new kelondroProfile ( ) ;
}
2008-01-20 02:22:46 +01:00
public synchronized boolean has ( byte [ ] key ) {
2007-01-08 14:13:30 +01:00
return ( get ( key ) ! = null ) ;
}
2007-03-14 09:55:05 +01:00
public synchronized kelondroRow . Entry get ( byte [ ] key ) {
2006-06-20 16:17:21 +02:00
return get ( key , 0 , key . length ) ;
}
2006-06-21 01:18:41 +02:00
private kelondroRow . Entry get ( byte [ ] key , int astart , int alength ) {
2006-06-23 14:49:42 +02:00
long handle = profile . startRead ( ) ;
2007-03-14 09:55:05 +01:00
int index = find ( key , astart , alength ) ;
2008-04-24 15:31:55 +02:00
kelondroRow . Entry entry = ( index > = 0 ) ? get ( index , true ) : null ;
2006-06-23 14:49:42 +02:00
profile . stopRead ( handle ) ;
return entry ;
2006-06-20 16:17:21 +02:00
}
2008-01-20 02:22:46 +01:00
public synchronized void putMultiple ( List < kelondroRow . Entry > rows ) {
2007-12-27 18:56:59 +01:00
Iterator < kelondroRow . Entry > i = rows . iterator ( ) ;
while ( i . hasNext ( ) ) put ( i . next ( ) ) ;
2007-02-25 22:06:26 +01:00
}
2006-10-30 03:39:39 +01:00
public kelondroRow . Entry put ( kelondroRow . Entry row , Date entryDate ) {
2006-10-13 01:14:41 +02:00
return put ( row ) ;
}
2007-03-14 09:55:05 +01:00
public synchronized kelondroRow . Entry put ( kelondroRow . Entry entry ) {
2006-10-24 15:48:16 +02:00
assert ( entry ! = null ) ;
2007-11-07 23:38:09 +01:00
assert ( entry . getPrimaryKeyBytes ( ) ! = null ) ;
2006-10-26 15:50:50 +02:00
//assert (!(serverLog.allZero(entry.getColBytes(super.sortColumn))));
2006-06-23 14:49:42 +02:00
long handle = profile . startWrite ( ) ;
2006-06-21 00:13:17 +02:00
int index = - 1 ;
2006-06-23 14:49:42 +02:00
kelondroRow . Entry oldentry = null ;
2008-01-23 21:18:36 +01:00
// when reaching a specific amount of un-sorted entries, re-sort all
if ( ( this . chunkcount - this . sortBound ) > collectionReSortLimit ) {
sort ( ) ;
}
2007-11-07 23:38:09 +01:00
index = find ( entry . bytes ( ) , ( rowdef . primaryKeyIndex < 0 ) ? 0 : super . rowdef . colstart [ rowdef . primaryKeyIndex ] , super . rowdef . primaryKeyLength ) ;
2007-03-14 09:55:05 +01:00
if ( index < 0 ) {
super . addUnique ( entry ) ;
} else {
2008-04-24 15:31:55 +02:00
oldentry = get ( index , true ) ;
2007-03-14 09:55:05 +01:00
set ( index , entry ) ;
2006-06-21 00:13:17 +02:00
}
2006-06-23 14:49:42 +02:00
profile . stopWrite ( handle ) ;
return oldentry ;
2006-06-21 00:13:17 +02:00
}
2006-06-22 17:42:09 +02:00
2007-10-22 17:26:47 +02:00
private synchronized kelondroRow . Entry remove ( byte [ ] a , int start , int length , boolean keepOrder ) {
2007-03-14 09:55:05 +01:00
int index = find ( a , start , length ) ;
if ( index < 0 ) return null ;
2007-04-19 15:37:02 +02:00
//System.out.println("remove: chunk found at index position (before remove) " + index + ", inset=" + serverLog.arrayList(super.chunkcache, super.rowdef.objectsize() * index, length + 10) + ", searchkey=" + serverLog.arrayList(a, start, length));
2008-04-24 15:31:55 +02:00
kelondroRow . Entry entry = super . get ( index , true ) ;
2007-10-22 17:26:47 +02:00
super . removeRow ( index , keepOrder ) ;
2007-04-19 15:37:02 +02:00
//System.out.println("remove: chunk found at index position (after remove) " + index + ", inset=" + serverLog.arrayList(super.chunkcache, super.rowdef.objectsize() * index, length) + ", searchkey=" + serverLog.arrayList(a, start, length));
2008-06-15 00:51:47 +02:00
int findagainindex = 0 ;
2007-05-16 12:48:26 +02:00
//System.out.println("kelondroRowSet.remove");
2008-06-15 00:51:47 +02:00
assert ( findagainindex = find ( a , start , length ) ) < 0 : " remove: chunk found again at index position (after remove) " + findagainindex + " , index(before) = " + index + " , inset= " + serverLog . arrayList ( super . chunkcache , super . rowdef . objectsize * findagainindex , length ) + " , searchkey= " + serverLog . arrayList ( a , start , length ) ; // check if the remove worked
2007-03-14 09:55:05 +01:00
return entry ;
2006-06-22 17:42:09 +02:00
}
2006-10-16 02:27:25 +02:00
2007-10-22 17:26:47 +02:00
public kelondroRow . Entry remove ( byte [ ] a , boolean keepOrder ) {
return remove ( a , 0 , a . length , keepOrder ) ;
2006-06-22 17:42:09 +02:00
}
2006-06-20 16:17:21 +02:00
2006-06-21 01:18:41 +02:00
private int find ( byte [ ] a , int astart , int alength ) {
2006-06-20 16:17:21 +02:00
// returns the chunknumber; -1 if not found
2006-12-06 04:02:57 +01:00
if ( rowdef . objectOrder = = null ) return iterativeSearch ( a , astart , alength , 0 , this . chunkcount ) ;
2008-01-25 00:58:18 +01:00
if ( ( this . chunkcount - this . sortBound ) > ( collectionReSortLimit < < 1 ) ) {
sort ( ) ;
}
2006-06-20 16:17:21 +02:00
2007-11-12 02:14:51 +01:00
if ( ( this . rowdef . objectOrder ! = null ) & & ( this . rowdef . objectOrder instanceof kelondroBase64Order ) & & ( this . sortBound > 4000 ) ) {
// first try to find in sorted area
final byte [ ] compiledPivot = compilePivot ( a , astart , alength ) ;
int p = binarySearchCompiledPivot ( compiledPivot ) ;
if ( p > = 0 ) return p ;
// then find in unsorted area
return iterativeSearchCompiledPivot ( compiledPivot , this . sortBound , this . chunkcount ) ;
} else {
// first try to find in sorted area
int p = binarySearch ( a , astart , alength ) ;
if ( p > = 0 ) return p ;
2006-06-20 16:17:21 +02:00
2007-11-12 02:14:51 +01:00
// then find in unsorted area
return iterativeSearch ( a , astart , alength , this . sortBound , this . chunkcount ) ;
}
2006-06-20 16:17:21 +02:00
}
2006-11-19 21:05:25 +01:00
private int iterativeSearch ( byte [ ] key , int astart , int alength , int leftBorder , int rightBound ) {
2007-11-12 02:14:51 +01:00
// returns the chunknumber
2006-12-06 04:02:57 +01:00
if ( rowdef . objectOrder = = null ) {
2006-11-19 21:05:25 +01:00
for ( int i = leftBorder ; i < rightBound ; i + + ) {
2006-06-20 16:17:21 +02:00
if ( match ( key , astart , alength , i ) ) return i ;
}
return - 1 ;
} else {
2007-11-12 02:14:51 +01:00
// we dont do a special handling of kelondroBase64Order here, because tests showed that this produces too much overhead
2006-11-19 21:05:25 +01:00
for ( int i = leftBorder ; i < rightBound ; i + + ) {
2006-06-20 16:17:21 +02:00
if ( compare ( key , astart , alength , i ) = = 0 ) return i ;
}
return - 1 ;
}
}
2007-11-12 02:14:51 +01:00
private int iterativeSearchCompiledPivot ( byte [ ] compiledPivot , int leftBorder , int rightBound ) {
// returns the chunknumber
assert ( rowdef . objectOrder ! = null ) ;
assert ( rowdef . objectOrder instanceof kelondroBase64Order ) ;
for ( int i = leftBorder ; i < rightBound ; i + + ) {
if ( comparePivot ( compiledPivot , i ) = = 0 ) return i ;
}
return - 1 ;
}
2006-06-20 16:17:21 +02:00
private int binarySearch ( byte [ ] key , int astart , int alength ) {
2006-07-03 17:14:54 +02:00
// returns the exact position of the key if the key exists,
// or -1 if the key does not exist
2006-12-06 04:02:57 +01:00
assert ( rowdef . objectOrder ! = null ) ;
2006-06-20 16:17:21 +02:00
int l = 0 ;
int rbound = this . sortBound ;
int p = 0 ;
int d ;
while ( l < rbound ) {
p = l + ( ( rbound - l ) > > 1 ) ;
d = compare ( key , astart , alength , p ) ;
if ( d = = 0 ) return p ;
2007-11-12 02:14:51 +01:00
if ( d < 0 ) rbound = p ; else l = p + 1 ;
}
return - 1 ;
}
private int binarySearchCompiledPivot ( byte [ ] compiledPivot ) {
// returns the exact position of the key if the key exists,
// or -1 if the key does not exist
assert ( rowdef . objectOrder ! = null ) ;
assert ( rowdef . objectOrder instanceof kelondroBase64Order ) ;
int l = 0 ;
int rbound = this . sortBound ;
int p = 0 ;
int d ;
while ( l < rbound ) {
p = l + ( ( rbound - l ) > > 1 ) ;
d = comparePivot ( compiledPivot , p ) ;
if ( d = = 0 ) return p ;
if ( d < 0 ) rbound = p ; else l = p + 1 ;
2006-06-20 16:17:21 +02:00
}
return - 1 ;
}
2006-08-16 21:49:31 +02:00
public int binaryPosition ( byte [ ] key , int astart , int alength ) {
2006-07-03 17:14:54 +02:00
// returns the exact position of the key if the key exists,
// or a position of an entry that is greater than the key if the
// key does not exist
2006-12-06 04:02:57 +01:00
assert ( rowdef . objectOrder ! = null ) ;
2006-07-03 17:14:54 +02:00
int l = 0 ;
int rbound = this . sortBound ;
int p = 0 ;
int d ;
while ( l < rbound ) {
p = l + ( ( rbound - l ) > > 1 ) ;
d = compare ( key , astart , alength , p ) ;
if ( d = = 0 ) return p ;
2007-11-12 02:14:51 +01:00
if ( d < 0 ) rbound = p ; else l = p + 1 ;
2006-07-03 17:14:54 +02:00
}
return l ;
}
2006-06-20 16:17:21 +02:00
2006-06-23 14:49:42 +02:00
public kelondroProfile profile ( ) {
return profile ;
}
2007-12-27 18:56:59 +01:00
public synchronized Iterator < byte [ ] > keys ( ) {
sort ( ) ;
return super . keys ( ) ;
}
public synchronized kelondroCloneableIterator < byte [ ] > keys ( boolean up , byte [ ] firstKey ) {
return new keyIterator ( up , firstKey ) ;
}
public class keyIterator implements kelondroCloneableIterator < byte [ ] > {
private boolean up ;
private byte [ ] first ;
private int p , bound ;
public keyIterator ( boolean up , byte [ ] firstKey ) {
// see that all elements are sorted
sort ( ) ;
this . up = up ;
this . first = firstKey ;
this . bound = sortBound ;
if ( first = = null ) {
p = 0 ;
} else {
p = binaryPosition ( first , 0 , first . length ) ; // check this to find bug in DHT selection enumeration
//System.out.println("binaryposition for key " + new String(firstKey) + " is " + p);
}
}
public keyIterator clone ( Object second ) {
return new keyIterator ( up , ( byte [ ] ) second ) ;
}
public boolean hasNext ( ) {
if ( p < 0 ) return false ;
if ( p > = size ( ) ) return false ;
if ( up ) {
return p < bound ;
} else {
return p > = 0 ;
}
}
public byte [ ] next ( ) {
byte [ ] key = getKey ( p ) ;
if ( up ) p + + ; else p - - ;
return key ;
}
public void remove ( ) {
throw new UnsupportedOperationException ( ) ;
}
}
public synchronized Iterator < kelondroRow . Entry > rows ( ) {
2007-05-16 12:48:26 +02:00
// iterates kelondroRow.Entry - type entries
2007-03-14 09:55:05 +01:00
sort ( ) ;
2006-07-04 16:47:27 +02:00
return super . rows ( ) ;
}
2007-12-27 18:56:59 +01:00
public synchronized kelondroCloneableIterator < kelondroRow . Entry > rows ( boolean up , byte [ ] firstKey ) {
2007-03-08 17:15:40 +01:00
return new rowIterator ( up , firstKey ) ;
2006-07-03 17:14:54 +02:00
}
2007-12-27 18:56:59 +01:00
public class rowIterator implements kelondroCloneableIterator < kelondroRow . Entry > {
2006-07-03 17:14:54 +02:00
2007-03-08 17:15:40 +01:00
private boolean up ;
2006-07-03 17:14:54 +02:00
private byte [ ] first ;
private int p , bound ;
2007-03-08 17:15:40 +01:00
public rowIterator ( boolean up , byte [ ] firstKey ) {
2006-11-20 01:27:02 +01:00
// see that all elements are sorted
2007-03-14 09:55:05 +01:00
sort ( ) ;
2006-07-03 17:14:54 +02:00
this . up = up ;
this . first = firstKey ;
this . bound = sortBound ;
if ( first = = null ) {
p = 0 ;
} else {
2007-05-09 19:59:36 +02:00
p = binaryPosition ( first , 0 , first . length ) ; // check this to find bug in DHT selection enumeration
2007-05-10 15:27:38 +02:00
//System.out.println("binaryposition for key " + new String(firstKey) + " is " + p);
2006-07-03 17:14:54 +02:00
}
}
2007-12-27 18:56:59 +01:00
public rowIterator clone ( Object second ) {
2007-03-08 23:07:17 +01:00
return new rowIterator ( up , ( byte [ ] ) second ) ;
2007-03-08 17:15:40 +01:00
}
2006-07-03 17:14:54 +02:00
public boolean hasNext ( ) {
2007-04-26 11:51:51 +02:00
if ( p < 0 ) return false ;
if ( p > = size ( ) ) return false ;
2006-07-03 17:14:54 +02:00
if ( up ) {
return p < bound ;
} else {
return p > = 0 ;
}
}
2007-12-27 18:56:59 +01:00
public kelondroRow . Entry next ( ) {
2008-04-24 15:31:55 +02:00
kelondroRow . Entry entry = get ( p , true ) ;
2006-07-03 17:14:54 +02:00
if ( up ) p + + ; else p - - ;
return entry ;
}
public void remove ( ) {
throw new UnsupportedOperationException ( ) ;
}
}
2006-06-20 16:17:21 +02:00
public static void main ( String [ ] args ) {
2007-04-20 09:53:58 +02:00
// sort/uniq-test
2007-05-16 12:48:26 +02:00
/ *
2007-04-20 09:53:58 +02:00
kelondroRow rowdef = new kelondroRow ( " Cardinal key-4 {b256}, byte[] payload-1 " , kelondroNaturalOrder . naturalOrder , 0 ) ;
kelondroRowSet rs = new kelondroRowSet ( rowdef , 0 ) ;
Random random = new Random ( 0 ) ;
kelondroRow . Entry entry ;
for ( int i = 0 ; i < 10000000 ; i + + ) {
entry = rowdef . newEntry ( ) ;
entry . setCol ( 0 , Math . abs ( random . nextLong ( ) % 1000000 ) ) ;
entry . setCol ( 1 , " a " . getBytes ( ) ) ;
rs . addUnique ( entry ) ;
}
System . out . println ( " before sort, size = " + rs . size ( ) ) ;
rs . sort ( ) ;
System . out . println ( " after sort, before uniq, size = " + rs . size ( ) ) ;
rs . uniq ( 10000 ) ;
System . out . println ( " after uniq, size = " + rs . size ( ) ) ;
2007-05-16 12:48:26 +02:00
* /
2007-04-20 09:53:58 +02:00
2006-06-20 16:17:21 +02:00
String [ ] test = { " eins " , " zwei " , " drei " , " vier " , " fuenf " , " sechs " , " sieben " , " acht " , " neun " , " zehn " } ;
2007-05-16 12:48:26 +02:00
kelondroRowSet d = new kelondroRowSet ( new kelondroRow ( " byte[] key-10, Cardinal x-4 {b256} " , kelondroNaturalOrder . naturalOrder , 0 ) , 0 ) ;
d . setOrdering ( kelondroNaturalOrder . naturalOrder , 0 ) ;
for ( int ii = 0 ; ii < test . length ; ii + + ) d . add ( test [ ii ] . getBytes ( ) ) ;
for ( int ii = 0 ; ii < test . length ; ii + + ) d . add ( test [ ii ] . getBytes ( ) ) ;
d . sort ( ) ;
2007-10-22 17:26:47 +02:00
d . remove ( " fuenf " . getBytes ( ) , 0 , 5 , false ) ;
2007-12-27 18:56:59 +01:00
Iterator < kelondroRow . Entry > ii = d . rows ( ) ;
2006-06-20 16:17:21 +02:00
String s ;
System . out . print ( " INPUT-ITERATOR: " ) ;
2007-05-16 12:48:26 +02:00
kelondroRow . Entry entry ;
while ( ii . hasNext ( ) ) {
2008-06-06 18:01:27 +02:00
entry = ii . next ( ) ;
s = new String ( entry . getColBytes ( 0 ) ) . trim ( ) ;
2006-06-20 16:17:21 +02:00
System . out . print ( s + " , " ) ;
2007-05-16 12:48:26 +02:00
if ( s . equals ( " drei " ) ) ii . remove ( ) ;
2006-06-20 16:17:21 +02:00
}
System . out . println ( " " ) ;
2007-05-16 12:48:26 +02:00
System . out . println ( " INPUT-TOSTRING: " + d . toString ( ) ) ;
d . sort ( ) ;
System . out . println ( " SORTED : " + d . toString ( ) ) ;
2007-11-09 01:51:38 +01:00
d . uniq ( ) ;
2007-05-16 12:48:26 +02:00
System . out . println ( " UNIQ : " + d . toString ( ) ) ;
d . trim ( false ) ;
System . out . println ( " TRIM : " + d . toString ( ) ) ;
2006-06-20 16:17:21 +02:00
2006-06-30 01:01:42 +02:00
/ *
2006-06-20 16:17:21 +02:00
// second test
c = new kelondroRowSet ( new kelondroRow ( new int [ ] { 10 , 3 } ) ) ;
c . setOrdering ( kelondroNaturalOrder . naturalOrder , 0 ) ;
Random rand = new Random ( 0 ) ;
long start = System . currentTimeMillis ( ) ;
long t , d = 0 ;
String w ;
for ( long k = 0 ; k < 60000 ; k + + ) {
t = System . currentTimeMillis ( ) ;
w = " a " + Long . toString ( rand . nextLong ( ) ) ;
2006-06-21 01:47:51 +02:00
c . add ( w . getBytes ( ) ) ;
2006-06-20 16:17:21 +02:00
if ( k % 10000 = = 0 )
System . out . println ( " added " + k + " entries in " +
( ( t - start ) / 1000 ) + " seconds, " +
( ( ( t - start ) > 1000 ) ? ( k / ( ( t - start ) / 1000 ) ) : k ) +
" entries/second, size = " + c . size ( ) ) ;
}
System . out . println ( " bevore sort: " + ( ( System . currentTimeMillis ( ) - start ) / 1000 ) + " seconds " ) ;
2006-06-22 17:42:09 +02:00
c . shape ( ) ;
2006-06-20 16:17:21 +02:00
System . out . println ( " after sort: " + ( ( System . currentTimeMillis ( ) - start ) / 1000 ) + " seconds " ) ;
c . uniq ( ) ;
System . out . println ( " after uniq: " + ( ( System . currentTimeMillis ( ) - start ) / 1000 ) + " seconds " ) ;
System . out . println ( " RESULT SIZE: " + c . size ( ) ) ;
System . out . println ( ) ;
// third test
c = new kelondroRowSet ( new kelondroRow ( new int [ ] { 10 , 3 } ) , 60000 ) ;
c . setOrdering ( kelondroNaturalOrder . naturalOrder , 0 ) ;
rand = new Random ( 0 ) ;
start = System . currentTimeMillis ( ) ;
d = 0 ;
for ( long k = 0 ; k < 60000 ; k + + ) {
t = System . currentTimeMillis ( ) ;
w = " a " + Long . toString ( rand . nextLong ( ) ) ;
2006-06-21 01:47:51 +02:00
if ( c . get ( w . getBytes ( ) , 0 , 10 ) = = null ) c . add ( w . getBytes ( ) ) ; else d + + ;
2006-06-20 16:17:21 +02:00
if ( k % 10000 = = 0 )
System . out . println ( " added " + k + " entries in " +
( ( t - start ) / 1000 ) + " seconds, " +
( ( ( t - start ) > 1000 ) ? ( k / ( ( t - start ) / 1000 ) ) : k ) +
" entries/second, " + d + " double, size = " + c . size ( ) +
" , sum = " + ( c . size ( ) + d ) ) ;
}
System . out . println ( " RESULT SIZE: " + c . size ( ) ) ;
2006-06-30 01:01:42 +02:00
* /
2006-10-04 00:55:59 +02:00
/ *
// performance test for put
2006-06-30 01:01:42 +02:00
long start = System . currentTimeMillis ( ) ;
2006-08-11 05:20:44 +02:00
kelondroRowSet c = new kelondroRowSet ( new kelondroRow ( " byte[] a-12, byte[] b-12 " ) , 0 ) ;
2006-06-30 01:01:42 +02:00
Random random = new Random ( 0 ) ;
byte [ ] key ;
for ( int i = 0 ; i < 100000 ; i + + ) {
key = randomHash ( random ) ;
c . put ( c . rowdef . newEntry ( new byte [ ] [ ] { key , key } ) ) ;
if ( i % 1000 = = 0 ) System . out . println ( i + " entries. " ) ;
}
System . out . println ( " RESULT SIZE: " + c . size ( ) ) ;
System . out . println ( " Time: " + ( ( System . currentTimeMillis ( ) - start ) / 1000 ) + " seconds " ) ;
2006-10-04 00:55:59 +02:00
* /
// remove test
long start = System . currentTimeMillis ( ) ;
2006-12-06 04:02:57 +01:00
kelondroRowSet c = new kelondroRowSet ( new kelondroRow ( " byte[] a-12, byte[] b-12 " , kelondroBase64Order . enhancedCoder , 0 ) , 0 ) ;
2006-10-04 00:55:59 +02:00
byte [ ] key ;
int testsize = 5000 ;
byte [ ] [ ] delkeys = new byte [ testsize / 5 ] [ ] ;
2007-05-16 12:48:26 +02:00
Random random = new Random ( 0 ) ;
2006-10-04 00:55:59 +02:00
for ( int i = 0 ; i < testsize ; i + + ) {
key = randomHash ( random ) ;
if ( i % 5 ! = 0 ) continue ;
delkeys [ i / 5 ] = key ;
}
random = new Random ( 0 ) ;
for ( int i = 0 ; i < testsize ; i + + ) {
key = randomHash ( random ) ;
c . put ( c . rowdef . newEntry ( new byte [ ] [ ] { key , key } ) ) ;
if ( i % 1000 = = 0 ) {
2007-10-22 17:26:47 +02:00
for ( int j = 0 ; j < delkeys . length ; j + + ) c . remove ( delkeys [ j ] , true ) ;
2007-03-14 09:55:05 +01:00
c . sort ( ) ;
2006-10-04 00:55:59 +02:00
}
}
2007-10-22 17:26:47 +02:00
for ( int j = 0 ; j < delkeys . length ; j + + ) c . remove ( delkeys [ j ] , true ) ;
2007-03-14 09:55:05 +01:00
c . sort ( ) ;
2006-10-04 00:55:59 +02:00
random = new Random ( 0 ) ;
for ( int i = 0 ; i < testsize ; i + + ) {
key = randomHash ( random ) ;
if ( i % 5 = = 0 ) continue ;
if ( c . get ( key ) = = null ) System . out . println ( " missing entry " + new String ( key ) ) ;
}
2007-03-14 09:55:05 +01:00
c . sort ( ) ;
2006-10-04 00:55:59 +02:00
System . out . println ( " RESULT SIZE: " + c . size ( ) ) ;
System . out . println ( " Time: " + ( ( System . currentTimeMillis ( ) - start ) / 1000 ) + " seconds " ) ;
2006-06-30 01:01:42 +02:00
}
public static byte [ ] randomHash ( final long r0 , final long r1 ) {
// a long can have 64 bit, but a 12-byte hash can have 6 * 12 = 72 bits
// so we construct a generic Hash using two long values
return ( kelondroBase64Order . enhancedCoder . encodeLong ( Math . abs ( r0 ) , 11 ) . substring ( 5 ) +
kelondroBase64Order . enhancedCoder . encodeLong ( Math . abs ( r1 ) , 11 ) . substring ( 5 ) ) . getBytes ( ) ;
}
public static byte [ ] randomHash ( Random r ) {
return randomHash ( r . nextLong ( ) , r . nextLong ( ) ) ;
2006-06-20 16:17:21 +02:00
}
2007-03-06 23:43:32 +01:00
public String filename ( ) {
return null ;
}
2007-04-05 12:14:48 +02:00
2006-06-20 16:17:21 +02:00
}