X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=source%2Fmir%2Fproducer%2FEntityBatchingProducerNode.java;h=e12bd79eb617a1086a48399d5f7ba03acf8c6fd0;hb=5757f37a1bc4e2dcad3054a66611b1a02fb7d8b3;hp=446404d6075086e3de7c297d2f5da8469e8d056c;hpb=f9a7655d20e23418320dd552f257893f25268d16;p=mir.git diff --git a/source/mir/producer/EntityBatchingProducerNode.java b/source/mir/producer/EntityBatchingProducerNode.java index 446404d6..e12bd79e 100755 --- a/source/mir/producer/EntityBatchingProducerNode.java +++ b/source/mir/producer/EntityBatchingProducerNode.java @@ -1,4 +1,4 @@ -/*package mir.producer; +package mir.producer; import java.util.*; import java.io.*; @@ -7,71 +7,198 @@ import mir.entity.*; import mir.storage.*; import mir.util.*; -// abstract public int getSize(String where) throws SQLException,StorageObjectException; - - public class EntityBatchingProducerNode implements ProducerNode { private Map verbs; - private EntityBatchingProducerNodeVerb defaultVerb; - private String key; - private StorageObject storage; - private EntityAdapterDefinition definition; + private String batchInfoKey; + private String batchDataKey; + private EntityAdapterModel model; + private String definition; private String whereClause; private String orderByClause; - private int nrArticlesPerBatch; - private int minNrArticlesInFirstBatch; + private String nrEntitiesToSkipExpression; + private String nrEntitiesPerBatchExpression; + private String minNrEntitiesInFirstBatchExpression; + private String defaultNrBatchesToProcessExpression; private ProducerNode batchSubNode; private ProducerNode batchListSubNode; public EntityBatchingProducerNode( - String aKey, - StorageObject aStorage, - EntityAdapterDefinition aDefinition, + String aBatchDataKey, + String aBatchInfoKey, + EntityAdapterModel aModel, + String aDefinition, String aWhereClause, String anOrderByClause, - int aNrArticlesPerBatch, - int aMinNrArticlesInFirstBatch, + int anrEntitiesPerBatchExpression, + int aminNrEntitiesInFirstBatchExpression, + int anrEntitiesToSkipExpression, + ProducerNode aBatchSubNode) { + this(aBatchDataKey, aBatchInfoKey, aModel, aDefinition, aWhereClause, + anOrderByClause, anrEntitiesPerBatchExpression, aminNrEntitiesInFirstBatchExpression, anrEntitiesToSkipExpression, + aBatchSubNode, null); + } + + public EntityBatchingProducerNode( + String aBatchDataKey, + String aBatchInfoKey, + EntityAdapterModel aModel, + String aDefinition, + String aWhereClause, + String anOrderByClause, + int aNrEntitiesPerBatch, + int aMinNrEntitiesInFirstBatch, + int aNrEntitiesToSkip, + ProducerNode aBatchSubNode, + ProducerNode aBatchListSubNode) { + + this(aBatchDataKey, aBatchInfoKey, aModel, aDefinition, + aWhereClause, anOrderByClause, + Integer.toString(aNrEntitiesPerBatch), + Integer.toString(aMinNrEntitiesInFirstBatch), + Integer.toString(aNrEntitiesToSkip), + "1", + aBatchSubNode, aBatchListSubNode); + + } + + public EntityBatchingProducerNode( + String aBatchDataKey, + String aBatchInfoKey, + EntityAdapterModel aModel, + String aDefinition, + String aWhereClause, + String anOrderByClause, + String anrEntitiesPerBatchExpression, + String aminNrEntitiesInFirstBatchExpression, + String anrEntitiesToSkipExpression, + String aDefaultNrBatchesToProcessExpression, ProducerNode aBatchSubNode, ProducerNode aBatchListSubNode) { batchSubNode = aBatchSubNode; batchListSubNode = aBatchListSubNode; - defaultVerb = null; verbs = new HashMap(); - key = aKey; - storage = aStorage; + batchDataKey = aBatchDataKey; + batchInfoKey = aBatchInfoKey; + model = aModel; definition = aDefinition; whereClause = aWhereClause; orderByClause = anOrderByClause; - nrArticlesPerBatch = aNrArticlesPerBatch; - minNrArticlesInFirstBatch = aMinNrArticlesInFirstBatch; + nrEntitiesToSkipExpression = anrEntitiesToSkipExpression; + nrEntitiesPerBatchExpression = anrEntitiesPerBatchExpression; + minNrEntitiesInFirstBatchExpression = aminNrEntitiesInFirstBatchExpression; + defaultNrBatchesToProcessExpression = aDefaultNrBatchesToProcessExpression; } public void produce(Map aValueMap, String aVerb, PrintWriter aLogger) throws ProducerFailure { - EntityBatchingProducerNodeVerb verb = (EntityBatchingProducerNodeVerb) verbs.get(aVerb); Iterator browser; + int nrEntities; + int nrBatchesAfterFirst; + int nrEntitiesInFirstBatch; + int nrBatchesToProcess; + List batchesData; + int i; + int position; + Map batchData; + String expandedWhereClause; + String expandedOrderByClause; + EntityBatchingProducerNodeVerb verb = (EntityBatchingProducerNodeVerb) verbs.get(aVerb); + + List batchLocations; + BatchLocation location; - if (verb==null) - verb = defaultVerb; + int nrEntitiesToSkip; + int nrEntitiesPerBatch; + int minNrEntitiesInFirstBatch; + +// ML: The order by clause should lead to a result set in _reverse order_: the first row will be +// the last entity presented on the last page - if (verb==null) - throw new ProducerFailure("EntityBatchingProducerNode: unknown verb '"+aVerb+"'", null); try { - browser = new EntityIteratorAdapter( - storage, - ParameterExpander.expandExpression( aValueMap, verb.whereClause ), - ParameterExpander.expandExpression( aValueMap, verb.orderByClause ), - -1, - definition ); - - while (browser.hasNext()) { - aLogger.println( verb.whereClause + ": next... "); - aValueMap.put(key, browser.next()); - super.produce(aValueMap, aVerb, aLogger); + if (verb==null) { + nrBatchesToProcess = ParameterExpander.evaluateIntegerExpressionWithDefault( aValueMap, defaultNrBatchesToProcessExpression, -1 ); + } + else { + nrBatchesToProcess=verb.nrBatchesToProcess; + } + + expandedWhereClause = ParameterExpander.expandExpression( aValueMap, whereClause ); + expandedOrderByClause = ParameterExpander.expandExpression( aValueMap, orderByClause ); + + nrEntitiesToSkip = ParameterExpander.evaluateIntegerExpression( aValueMap, nrEntitiesToSkipExpression); + nrEntitiesPerBatch = ParameterExpander.evaluateIntegerExpression( aValueMap, nrEntitiesPerBatchExpression); + minNrEntitiesInFirstBatch = ParameterExpander.evaluateIntegerExpression( aValueMap, minNrEntitiesInFirstBatchExpression); + + batchesData = new Vector(); + batchLocations = new Vector(); + + nrEntities = model.getMappingForName(definition).getStorage().getSize(expandedWhereClause)-nrEntitiesToSkip; + nrEntitiesInFirstBatch = nrEntities % nrEntitiesPerBatch; + while (nrEntitiesInFirstBatch=nrEntitiesPerBatch) + nrEntitiesInFirstBatch = nrEntitiesInFirstBatch + nrEntitiesPerBatch; + nrBatchesAfterFirst = (nrEntities-nrEntitiesInFirstBatch)/nrEntitiesPerBatch; + + batchLocations.add(new BatchLocation(nrBatchesAfterFirst*nrEntitiesPerBatch, nrEntitiesInFirstBatch)); + batchData = new HashMap(); + batchData.put("identifier", ""); + batchData.put("index", new Integer(nrBatchesAfterFirst+1)); + batchData.put("size", new Integer(nrEntitiesInFirstBatch)); + batchesData.add(batchData); + + for (i=0; inrBatchesAfterFirst+1) { + nrBatchesToProcess = nrBatchesAfterFirst+1; + } + + if (batchSubNode!=null) { + for (i=0; i0) + batchData.put("previous", batchesData.get(i-1)); + else + batchData.put("previous", null); + + if (i