public void addWorkToQueue(T entity,
Serializable id,
WorkType workType,
List queue,
SearchFactory searchFactory) {
Class entityClass = Hibernate.getClass( entity );
//TODO with the caller loop we are in a n^2: optimize it using a HashMap for work recognition
for ( LuceneWork luceneWork : queue) {
//whatever the actual work, we should ignore
if ( luceneWork.getEntityClass() == entityClass
&& luceneWork.getId().equals( id ) ) {//find a way to use Type.equals(x,y)
return;
}
}
boolean searchForContainers = false;
if ( workType == WorkType.ADD ) {
Document doc = getDocument( entity, id );
queue.add( new AddLuceneWork( id, entityClass, doc ) );
searchForContainers = true;
}
else if ( workType == WorkType.DELETE ) {
queue.add( new DeleteLuceneWork(id, entityClass) );
}
else if ( workType == WorkType.UPDATE ) {
Document doc = getDocument( entity, id );
/**
* even with Lucene 2.1, use of indexWriter to update is not an option
* We can only delete by term, and the index doesn't have a term that
* uniquely identify the entry.
* But essentially the optimization we are doing is the same Lucene is doing, the only extra cost is the
* double file opening.
*/
queue.add( new DeleteLuceneWork(id, entityClass) );
queue.add( new AddLuceneWork( id, entityClass, doc ) );
searchForContainers = true;
}
else {
throw new AssertionFailure("Unknown WorkType: " + workType);
}
/**
* When references are changed, either null or another one, we expect dirty checking to be triggered (both sides
* have to be updated)
* When the internal object is changed, we apply the {Add|Update}Work on containedIns
*/
if (searchForContainers) {
processContainedIn(entity, queue, rootPropertiesMetadata, searchFactory);
}
}
|