2006-06-01 18:01:24 +02:00
// kelondroapTable.java
2005-04-07 21:19:42 +02:00
// -----------------------
// (C) by Michael Peter Christen; mc@anomic.de
// first published on http://www.anomic.de
// Frankfurt, Germany, 2005
// last major change: 13.03.2005
//
// 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
//
// Using this software in any meaning (reading, learning, copying, compiling,
// running) means that you agree that the Author(s) is (are) not responsible
// for cost, loss of data or any harm that may be caused directly or indirectly
// by usage of this softare or this documentation. The usage of this software
// is on your own risk. The installation and usage (starting/running) of this
// software may allow other people or application to access your computer and
// any attached devices and is highly dependent on the configuration of the
// software which must be done by the user of the software; the author(s) is
// (are) also not responsible for proper configuration and usage of the
// software, even if provoked by documentation provided together with
// the software.
//
// Any changes to this file according to the GPL as documented in the file
// gpl.txt aside this file in the shipment you received can be done to the
// lines that follows this copyright notice here, but changes must not be
// done inside the copyright notive above. A re-distribution must contain
// the intact and unchanged copyright notice.
// Contributions and changes to the program code must be marked as such.
// this is mainly a convenience class to bundle many kelondroMap Objects
package de.anomic.kelondro ;
2005-05-05 07:32:19 +02:00
import java.io.File ;
import java.io.IOException ;
import java.util.HashMap ;
import java.util.Iterator ;
import java.util.Map ;
2005-04-07 21:19:42 +02:00
2006-06-01 18:01:24 +02:00
public class kelondroMapTable {
2005-04-07 21:19:42 +02:00
HashMap mTables , tTables , sTables ;
File tablesPath ;
2006-06-01 18:01:24 +02:00
public kelondroMapTable ( File tablesPath ) {
2005-04-07 21:19:42 +02:00
this . mTables = new HashMap ( ) ;
this . tTables = new HashMap ( ) ;
this . sTables = new HashMap ( ) ;
this . tablesPath = tablesPath ;
if ( ! ( tablesPath . exists ( ) ) ) tablesPath . mkdirs ( ) ;
}
2006-07-04 01:57:33 +02:00
public void declareMaps (
2007-05-09 19:59:36 +02:00
String tablename , int keysize , kelondroOrder objectOrder , int nodesize , int cacheslots ,
2007-06-26 16:37:10 +02:00
char fillChar , boolean resetOnFail ) {
2007-05-09 19:59:36 +02:00
declareMaps ( tablename , keysize , objectOrder , nodesize , cacheslots , null , null , null , fillChar , resetOnFail ) ;
2005-04-07 21:19:42 +02:00
}
2006-07-04 01:57:33 +02:00
public void declareMaps (
2007-05-09 19:59:36 +02:00
String tablename , int keysize , kelondroOrder objectOrder , int nodesize , int cacheslots ,
2007-06-26 16:37:10 +02:00
String [ ] sortfields , String [ ] longaccfields , String [ ] doubleaccfields , char fillChar , boolean resetOnFail ) {
2007-05-09 19:59:36 +02:00
declareMaps ( tablename , keysize , objectOrder , nodesize , cacheslots , sortfields , longaccfields , doubleaccfields , fillChar , 0 , resetOnFail ) ;
2005-04-07 21:19:42 +02:00
}
2006-07-04 01:57:33 +02:00
public void declareMaps (
2007-05-09 19:59:36 +02:00
String tablename , int keysize , kelondroOrder objectOrder , int nodesize , int cacheslots ,
2007-02-01 10:48:31 +01:00
String [ ] sortfields , String [ ] longaccfields , String [ ] doubleaccfields , char fillChar ,
2007-06-26 16:37:10 +02:00
long preloadTime , boolean resetOnFail ) {
2005-04-07 21:19:42 +02:00
if ( mTables . containsKey ( tablename ) ) throw new RuntimeException ( " kelondroTables.declareMap: table ' " + tablename + " ' declared twice. " ) ;
if ( tTables . containsKey ( tablename ) ) throw new RuntimeException ( " kelondroTables.declareMap: table ' " + tablename + " ' declared already in other context. " ) ;
File tablefile = new File ( tablesPath , " table. " + tablename + " .mdb " ) ;
kelondroDyn dyn ;
2006-08-24 04:19:25 +02:00
if ( ! ( tablefile . exists ( ) ) ) tablefile . getParentFile ( ) . mkdirs ( ) ;
2007-05-09 19:59:36 +02:00
dyn = new kelondroDyn ( tablefile , true , true , preloadTime , keysize , nodesize , fillChar , objectOrder , true , false , resetOnFail ) ;
2007-02-02 14:12:31 +01:00
kelondroMapObjects map = new kelondroMapObjects ( dyn , cacheslots , sortfields , longaccfields , doubleaccfields , null , null ) ;
2005-04-07 21:19:42 +02:00
mTables . put ( tablename , map ) ;
}
2007-06-26 16:37:10 +02:00
public void declareTree ( String tablename , kelondroRow rowdef , long buffersize /*bytes*/ , long preloadTime ) {
2005-04-07 21:19:42 +02:00
if ( mTables . containsKey ( tablename ) ) throw new RuntimeException ( " kelondroTables.declareTree: table ' " + tablename + " ' declared already in other context. " ) ;
if ( tTables . containsKey ( tablename ) ) throw new RuntimeException ( " kelondroTables.declareTree: table ' " + tablename + " ' declared twice. " ) ;
File tablefile = new File ( tablesPath , " table. " + tablename + " .tdb " ) ;
2007-03-06 23:43:32 +01:00
kelondroIndex Tree = new kelondroCache ( kelondroTree . open ( tablefile , true , preloadTime , rowdef ) , true , false ) ;
2005-04-07 21:19:42 +02:00
tTables . put ( tablename , Tree ) ;
}
public synchronized void update ( String tablename , String key , Map map ) throws IOException {
2007-01-30 00:51:10 +01:00
kelondroMapObjects table = ( kelondroMapObjects ) mTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( table = = null ) throw new RuntimeException ( " kelondroTables.update: map table ' " + tablename + " ' does not exist. " ) ;
2005-06-01 00:12:43 +02:00
if ( key . length ( ) > table . keySize ( ) ) key = key . substring ( 0 , table . keySize ( ) ) ;
2005-04-07 21:19:42 +02:00
table . set ( key , map ) ;
mTables . put ( tablename , table ) ;
}
2006-06-02 14:45:57 +02:00
public synchronized void update ( String tablename , kelondroRow . Entry row /* first element is the unique key = index */ ) throws IOException {
2006-10-24 15:48:16 +02:00
kelondroIndex tree = ( kelondroIndex ) tTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( tree = = null ) throw new RuntimeException ( " kelondroTables.update: tree table ' " + tablename + " ' does not exist. " ) ;
tree . put ( row ) ;
tTables . put ( tablename , tree ) ;
}
2007-06-26 16:37:10 +02:00
public synchronized Map selectMap ( String tablename , String key ) {
2007-01-30 00:51:10 +01:00
kelondroMapObjects table = ( kelondroMapObjects ) mTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( table = = null ) throw new RuntimeException ( " kelondroTables.selectMap: map table ' " + tablename + " ' does not exist. " ) ;
2005-06-01 00:12:43 +02:00
if ( key . length ( ) > table . keySize ( ) ) key = key . substring ( 0 , table . keySize ( ) ) ;
2007-01-30 00:51:10 +01:00
return table . getMap ( key ) ;
2005-04-07 21:19:42 +02:00
}
2006-06-01 01:31:46 +02:00
public synchronized kelondroRow . Entry selectByte ( String tablename , String key ) throws IOException {
2006-10-24 15:48:16 +02:00
kelondroIndex tree = ( kelondroIndex ) tTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( tree = = null ) throw new RuntimeException ( " kelondroTables.selectByte: tree table ' " + tablename + " ' does not exist. " ) ;
return tree . get ( key . getBytes ( ) ) ;
}
2007-01-30 00:51:10 +01:00
public synchronized kelondroMapObjects . mapIterator /* of Map-Elements */ maps ( String tablename , boolean up , boolean rotating ) throws IOException {
kelondroMapObjects table = ( kelondroMapObjects ) mTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( table = = null ) throw new RuntimeException ( " kelondroTables.maps: map table ' " + tablename + " ' does not exist. " ) ;
return table . maps ( up , rotating ) ;
}
2007-03-08 23:07:17 +01:00
public synchronized kelondroMapObjects . mapIterator /* of Map-Elements */ maps ( String tablename , boolean up , boolean rotating , byte [ ] firstKey , byte [ ] secondKey ) throws IOException {
2007-01-30 00:51:10 +01:00
kelondroMapObjects table = ( kelondroMapObjects ) mTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( table = = null ) throw new RuntimeException ( " kelondroTables.maps: map table ' " + tablename + " ' does not exist. " ) ;
2007-03-08 23:07:17 +01:00
return table . maps ( up , rotating , firstKey , secondKey ) ;
2005-04-07 21:19:42 +02:00
}
2007-01-30 00:51:10 +01:00
public synchronized kelondroMapObjects . mapIterator /* of Map-Elements */ maps ( String tablename , boolean up , String field ) {
kelondroMapObjects table = ( kelondroMapObjects ) mTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( table = = null ) throw new RuntimeException ( " kelondroTables.maps: map table ' " + tablename + " ' does not exist. " ) ;
return table . maps ( up , field ) ;
}
2007-03-08 23:07:17 +01:00
public synchronized kelondroCloneableIterator /* of kelondroRow.Entry-Elements */ rows ( String tablename , boolean up , boolean rotating , byte [ ] firstKey , byte [ ] secondKey ) throws IOException {
2006-10-24 15:48:16 +02:00
kelondroIndex tree = ( kelondroIndex ) tTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( tree = = null ) throw new RuntimeException ( " kelondroTables.bytes: tree table ' " + tablename + " ' does not exist. " ) ;
2007-03-08 17:15:40 +01:00
kelondroCloneableIterator i = tree . rows ( up , firstKey ) ;
2007-03-08 23:07:17 +01:00
if ( rotating ) return new kelondroRotateIterator ( i , secondKey ) ; else return i ;
2005-04-07 21:19:42 +02:00
}
// if you need the long-values from a row-iteration, please use kelondroRecords.bytes2long to convert from byte[] to long
public synchronized void delete ( String tablename , String key ) throws IOException {
2007-01-30 00:51:10 +01:00
kelondroMapObjects table = ( kelondroMapObjects ) mTables . get ( tablename ) ;
2005-06-01 00:12:43 +02:00
if ( key . length ( ) > table . keySize ( ) ) key = key . substring ( 0 , table . keySize ( ) ) ;
2005-04-07 21:19:42 +02:00
if ( table ! = null ) { table . remove ( key ) ; mTables . put ( tablename , table ) ; return ; }
2006-10-24 15:48:16 +02:00
kelondroIndex Tree = ( kelondroIndex ) tTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( Tree ! = null ) { Tree . remove ( key . getBytes ( ) ) ; tTables . put ( tablename , Tree ) ; return ; }
throw new RuntimeException ( " kelondroTables.delete: table ' " + tablename + " ' does not exist. " ) ;
}
2007-02-01 10:48:31 +01:00
public synchronized long longAccumulator ( String tablename , String field ) {
2007-01-30 00:51:10 +01:00
kelondroMapObjects table = ( kelondroMapObjects ) mTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( table = = null ) throw new RuntimeException ( " kelondroTables.accumulator: map table ' " + tablename + " ' does not exist. " ) ;
2007-02-01 10:48:31 +01:00
return table . getLongAcc ( field ) ;
}
public synchronized double doubleAccumulator ( String tablename , String field ) {
kelondroMapObjects table = ( kelondroMapObjects ) mTables . get ( tablename ) ;
if ( table = = null ) throw new RuntimeException ( " kelondroTables.accumulator: map table ' " + tablename + " ' does not exist. " ) ;
return table . getDoubleAcc ( field ) ;
2005-04-07 21:19:42 +02:00
}
public synchronized int size ( String tablename ) {
2007-01-30 00:51:10 +01:00
kelondroMapObjects table = ( kelondroMapObjects ) mTables . get ( tablename ) ;
2005-04-07 21:19:42 +02:00
if ( table ! = null ) return table . size ( ) ;
2006-10-24 15:48:16 +02:00
kelondroIndex Tree = ( kelondroIndex ) tTables . get ( tablename ) ;
2007-04-19 15:37:02 +02:00
if ( Tree ! = null ) return Tree . size ( ) ;
2005-04-07 21:19:42 +02:00
throw new RuntimeException ( " kelondroTables.accumulator: table ' " + tablename + " ' does not exist. " ) ;
}
2007-03-09 09:48:47 +01:00
public void close ( ) {
2005-04-07 21:19:42 +02:00
Iterator tablesIt = mTables . values ( ) . iterator ( ) ;
2007-01-30 00:51:10 +01:00
while ( tablesIt . hasNext ( ) ) ( ( kelondroMapObjects ) tablesIt . next ( ) ) . close ( ) ;
2005-04-07 21:19:42 +02:00
mTables = null ;
Iterator TreeIt = tTables . values ( ) . iterator ( ) ;
2006-10-24 15:48:16 +02:00
while ( TreeIt . hasNext ( ) ) ( ( kelondroIndex ) TreeIt . next ( ) ) . close ( ) ;
2005-04-07 21:19:42 +02:00
tTables = null ;
}
}