Skip to content

Commit

Permalink
HSEARCH-540 Avoid performing the queue twice when nothing has changed
Browse files Browse the repository at this point in the history
The double registration leads to double processing of the queue which can be avoided by a flag.

git-svn-id: https://svn.jboss.org/repos/hibernate/search/branches/Branch_3_2@19976 1b8cb986-b30d-0410-93ca-fae66ebed9b2
  • Loading branch information
emmanuelbernard committed Jul 20, 2010
1 parent b5b4e41 commit 64e8f38
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
Expand Up @@ -42,8 +42,10 @@ public class WorkQueue {
private List<Work> queue;

private List<LuceneWork> sealedQueue;
//is this class supposed to be
//flag indicating if the sealed data has be provided meaning that it should no longer be modified
private boolean usedSealedData;
//flag indicating if data has been sealed and not modified since
private boolean sealedAndUnchanged;

public WorkQueue(int size) {
queue = new ArrayList<Work>(size);
Expand All @@ -53,6 +55,10 @@ private WorkQueue(List<Work> queue) {
this.queue = queue;
}

public boolean isSealedAndUnchanged() {
return sealedAndUnchanged;
}

public WorkQueue() {
this(10);
}
Expand All @@ -62,6 +68,7 @@ public void add(Work work) {
//something is wrong fail with exception
throw new AssertionFailure( "Attempting to add a work in a used sealed queue" );
}
this.sealedAndUnchanged = false;
queue.add(work);
}

Expand All @@ -72,12 +79,13 @@ public List<Work> getQueue() {
public WorkQueue splitQueue() {
WorkQueue subQueue = new WorkQueue( queue );
this.queue = new ArrayList<Work>( queue.size() );
this.sealedAndUnchanged = false;
return subQueue;
}

public List<LuceneWork> getSealedQueue() {
if (sealedQueue == null) throw new AssertionFailure("Access a Sealed WorkQueue which has not been sealed");
usedSealedData = true;
this.sealedAndUnchanged = false;
return sealedQueue;
}

Expand All @@ -93,11 +101,13 @@ public void setSealedQueue(List<LuceneWork> sealedQueue) {
* when the flush ordering is fixed, add the following line
* queue = Collections.EMPTY_LIST;
*/
this.sealedAndUnchanged = true;
this.sealedQueue = sealedQueue;
}

public void clear() {
queue.clear();
this.sealedAndUnchanged = false;
if (sealedQueue != null) sealedQueue.clear();
}

Expand Down
Expand Up @@ -134,19 +134,22 @@ public void add(Work work, WorkQueue workQueue) {
}

public void prepareWorks(WorkQueue workQueue) {
List<Work> queue = workQueue.getQueue();
int initialSize = queue.size();
List<LuceneWork> luceneQueue = new ArrayList<LuceneWork>( initialSize ); //TODO load factor for containedIn
/**
* Collection work type are process second, so if the owner entity has already been processed for whatever reason
* the work will be ignored.
* However if the owner entity has not been processed, an "UPDATE" work is executed
*
* Processing collection works last is mandatory to avoid reindexing a object to be deleted
*/
processWorkByLayer( queue, initialSize, luceneQueue, Layer.FIRST );
processWorkByLayer( queue, initialSize, luceneQueue, Layer.SECOND );
workQueue.setSealedQueue( optimize( luceneQueue ) );
final boolean alreadyProcessedAndUnchanged = workQueue.isSealedAndUnchanged();
if ( !alreadyProcessedAndUnchanged ) {
List<Work> queue = workQueue.getQueue();
int initialSize = queue.size();
List<LuceneWork> luceneQueue = new ArrayList<LuceneWork>( initialSize ); //TODO load factor for containedIn
/**
* Collection work type are process second, so if the owner entity has already been processed for whatever reason
* the work will be ignored.
* However if the owner entity has not been processed, an "UPDATE" work is executed
*
* Processing collection works last is mandatory to avoid reindexing a object to be deleted
*/
processWorkByLayer( queue, initialSize, luceneQueue, Layer.FIRST );
processWorkByLayer( queue, initialSize, luceneQueue, Layer.SECOND );
workQueue.setSealedQueue( optimize( luceneQueue ) );
}
}

private List<LuceneWork> optimize(List<LuceneWork> luceneQueue) {
Expand Down

0 comments on commit 64e8f38

Please sign in to comment.