2006-02-21 01:30:38 +01:00
// yacyNewsQueue.java
// -----------------------
// part of YaCy
2008-07-20 19:14:51 +02:00
// (C) by Michael Peter Christen; mc@yacy.net
2006-02-21 01:30:38 +01:00
// first published on http://www.anomic.de
// Frankfurt, Germany, 2005
//
// $LastChangedDate$
// $LastChangedRevision$
// $LastChangedBy$
//
// 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 notice 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.yacy ;
import java.io.File ;
import java.io.IOException ;
2006-09-22 07:40:29 +02:00
import java.io.UnsupportedEncodingException ;
2006-02-21 01:30:38 +01:00
import java.util.Iterator ;
2009-01-30 16:33:00 +01:00
import de.anomic.kelondro.index.Row ;
import de.anomic.kelondro.index.ObjectIndex ;
2009-01-30 23:08:08 +01:00
import de.anomic.kelondro.order.Base64Order ;
2009-06-28 23:37:37 +02:00
import de.anomic.kelondro.table.Table ;
2009-03-06 16:53:20 +01:00
import de.anomic.kelondro.util.DateFormatter ;
2009-03-30 17:31:25 +02:00
import de.anomic.kelondro.util.FileUtils ;
2009-01-30 23:44:20 +01:00
import de.anomic.kelondro.util.kelondroException ;
2006-02-21 01:30:38 +01:00
import de.anomic.server.serverCodings ;
public class yacyNewsDB {
2008-08-02 14:12:04 +02:00
private final File path ;
2009-01-30 16:33:00 +01:00
protected ObjectIndex news ;
2006-02-21 01:30:38 +01:00
2008-08-02 14:12:04 +02:00
public yacyNewsDB ( final File path ) {
2006-02-21 01:30:38 +01:00
this . path = path ;
2009-06-28 23:37:37 +02:00
this . news = new Table ( path , yacyNewsRecord . rowdef , Table . tailCacheUsageAuto , 10 , 0 ) ;
2008-01-24 23:49:00 +01:00
//this.news = new kelondroCache(kelondroTree.open(path, true, preloadTime, yacyNewsRecord.rowdef));
2006-02-21 01:30:38 +01:00
}
2007-06-26 16:37:10 +02:00
private void resetDB ( ) {
2008-08-02 14:12:04 +02:00
try { close ( ) ; } catch ( final Exception e ) { }
2009-03-30 17:31:25 +02:00
if ( path . exists ( ) ) FileUtils . deletedelete ( path ) ;
2009-06-28 23:37:37 +02:00
this . news = new Table ( path , yacyNewsRecord . rowdef , Table . tailCacheUsageAuto , 10 , 0 ) ;
2006-05-09 12:03:12 +02:00
}
2006-02-21 01:30:38 +01:00
public void close ( ) {
2007-03-09 09:48:47 +01:00
if ( news ! = null ) news . close ( ) ;
2006-02-21 01:30:38 +01:00
news = null ;
}
2008-08-06 21:43:12 +02:00
protected void finalize ( ) {
2006-02-21 01:30:38 +01:00
close ( ) ;
}
2007-06-26 16:37:10 +02:00
public int size ( ) {
2006-02-21 01:30:38 +01:00
return news . size ( ) ;
}
2008-08-02 14:12:04 +02:00
public void remove ( final String id ) throws IOException {
2008-07-05 02:35:20 +02:00
news . remove ( id . getBytes ( ) ) ;
2006-02-21 01:30:38 +01:00
}
2008-08-02 14:12:04 +02:00
public synchronized yacyNewsRecord put ( final yacyNewsRecord record ) throws IOException {
2006-02-21 01:30:38 +01:00
try {
2009-03-11 21:23:19 +01:00
return b2r ( news . replace ( r2b ( record ) ) ) ;
2008-08-02 14:12:04 +02:00
} catch ( final kelondroException e ) {
2006-02-21 01:30:38 +01:00
resetDB ( ) ;
2009-03-11 21:23:19 +01:00
return b2r ( news . replace ( r2b ( record ) ) ) ;
2006-02-21 01:30:38 +01:00
}
}
2008-01-17 13:12:52 +01:00
public synchronized Iterator < yacyNewsRecord > news ( ) throws IOException {
2006-02-21 01:30:38 +01:00
// the iteration iterates yacyNewsRecord - type objects
return new recordIterator ( ) ;
}
2008-01-17 13:12:52 +01:00
public class recordIterator implements Iterator < yacyNewsRecord > {
2006-02-21 01:30:38 +01:00
2009-01-30 16:33:00 +01:00
Iterator < Row . Entry > rowIterator ;
2006-02-21 01:30:38 +01:00
public recordIterator ( ) throws IOException {
2009-02-24 11:40:20 +01:00
rowIterator = news . rows ( ) ;
2006-02-21 01:30:38 +01:00
}
public boolean hasNext ( ) {
2006-03-18 00:43:24 +01:00
return rowIterator . hasNext ( ) ;
2006-02-21 01:30:38 +01:00
}
2008-01-17 13:12:52 +01:00
public yacyNewsRecord next ( ) {
2008-06-06 18:01:27 +02:00
return b2r ( rowIterator . next ( ) ) ;
2006-02-21 01:30:38 +01:00
}
public void remove ( ) {
2009-02-17 10:12:47 +01:00
throw new UnsupportedOperationException ( ) ;
2006-02-21 01:30:38 +01:00
}
}
2008-08-02 14:12:04 +02:00
public synchronized yacyNewsRecord get ( final String id ) throws IOException {
2006-02-21 01:30:38 +01:00
try {
return b2r ( news . get ( id . getBytes ( ) ) ) ;
2008-08-02 14:12:04 +02:00
} catch ( final kelondroException e ) {
2006-02-21 01:30:38 +01:00
resetDB ( ) ;
return null ;
}
}
2009-01-30 16:33:00 +01:00
protected final static yacyNewsRecord b2r ( final Row . Entry b ) {
2006-06-01 01:31:46 +02:00
if ( b = = null ) return null ;
2007-07-13 11:41:55 +02:00
return yacyNewsRecord . newRecord (
2006-06-01 01:31:46 +02:00
b . getColString ( 0 , null ) ,
2006-09-22 07:40:29 +02:00
b . getColString ( 1 , " UTF-8 " ) ,
2009-01-30 16:33:00 +01:00
( b . empty ( 2 ) ) ? null : DateFormatter . parseShortSecond ( b . getColString ( 2 , null ) , DateFormatter . UTCDiffString ( ) ) ,
2006-08-11 05:20:44 +02:00
( int ) b . getColLong ( 3 ) ,
2006-10-10 22:09:26 +02:00
serverCodings . string2map ( b . getColString ( 4 , " UTF-8 " ) , " , " )
2006-06-01 01:31:46 +02:00
) ;
}
2009-01-30 16:33:00 +01:00
protected final Row . Entry r2b ( final yacyNewsRecord r ) {
2007-07-13 11:41:55 +02:00
if ( r = = null ) return null ;
2006-09-22 07:40:29 +02:00
try {
2008-08-02 14:12:04 +02:00
final String attributes = r . attributes ( ) . toString ( ) ;
2006-09-22 07:40:29 +02:00
if ( attributes . length ( ) > yacyNewsRecord . attributesMaxLength ) throw new IllegalArgumentException ( " attribute length= " + attributes . length ( ) + " exceeds maximum size= " + yacyNewsRecord . attributesMaxLength ) ;
2009-01-30 16:33:00 +01:00
final Row . Entry entry = this . news . row ( ) . newEntry ( ) ;
2006-09-22 07:40:29 +02:00
entry . setCol ( 0 , r . id ( ) . getBytes ( ) ) ;
entry . setCol ( 1 , r . category ( ) . getBytes ( " UTF-8 " ) ) ;
2009-01-30 16:33:00 +01:00
entry . setCol ( 2 , ( r . received ( ) = = null ) ? null : DateFormatter . formatShortSecond ( r . received ( ) ) . getBytes ( ) ) ;
entry . setCol ( 3 , Base64Order . enhancedCoder . encodeLong ( r . distributed ( ) , 2 ) . getBytes ( ) ) ;
2006-09-22 07:40:29 +02:00
entry . setCol ( 4 , attributes . getBytes ( " UTF-8 " ) ) ;
return entry ;
2008-08-02 14:12:04 +02:00
} catch ( final UnsupportedEncodingException e ) {
2006-09-22 07:40:29 +02:00
// ignore this. this should never occure
return null ;
}
2006-02-21 01:30:38 +01:00
}
}