2005-05-17 10:25:04 +02:00
// plasmaWordIndexCache.java
// -------------------------
// part of YACY
// (C) by Michael Peter Christen; mc@anomic.de
// first published on http://www.anomic.de
// Frankfurt, Germany, 2005
// last major change: 6.5.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.
package de.anomic.plasma ;
import java.io.* ;
import java.util.* ;
import java.lang.RuntimeException ;
import de.anomic.kelondro.* ;
2005-06-09 12:34:20 +02:00
import de.anomic.server.logging.serverLog ;
2005-05-17 10:25:04 +02:00
import de.anomic.yacy.yacySeedDB ;
public final class plasmaWordIndexCache implements plasmaWordIndexInterface {
// environment constants
private static final String indexDumpFileName = " indexDump0.stack " ;
2005-05-18 23:52:17 +02:00
private static final String oldSingletonFileName = " indexSingletons0.db " ;
private static final String newSingletonFileName = " indexAssortment001.db " ;
2005-05-23 01:59:52 +02:00
private static final String indexAssortmentClusterPath = " ACLUSTER " ;
2005-05-29 02:34:01 +02:00
private static final int assortmentLimit = 50 ;
2005-05-29 13:56:40 +02:00
private static final int ramcacheLimit = 51 ;
2005-05-22 15:27:54 +02:00
2005-05-17 10:25:04 +02:00
// class variables
private File databaseRoot ;
private plasmaWordIndexInterface backend ;
private TreeMap cache ;
private kelondroMScoreCluster hashScore ;
2005-05-31 19:39:14 +02:00
private kelondroMScoreCluster hashDate ;
private long startTime ;
2005-05-17 10:25:04 +02:00
private int maxWords ;
private serverLog log ;
2005-05-22 15:27:54 +02:00
private plasmaWordIndexAssortmentCluster assortmentCluster ;
2005-05-31 19:39:14 +02:00
private int assortmentBufferSize ; //kb
private flush flushThread ;
2005-05-17 10:25:04 +02:00
// calculated constants
private static String minKey , maxKey ;
static {
maxKey = " " ;
for ( int i = 0 ; i < yacySeedDB . commonHashLength ; i + + ) maxKey + = 'z' ;
minKey = " " ;
for ( int i = 0 ; i < yacySeedDB . commonHashLength ; i + + ) maxKey + = '-' ;
}
2005-05-31 19:39:14 +02:00
public plasmaWordIndexCache ( File databaseRoot , plasmaWordIndexInterface backend , int assortmentbufferkb , serverLog log ) {
2005-05-23 01:59:52 +02:00
// migrate#1
2005-05-18 23:52:17 +02:00
File oldSingletonFile = new File ( databaseRoot , oldSingletonFileName ) ;
File newSingletonFile = new File ( databaseRoot , newSingletonFileName ) ;
if ( ( oldSingletonFile . exists ( ) ) & & ( ! ( newSingletonFile . exists ( ) ) ) ) oldSingletonFile . renameTo ( newSingletonFile ) ;
2005-05-23 01:59:52 +02:00
// create new assortment cluster path
File assortmentClusterPath = new File ( databaseRoot , indexAssortmentClusterPath ) ;
if ( ! ( assortmentClusterPath . exists ( ) ) ) assortmentClusterPath . mkdirs ( ) ;
// migrate#2
File acSingletonFile = new File ( assortmentClusterPath , newSingletonFileName ) ;
if ( ( newSingletonFile . exists ( ) ) & & ( ! ( acSingletonFile . exists ( ) ) ) ) newSingletonFile . renameTo ( acSingletonFile ) ;
2005-05-31 19:39:14 +02:00
// create flushing thread
flushThread = new flush ( ) ;
2005-05-17 10:25:04 +02:00
// creates a new index cache
// the cache has a back-end where indexes that do not fit in the cache are flushed
this . databaseRoot = databaseRoot ;
2005-05-31 19:39:14 +02:00
this . assortmentBufferSize = assortmentbufferkb ;
2005-05-17 10:25:04 +02:00
this . cache = new TreeMap ( ) ;
this . hashScore = new kelondroMScoreCluster ( ) ;
2005-05-31 19:39:14 +02:00
this . hashDate = new kelondroMScoreCluster ( ) ;
this . startTime = System . currentTimeMillis ( ) ;
2005-05-17 10:25:04 +02:00
this . maxWords = 10000 ;
this . backend = backend ;
this . log = log ;
2005-05-31 19:39:14 +02:00
this . assortmentCluster = new plasmaWordIndexAssortmentCluster ( assortmentClusterPath , assortmentLimit , assortmentBufferSize , log ) ;
2005-05-18 23:52:17 +02:00
2005-05-17 10:25:04 +02:00
// read in dump of last session
try {
restore ( ) ;
} catch ( IOException e ) {
log . logError ( " unable to restore cache dump: " + e . getMessage ( ) ) ;
e . printStackTrace ( ) ;
}
2005-05-31 19:39:14 +02:00
// start permanent flushing
flushThread . start ( ) ;
2005-05-17 10:25:04 +02:00
}
2005-05-31 19:39:14 +02:00
2005-05-17 10:25:04 +02:00
private void dump ( int waitingSeconds ) throws IOException {
log . logSystem ( " creating dump for index cache, " + cache . size ( ) + " words (and much more urls) " ) ;
File indexDumpFile = new File ( databaseRoot , indexDumpFileName ) ;
if ( indexDumpFile . exists ( ) ) indexDumpFile . delete ( ) ;
2005-05-18 23:52:17 +02:00
kelondroStack dumpStack = new kelondroStack ( indexDumpFile , 1024 , plasmaWordIndexAssortment . bufferStructureBasis ) ;
2005-05-17 10:25:04 +02:00
long startTime = System . currentTimeMillis ( ) ;
long messageTime = System . currentTimeMillis ( ) + 5000 ;
long wordsPerSecond = 0 , wordcount = 0 , urlcount = 0 ;
synchronized ( cache ) {
Iterator i = cache . entrySet ( ) . iterator ( ) ;
Map . Entry entry ;
String wordHash ;
plasmaWordIndexEntryContainer container ;
2005-05-31 19:39:14 +02:00
long updateTime ;
2005-05-17 10:25:04 +02:00
plasmaWordIndexEntry wordEntry ;
byte [ ] [ ] row = new byte [ 5 ] [ ] ;
2005-06-21 03:17:25 +02:00
System . gc ( ) ; // this can speed up the assortment, because they may better use the cache
2005-05-17 10:25:04 +02:00
while ( i . hasNext ( ) ) {
// get entries
entry = ( Map . Entry ) i . next ( ) ;
wordHash = ( String ) entry . getKey ( ) ;
2005-05-31 19:39:14 +02:00
updateTime = getUpdateTime ( wordHash ) ;
2005-05-17 10:25:04 +02:00
container = ( plasmaWordIndexEntryContainer ) entry . getValue ( ) ;
// put entries on stack
if ( container ! = null ) {
Iterator ci = container . entries ( ) ;
while ( ci . hasNext ( ) ) {
wordEntry = ( plasmaWordIndexEntry ) ci . next ( ) ;
row [ 0 ] = wordHash . getBytes ( ) ;
row [ 1 ] = kelondroRecords . long2bytes ( container . size ( ) , 4 ) ;
2005-05-31 19:39:14 +02:00
row [ 2 ] = kelondroRecords . long2bytes ( updateTime , 8 ) ;
2005-05-17 10:25:04 +02:00
row [ 3 ] = wordEntry . getUrlHash ( ) . getBytes ( ) ;
row [ 4 ] = wordEntry . toEncodedForm ( true ) . getBytes ( ) ;
dumpStack . push ( row ) ;
urlcount + + ;
}
}
wordcount + + ;
2005-06-21 03:17:25 +02:00
i . remove ( ) ; // free some mem
2005-05-17 10:25:04 +02:00
// write a log
if ( System . currentTimeMillis ( ) > messageTime ) {
2005-06-21 03:17:25 +02:00
System . gc ( ) ; // for better statistic
2005-05-17 10:25:04 +02:00
wordsPerSecond = wordcount * 1000 / ( 1 + System . currentTimeMillis ( ) - startTime ) ;
2005-06-23 04:07:45 +02:00
log . logInfo ( " dumping status: " + wordcount + " words done, " + ( cache . size ( ) / ( wordsPerSecond + 1 ) ) + " seconds remaining, free mem = " + ( Runtime . getRuntime ( ) . freeMemory ( ) / 1024 / 1024 ) + " MB " ) ;
2005-05-17 10:25:04 +02:00
messageTime = System . currentTimeMillis ( ) + 5000 ;
}
}
}
dumpStack . close ( ) ;
log . logSystem ( " dumped " + urlcount + " word/url relations in " + ( ( System . currentTimeMillis ( ) - startTime ) / 1000 ) + " seconds " ) ;
}
private long restore ( ) throws IOException {
File indexDumpFile = new File ( databaseRoot , indexDumpFileName ) ;
if ( ! ( indexDumpFile . exists ( ) ) ) return 0 ;
2005-05-18 23:52:17 +02:00
kelondroStack dumpStack = new kelondroStack ( indexDumpFile , 1024 ) ;
2005-05-17 10:25:04 +02:00
log . logSystem ( " restore dump of index cache, " + dumpStack . size ( ) + " word/url relations " ) ;
long startTime = System . currentTimeMillis ( ) ;
long messageTime = System . currentTimeMillis ( ) + 5000 ;
long urlCount = 0 , urlsPerSecond = 0 ;
2005-06-16 20:31:28 +02:00
try {
synchronized ( cache ) {
Iterator i = dumpStack . iterator ( ) ;
kelondroRecords . Node node ;
String wordHash ;
plasmaWordIndexEntryContainer container ;
long creationTime ;
plasmaWordIndexEntry wordEntry ;
byte [ ] [ ] row ;
2005-06-19 15:37:17 +02:00
Runtime rt = Runtime . getRuntime ( ) ;
2005-06-21 03:17:25 +02:00
System . gc ( ) ; // this is not for performance, but only to make the statistic work better
2005-06-16 20:31:28 +02:00
while ( i . hasNext ( ) ) {
// get out one entry
node = ( kelondroRecords . Node ) i . next ( ) ;
row = node . getValues ( ) ;
wordHash = new String ( row [ 0 ] ) ;
creationTime = kelondroRecords . bytes2long ( row [ 2 ] ) ;
wordEntry = new plasmaWordIndexEntry ( new String ( row [ 3 ] ) , new String ( row [ 4 ] ) ) ;
// store to cache
addEntry ( wordHash , wordEntry , creationTime ) ;
urlCount + + ;
2005-06-19 15:37:17 +02:00
// protect against memory shortage
2005-06-23 14:12:12 +02:00
while ( rt . freeMemory ( ) < 1000000 ) flushFromMem ( ) ;
2005-06-16 20:31:28 +02:00
// write a log
if ( System . currentTimeMillis ( ) > messageTime ) {
2005-06-21 03:17:25 +02:00
System . gc ( ) ; // for better statistic
2005-06-16 20:31:28 +02:00
urlsPerSecond = 1 + urlCount * 1000 / ( 1 + System . currentTimeMillis ( ) - startTime ) ;
2005-06-21 03:17:25 +02:00
log . logInfo ( " restoring status: " + urlCount + " urls done, " + ( ( dumpStack . size ( ) - urlCount ) / urlsPerSecond ) + " seconds remaining, free mem = " + ( Runtime . getRuntime ( ) . freeMemory ( ) / 1024 / 1024 ) + " MB " ) ;
2005-06-16 20:31:28 +02:00
messageTime = System . currentTimeMillis ( ) + 5000 ;
}
2005-05-17 10:25:04 +02:00
}
}
2005-06-16 20:31:28 +02:00
dumpStack . close ( ) ;
log . logSystem ( " restored " + cache . size ( ) + " words in " + ( ( System . currentTimeMillis ( ) - startTime ) / 1000 ) + " seconds " ) ;
} catch ( kelondroException e ) {
// restore failed
log . logError ( " restore of indexCache dump failed: " + e . getMessage ( ) ) ;
e . printStackTrace ( ) ;
2005-05-17 10:25:04 +02:00
}
return urlCount ;
}
// cache settings
public int maxURLinWordCache ( ) {
return hashScore . getScore ( hashScore . getMaxObject ( ) ) ;
}
public int wordCacheRAMSize ( ) {
return cache . size ( ) ;
}
public void setMaxWords ( int maxWords ) {
this . maxWords = maxWords ;
}
2005-05-22 15:27:54 +02:00
public int [ ] assortmentsSizes ( ) {
return assortmentCluster . sizes ( ) ;
2005-05-18 23:52:17 +02:00
}
2005-05-17 10:25:04 +02:00
public int size ( ) {
2005-05-22 15:27:54 +02:00
return java . lang . Math . max ( assortmentCluster . sizeTotal ( ) , java . lang . Math . max ( backend . size ( ) , cache . size ( ) ) ) ;
2005-05-17 10:25:04 +02:00
}
public Iterator wordHashes ( String startWordHash , boolean up ) {
// here we merge 3 databases into one view:
// - the RAM Cache
2005-05-31 19:39:14 +02:00
// - the assortmentCluster File Cache
2005-05-17 10:25:04 +02:00
// - the backend
if ( ! ( up ) ) throw new RuntimeException ( " plasmaWordIndexCache.wordHashes can only count up " ) ;
return new kelondroMergeIterator (
new kelondroMergeIterator (
cache . keySet ( ) . iterator ( ) ,
2005-05-22 15:27:54 +02:00
assortmentCluster . hashConjunction ( startWordHash , true ) ,
2005-05-17 10:25:04 +02:00
true ) ,
backend . wordHashes ( startWordHash , true ) ,
true ) ;
}
2005-05-31 19:39:14 +02:00
private class flush extends Thread {
boolean terminate , pause ;
public flush ( ) {
terminate = false ;
pause = false ;
}
public void run ( ) {
String nextHash ;
2005-06-19 15:37:17 +02:00
Runtime rt = Runtime . getRuntime ( ) ;
2005-05-31 19:39:14 +02:00
while ( ! terminate ) {
if ( pause ) {
try { this . sleep ( 300 ) ; } catch ( InterruptedException e ) { }
} else {
2005-06-01 16:24:25 +02:00
flushFromMem ( ) ;
2005-06-19 15:37:17 +02:00
if ( ( rt . freeMemory ( ) > 1000000 ) | | ( cache . size ( ) = = 0 ) ) try {
this . sleep ( 10 + java . lang . Math . min ( 1000 , 10 * maxWords / ( cache . size ( ) + 1 ) ) ) ;
} catch ( InterruptedException e ) { }
2005-05-31 19:39:14 +02:00
}
}
}
public void pause ( ) {
pause = true ;
}
public void proceed ( ) {
pause = false ;
}
public void terminate ( ) {
terminate = true ;
}
}
2005-06-01 16:24:25 +02:00
private void flushFromMem ( ) {
// select appropriate hash
// we have 2 different methods to find a good hash:
// - the oldest entry in the cache
// - the entry with maximum count
if ( cache . size ( ) = = 0 ) return ;
flushThread . pause ( ) ;
try {
int count = hashScore . getMaxScore ( ) ;
String hash = ( String ) hashScore . getMaxObject ( ) ;
long time = ( hash = = null ) ? System . currentTimeMillis ( ) : longTime ( hashDate . getScore ( hash ) ) ;
if ( ( count > ramcacheLimit ) & & ( System . currentTimeMillis ( ) - time > 10000 ) ) {
// flush high-score entries
flushFromMem ( hash , true ) ;
} else {
// flush oldest entries
hash = ( String ) hashDate . getMinObject ( ) ;
flushFromMem ( hash , true ) ;
}
} catch ( Exception e ) {
log . logError ( " flushFromMem: " + e . getMessage ( ) ) ;
e . printStackTrace ( ) ;
}
flushThread . proceed ( ) ;
}
2005-05-17 10:25:04 +02:00
private int flushFromMem ( String key , boolean reintegrate ) {
// this method flushes indexes out from the ram to the disc.
// at first we check the singleton database and act accordingly
// if we we are to flush an index, but see also an entry in the singletons, we
// decide upn the 'reintegrate'-Flag:
// true: do not flush to disc, but re-Integrate the singleton to the RAM
// false: flush the singleton together with container to disc
plasmaWordIndexEntryContainer container = null ;
long time ;
synchronized ( cache ) {
// get the container
container = ( plasmaWordIndexEntryContainer ) cache . get ( key ) ;
if ( container = = null ) return 0 ; // flushing of nonexisting key
2005-05-31 19:39:14 +02:00
time = getUpdateTime ( key ) ;
2005-05-17 10:25:04 +02:00
// remove it from the cache
cache . remove ( key ) ;
hashScore . deleteScore ( key ) ;
2005-05-31 19:39:14 +02:00
hashDate . deleteScore ( key ) ;
2005-05-17 10:25:04 +02:00
}
2005-05-18 23:52:17 +02:00
2005-05-17 10:25:04 +02:00
// now decide where to flush that container
2005-05-22 15:27:54 +02:00
plasmaWordIndexEntryContainer flushedFromAssortment = assortmentCluster . removeFromAll ( key ) ;
2005-05-23 01:59:52 +02:00
if ( ( flushedFromAssortment = = null ) | | ( flushedFromAssortment . size ( ) = = 0 ) ) {
2005-05-22 15:27:54 +02:00
// not found in assortments
if ( container . size ( ) < = assortmentLimit ) {
// this fits into the assortments
plasmaWordIndexEntryContainer feedback = assortmentCluster . storeTry ( key , container ) ;
if ( feedback = = null ) {
return container . size ( ) ;
} else {
// *** should care about another option here ***
return backend . addEntries ( feedback , time ) ;
}
2005-05-17 10:25:04 +02:00
} else {
// store to back-end; this should be a rare case
return backend . addEntries ( container , time ) ;
}
} else {
2005-05-22 15:27:54 +02:00
// we have some records and must integrate them into the flush
container . add ( flushedFromAssortment ) ;
2005-05-19 18:41:08 +02:00
// possibly reintegrate
if ( reintegrate ) {
2005-05-22 15:27:54 +02:00
// put assortmentRecord together with container back to ram
2005-05-19 18:41:08 +02:00
synchronized ( cache ) {
cache . put ( key , container ) ;
hashScore . setScore ( key , container . size ( ) ) ;
2005-05-31 19:39:14 +02:00
hashDate . setScore ( key , intTime ( time ) ) ;
2005-05-19 18:41:08 +02:00
}
2005-05-23 01:59:52 +02:00
return - flushedFromAssortment . size ( ) ;
2005-05-19 18:41:08 +02:00
} else {
// add this to the backend
2005-05-23 01:59:52 +02:00
return backend . addEntries ( container , java . lang . Math . max ( time , flushedFromAssortment . updated ( ) ) ) - flushedFromAssortment . size ( ) ;
2005-05-17 10:25:04 +02:00
}
}
}
2005-05-31 19:39:14 +02:00
private int intTime ( long longTime ) {
return ( int ) ( ( longTime - startTime ) / 1000 ) ;
}
private long longTime ( int intTime ) {
return ( ( long ) intTime ) * ( ( long ) 1000 ) + startTime ;
}
2005-05-23 01:59:52 +02:00
private boolean flushFromAssortmentCluster ( String key ) {
2005-06-19 15:37:17 +02:00
// this should only be called if the assortment shall be deleted or returned in an index entity
2005-05-22 15:27:54 +02:00
plasmaWordIndexEntryContainer container = assortmentCluster . removeFromAll ( key ) ;
if ( container = = null ) {
2005-05-17 10:25:04 +02:00
return false ;
} else {
2005-05-22 15:27:54 +02:00
// we have a non-empty entry-container
2005-05-17 10:25:04 +02:00
// integrate it to the backend
2005-05-22 15:27:54 +02:00
return backend . addEntries ( container , container . updated ( ) ) > 0 ;
2005-05-17 10:25:04 +02:00
}
}
2005-06-01 16:24:25 +02:00
/ *
2005-05-24 01:50:35 +02:00
private synchronized int flushFromMemToLimit ( ) {
2005-05-31 19:39:14 +02:00
if ( ( hashScore . size ( ) = = 0 ) | | ( cache . size ( ) = = 0 ) ) return 0 ;
2005-05-17 10:25:04 +02:00
2005-05-31 19:39:14 +02:00
flushThread . pause ( ) ;
2005-05-17 10:25:04 +02:00
int count = 0 ;
//serverLog.logDebug("PLASMA INDEXING", "flushSpecific: hashScore.size=" + hashScore.size() + ", cache.size=" + cache.size());
synchronized ( hashScore ) {
String key ;
Long createTime ;
// generate flush list
Iterator i = hashScore . scores ( true ) ;
2005-05-23 01:59:52 +02:00
TreeMap [ ] clusterCandidate = new TreeMap [ hashScore . getMaxScore ( ) ] ;
for ( int k = 0 ; k < clusterCandidate . length ; k + + ) clusterCandidate [ k ] = new TreeMap ( ) ; // by create time ordered hash-list
2005-05-17 10:25:04 +02:00
while ( i . hasNext ( ) ) {
// get the entry properties
key = ( String ) i . next ( ) ;
2005-05-31 19:39:14 +02:00
createTime = new Long ( longTime ( hashDate . getScore ( key ) ) ) ;
2005-05-17 10:25:04 +02:00
count = hashScore . getScore ( key ) ;
// put it into a specific ohl
2005-05-23 01:59:52 +02:00
clusterCandidate [ count - 1 ] . put ( createTime , key ) ;
2005-05-17 10:25:04 +02:00
//System.out.println("COUNT FOR KEY " + key + ": " + count);
}
// print statistics
2005-05-25 15:48:48 +02:00
for ( int k = 0 ; k < clusterCandidate . length ; k + + )
log . logDebug ( " FLUSH-LIST " + ( k + 1 ) + " : " + clusterCandidate [ k ] . size ( ) + " entries " ) ;
2005-05-17 10:25:04 +02:00
2005-05-23 01:59:52 +02:00
Map . Entry entry ;
int candidateCounter ;
2005-05-23 17:07:16 +02:00
count = 0 ;
// flush high-scores that accumultated too much
2005-05-25 15:48:48 +02:00
for ( int cluster = clusterCandidate . length ; cluster > = ramcacheLimit ; cluster - - ) {
2005-05-23 17:07:16 +02:00
candidateCounter = 0 ;
i = clusterCandidate [ cluster - 1 ] . entrySet ( ) . iterator ( ) ;
while ( i . hasNext ( ) ) {
entry = ( Map . Entry ) i . next ( ) ;
key = ( String ) entry . getValue ( ) ;
createTime = ( Long ) entry . getKey ( ) ;
count + = java . lang . Math . abs ( flushFromMem ( key , false ) ) ;
2005-05-24 01:50:35 +02:00
candidateCounter + = cluster ;
log . logDebug ( " flushed high-cluster over limit # " + cluster + " , key= " + key + " , count= " + count + " , cachesize= " + cache . size ( ) ) ;
2005-05-23 17:07:16 +02:00
}
}
2005-05-23 01:59:52 +02:00
// flush from assortment cluster
2005-05-24 01:50:35 +02:00
for ( int cluster = 1 ; cluster < = java . lang . Math . min ( clusterCandidate . length , assortmentLimit ) ; cluster + + ) {
2005-05-23 01:59:52 +02:00
candidateCounter = 0 ;
// select a specific cluster
2005-05-24 01:50:35 +02:00
i = clusterCandidate [ cluster - 1 ] . entrySet ( ) . iterator ( ) ;
2005-05-23 01:59:52 +02:00
// check each element in this flush-list: too old?
while ( i . hasNext ( ) ) {
entry = ( Map . Entry ) i . next ( ) ;
key = ( String ) entry . getValue ( ) ;
createTime = ( Long ) entry . getKey ( ) ;
2005-05-25 15:48:48 +02:00
if ( ( createTime ! = null ) & & ( ( System . currentTimeMillis ( ) - createTime . longValue ( ) ) > ( 20000 + ( cluster * 5000 ) ) ) ) {
//log.logDebug("flushing key " + key + ", count=" + count + ", cachesize=" + cache.size() + ", singleton-size=" + singletons.size());
2005-05-23 01:59:52 +02:00
count + = java . lang . Math . abs ( flushFromMem ( key , true ) ) ;
2005-05-24 01:50:35 +02:00
candidateCounter + = cluster ;
2005-05-23 01:59:52 +02:00
}
}
2005-05-24 01:50:35 +02:00
if ( candidateCounter > 0 ) log . logDebug ( " flushed low-cluster # " + cluster + " , count= " + count + " , candidateCounter= " + candidateCounter + " , cachesize= " + cache . size ( ) ) ;
2005-05-23 01:59:52 +02:00
}
2005-05-17 10:25:04 +02:00
2005-05-23 15:15:28 +02:00
// stop flushing if cache is shrinked enough
// avoid as possible to flush high-scores
2005-05-31 19:39:14 +02:00
if ( cache . size ( ) < this . maxWords - 100 ) {
flushThread . proceed ( ) ;
return count ;
}
2005-05-23 15:15:28 +02:00
2005-05-17 10:25:04 +02:00
// flush high-scores
2005-05-25 15:48:48 +02:00
for ( int cluster = java . lang . Math . min ( clusterCandidate . length , ramcacheLimit ) ; cluster > assortmentLimit ; cluster - - ) {
2005-05-23 01:59:52 +02:00
candidateCounter = 0 ;
i = clusterCandidate [ cluster - 1 ] . entrySet ( ) . iterator ( ) ;
2005-05-17 10:25:04 +02:00
while ( i . hasNext ( ) ) {
2005-05-24 01:50:35 +02:00
entry = ( Map . Entry ) i . next ( ) ;
2005-05-17 10:25:04 +02:00
key = ( String ) entry . getValue ( ) ;
createTime = ( Long ) entry . getKey ( ) ;
2005-05-23 01:59:52 +02:00
if ( ( createTime ! = null ) & & ( ( System . currentTimeMillis ( ) - createTime . longValue ( ) ) > ( 600000 / cluster ) ) ) {
count + = java . lang . Math . abs ( flushFromMem ( key , false ) ) ;
2005-05-24 01:50:35 +02:00
candidateCounter + = cluster ;
log . logDebug ( " flushed high-cluster below limit # " + cluster + " , key= " + key + " , count= " + count + " , cachesize= " + cache . size ( ) ) ;
2005-05-17 10:25:04 +02:00
}
2005-05-31 19:39:14 +02:00
if ( cache . size ( ) < this . maxWords - 100 ) {
flushThread . proceed ( ) ;
return count ;
}
2005-05-17 10:25:04 +02:00
}
}
}
2005-05-31 19:39:14 +02:00
flushThread . proceed ( ) ;
2005-05-17 10:25:04 +02:00
return count ;
}
2005-06-01 16:24:25 +02:00
* /
2005-05-17 10:25:04 +02:00
2005-06-01 16:24:25 +02:00
2005-05-17 10:25:04 +02:00
public plasmaWordIndexEntity getIndex ( String wordHash , boolean deleteIfEmpty ) {
2005-05-31 19:39:14 +02:00
flushThread . pause ( ) ;
2005-05-17 10:25:04 +02:00
flushFromMem ( wordHash , false ) ;
2005-05-23 01:59:52 +02:00
flushFromAssortmentCluster ( wordHash ) ;
2005-05-31 19:39:14 +02:00
flushThread . proceed ( ) ;
2005-05-17 10:25:04 +02:00
return backend . getIndex ( wordHash , deleteIfEmpty ) ;
}
2005-05-31 19:39:14 +02:00
public long getUpdateTime ( String wordHash ) {
plasmaWordIndexEntryContainer entries = ( plasmaWordIndexEntryContainer ) cache . get ( wordHash ) ;
if ( entries = = null ) return 0 ;
return entries . updated ( ) ;
/ *
Long time = new Long ( longTime ( hashDate . getScore ( wordHash ) ) ) ;
2005-05-17 10:25:04 +02:00
if ( time = = null ) return 0 ;
return time . longValue ( ) ;
2005-05-31 19:39:14 +02:00
* /
2005-05-17 10:25:04 +02:00
}
public void deleteIndex ( String wordHash ) {
2005-05-31 19:39:14 +02:00
flushThread . pause ( ) ;
2005-05-17 10:25:04 +02:00
synchronized ( cache ) {
cache . remove ( wordHash ) ;
hashScore . deleteScore ( wordHash ) ;
2005-05-31 19:39:14 +02:00
hashDate . deleteScore ( wordHash ) ;
2005-05-17 10:25:04 +02:00
}
2005-05-22 15:27:54 +02:00
assortmentCluster . removeFromAll ( wordHash ) ;
2005-05-17 10:25:04 +02:00
backend . deleteIndex ( wordHash ) ;
2005-05-31 19:39:14 +02:00
flushThread . proceed ( ) ;
2005-05-17 10:25:04 +02:00
}
public synchronized int removeEntries ( String wordHash , String [ ] urlHashes , boolean deleteComplete ) {
2005-05-31 19:39:14 +02:00
flushThread . pause ( ) ;
2005-05-17 10:25:04 +02:00
flushFromMem ( wordHash , false ) ;
2005-05-23 01:59:52 +02:00
flushFromAssortmentCluster ( wordHash ) ;
2005-05-31 19:39:14 +02:00
int removed = backend . removeEntries ( wordHash , urlHashes , deleteComplete ) ;
flushThread . proceed ( ) ;
return removed ;
2005-05-17 10:25:04 +02:00
}
2005-05-22 15:27:54 +02:00
public synchronized int addEntries ( plasmaWordIndexEntryContainer container , long updateTime ) {
2005-05-31 19:39:14 +02:00
flushThread . pause ( ) ;
2005-05-17 10:25:04 +02:00
//serverLog.logDebug("PLASMA INDEXING", "addEntryToIndexMem: cache.size=" + cache.size() + "; hashScore.size=" + hashScore.size());
2005-06-01 16:24:25 +02:00
while ( cache . size ( ) > = this . maxWords ) flushFromMem ( ) ;
2005-06-23 04:07:45 +02:00
if ( ( cache . size ( ) > 10000 ) & & ( Runtime . getRuntime ( ) . freeMemory ( ) < 5000000 ) ) flushFromMem ( ) ;
if ( ( cache . size ( ) > 0 ) & & ( Runtime . getRuntime ( ) . freeMemory ( ) < 1000000 ) ) flushFromMem ( ) ;
2005-05-17 10:25:04 +02:00
//if (flushc > 0) serverLog.logDebug("PLASMA INDEXING", "addEntryToIndexMem - flushed " + flushc + " entries");
// put new words into cache
int added = 0 ;
synchronized ( cache ) {
String wordHash = container . wordHash ( ) ;
plasmaWordIndexEntryContainer entries = ( plasmaWordIndexEntryContainer ) cache . get ( wordHash ) ; // null pointer exception? wordhash != null! must be cache==null
if ( entries = = null ) entries = new plasmaWordIndexEntryContainer ( wordHash ) ;
added = entries . add ( container ) ;
if ( added > 0 ) {
cache . put ( wordHash , entries ) ;
hashScore . addScore ( wordHash , added ) ;
2005-05-31 19:39:14 +02:00
hashDate . setScore ( wordHash , intTime ( updateTime ) ) ;
2005-05-17 10:25:04 +02:00
}
2005-06-19 15:37:17 +02:00
entries = null ;
2005-05-17 10:25:04 +02:00
}
//System.out.println("DEBUG: cache = " + cache.toString());
2005-05-31 19:39:14 +02:00
flushThread . proceed ( ) ;
2005-05-17 10:25:04 +02:00
return added ;
}
2005-05-22 15:27:54 +02:00
private void addEntry ( String wordHash , plasmaWordIndexEntry newEntry , long updateTime ) {
2005-06-19 15:37:17 +02:00
flushThread . pause ( ) ;
plasmaWordIndexEntryContainer container = ( plasmaWordIndexEntryContainer ) cache . get ( wordHash ) ;
if ( container = = null ) container = new plasmaWordIndexEntryContainer ( wordHash ) ;
plasmaWordIndexEntry [ ] entries = new plasmaWordIndexEntry [ ] { newEntry } ;
if ( container . add ( entries , updateTime ) > 0 ) {
cache . put ( wordHash , container ) ;
2005-05-17 10:25:04 +02:00
hashScore . incScore ( wordHash ) ;
2005-05-31 19:39:14 +02:00
hashDate . setScore ( wordHash , intTime ( updateTime ) ) ;
2005-05-17 10:25:04 +02:00
}
2005-06-19 15:37:17 +02:00
entries = null ;
container = null ;
2005-05-31 19:39:14 +02:00
flushThread . proceed ( ) ;
2005-05-17 10:25:04 +02:00
}
public void close ( int waitingSeconds ) {
2005-05-31 19:39:14 +02:00
// stop permanent flushing
flushThread . terminate ( ) ;
try { flushThread . join ( 5000 ) ; } catch ( InterruptedException e ) { }
// close cluster
2005-05-22 15:27:54 +02:00
assortmentCluster . close ( ) ;
2005-05-17 10:25:04 +02:00
try {
dump ( waitingSeconds ) ;
} catch ( IOException e ) {
log . logError ( " unable to dump cache: " + e . getMessage ( ) ) ;
e . printStackTrace ( ) ;
}
}
}