com.atlassian.bonnie
Class LuceneConnection

java.lang.Object
  extended bycom.atlassian.bonnie.LuceneConnection
All Implemented Interfaces:
ILuceneConnection

public class LuceneConnection
extends java.lang.Object
implements ILuceneConnection

TODO reentrancy to/from reader/writer ... allow it? these operations can occur concurrently, using the same reader (writer closed beforehand): - withSearch - withReader

these operations will be serialized against all other operatons - withWriter (closes any readers closed beforehand) - withReaderAndDeletes (writers closed beforehand, reader+writer closed afterwards) - optimize (any readers closed beforehand) - flushWriter (closes readers and writers)

also see: - "What are all possible concurrent Lucene requests?" http://www.jguru.com/faq/view.jsp?EID=913302

Author:
Matt Quail

Nested Class Summary
 class LuceneConnection.TempIndexWriterData
           
 
Nested classes inherited from class com.atlassian.bonnie.ILuceneConnection
ILuceneConnection.BatchUpdateAction, ILuceneConnection.Configuration, ILuceneConnection.ReaderAction, ILuceneConnection.SearcherAction, ILuceneConnection.WriterAction
 
Field Summary
protected  org.apache.lucene.analysis.Analyzer analyzerForIndexing
           
static org.apache.log4j.Category LOG
           
static java.lang.String REINDEXING_FILENAME
           
protected  boolean searcherOpensNewReader
           
static int WRITER_BATCH
           
static int WRITER_DEFAULT
           
static int WRITER_INTERACTIVE
           
 
Fields inherited from interface com.atlassian.bonnie.ILuceneConnection
DEFAULT_CONFIGURATION
 
Constructor Summary
LuceneConnection()
          Default constructor.
LuceneConnection(org.apache.lucene.store.Directory directory, org.apache.lucene.analysis.Analyzer analyzerForIndexing)
          Create a new lucene connection, setting the index directory to be the specified directory.
LuceneConnection(org.apache.lucene.store.Directory directory, org.apache.lucene.analysis.Analyzer analyzerForIndexing, ILuceneConnection.Configuration configuration)
          Create a new lucene connection, setting the index directory to be the specified directory.
LuceneConnection(java.io.File location, org.apache.lucene.analysis.Analyzer analyzerForIndexing)
          Create a new lucene connection, setting the index directory to be the specified location.
LuceneConnection(java.io.File location, org.apache.lucene.analysis.Analyzer analyzerForIndexing, ILuceneConnection.Configuration configuration)
          Create a new lucene connection, setting the index directory to be the specified location.
 
Method Summary
 void close()
          Closes the reader and the writer.
 void flushWriter()
           
protected  org.apache.lucene.store.Directory getDirectory()
           
 java.io.File getIndexDir()
           
 int getNumDocs()
          Returns the number of documents in the index.
protected  org.apache.lucene.index.IndexReader getReader()
           
protected  org.apache.lucene.search.IndexSearcher getSearcher()
           
protected  org.apache.lucene.index.IndexWriter getWriter()
           
protected  org.apache.lucene.index.IndexWriter getWriter(int flags)
           
 boolean isIndexCreated()
          Returns true if the index has been created.
 boolean isReIndexing()
           
 org.apache.lucene.search.IndexSearcher leakSearcher()
          Get the current Searcher from the ILuceneConnection.
 void optimize()
          Perform an optimize on the index.
 void recreateIndexDirectory()
          Create or recreate the underlying directory the index is contained in.
 void setAlwaysUnlock(boolean alwaysUnlock)
           
 void setIndexDir(java.lang.String location)
           
 void setReIndexing(boolean b)
          Sets reindexing flag.
 void setSearcherOpensNewReader(boolean searcherOpensNewReader)
           
protected  boolean shouldCreateIndexDirectory()
          Returns true if the index directory should be created.
 void truncateIndex()
           
 void unlockIfNeeded()
           
 void withBatchUpdate(ILuceneConnection.BatchUpdateAction action)
          Perform multiple writes to the index.
 void withDeleteAndWrites(ILuceneConnection.ReaderAction readerAction, ILuceneConnection.WriterAction writerAction)
          Perform an Atomic delete and add.
 java.lang.Object withReader(ILuceneConnection.ReaderAction action)
          Idempotent operation.
 void withReaderAndDeletes(ILuceneConnection.ReaderAction action)
          Delete stuff in the index.
 void withSearch(ILuceneConnection.SearcherAction action)
           
 void withWriter(ILuceneConnection.WriterAction action)
          Add (write) documents to the index
 void withWriter(ILuceneConnection.WriterAction action, int flags)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG

public static final org.apache.log4j.Category LOG

WRITER_DEFAULT

public static final int WRITER_DEFAULT
See Also:
Constant Field Values

WRITER_INTERACTIVE

public static final int WRITER_INTERACTIVE
See Also:
Constant Field Values

WRITER_BATCH

public static final int WRITER_BATCH
See Also:
Constant Field Values

REINDEXING_FILENAME

public static final java.lang.String REINDEXING_FILENAME
See Also:
Constant Field Values

analyzerForIndexing

protected org.apache.lucene.analysis.Analyzer analyzerForIndexing

searcherOpensNewReader

protected boolean searcherOpensNewReader
Constructor Detail

LuceneConnection

public LuceneConnection()
Default constructor.


LuceneConnection

public LuceneConnection(java.io.File location,
                        org.apache.lucene.analysis.Analyzer analyzerForIndexing)
Create a new lucene connection, setting the index directory to be the specified location.

Parameters:
location - of the index directory.
analyzerForIndexing -

LuceneConnection

public LuceneConnection(java.io.File location,
                        org.apache.lucene.analysis.Analyzer analyzerForIndexing,
                        ILuceneConnection.Configuration configuration)
Create a new lucene connection, setting the index directory to be the specified location.

Parameters:
location - of the index directory.
analyzerForIndexing -
configuration - of the Lucene IndexWriter settings

LuceneConnection

public LuceneConnection(org.apache.lucene.store.Directory directory,
                        org.apache.lucene.analysis.Analyzer analyzerForIndexing)
Create a new lucene connection, setting the index directory to be the specified directory.

Parameters:
directory - used for storing the lucene index.
analyzerForIndexing -

LuceneConnection

public LuceneConnection(org.apache.lucene.store.Directory directory,
                        org.apache.lucene.analysis.Analyzer analyzerForIndexing,
                        ILuceneConnection.Configuration configuration)
Create a new lucene connection, setting the index directory to be the specified directory.

Parameters:
directory - used for storing the lucene index.
analyzerForIndexing -
configuration - of the Lucene IndexWriter settings
Method Detail

withSearch

public void withSearch(ILuceneConnection.SearcherAction action)
                throws LuceneException
Specified by:
withSearch in interface ILuceneConnection
Throws:
LuceneException

getSearcher

protected org.apache.lucene.search.IndexSearcher getSearcher()
                                                      throws java.io.IOException
Throws:
java.io.IOException

withReader

public java.lang.Object withReader(ILuceneConnection.ReaderAction action)
                            throws LuceneException
Description copied from interface: ILuceneConnection
Idempotent operation. Just for querying, do not delete documents with this action use #withReaderAndDeletes(ReaderAction) to perform index deletes.

Specified by:
withReader in interface ILuceneConnection
Throws:
LuceneException

withReaderAndDeletes

public void withReaderAndDeletes(ILuceneConnection.ReaderAction action)
                          throws LuceneException
Description copied from interface: ILuceneConnection
Delete stuff in the index.

Specified by:
withReaderAndDeletes in interface ILuceneConnection
Throws:
LuceneException

withWriter

public void withWriter(ILuceneConnection.WriterAction action)
                throws LuceneException
Description copied from interface: ILuceneConnection
Add (write) documents to the index

Specified by:
withWriter in interface ILuceneConnection
Throws:
LuceneException

withWriter

public void withWriter(ILuceneConnection.WriterAction action,
                       int flags)
                throws LuceneException
Throws:
LuceneException

withDeleteAndWrites

public void withDeleteAndWrites(ILuceneConnection.ReaderAction readerAction,
                                ILuceneConnection.WriterAction writerAction)
                         throws LuceneException
Description copied from interface: ILuceneConnection
Perform an Atomic delete and add. Shouldn't perform too many writes, for multiple writes use ILuceneConnection.withBatchUpdate(com.atlassian.bonnie.ILuceneConnection.BatchUpdateAction) as it will use the batch mode configuration, speeding up the writes.

Specified by:
withDeleteAndWrites in interface ILuceneConnection
Throws:
LuceneException

withBatchUpdate

public void withBatchUpdate(ILuceneConnection.BatchUpdateAction action)
Description copied from interface: ILuceneConnection
Perform multiple writes to the index. Holds a writeLock on the index for the whole time, and will use the batch configuration settings.

Specified by:
withBatchUpdate in interface ILuceneConnection

optimize

public void optimize()
              throws LuceneException
Description copied from interface: ILuceneConnection
Perform an optimize on the index. Holds a writeLock and can take a long time (depending on the size of the index, how much optimization needs to be done... and whether a virus-checker is installed :) ).

Specified by:
optimize in interface ILuceneConnection
Throws:
LuceneException

setSearcherOpensNewReader

public void setSearcherOpensNewReader(boolean searcherOpensNewReader)

flushWriter

public void flushWriter()
                 throws LuceneException
Throws:
LuceneException

getReader

protected final org.apache.lucene.index.IndexReader getReader()
                                                       throws LuceneException
Throws:
LuceneException

getWriter

protected org.apache.lucene.index.IndexWriter getWriter()
                                                 throws LuceneException
Throws:
LuceneException

getWriter

protected org.apache.lucene.index.IndexWriter getWriter(int flags)
                                                 throws LuceneException
Throws:
LuceneException

close

public void close()
Description copied from interface: ILuceneConnection
Closes the reader and the writer.

Specified by:
close in interface ILuceneConnection

isReIndexing

public boolean isReIndexing()
                     throws java.io.IOException
Throws:
java.io.IOException

setReIndexing

public void setReIndexing(boolean b)
                   throws java.io.IOException
Sets reindexing flag. This is NOT a simple setter method. If true, the index's timestamp is updated and a .reindexing file is created as a flag. If false, the .reindexing file is deleted and the writer is closed.

Parameters:
b -
Throws:
java.io.IOException

recreateIndexDirectory

public void recreateIndexDirectory()
Description copied from interface: ILuceneConnection
Create or recreate the underlying directory the index is contained in. Blow away any indexes that currently live there.

Specified by:
recreateIndexDirectory in interface ILuceneConnection

leakSearcher

public org.apache.lucene.search.IndexSearcher leakSearcher()
Description copied from interface: ILuceneConnection
Get the current Searcher from the ILuceneConnection.

Do not forget to call the Searcher's close() method once the searcher is no longer neeeded.

Specified by:
leakSearcher in interface ILuceneConnection

shouldCreateIndexDirectory

protected boolean shouldCreateIndexDirectory()
                                      throws java.io.IOException
Returns true if the index directory should be created. This implementation will return true if and only if the index does not already exist.

Returns:
Throws:
java.io.IOException

truncateIndex

public void truncateIndex()
                   throws java.io.IOException
Throws:
java.io.IOException

getDirectory

protected org.apache.lucene.store.Directory getDirectory()
                                                  throws java.io.IOException
Returns:
Throws:
java.io.IOException

getIndexDir

public java.io.File getIndexDir()

setIndexDir

public void setIndexDir(java.lang.String location)

getNumDocs

public int getNumDocs()
Description copied from interface: ILuceneConnection
Returns the number of documents in the index.

Specified by:
getNumDocs in interface ILuceneConnection

isIndexCreated

public boolean isIndexCreated()
Description copied from interface: ILuceneConnection
Returns true if the index has been created. This means that the index directory itself exists AND has been initialised with the default required index files.

Specified by:
isIndexCreated in interface ILuceneConnection
Returns:
true if the index exists, false otherwise.

unlockIfNeeded

public void unlockIfNeeded()
                    throws java.io.IOException
Throws:
java.io.IOException

setAlwaysUnlock

public void setAlwaysUnlock(boolean alwaysUnlock)


Copyright © 2006-2009 Atlassian Software Systems Pty Ltd. All Rights Reserved.