org.hibernate.search.backend.impl.lucene
Class NRTWorkspaceImpl

java.lang.Object
  extended by org.hibernate.search.backend.impl.lucene.AbstractWorkspaceImpl
      extended by org.hibernate.search.backend.impl.lucene.NRTWorkspaceImpl
All Implemented Interfaces:
DirectoryBasedReaderProvider, ReaderProvider, Workspace

public class NRTWorkspaceImpl
extends AbstractWorkspaceImpl
implements DirectoryBasedReaderProvider

The Workspace implementation to be used to take advantage of NRT Lucene features. IndexReader instances are obtained directly from the IndexWriter, which is not forced to flush all pending changes to the Directory structure. In current version Lucene still requires to flush delete operations, or the IndexReaders retrieved via NRT will include deleted Document instances in queries; flushing delete operations happens to be quite expensive to this Workspace implementation attempts to detect when such a flush operation is needed. Applying write operations flags "indexReader requirements" with needs for either normal flush or flushed including deletes, but doesn't actually update IndexReader instances: these instances are updated only if and when a fresh IndexReader is requested via openIndexReader(); this method will check if it can return the last opened IndexReader or if this is stale and it needs to open a fresh copy using NRT from the current IndexWriter. Generation counters are used to track need-at-least version versus last-updated-at version: shared state is avoided between index writers and reader threads to avoid high complexity. The method afterTransactionApplied(boolean, boolean) might trigger multiple times flagging the index to be dirty without triggering an actual IndexReader refresh, so the version counters can have gaps: method refreshReaders() will always jump to latest seen version, as it will refresh the index to satisfy both kinds of flush requirements (writes and deletes). We keep a reference IndexReader in the currentReader atomic reference as a fast path for multiple read events when the index is not dirty. This class implements both Workspace and ReaderProvider.

Author:
Sanne Grinovero (C) 2011 Red Hat Inc.

Field Summary
 
Fields inherited from class org.hibernate.search.backend.impl.lucene.AbstractWorkspaceImpl
writerHolder
 
Constructor Summary
NRTWorkspaceImpl(DirectoryBasedIndexManager indexManager, WorkerBuildContext buildContext, Properties cfg)
           
 
Method Summary
 void afterTransactionApplied(boolean someFailureHappened, boolean streaming)
          Invoked after all changes of a transaction are applied.
 void closeIndexReader(org.apache.lucene.index.IndexReader reader)
           
 void flush()
          Makes sure eventually pending changes are made visible to IndexReaders.
 void initialize(DirectoryBasedIndexManager indexManager, Properties props)
           
 void notifyWorkApplied(LuceneWork work)
          Some workspaces need this to determine for example the kind of flush operations which are safe to apply.
 org.apache.lucene.index.IndexReader openIndexReader()
           
 void stop()
           
 
Methods inherited from class org.hibernate.search.backend.impl.lucene.AbstractWorkspaceImpl
areSingleTermDeletesSafe, getAnalyzer, getDocumentBuilder, getEntitiesInIndexManager, getIndexWriter, getIndexWriter, incrementModificationCounter, optimizerPhase, performOptimization, shutDownNow
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NRTWorkspaceImpl

public NRTWorkspaceImpl(DirectoryBasedIndexManager indexManager,
                        WorkerBuildContext buildContext,
                        Properties cfg)
Method Detail

afterTransactionApplied

public void afterTransactionApplied(boolean someFailureHappened,
                                    boolean streaming)
Description copied from interface: Workspace
Invoked after all changes of a transaction are applied. Must be invoked strictly once after every Workspace.getIndexWriter() in a finally block as implementations might rely on counters to release the IndexWriter.

Specified by:
afterTransactionApplied in interface Workspace
Specified by:
afterTransactionApplied in class AbstractWorkspaceImpl
Parameters:
someFailureHappened - usually false, set to true if errors where caught while using the IndexWriter
streaming - if no immediate visibility of the change is required (hint for performance)

openIndexReader

public org.apache.lucene.index.IndexReader openIndexReader()
Specified by:
openIndexReader in interface ReaderProvider

closeIndexReader

public void closeIndexReader(org.apache.lucene.index.IndexReader reader)
Specified by:
closeIndexReader in interface ReaderProvider

initialize

public void initialize(DirectoryBasedIndexManager indexManager,
                       Properties props)
Specified by:
initialize in interface DirectoryBasedReaderProvider

stop

public void stop()
Specified by:
stop in interface DirectoryBasedReaderProvider

flush

public void flush()
Description copied from interface: Workspace
Makes sure eventually pending changes are made visible to IndexReaders.

Specified by:
flush in interface Workspace

notifyWorkApplied

public void notifyWorkApplied(LuceneWork work)
Description copied from interface: Workspace
Some workspaces need this to determine for example the kind of flush operations which are safe to apply. Generally used for statistics.

Specified by:
notifyWorkApplied in interface Workspace
Parameters:
work - the LuceneWork which was just processed


Copyright © 2006-2013 Red Hat Middleware, LLC. All Rights Reserved