Regain 2.1.0-STABLE API

net.sf.regain.crawler
Class IndexWriterManager

java.lang.Object
  extended by net.sf.regain.crawler.IndexWriterManager

public class IndexWriterManager
extends Object

Kontrolliert und kapselt die Erstellung des Suchindex.

Anwendung:
Rufen Sie für jedes Dokument addToIndex(RawDocument, ErrorLogger) auf. Rufen Sie am Ende close(boolean) auf, um den Index zu schlie�en. Danach sind keine weiteren Aufrufe von addToIndex(RawDocument, ErrorLogger) erlaubt.

Author:
Til Schneider, www.murfman.de

Field Summary
private static int ALL_CLOSED_MODE
          The all closed mode.
private static String BREAKPOINT_INDEX_SUBDIR
          The name of the index sub directory that contains a breakpoint.
private  Profiler mAddToIndexProfiler
          Der Profiler der das Hinzufügen zum Index mißt.
private  File mAnalysisDir
          Das Verzeichnis, in dem die Analyse-Dateien erstellt werden soll.
private  org.apache.lucene.analysis.Analyzer mAnalyzer
          Der Analyzer, der vom IndexWriter genutzt werden soll.
private  File mBreakpointIndexDir
          The directory to create breakpoint indices.
private  Profiler mBreakpointProfiler
          The profiler for the breakpoint creation.
private  CrawlerConfig mConfig
          The crawler configuration.
private  DocumentFactory mDocumentFactory
          Die DocumentFactory, die die Inhalte für die Indizierung aufbereitet.
private  File mErrorLogFile
          The file where the error log should be stored.
private  FileOutputStream mErrorLogStream
          The stream used for writing errors to the error log of the index.
private  PrintWriter mErrorLogWriter
          The print writer used for writing errors to the error log of the index.
private  org.apache.lucene.index.IndexReader mIndexReader
          Der gekapselte IndexReader.
private  org.apache.lucene.search.IndexSearcher mIndexSearcher
          Der gekapselte IndexSearcher.
private  org.apache.lucene.index.IndexWriter mIndexWriter
          Der gekapselte IndexWriter, der den eigentlichen Index erstellt.
private  int mInitialDocCount
          The number of documents that were in the (old) index when the IndexWriterManager was created.
private static org.apache.log4j.Logger mLog
          The logger for this class
private  org.apache.lucene.store.Directory mLuceneTempIndexDir
          The lucene representation of mTempIndexDir.
private  File mNewIndexDir
          Das Verzeichnis, in dem der Suchindex am Ende stehen soll, wenn es keine fatalen Fehler gab.
private  File mQuarantineIndexDir
          Das Verzeichnis, in dem der Suchindex am Ende stehen soll, wenn es fatale Fehler gab.
private  boolean mRetryFailedDocs
          Specifies whether a document that couldn't be prepared the last time should be retried.
private  File mTempIndexDir
          Das Verzeichnis, in dem der neue Suchindex aufgebaut werden soll.
private  boolean mUpdateIndex
          Gibt an, ob ein bestehender Index aktualisiert wird.
private  HashMap<String,String> mUrlsToDeleteHash
          enthält die URL und den LastUpdated-String aller Dokumente, deren Eintr�ge beim Abschlie�en des Index entfernt werden m�ssen.
private static String NEW_INDEX_SUBDIR
          Der Name des Index-Unterverzeichnisses, in das der neue Index gestellt werden soll, sobald er fertig ist ohne dass fatale Fehler aufgetreten sind.
private  CrawlerPluginManager pluginManager
          Crawler Plugin Manager instance
private static String QUARANTINE_INDEX_SUBDIR
          Der Name des Index-Unterverzeichnisses, in das der neue Index gestellt werden soll, sobald er fertig ist, wobei fatale Fehler sufgetreten sind.
private static int READING_MODE
          The reading mode.
private static long RENAME_TIMEOUT
          Workaround: Unter Windows klappt das Umbenennen unmittelbar nach Schlie�en des Index nicht.
private static int SEARCHING_MODE
          The searching mode.
private static String TEMP_INDEX_SUBDIR
          Der Name des Index-Unterverzeichnisses, in dem der neue Index aufgebaut werden soll.
private static String WORKING_INDEX_SUBDIR
          Der Name des Index-Unterverzeichnisses, in dem der genutzte Index steht.
private static boolean WRITE_TERMS_SORTED
          Gibt an, ob die Terme sortiert in die Terme-Datei geschrieben werden soll.
private static int WRITING_MODE
          The writing mode.
 
Constructor Summary
IndexWriterManager(CrawlerConfig config, boolean updateIndex, boolean retryFailedDocs)
          Erzeugt eine neue IndexWriterManager-Instanz.
 
Method Summary
 void addToIndex(RawDocument rawDocument, ErrorLogger errorLogger)
          Adds a document to an index.
 void close(boolean putIntoQuarantine)
          Optimiert und schlie�t den Index
private  boolean copyExistingIndex(File indexDir, String analyzerType)
          Kopiert den zuletzt erstellten Index in das Arbeitsverzeichnis.
 void createBreakpoint()
          Creates a breakpoint.
private  org.apache.lucene.index.IndexWriter createIndexWriter(boolean createNewIndex)
           
 void createNewIndexEntry(RawDocument rawDocument, ErrorLogger errorLogger)
          Creates a indexable document and add this to the index
private  void deleteOldIndex(File oldIndexDir)
          Delets an old index directory.
 int getAddedDocCount()
          Gets the number of documents that were added to the index.
 DocumentFactory getDocumentFactory()
          Getter for the current and initialised DocumentFactory.
 int getIndexEntryCount()
          Gibt die Anzahl der Eintr�ge im Index zurück.
 int getInitialDocCount()
          Gets the number of documents that were in the (old) index when the IndexWriterManager was created.
 int getRemovedDocCount()
          Gets the number of documents that will be removed from the index.
 boolean getUpdateIndex()
          Gibt zurück, ob ein bestehender Index aktualisiert wird.
 boolean isAlreadyIndexed(String url)
          Lookup for a document matching to a given url.
private  boolean isMarkedForDeletion(org.apache.lucene.document.Document doc)
          Gibt zurück, ob ein Dokument für die Löschung vorgemerkt wurde.
 void logError(String msg, Throwable thr)
          Logs an error at the error log of the index.
private  void markForDeletion(org.apache.lucene.document.Document doc)
          Merkt ein Dokument für die sp�tere L�schung vor.
 void parseDocument(RawDocument rawDocument, ErrorLogger errorLogger)
          Creates a document but don't add this to the index
private  void prepareBreakpoint()
          Prepares a breakpoint.
private  void removeObsoleteEntries()
          Goes through the index and deletes all obsolete entries.
 void removeObsoleteEntries(UrlChecker urlChecker)
          Goes through the index and deletes all obsolete entries.
private  void setIndexMode(int mode)
          Sets the current mode The are the following modes: Writing mode: The mIndexWriter is opened, the mIndexSearcher may be opened, the mIndexReader is closed.
private  void writeTermFile(File indexDir, File termFile)
          Erzeugt eine Datei, die alle Terme (also alle erlaubten Suchtexte) enthält.
private  int writeTermsSimply(org.apache.lucene.index.TermEnum termEnum, PrintWriter writer)
          Schreibt die Terme so wie sie vom IndexReader kommen in den Writer.
private  int writeTermsSorted(org.apache.lucene.index.TermEnum termEnum, PrintWriter writer)
          Schreibt die Terme vom IndexReader sortiert in den Writer.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mLog

private static org.apache.log4j.Logger mLog
The logger for this class


NEW_INDEX_SUBDIR

private static final String NEW_INDEX_SUBDIR
Der Name des Index-Unterverzeichnisses, in das der neue Index gestellt werden soll, sobald er fertig ist ohne dass fatale Fehler aufgetreten sind.

Die Suchmaske wird, sobald es diese Verzeichnis gibt seine Suche darauf umstellen. Dabei wird es in "index" umbenannt.

See Also:
Constant Field Values

QUARANTINE_INDEX_SUBDIR

private static final String QUARANTINE_INDEX_SUBDIR
Der Name des Index-Unterverzeichnisses, in das der neue Index gestellt werden soll, sobald er fertig ist, wobei fatale Fehler sufgetreten sind.

See Also:
Constant Field Values

WORKING_INDEX_SUBDIR

private static final String WORKING_INDEX_SUBDIR
Der Name des Index-Unterverzeichnisses, in dem der genutzte Index steht.

See Also:
Constant Field Values

TEMP_INDEX_SUBDIR

private static final String TEMP_INDEX_SUBDIR
Der Name des Index-Unterverzeichnisses, in dem der neue Index aufgebaut werden soll.

See Also:
Constant Field Values

BREAKPOINT_INDEX_SUBDIR

private static final String BREAKPOINT_INDEX_SUBDIR
The name of the index sub directory that contains a breakpoint.

NOTE: The crawler creates periodically so called breakpoints. If the crawler should be stopped before it is finished it can use a breakpoint to go on the next time. Besides the search mask can use a breakpoint if no other index exists. So the user can already search before the first index was fully created.

See Also:
Constant Field Values

WRITE_TERMS_SORTED

private static final boolean WRITE_TERMS_SORTED
Gibt an, ob die Terme sortiert in die Terme-Datei geschrieben werden soll.

See Also:
writeTermFile(File, File), Constant Field Values

RENAME_TIMEOUT

private static final long RENAME_TIMEOUT
Workaround: Unter Windows klappt das Umbenennen unmittelbar nach Schlie�en des Index nicht. Wahrscheinlich sind die Filepointer auf die gerade geschlossenen Dateien noch nicht richtig aufger�umt, so dass ein Umbenennen des Indexverzeichnisses fehl schl�gt. Das Umbenennen wird daher regelm��ig probiert, bis es entweder funktioniert oder bis der Timeout abgelaufen ist.

See Also:
Constant Field Values

WRITING_MODE

private static final int WRITING_MODE
The writing mode.

See Also:
setIndexMode(int), Constant Field Values

READING_MODE

private static final int READING_MODE
The reading mode.

See Also:
setIndexMode(int), Constant Field Values

SEARCHING_MODE

private static final int SEARCHING_MODE
The searching mode.

See Also:
setIndexMode(int), Constant Field Values

ALL_CLOSED_MODE

private static final int ALL_CLOSED_MODE
The all closed mode.

See Also:
setIndexMode(int), Constant Field Values

mConfig

private CrawlerConfig mConfig
The crawler configuration.


mAnalyzer

private org.apache.lucene.analysis.Analyzer mAnalyzer
Der Analyzer, der vom IndexWriter genutzt werden soll.


mIndexWriter

private org.apache.lucene.index.IndexWriter mIndexWriter
Der gekapselte IndexWriter, der den eigentlichen Index erstellt.


mIndexReader

private org.apache.lucene.index.IndexReader mIndexReader
Der gekapselte IndexReader. Wird zum L�schen von Dokumenten aus dem Index ben�tigt.

Ist null, wenn der Index nicht aktualisiert werden soll.


mIndexSearcher

private org.apache.lucene.search.IndexSearcher mIndexSearcher
Der gekapselte IndexSearcher. Wird zum Finden von Dokumenten ben�tigt.

Ist null, wenn der Index nicht aktualisiert werden soll.


mUpdateIndex

private boolean mUpdateIndex
Gibt an, ob ein bestehender Index aktualisiert wird.

Anderenfalls wird ein komplett neuer Index angelegt.


mRetryFailedDocs

private boolean mRetryFailedDocs
Specifies whether a document that couldn't be prepared the last time should be retried.


mDocumentFactory

private DocumentFactory mDocumentFactory
Die DocumentFactory, die die Inhalte für die Indizierung aufbereitet.


mNewIndexDir

private File mNewIndexDir
Das Verzeichnis, in dem der Suchindex am Ende stehen soll, wenn es keine fatalen Fehler gab.


mQuarantineIndexDir

private File mQuarantineIndexDir
Das Verzeichnis, in dem der Suchindex am Ende stehen soll, wenn es fatale Fehler gab.


mTempIndexDir

private File mTempIndexDir
Das Verzeichnis, in dem der neue Suchindex aufgebaut werden soll.


mLuceneTempIndexDir

private org.apache.lucene.store.Directory mLuceneTempIndexDir
The lucene representation of mTempIndexDir.


mBreakpointIndexDir

private File mBreakpointIndexDir
The directory to create breakpoint indices.


mAnalysisDir

private File mAnalysisDir
Das Verzeichnis, in dem die Analyse-Dateien erstellt werden soll.


mErrorLogFile

private File mErrorLogFile
The file where the error log should be stored.


mErrorLogStream

private FileOutputStream mErrorLogStream
The stream used for writing errors to the error log of the index. May be null.


mErrorLogWriter

private PrintWriter mErrorLogWriter
The print writer used for writing errors to the error log of the index. May be null.


mInitialDocCount

private int mInitialDocCount
The number of documents that were in the (old) index when the IndexWriterManager was created.


mAddToIndexProfiler

private Profiler mAddToIndexProfiler
Der Profiler der das Hinzufügen zum Index mißt.


mBreakpointProfiler

private Profiler mBreakpointProfiler
The profiler for the breakpoint creation.


mUrlsToDeleteHash

private HashMap<String,String> mUrlsToDeleteHash
enthält die URL und den LastUpdated-String aller Dokumente, deren Eintr�ge beim Abschlie�en des Index entfernt werden m�ssen.

Die URL bildet den key, der LastUpdated-String die value.


pluginManager

private CrawlerPluginManager pluginManager
Crawler Plugin Manager instance

Constructor Detail

IndexWriterManager

public IndexWriterManager(CrawlerConfig config,
                          boolean updateIndex,
                          boolean retryFailedDocs)
                   throws RegainException
Erzeugt eine neue IndexWriterManager-Instanz.

Parameters:
config - Die zu verwendende Konfiguration.
updateIndex - Gibt an, ob ein bereits bestehender Index aktualisiert werden soll.
retryFailedDocs - Specifies whether a document that couldn't be prepared the last time should be retried.
Throws:
RegainException - Wenn der neue Index nicht vorbereitet werden konnte.
Method Detail

getUpdateIndex

public boolean getUpdateIndex()
Gibt zurück, ob ein bestehender Index aktualisiert wird.

Anderenfalls wird ein komplett neuer Index angelegt.

Returns:
Ob ein bestehender Index aktualisiert wird.

getInitialDocCount

public int getInitialDocCount()
Gets the number of documents that were in the (old) index when the IndexWriterManager was created.

Returns:
The initial number of documents in the index.

getAddedDocCount

public int getAddedDocCount()
Gets the number of documents that were added to the index.

Returns:
The number of documents added to the index.

getRemovedDocCount

public int getRemovedDocCount()
Gets the number of documents that will be removed from the index.

Returns:
The number of documents removed from the index.

logError

public void logError(String msg,
                     Throwable thr)
              throws RegainException
Logs an error at the error log of the index.

Parameters:
msg - The error message.
thr - The error to log. May be null.
Throws:
RegainException - If writing to the error log failed.

setIndexMode

private void setIndexMode(int mode)
                   throws RegainException
Sets the current mode

The are the following modes:

If the index already is in the wanted mode nothing happens. This method is very fast in this case.

Parameters:
mode - The mode the index should have. Must be one of WRITING_MODE, READING_MODE, SEARCHING_MODE or ALL_CLOSED_MODE.
Throws:
RegainException - If closing or opening failed.

createIndexWriter

private org.apache.lucene.index.IndexWriter createIndexWriter(boolean createNewIndex)
                                                       throws IOException
Throws:
IOException

copyExistingIndex

private boolean copyExistingIndex(File indexDir,
                                  String analyzerType)
                           throws RegainException
Kopiert den zuletzt erstellten Index in das Arbeitsverzeichnis.

Parameters:
indexDir - Das Verzeichnis, in dem der Index liegt.
analyzerType - Der Analyzer-Typ, den der alte Index haben muss, um übernommen zu werden.
Returns:
Ob ein alter Index gefunden wurde.
Throws:
RegainException - Wenn das Kopieren fehl schlug.

isAlreadyIndexed

public boolean isAlreadyIndexed(String url)
                         throws RegainException
Lookup for a document matching to a given url.

Parameters:
url - to check for
Returns:
true if there exist the document for the url in the index.
Throws:
RegainException - if checking for url failed

addToIndex

public void addToIndex(RawDocument rawDocument,
                       ErrorLogger errorLogger)
                throws RegainException
Adds a document to an index.

Parameters:
rawDocument - the document to add to a index
errorLogger - The error logger to use for logging errors.
Throws:
RegainException - if adding of the document failed

createNewIndexEntry

public void createNewIndexEntry(RawDocument rawDocument,
                                ErrorLogger errorLogger)
                         throws RegainException
Creates a indexable document and add this to the index

Parameters:
rawDocument - which will be parsed
errorLogger - The error logger to use for logging errors.
Throws:
RegainException - if indexing of the document failed

parseDocument

public void parseDocument(RawDocument rawDocument,
                          ErrorLogger errorLogger)
                   throws RegainException
Creates a document but don't add this to the index

Parameters:
rawDocument - which will be parsed
errorLogger - The error logger to use for logging errors.
Throws:
RegainException - if parsing of the document failed

getDocumentFactory

public DocumentFactory getDocumentFactory()
Getter for the current and initialised DocumentFactory.

Returns:
the current and initialised DocumentFactory

removeObsoleteEntries

public void removeObsoleteEntries(UrlChecker urlChecker)
                           throws RegainException
Goes through the index and deletes all obsolete entries.

Entries are obsolete if they are marked for deletion by the IndexWriterManager (see mUrlsToDeleteHash) or if the don't neither match an entry of the urlToKeepSet nor of the prefixesToKeepArr.

Parameters:
urlChecker - The UrlChecker to use for deciding whether an index entry should be kept in the index or not. If null only the documents in the mUrlsToDeleteHash will be deleted.
Throws:
RegainException - If an index entry could either not be read or deleted.

removeObsoleteEntries

private void removeObsoleteEntries()
                            throws RegainException
Goes through the index and deletes all obsolete entries.

Entries are obsolete if they are marked for deletion by the IndexWriterManager (see mUrlsToDeleteHash).

Throws:
RegainException - If an index entry could either not be read or deleted.

markForDeletion

private void markForDeletion(org.apache.lucene.document.Document doc)
Merkt ein Dokument für die sp�tere L�schung vor.

Diese Methode ist Teil eines Workaround: Ein alter Eintrag, der durch einen neuen ersetzt wird, wird nicht sofort gel�scht, sondern nur zur L�schung vorgemerkt. Auf diese Weise wird ein seltener Fehler umgangen, der das Schlie�en des IndexWriter verhindert, wenn h�ufig zwischen InderWriter und IndexReader gewechselt wird.

Parameters:
doc - Das vorzumerkende Dokument.

isMarkedForDeletion

private boolean isMarkedForDeletion(org.apache.lucene.document.Document doc)
Gibt zurück, ob ein Dokument für die Löschung vorgemerkt wurde.

Parameters:
doc - Das zu prüfende Dokument.
Returns:
Ob das Dokument für die Löschung vorgemerkt wurde.

getIndexEntryCount

public int getIndexEntryCount()
                       throws RegainException
Gibt die Anzahl der Eintr�ge im Index zurück.

Returns:
Die Anzahl der Eintr�ge im Index.
Throws:
RegainException - Wenn die Anzahl nicht ermittelt werden konnte.

prepareBreakpoint

private void prepareBreakpoint()
                        throws RegainException
Prepares a breakpoint.

Throws:
RegainException - If preparing the breakpoint failed.

createBreakpoint

public void createBreakpoint()
                      throws RegainException
Creates a breakpoint.

Throws:
RegainException - If creating the breakpoint failed.

close

public void close(boolean putIntoQuarantine)
           throws RegainException
Optimiert und schlie�t den Index

Parameters:
putIntoQuarantine - Gibt an, ob der Index in Quarant�ne soll.
Throws:
RegainException - Wenn der Index nicht geschlossen werden konnte.

deleteOldIndex

private void deleteOldIndex(File oldIndexDir)
                     throws RegainException
Delets an old index directory.

Parameters:
oldIndexDir - The old index directory.
Throws:
RegainException - If deleting failed.

writeTermFile

private void writeTermFile(File indexDir,
                           File termFile)
                    throws RegainException
Erzeugt eine Datei, die alle Terme (also alle erlaubten Suchtexte) enthält.

Parameters:
indexDir - Das Verzeichnis, in dem der Index steht.
termFile - Der Ort, wo die Datei erstellt werden soll.
Throws:
RegainException - Wenn die Erstellung fehlgeschlagen ist.

writeTermsSimply

private int writeTermsSimply(org.apache.lucene.index.TermEnum termEnum,
                             PrintWriter writer)
                      throws IOException
Schreibt die Terme so wie sie vom IndexReader kommen in den Writer.

Diese Methode braucht minimale Ressourcen.

Parameters:
termEnum - Die Aufz�hlung mit allen Termen.
writer - Der Writer auf den geschrieben werden soll.
Returns:
Die Anzahl der Terme.
Throws:
IOException - Wenn das Schreiben fehl schlug.

writeTermsSorted

private int writeTermsSorted(org.apache.lucene.index.TermEnum termEnum,
                             PrintWriter writer)
                      throws IOException
Schreibt die Terme vom IndexReader sortiert in den Writer.

Um die Terme sortieren zu können, m�ssen sie zwischengespeichert werden. Falls es zu viele sind, k�nnte das schief gehen. In diesem Fall sollte man auf simples Schreiben umstellen.

Parameters:
termEnum - Die Aufz�hlung mit allen Termen.
writer - Der Writer auf den geschrieben werden soll.
Returns:
Die Anzahl der Terme.
Throws:
IOException - Wenn das Schreiben fehl schlug.

Regain 2.1.0-STABLE API

Regain 2.1.0-STABLE, Copyright (C) 2004-2010 Til Schneider, www.murfman.de, Thomas Tesche, www.clustersystems.info