VClassGroupCache now updates when search index is changed NIHVIVO-3455 NIHVIVO-3458

This commit is contained in:
briancaruso 2011-12-08 21:22:32 +00:00
parent 52c3838b44
commit d8615fd1f5
2 changed files with 73 additions and 10 deletions

View file

@ -41,6 +41,8 @@ import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilterUtils;
import edu.cornell.mannlib.vitro.webapp.dao.filtering.filters.VitroFilters;
import edu.cornell.mannlib.vitro.webapp.search.VitroSearchTermNames;
import edu.cornell.mannlib.vitro.webapp.search.beans.ProhibitedFromSearch;
import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexBuilder;
import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexingEventListener;
import edu.cornell.mannlib.vitro.webapp.search.solr.SolrSetup;
import edu.cornell.mannlib.vitro.webapp.startup.StartupStatus;
import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread;
@ -52,7 +54,7 @@ import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread;
* As of VIVO release 1.4, the counts come from the solr index. Before that they
* came from the DAOs.
*/
public class VClassGroupCache {
public class VClassGroupCache implements IndexingEventListener {
private static final Log log = LogFactory.getLog(VClassGroupCache.class);
private static final String ATTRIBUTE_NAME = "VClassGroupCache";
@ -453,6 +455,10 @@ public class VClassGroupCache {
}
/* ****************** Jena Model Change Listener***************************** */
/**
* Listen for changes to what class group classes are in and their display rank.
*/
protected class VClassGroupCacheChangeListener extends StatementListener {
public void addedStatement(Statement stmt) {
checkAndDoUpdate(stmt);
@ -488,11 +494,15 @@ public class VClassGroupCache {
public static class Setup implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext servletContext = sce.getServletContext();
VClassGroupCache vcgc = new VClassGroupCache(servletContext);
ServletContext context = sce.getServletContext();
VClassGroupCache vcgc = new VClassGroupCache(context);
vcgc.requestCacheUpdate();
servletContext.setAttribute(ATTRIBUTE_NAME,vcgc);
log.info("VClassGroupCache added to context");
context.setAttribute(ATTRIBUTE_NAME,vcgc);
log.info("VClassGroupCache added to context");
IndexBuilder indexBuilder = IndexBuilder.getBuilder(context);
indexBuilder.addIndexBuilderListener(vcgc);
log.info("VClassGroupCache set to listen to events from IndexBuilder");
}
@Override
@ -512,4 +522,22 @@ public class VClassGroupCache {
return (VClassGroupCache) sc.getAttribute(ATTRIBUTE_NAME);
}
/**
* Handle notification of events from the IndexBuilder.
*/
@Override
public void notifyOfIndexingEvent(EventTypes event) {
switch( event ){
case FINISH_FULL_REBUILD:
case FINISHED_UPDATE:
log.debug("rebuilding because of IndexBuilder " + event.name());
requestCacheUpdate();
break;
default:
log.debug("ignoring event type " + event.name());
break;
}
}
}

View file

@ -7,6 +7,7 @@ import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedQueue;
@ -73,6 +74,10 @@ public class IndexBuilder extends VitroBackgroundThread {
/** Number of threads to use during indexing. */
protected int numberOfThreads = 10;
/** List of IndexingEventListeners */
protected LinkedList<IndexingEventListener> indexingEventListeners =
new LinkedList<IndexingEventListener>();
public static final int MAX_REINDEX_THREADS= 10;
public static final int MAX_UPDATE_THREADS= 10;
public static final int MAX_THREADS = Math.max( MAX_UPDATE_THREADS, MAX_REINDEX_THREADS);
@ -132,7 +137,7 @@ public class IndexBuilder extends VitroBackgroundThread {
//set flag for full index rebuild
this.reindexRequested = true;
//wake up
this.notifyAll();
this.notifyAll();
}
/**
@ -142,7 +147,15 @@ public class IndexBuilder extends VitroBackgroundThread {
//wake up thread and it will attempt to index anything in changedUris
this.notifyAll();
}
/**
* Add a listener for indexing events. Methods on listener will be called when
* events happen in the IndexBuilder. This is not a Jena ModelListener.
*/
public synchronized void addIndexBuilderListener(IndexingEventListener listener){
indexingEventListeners.add(listener);
}
/**
* This is called when the system shuts down.
*/
@ -159,13 +172,22 @@ public class IndexBuilder extends VitroBackgroundThread {
if( reindexRequested ){
setWorkLevel(WorkLevel.WORKING, FLAG_REBUILDING);
log.debug("full re-index requested");
notifyListeners( IndexingEventListener.EventTypes.START_FULL_REBUILD );
indexRebuild();
notifyListeners( IndexingEventListener.EventTypes.FINISH_FULL_REBUILD );
setWorkLevel(WorkLevel.IDLE);
}else if( !changedStmtQueue.isEmpty() ){
setWorkLevel(WorkLevel.WORKING, FLAG_UPDATING);
Thread.sleep(WAIT_AFTER_NEW_WORK_INTERVAL); //wait a bit to let a bit more work to come into the queue
setWorkLevel(WorkLevel.WORKING, FLAG_UPDATING);
//wait a bit to let a bit more work to come into the queue
Thread.sleep(WAIT_AFTER_NEW_WORK_INTERVAL);
log.debug("work found for IndexBuilder, starting update");
notifyListeners( IndexingEventListener.EventTypes.START_UPDATE );
updatedIndex();
notifyListeners( IndexingEventListener.EventTypes.FINISHED_UPDATE );
setWorkLevel(WorkLevel.IDLE);
} else {
log.debug("there is no indexing working to do, waiting for work");
@ -179,7 +201,9 @@ public class IndexBuilder extends VitroBackgroundThread {
}
if( indexer != null)
indexer.abortIndexingAndCleanUp();
indexer.abortIndexingAndCleanUp();
notifyListeners( IndexingEventListener.EventTypes.SHUTDOWN );
}
@ -440,5 +464,16 @@ public class IndexBuilder extends VitroBackgroundThread {
private final List<String> updatedUris = new ArrayList<String>();
private final List<String> deletedUris = new ArrayList<String>();
}
protected void notifyListeners(IndexingEventListener.EventTypes event){
for ( IndexingEventListener listener : indexingEventListeners ){
try{
if(listener != null )
listener.notifyOfIndexingEvent( event );
}catch(Throwable th){
log.error("problem during NotifyListeners(): " , th);
}
}
}
}