Fixed ALF-4676: WorkProviderIterator over BatchProcessWorkProvider does not fetch all results

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@22297 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Derek Hulley
2010-09-07 14:10:59 +00:00
parent 9e87100a6b
commit 494db3841a
2 changed files with 44 additions and 17 deletions

View File

@@ -336,9 +336,8 @@ public class AuthorityMigrationPatch extends AbstractPatch
return authNodeRef; return authNodeRef;
} }
/* /**
* (non-Javadoc) * TODO: The walking of the group associations should be wrapped up in a BatchProcessWorkProvider, if possible
* @see org.alfresco.repo.admin.patch.AbstractPatch#applyInternal()
*/ */
@Override @Override
protected String applyInternal() throws Exception protected String applyInternal() throws Exception

View File

@@ -24,6 +24,7 @@ import java.io.Writer;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@@ -141,13 +142,23 @@ public class BatchProcessor<T> implements BatchMonitor
retryingTransactionHelper, retryingTransactionHelper,
new BatchProcessWorkProvider<T>() new BatchProcessWorkProvider<T>()
{ {
boolean hasMore = true;
public int getTotalEstimatedWorkSize() public int getTotalEstimatedWorkSize()
{ {
return collection.size(); return collection.size();
} }
public Collection<T> getNextWork() public Collection<T> getNextWork()
{ {
return collection; // Only return the collection once
if (hasMore)
{
hasMore = false;
return collection;
}
else
{
return Collections.emptyList();
}
} }
}, },
workerThreads, batchSize, workerThreads, batchSize,
@@ -541,27 +552,44 @@ public class BatchProcessor<T> implements BatchMonitor
public boolean hasNext() public boolean hasNext()
{ {
if (currentIterator == null) boolean hasNext = false;
if (workProvider == null)
{ {
if (workProvider != null) // The workProvider was exhausted
hasNext = false;
}
else
{
if (currentIterator != null)
{
// See if there there is any more on this specific iterator
hasNext = currentIterator.hasNext();
}
// If we don't have a next (remember that the workProvider is still available)
// go and get more results
if (!hasNext)
{ {
Collection<T> nextWork = workProvider.getNextWork(); Collection<T> nextWork = workProvider.getNextWork();
if (nextWork == null) if (nextWork == null)
{ {
throw new RuntimeException("BatchProcessWorkProvider returned 'null' work: " + workProvider); throw new RuntimeException("BatchProcessWorkProvider returned 'null' work: " + workProvider);
} }
currentIterator = nextWork.iterator(); // Check that there are some results at all
if (nextWork.size() == 0)
{
// An empty collection indicates that there are no more results
workProvider = null;
currentIterator = null;
hasNext = false;
}
else
{
// There were some results, so get a new iterator
currentIterator = nextWork.iterator();
hasNext = currentIterator.hasNext();
}
} }
else
{
// The null workProvider indicates that it was exhausted
}
}
boolean hasNext = (currentIterator == null) ? false : currentIterator.hasNext();
if (!hasNext)
{
workProvider = null; // No more work to get
currentIterator = null;
} }
return hasNext; return hasNext;
} }