Author: bpoussin Date: 2012-08-31 20:17:43 +0200 (Fri, 31 Aug 2012) New Revision: 3754 Url: http://forge.codelutin.com/repositories/revision/isis-fish/3754 Log: avec un peu de cache, ca va beaucoup mieux :) 13min -> 50s y'a plus qu'a verifier reellement les resultats :) Modified: trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultMappedStorage.java Modified: trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultMappedStorage.java =================================================================== --- trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultMappedStorage.java 2012-08-31 16:16:11 UTC (rev 3753) +++ trunk/src/main/java/fr/ifremer/isisfish/datastore/ResultMappedStorage.java 2012-08-31 18:17:43 UTC (rev 3754) @@ -27,6 +27,7 @@ import fr.ifremer.isisfish.IsisFishDAOHelper; import fr.ifremer.isisfish.IsisFishException; +import fr.ifremer.isisfish.IsisFishRuntimeException; import fr.ifremer.isisfish.entities.ActiveRule; import fr.ifremer.isisfish.entities.ActiveRuleDAO; import fr.ifremer.isisfish.entities.Population; @@ -47,11 +48,14 @@ import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; +import org.apache.commons.collections.BidiMap; +import org.apache.commons.collections.bidimap.DualHashBidiMap; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -109,6 +113,8 @@ static final private String SEP = ":"; protected TopiaContext tx; + /** en cle les representation interne (get) en valeur les valeurs decoree (getKey) */ + protected BidiMap cache = new DualHashBidiMap(); public EntitySemanticsDecorator() { } @@ -118,47 +124,54 @@ } public Object decorate(Object internalValue) { - Object result = internalValue; - if (internalValue instanceof String) { - if (StringUtils.startsWith((String)internalValue, Month.class.getName())) { - String val = StringUtils.substringAfter((String)internalValue, SEP); - int monthNumber = Integer.parseInt(val); - result = new Month(monthNumber); - } else if (StringUtils.startsWith((String)internalValue, TimeStep.class.getName())) { - String val = StringUtils.substringAfter((String)internalValue, SEP); - int stepNumber = Integer.parseInt(val); - result = new TimeStep(stepNumber); - } else if (StringUtils.startsWith((String)internalValue, "fr.ifremer.isisfish.entities.")) { - if (tx == null) { - result = StringUtils.substringAfter((String)internalValue, SEP); - } else { - try { - String id = StringUtils.substringBefore((String)internalValue, SEP); - result = tx.findByTopiaId(id); - } catch (TopiaException eee) { - log.info("Fallback use string representation because" - + " i can't decorate (String->Entity): " - + internalValue, eee); - // si on arrive pas a convertir cette fois-ci, on - // renvoi internalValue pour que le undecorate est toutes les infos - // et donc que la prochaine fois on y arrive peut etre + Object result = cache.get(internalValue); + if (result == null && internalValue != null) { + result = internalValue; + if (internalValue instanceof String) { + if (StringUtils.startsWith((String)internalValue, Month.class.getName())) { + String val = StringUtils.substringAfter((String)internalValue, SEP); + int monthNumber = Integer.parseInt(val); + result = new Month(monthNumber); + } else if (StringUtils.startsWith((String)internalValue, TimeStep.class.getName())) { + String val = StringUtils.substringAfter((String)internalValue, SEP); + int stepNumber = Integer.parseInt(val); + result = new TimeStep(stepNumber); + } else if (StringUtils.startsWith((String)internalValue, "fr.ifremer.isisfish.entities.")) { + if (tx == null) { + result = StringUtils.substringAfter((String)internalValue, SEP); + } else { + try { + String id = StringUtils.substringBefore((String)internalValue, SEP); + result = tx.findByTopiaId(id); + } catch (TopiaException eee) { + log.info("Fallback use string representation because" + + " i can't decorate (String->Entity): " + + internalValue, eee); + // si on arrive pas a convertir cette fois-ci, on + // renvoi internalValue pour que le undecorate est toutes les infos + // et donc que la prochaine fois on y arrive peut etre + } } } } + cache.put(internalValue, result); } return result; } public Object undecorate(Object decoratedValue) { - String result; - if (decoratedValue instanceof Month) { - result = Month.class.getName() + SEP + ((Month)decoratedValue).getMonthNumber(); - } else if (decoratedValue instanceof TimeStep) { - result = TimeStep.class.getName() + SEP + ((TimeStep)decoratedValue).getStep(); - } else if (decoratedValue instanceof TopiaEntity) { - result = ((TopiaEntity)decoratedValue).getTopiaId() + SEP + decoratedValue; - } else { - result = String.valueOf(decoratedValue); + Object result = cache.getKey(decoratedValue); + if (result == null && decoratedValue != null) { + if (decoratedValue instanceof Month) { + result = Month.class.getName() + SEP + ((Month)decoratedValue).getMonthNumber(); + } else if (decoratedValue instanceof TimeStep) { + result = TimeStep.class.getName() + SEP + ((TimeStep)decoratedValue).getStep(); + } else if (decoratedValue instanceof TopiaEntity) { + result = ((TopiaEntity)decoratedValue).getTopiaId() + SEP + decoratedValue; + } else { + result = String.valueOf(decoratedValue); + } + cache.put(result, decoratedValue); } return result; } @@ -575,6 +588,19 @@ return getMatrix(step, newName, null); } + /** + * Retourne la matrice stocke pour un pas de temps + * @param step le pas de temps que l'on souhaite + * @param pop la population pour lequelle on souhaite le resultat + * @param name le nom des resultats dont on veut la matrice + * @return La matrice demandée ou null si aucune matrice ne correspond a + * la demande. + */ + public MatrixND getMatrix(TimeStep step, Population pop, String name, TopiaContext tx) { + String newName = name + " " + pop; + return getMatrix(step, newName, tx); + } + public MatrixND getMatrix(TimeStep step, String name) { return getMatrix(step, name, null); } @@ -588,6 +614,7 @@ ResultMapped r = getResult(step, name); MatrixND mat = null; if (r != null) { + tx = getTx(tx); mat = r.getMatrix(tx); } return mat; @@ -704,6 +731,7 @@ if (resultMat != null) { // on decore la matrice resultat au dernier moment, tous les calcules // ce font avec les strings + tx = getTx(tx); resultMat = new MatrixSemanticsDecorator(resultMat, new EntitySemanticsDecorator(tx)); } return resultMat; @@ -763,6 +791,29 @@ return result; } + /** + * Try to find better tx. If argument is not null, return it. otherwize + * try to get tx in SimulationContext. + * + * @param tx + * @return + */ + protected TopiaContext getTx(TopiaContext tx) { + TopiaContext result = tx; + if (tx == null) { + // si on a pas de tx, on recherche si on est dans une simulation + // pour recuperer la tx de la simulation + if (simulation == SimulationContext.get().getSimulationStorage()) { + try { + result = SimulationContext.get().getDB(); + } catch (TopiaException eee) { + throw new IsisFishRuntimeException("Can't get database from SimulationContext", eee); + } + } + } + return result; + } + /* * @see fr.ifremer.isisfish.simulator.SimulationListener#afterSimulation(fr.ifremer.isisfish.simulator.SimulationContext) */
participants (1)
-
bpoussin@users.forge.codelutin.com