2006-06-13 17:05:11 +02:00
// kelondroFlexTable.java
2006-06-02 00:13:52 +02:00
// (C) 2006 by Michael Peter Christen; mc@anomic.de, Frankfurt a. M., Germany
// first published 01.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
2006-06-01 18:18:27 +02:00
package de.anomic.kelondro ;
2006-06-02 00:13:52 +02:00
2006-06-01 18:18:27 +02:00
import java.io.File ;
import java.io.IOException ;
2007-02-27 16:54:02 +01:00
import java.util.ArrayList ;
2006-10-13 01:14:41 +02:00
import java.util.Date ;
2007-03-06 23:43:32 +01:00
import java.util.HashMap ;
2006-06-09 14:52:57 +02:00
import java.util.Iterator ;
2007-02-25 22:06:26 +01:00
import java.util.List ;
2007-02-27 16:54:02 +01:00
import java.util.Map ;
2007-02-25 22:06:26 +01:00
import java.util.TreeMap ;
2006-06-01 18:18:27 +02:00
2007-03-06 23:43:32 +01:00
import de.anomic.server.serverMemory ;
2006-10-24 15:48:16 +02:00
import de.anomic.server.logging.serverLog ;
2006-06-01 18:18:27 +02:00
public class kelondroFlexTable extends kelondroFlexWidthArray implements kelondroIndex {
2007-03-06 23:43:32 +01:00
// static tracker objects
private static TreeMap tableTracker = new TreeMap ( ) ;
// class objects
2007-04-19 15:37:02 +02:00
protected kelondroBytesIntMap index ;
2006-10-26 15:50:50 +02:00
private boolean RAMIndex ;
2007-04-05 12:14:48 +02:00
public kelondroFlexTable ( File path , String tablename , long preloadTime , kelondroRow rowdef , boolean resetOnFail ) {
2006-11-05 22:30:53 +01:00
// the buffersize applies to a possible load of the ram-index
// if the ram is not sufficient, a tree file is generated
// if, and only if a tree file exists, the preload time is applied
2007-04-05 12:14:48 +02:00
super ( path , tablename , rowdef , resetOnFail ) ;
2007-04-05 21:08:59 +02:00
if ( ( super . col [ 0 ] . size ( ) < 0 ) & & ( resetOnFail ) ) try {
super . reset ( ) ;
} catch ( IOException e2 ) {
e2 . printStackTrace ( ) ;
throw new kelondroException ( e2 . getMessage ( ) ) ;
}
try {
2007-03-06 23:43:32 +01:00
long neededRAM = ( long ) ( ( super . row ( ) . column ( 0 ) . cellwidth ( ) + 4 ) * super . size ( ) * kelondroRowCollection . growfactor ) ;
2006-11-05 22:30:53 +01:00
File newpath = new File ( path , tablename ) ;
2006-06-30 14:54:19 +02:00
File indexfile = new File ( newpath , " col.000.index " ) ;
2007-04-05 12:14:48 +02:00
String description = " " ;
description = new String ( this . col [ 0 ] . getDescription ( ) ) ;
2006-08-05 01:04:03 +02:00
int p = description . indexOf ( ';' , 4 ) ;
long stt = ( p > 0 ) ? Long . parseLong ( description . substring ( 4 , p ) ) : 0 ;
System . out . println ( " *** Last Startup time: " + stt + " milliseconds " ) ;
2006-06-30 14:54:19 +02:00
long start = System . currentTimeMillis ( ) ;
2007-04-05 21:08:59 +02:00
2007-06-11 23:33:45 +02:00
if ( serverMemory . request ( neededRAM , true ) ) {
2006-11-05 22:30:53 +01:00
// we can use a RAM index
if ( indexfile . exists ( ) ) {
// delete existing index file
System . out . println ( " *** Delete File index " + indexfile ) ;
indexfile . delete ( ) ;
}
// fill the index
2006-06-30 14:54:19 +02:00
System . out . print ( " *** Loading RAM index for " + size ( ) + " entries from " + newpath ) ;
2007-05-16 12:48:26 +02:00
index = initializeRamIndex ( ) ;
2006-06-30 14:54:19 +02:00
System . out . println ( " -done- " ) ;
2007-05-16 12:48:26 +02:00
System . out . println ( index . size ( )
2006-06-30 14:54:19 +02:00
+ " index entries initialized and sorted from "
+ super . col [ 0 ] . size ( ) + " keys. " ) ;
2006-10-26 15:50:50 +02:00
RAMIndex = true ;
2007-03-06 23:43:32 +01:00
tableTracker . put ( this . filename ( ) , this ) ;
2006-11-05 22:30:53 +01:00
} else {
// too less ram for a ram index
2007-05-16 12:48:26 +02:00
kelondroIndex ki ;
2006-11-05 22:30:53 +01:00
if ( indexfile . exists ( ) ) {
// use existing index file
System . out . println ( " *** Using File index " + indexfile ) ;
2007-05-16 12:48:26 +02:00
ki = new kelondroCache ( kelondroTree . open ( indexfile , true , preloadTime , treeIndexRow ( rowdef . width ( 0 ) , rowdef . objectOrder ) , 2 , 80 ) , true , false ) ;
2006-11-05 22:30:53 +01:00
RAMIndex = false ;
2006-11-06 03:05:39 +01:00
} else {
2006-11-05 22:30:53 +01:00
// generate new index file
2006-11-06 03:05:39 +01:00
System . out . println ( " *** Generating File index for " + size ( ) + " entries from " + indexfile ) ;
2007-03-08 17:15:40 +01:00
System . out . println ( " *** Cause: too less RAM ( " + serverMemory . available ( ) + " Bytes) configured. Assign at least " + ( neededRAM / 1024 / 1024 ) + " MB more RAM to enable a RAM index. " ) ;
2007-03-06 23:43:32 +01:00
ki = initializeTreeIndex ( indexfile , preloadTime , rowdef . objectOrder , rowdef . primaryKey ) ;
2006-11-05 22:30:53 +01:00
System . out . println ( " -done- " ) ;
System . out . println ( ki . size ( ) + " entries indexed from " + super . col [ 0 ] . size ( ) + " keys. " ) ;
RAMIndex = false ;
}
2007-04-19 15:37:02 +02:00
index = new kelondroBytesIntMap ( ki ) ;
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
2006-06-30 14:54:19 +02:00
}
// assign index to wrapper
description = " stt= " + Long . toString ( System . currentTimeMillis ( ) - start ) + " ; " ;
super . col [ 0 ] . setDescription ( description . getBytes ( ) ) ;
2007-04-05 12:14:48 +02:00
} catch ( IOException e ) {
if ( resetOnFail ) {
RAMIndex = true ;
2007-05-16 12:48:26 +02:00
index = new kelondroBytesIntMap ( super . row ( ) . column ( 0 ) . cellwidth ( ) , super . rowdef . objectOrder , 0 ) ;
2007-04-05 12:14:48 +02:00
} else {
throw new kelondroException ( e . getMessage ( ) ) ;
}
}
}
public void reset ( ) throws IOException {
super . reset ( ) ;
RAMIndex = true ;
2007-05-16 12:48:26 +02:00
index = new kelondroBytesIntMap ( super . row ( ) . column ( 0 ) . cellwidth ( ) , super . rowdef . objectOrder , 0 ) ;
2006-06-30 14:54:19 +02:00
}
2006-07-02 02:44:38 +02:00
2006-11-05 22:30:53 +01:00
public static int staticSize ( File path , String tablename ) {
return kelondroFlexWidthArray . staticsize ( path , tablename ) ;
}
2006-11-06 03:05:39 +01:00
public static int staticRAMIndexNeed ( File path , String tablename , kelondroRow rowdef ) {
2007-03-08 00:55:52 +01:00
return ( int ) ( ( rowdef . column ( 0 ) . cellwidth ( ) + 4 ) * staticSize ( path , tablename ) * kelondroRowSet . growfactor ) ;
2006-11-06 03:05:39 +01:00
}
2006-10-26 15:50:50 +02:00
public boolean hasRAMIndex ( ) {
return RAMIndex ;
}
2007-04-19 16:55:19 +02:00
public synchronized boolean has ( byte [ ] key ) throws IOException {
2006-10-26 15:50:50 +02:00
// it is not recommended to implement or use a has predicate unless
// it can be ensured that it causes no IO
2007-08-03 13:44:58 +02:00
if ( ( kelondroAbstractRecords . debugmode ) & & ( RAMIndex ! = true ) ) serverLog . logWarning ( " kelondroFlexTable " , " RAM index warning in file " + super . tablename ) ;
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
return index . geti ( key ) > = 0 ;
2006-10-26 15:50:50 +02:00
}
2007-05-16 12:48:26 +02:00
private kelondroBytesIntMap initializeRamIndex ( ) {
2007-04-05 16:58:29 +02:00
int space = super . col [ 0 ] . size ( ) + 1 ;
if ( space < 0 ) throw new kelondroException ( " wrong space: " + space ) ;
2007-05-16 12:48:26 +02:00
kelondroBytesIntMap ri = new kelondroBytesIntMap ( super . row ( ) . column ( 0 ) . cellwidth ( ) , super . rowdef . objectOrder , space ) ;
2006-07-12 01:44:44 +02:00
Iterator content = super . col [ 0 ] . contentNodes ( - 1 ) ;
2007-08-03 13:44:58 +02:00
kelondroNode node ;
2006-06-09 14:52:57 +02:00
int i ;
2006-10-25 03:21:05 +02:00
byte [ ] key ;
2006-06-09 14:52:57 +02:00
while ( content . hasNext ( ) ) {
2007-08-03 13:44:58 +02:00
node = ( kelondroNode ) content . next ( ) ;
2006-06-09 14:52:57 +02:00
i = node . handle ( ) . hashCode ( ) ;
2006-10-25 03:21:05 +02:00
key = node . getKey ( ) ;
assert ( key ! = null ) : " DEBUG: empty key in initializeRamIndex " ; // should not happen; if it does, it is an error of the condentNodes iterator
2007-04-19 15:37:02 +02:00
//System.out.println("ENTRY: " + serverLog.arrayList(indexentry.bytes(), 0, indexentry.objectsize()));
2007-05-16 12:48:26 +02:00
try { ri . addi ( key , i ) ; } catch ( IOException e ) { } // no IOException can happen here
2006-06-28 18:14:47 +02:00
if ( ( i % 10000 ) = = 0 ) {
System . out . print ( '.' ) ;
System . out . flush ( ) ;
}
2006-06-07 17:40:29 +02:00
}
2006-06-28 18:14:47 +02:00
System . out . print ( " -ordering- " ) ;
System . out . flush ( ) ;
2007-05-16 16:36:56 +02:00
//int sbu = ri.size();
//if (ri.size() != sbu) serverLog.logSevere("kelondroFlexTable.initializeRamIndex: " + tablename, "; size before uniq = " + sbu + ", after uniq = " + ri.size());
2006-06-30 14:54:19 +02:00
return ri ;
2006-06-01 18:18:27 +02:00
}
2006-06-02 00:13:52 +02:00
2007-03-06 23:43:32 +01:00
private kelondroIndex initializeTreeIndex ( File indexfile , long preloadTime , kelondroOrder objectOrder , int primaryKey ) throws IOException {
2007-05-16 12:48:26 +02:00
kelondroIndex treeindex = new kelondroCache ( new kelondroTree ( indexfile , true , preloadTime , treeIndexRow ( rowdef . width ( 0 ) , objectOrder ) , 2 , 80 ) , true , false ) ;
2006-07-12 01:44:44 +02:00
Iterator content = super . col [ 0 ] . contentNodes ( - 1 ) ;
2007-08-03 13:44:58 +02:00
kelondroNode node ;
2006-06-30 14:54:19 +02:00
kelondroRow . Entry indexentry ;
2006-10-31 03:45:41 +01:00
int i , c = 0 , all = super . col [ 0 ] . size ( ) ;
long start = System . currentTimeMillis ( ) ;
long last = start ;
2006-06-30 14:54:19 +02:00
while ( content . hasNext ( ) ) {
2007-08-03 13:44:58 +02:00
node = ( kelondroNode ) content . next ( ) ;
2006-06-30 14:54:19 +02:00
i = node . handle ( ) . hashCode ( ) ;
2006-08-16 21:49:31 +02:00
indexentry = treeindex . row ( ) . newEntry ( ) ;
2006-06-30 14:54:19 +02:00
indexentry . setCol ( 0 , node . getValueRow ( ) ) ;
2006-08-11 05:20:44 +02:00
indexentry . setCol ( 1 , i ) ;
2006-10-31 03:45:41 +01:00
treeindex . addUnique ( indexentry ) ;
c + + ;
if ( System . currentTimeMillis ( ) - last > 30000 ) {
2006-11-06 03:05:39 +01:00
System . out . println ( " .. generated " + c + " / " + all + " entries, " + ( ( System . currentTimeMillis ( ) - start ) / c * ( all - c ) / 60000 ) + " minutes remaining " ) ;
2006-06-30 14:54:19 +02:00
System . out . flush ( ) ;
2006-10-31 03:45:41 +01:00
last = System . currentTimeMillis ( ) ;
2006-06-30 14:54:19 +02:00
}
2006-06-02 00:13:52 +02:00
}
2006-08-16 21:49:31 +02:00
return treeindex ;
2006-06-02 00:13:52 +02:00
}
2007-05-16 12:48:26 +02:00
private static final kelondroRow treeIndexRow ( int keywidth , kelondroOrder objectOrder ) {
return new kelondroRow ( " byte[] key- " + keywidth + " , int reference-4 {b256} " , objectOrder , 0 ) ;
2006-08-24 04:19:25 +02:00
}
2006-06-13 17:05:11 +02:00
public synchronized kelondroRow . Entry get ( byte [ ] key ) throws IOException {
2007-09-04 01:43:55 +02:00
if ( index = = null ) return null ; // case may happen during shutdown
2007-04-19 15:37:02 +02:00
int pos = index . geti ( key ) ;
2007-05-16 16:36:56 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) + " , analysis: " + index . consistencyAnalysis ( ) ;
2007-04-19 15:37:02 +02:00
if ( pos < 0 ) return null ;
// i may be greater than this.size(), because this table may have deleted entries
// the deleted entries are subtracted from the 'real' tablesize,
// so the size may be smaller than an index to a row entry
kelondroRow . Entry result = super . get ( pos ) ;
assert result ! = null ;
assert rowdef . objectOrder . compare ( result . getColBytes ( rowdef . primaryKey ) , key ) = = 0 : " key and row does not match; key = " + serverLog . arrayList ( key , 0 , key . length ) + " row.key = " + serverLog . arrayList ( result . getColBytes ( rowdef . primaryKey ) , 0 , rowdef . width ( rowdef . primaryKey ) ) ;
return result ;
}
2007-02-25 22:06:26 +01:00
2007-05-16 12:48:26 +02:00
public synchronized void putMultiple ( List rows ) throws IOException {
2007-02-25 22:06:26 +01:00
// put a list of entries in a ordered way.
// this should save R/W head positioning time
Iterator i = rows . iterator ( ) ;
kelondroRow . Entry row ;
int pos ;
byte [ ] key ;
2007-02-27 16:54:02 +01:00
TreeMap old_rows_ordered = new TreeMap ( ) ;
ArrayList new_rows_sequential = new ArrayList ( ) ;
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
2007-02-25 22:06:26 +01:00
while ( i . hasNext ( ) ) {
row = ( kelondroRow . Entry ) i . next ( ) ;
key = row . getColBytes ( 0 ) ;
2007-04-19 15:37:02 +02:00
pos = index . geti ( key ) ;
2007-02-25 22:06:26 +01:00
if ( pos < 0 ) {
2007-02-27 16:54:02 +01:00
new_rows_sequential . add ( row ) ;
2007-02-25 22:06:26 +01:00
} else {
2007-02-27 16:54:02 +01:00
old_rows_ordered . put ( new Integer ( pos ) , row ) ;
2007-02-25 22:06:26 +01:00
}
}
2007-02-27 16:54:02 +01:00
// overwrite existing entries in index
super . setMultiple ( old_rows_ordered ) ;
// write new entries to index
2007-05-16 12:48:26 +02:00
addUniqueMultiple ( new_rows_sequential ) ;
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
2007-02-25 22:06:26 +01:00
}
2006-06-01 18:18:27 +02:00
2006-10-19 23:14:37 +02:00
public synchronized kelondroRow . Entry put ( kelondroRow . Entry row , Date entryDate ) throws IOException {
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
return put ( row ) ;
2006-10-13 01:14:41 +02:00
}
2006-06-13 17:05:11 +02:00
public synchronized kelondroRow . Entry put ( kelondroRow . Entry row ) throws IOException {
2006-10-24 15:48:16 +02:00
assert ( row ! = null ) ;
assert ( ! ( serverLog . allZero ( row . getColBytes ( 0 ) ) ) ) ;
2007-04-03 14:10:12 +02:00
assert row . objectsize ( ) < = this . rowdef . objectsize ;
2007-03-08 17:15:40 +01:00
byte [ ] key = row . getColBytes ( 0 ) ;
2007-09-04 01:43:55 +02:00
if ( index = = null ) return null ; // case may appear during shutdown
2007-04-19 15:37:02 +02:00
int pos = index . geti ( key ) ;
2007-03-08 17:15:40 +01:00
if ( pos < 0 ) {
2007-04-19 15:37:02 +02:00
pos = super . add ( row ) ;
index . puti ( key , pos ) ;
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
2006-10-16 17:04:16 +02:00
return null ;
}
2007-04-19 15:37:02 +02:00
//System.out.println("row.key=" + serverLog.arrayList(row.bytes(), 0, row.objectsize()));
2007-03-08 17:15:40 +01:00
kelondroRow . Entry oldentry = super . get ( pos ) ;
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
if ( oldentry = = null ) {
serverLog . logSevere ( " kelondroFlexTable " , " put(): index failure; the index pointed to a cell which is empty. content.size() = " + this . size ( ) + " , index.size() = " + ( ( index = = null ) ? 0 : index . size ( ) ) ) ;
// patch bug ***** FIND CAUSE! (see also: remove)
int oldindex = index . removei ( key ) ;
assert oldindex > = 0 ;
assert index . geti ( key ) = = - 1 ;
// here is this.size() > index.size() because of remove operation above
index . puti ( key , super . add ( row ) ) ;
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
return null ;
}
assert oldentry ! = null : " overwrite of empty position " + pos + " , index management must have failed before " ;
assert rowdef . objectOrder . compare ( oldentry . getColBytes ( rowdef . primaryKey ) , key ) = = 0 : " key and row does not match; key = " + serverLog . arrayList ( key , 0 , key . length ) + " row.key = " + serverLog . arrayList ( oldentry . getColBytes ( rowdef . primaryKey ) , 0 , rowdef . width ( rowdef . primaryKey ) ) ;
2007-03-08 17:15:40 +01:00
super . set ( pos , row ) ;
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
2007-02-20 09:35:51 +01:00
return oldentry ;
2006-06-01 18:18:27 +02:00
}
2006-10-19 23:14:37 +02:00
public synchronized void addUnique ( kelondroRow . Entry row ) throws IOException {
2007-04-03 14:10:12 +02:00
assert row . objectsize ( ) = = this . rowdef . objectsize ;
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
index . addi ( row . getColBytes ( 0 ) , super . add ( row ) ) ;
2006-10-19 23:14:37 +02:00
}
2007-05-16 12:48:26 +02:00
public synchronized void addUniqueMultiple ( List rows ) throws IOException {
2007-02-27 16:54:02 +01:00
// add a list of entries in a ordered way.
// this should save R/W head positioning time
TreeMap indexed_result = super . addMultiple ( rows ) ;
// indexed_result is a Integer/byte[] relation
// that is used here to store the index
Iterator i = indexed_result . entrySet ( ) . iterator ( ) ;
Map . Entry entry ;
while ( i . hasNext ( ) ) {
entry = ( Map . Entry ) i . next ( ) ;
2007-04-19 15:37:02 +02:00
index . puti ( ( byte [ ] ) entry . getValue ( ) , ( ( Integer ) entry . getKey ( ) ) . intValue ( ) ) ;
2007-02-27 16:54:02 +01:00
}
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
2007-02-27 16:54:02 +01:00
}
2006-06-13 17:05:11 +02:00
public synchronized kelondroRow . Entry remove ( byte [ ] key ) throws IOException {
2007-04-19 15:37:02 +02:00
int i = index . removei ( key ) ;
assert ( index . geti ( key ) < 0 ) ; // must be deleted
if ( i < 0 ) {
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
return null ;
2007-04-17 17:15:47 +02:00
}
kelondroRow . Entry r = super . get ( i ) ;
2007-04-19 15:37:02 +02:00
if ( r = = null ) {
serverLog . logSevere ( " kelondroFlexTable " , " remove(): index failure; the index pointed to a cell which is empty. content.size() = " + this . size ( ) + " , index.size() = " + ( ( index = = null ) ? 0 : index . size ( ) ) ) ;
// patch bug ***** FIND CAUSE! (see also: put)
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
return null ;
}
assert r ! = null : " r == null " ; // should be avoided with path above
assert rowdef . objectOrder . compare ( r . getColBytes ( rowdef . primaryKey ) , key ) = = 0 : " key and row does not match; key = " + serverLog . arrayList ( key , 0 , key . length ) + " row.key = " + serverLog . arrayList ( r . getColBytes ( rowdef . primaryKey ) , 0 , rowdef . width ( rowdef . primaryKey ) ) ;
2007-04-17 17:15:47 +02:00
super . remove ( i ) ;
assert super . get ( i ) = = null : " i = " + i + " , get(i) = " + serverLog . arrayList ( super . get ( i ) . bytes ( ) , 0 , 12 ) ;
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
return r ;
2006-06-01 18:18:27 +02:00
}
2006-10-16 17:04:16 +02:00
public synchronized kelondroRow . Entry removeOne ( ) throws IOException {
2007-04-19 15:37:02 +02:00
int i = index . removeonei ( ) ;
2006-10-16 17:04:16 +02:00
if ( i < 0 ) return null ;
kelondroRow . Entry r ;
r = super . get ( i ) ;
2007-04-17 17:15:47 +02:00
super . remove ( i ) ;
2007-04-19 15:37:02 +02:00
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
return r ;
2006-10-16 17:04:16 +02:00
}
2007-03-08 17:15:40 +01:00
public synchronized kelondroCloneableIterator rows ( boolean up , byte [ ] firstKey ) throws IOException {
2007-04-19 15:37:02 +02:00
if ( index = = null ) return new rowIterator ( index , up , firstKey ) ;
assert this . size ( ) = = index . size ( ) : " content.size() = " + this . size ( ) + " , index.size() = " + index . size ( ) ;
return new rowIterator ( index , up , firstKey ) ;
2006-08-05 21:18:33 +02:00
}
2007-03-08 17:15:40 +01:00
public class rowIterator implements kelondroCloneableIterator {
2006-08-05 21:18:33 +02:00
2007-03-08 17:15:40 +01:00
kelondroCloneableIterator indexIterator ;
2007-03-08 23:07:17 +01:00
kelondroBytesIntMap index ;
boolean up ;
2006-08-05 21:18:33 +02:00
2007-03-08 17:15:40 +01:00
public rowIterator ( kelondroBytesIntMap index , boolean up , byte [ ] firstKey ) throws IOException {
2007-03-08 23:07:17 +01:00
this . index = index ;
this . up = up ;
2007-03-08 17:15:40 +01:00
indexIterator = index . rows ( up , firstKey ) ;
}
2007-03-08 23:07:17 +01:00
public Object clone ( Object modifier ) {
try {
return new rowIterator ( index , up , ( byte [ ] ) modifier ) ;
} catch ( IOException e ) {
return null ;
}
2006-08-05 21:18:33 +02:00
}
public boolean hasNext ( ) {
return indexIterator . hasNext ( ) ;
}
public Object next ( ) {
2007-02-04 05:39:47 +01:00
kelondroRow . Entry idxEntry = null ;
while ( ( indexIterator . hasNext ( ) ) & & ( idxEntry = = null ) ) {
idxEntry = ( kelondroRow . Entry ) indexIterator . next ( ) ;
}
if ( idxEntry = = null ) {
serverLog . logSevere ( " kelondroFlexTable.rowIterator: " + tablename , " indexIterator returned null " ) ;
return null ;
}
2006-08-11 05:20:44 +02:00
int idx = ( int ) idxEntry . getColLong ( 1 ) ;
2006-08-05 21:18:33 +02:00
try {
return get ( idx ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
return null ;
}
}
public void remove ( ) {
indexIterator . remove ( ) ;
}
}
2006-10-16 17:04:16 +02:00
public kelondroProfile profile ( ) {
2007-04-19 15:37:02 +02:00
return index . profile ( ) ;
2006-10-16 17:04:16 +02:00
}
2007-03-06 23:43:32 +01:00
public static final Iterator filenames ( ) {
// iterates string objects; all file names from record tracker
return tableTracker . keySet ( ) . iterator ( ) ;
2006-10-24 15:48:16 +02:00
}
2007-03-06 23:43:32 +01:00
public static final kelondroProfile profileStats ( String filename ) {
// returns a map for each file in the tracker;
// the map represents properties for each record oobjects,
// i.e. for cache memory allocation
kelondroFlexTable theFlexTable = ( kelondroFlexTable ) tableTracker . get ( filename ) ;
return theFlexTable . profile ( ) ;
2006-10-24 15:48:16 +02:00
}
2007-03-06 23:43:32 +01:00
public static final Map memoryStats ( String filename ) {
// returns a map for each file in the tracker;
// the map represents properties for each record oobjects,
// i.e. for cache memory allocation
kelondroFlexTable theFlexTable = ( kelondroFlexTable ) tableTracker . get ( filename ) ;
return theFlexTable . memoryStats ( ) ;
2006-10-24 15:48:16 +02:00
}
2007-03-06 23:43:32 +01:00
private final Map memoryStats ( ) {
// returns statistical data about this object
HashMap map = new HashMap ( ) ;
2007-06-26 16:37:10 +02:00
map . put ( " tableIndexChunkSize " , ( ! RAMIndex ) ? " 0 " : Integer . toString ( index . row ( ) . objectsize ) ) ;
map . put ( " tableIndexCount " , ( ! RAMIndex ) ? " 0 " : Integer . toString ( index . size ( ) ) ) ;
map . put ( " tableIndexMem " , ( ! RAMIndex ) ? " 0 " : Integer . toString ( ( int ) ( index . row ( ) . objectsize * index . size ( ) * kelondroRowCollection . growfactor ) ) ) ;
2007-03-06 23:43:32 +01:00
return map ;
2006-10-24 15:48:16 +02:00
}
2007-03-09 09:48:47 +01:00
public synchronized void close ( ) {
2007-03-06 23:43:32 +01:00
if ( tableTracker . remove ( this . filename ) = = null ) {
serverLog . logWarning ( " kelondroFlexTable " , " close(): file ' " + this . filename + " ' was not tracked with record tracker. " ) ;
}
2007-04-19 15:37:02 +02:00
if ( ( index ! = null ) & & ( this . size ( ) ! = ( ( index = = null ) ? 0 : index . size ( ) ) ) ) {
serverLog . logSevere ( " kelondroFlexTable " , " close(): inconsistent content/index size. content.size() = " + this . size ( ) + " , index.size() = " + ( ( index = = null ) ? 0 : index . size ( ) ) ) ;
}
if ( index ! = null ) { index . close ( ) ; index = null ; }
2006-10-24 15:48:16 +02:00
super . close ( ) ;
}
2007-03-09 09:48:47 +01:00
public static void main ( String [ ] args ) {
// open a file, add one entry and exit
File f = new File ( args [ 0 ] ) ;
String name = args [ 1 ] ;
kelondroRow row = new kelondroRow ( " Cardinal key-4 {b256}, byte[] x-64 " , kelondroNaturalOrder . naturalOrder , 0 ) ;
try {
2007-04-05 12:14:48 +02:00
kelondroFlexTable t = new kelondroFlexTable ( f , name , 0 , row , true ) ;
2007-03-09 09:48:47 +01:00
kelondroRow . Entry entry = row . newEntry ( ) ;
entry . setCol ( 0 , System . currentTimeMillis ( ) ) ;
entry . setCol ( 1 , " dummy " . getBytes ( ) ) ;
t . put ( entry ) ;
t . close ( ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
}
2006-06-01 18:18:27 +02:00
}