Tony CHEMIT pushed to branch develop at ultreiaio / ird-t3
Commits:
-
772a1f75
by Tony CHEMIT at 2018-03-23T17:00:07Z
7 changed files:
- pom.xml
- t3-actions/src/main/java/fr/ird/t3/actions/data/level2/Level2InputContext.java
- t3-domain/src/main/java/fr/ird/t3/actions/stratum/LevelInputContext.java
- t3-domain/src/main/java/fr/ird/t3/actions/stratum/LevelStratumConfigurationIterator.java
- t3-domain/src/main/java/fr/ird/t3/actions/stratum/StratumConfiguration.java
- t3-domain/src/main/java/fr/ird/t3/entities/T3TopiaPersistenceContext.java
- + t3-domain/src/main/java/fr/ird/t3/entities/cache/ActivityCache.java
Changes:
| ... | ... | @@ -99,7 +99,7 @@ |
| 99 | 99 |
<projectId>ird-t3</projectId>
|
| 100 | 100 |
<stageId>frirdt3</stageId>
|
| 101 | 101 |
<applicationName>t3</applicationName>
|
| 102 |
- <t3-data.version>1.6-SNAPSHOT</t3-data.version>
|
|
| 102 |
+ <t3-data.version>1.6</t3-data.version>
|
|
| 103 | 103 |
<lib.version.java4all.i18n>4.0-alpha-11</lib.version.java4all.i18n>
|
| 104 | 104 |
|
| 105 | 105 |
<env>dev</env>
|
| ... | ... | @@ -25,7 +25,6 @@ import fr.ird.t3.actions.stratum.LevelInputContext; |
| 25 | 25 |
import fr.ird.t3.entities.data.CorrectedElementaryCatch;
|
| 26 | 26 |
|
| 27 | 27 |
import java.util.Collection;
|
| 28 |
-import java.util.concurrent.ExecutionException;
|
|
| 29 | 28 |
|
| 30 | 29 |
/**
|
| 31 | 30 |
* A context to get all input data for the execution of action.
|
| ... | ... | @@ -40,8 +39,8 @@ public class Level2InputContext extends LevelInputContext<Level2Configuration, L |
| 40 | 39 |
super(action);
|
| 41 | 40 |
}
|
| 42 | 41 |
|
| 43 |
- public Collection<CorrectedElementaryCatch> getCorrectedElementaryCatch(String activityId) throws ExecutionException {
|
|
| 44 |
- return getActivityCache().get(activityId).getCorrectedElementaryCatch();
|
|
| 42 |
+ public Collection<CorrectedElementaryCatch> getCorrectedElementaryCatch(String activityId) {
|
|
| 43 |
+ return getActivityCache().forId(activityId).getCorrectedElementaryCatch();
|
|
| 45 | 44 |
}
|
| 46 | 45 |
|
| 47 | 46 |
}
|
| ... | ... | @@ -21,12 +21,10 @@ package fr.ird.t3.actions.stratum; |
| 21 | 21 |
* #L%
|
| 22 | 22 |
*/
|
| 23 | 23 |
|
| 24 |
-import com.google.common.cache.CacheBuilder;
|
|
| 25 |
-import com.google.common.cache.CacheLoader;
|
|
| 26 |
-import com.google.common.cache.LoadingCache;
|
|
| 27 | 24 |
import fr.ird.t3.actions.T3Action;
|
| 28 | 25 |
import fr.ird.t3.entities.T3Predicates;
|
| 29 | 26 |
import fr.ird.t3.entities.T3TopiaPersistenceContext;
|
| 27 |
+import fr.ird.t3.entities.cache.ActivityCache;
|
|
| 30 | 28 |
import fr.ird.t3.entities.cache.LengthWeightConversionWithContextCache;
|
| 31 | 29 |
import fr.ird.t3.entities.cache.WeightCategoryTreatmentCache;
|
| 32 | 30 |
import fr.ird.t3.entities.cache.ZoneStratumCache;
|
| ... | ... | @@ -53,7 +51,6 @@ import fr.ird.t3.services.ioc.InjectFromDAO; |
| 53 | 51 |
import java.util.Collection;
|
| 54 | 52 |
import java.util.Date;
|
| 55 | 53 |
import java.util.List;
|
| 56 |
-import java.util.Objects;
|
|
| 57 | 54 |
import java.util.Set;
|
| 58 | 55 |
import java.util.function.Predicate;
|
| 59 | 56 |
import java.util.function.Supplier;
|
| ... | ... | @@ -70,7 +67,7 @@ public abstract class LevelInputContext<C extends LevelConfigurationWithStratum, |
| 70 | 67 |
/**
|
| 71 | 68 |
* Cache of activity (to avoid to reload them for catch and sample stratum).
|
| 72 | 69 |
*/
|
| 73 |
- private final LoadingCache<String, Activity> activityCache;
|
|
| 70 |
+ private final ActivityCache activityCache;
|
|
| 74 | 71 |
/**
|
| 75 | 72 |
* Cache of {@link WeightCategoryTreatment}.
|
| 76 | 73 |
*/
|
| ... | ... | @@ -155,14 +152,8 @@ public abstract class LevelInputContext<C extends LevelConfigurationWithStratum, |
| 155 | 152 |
} catch (Exception e) {
|
| 156 | 153 |
throw new IllegalStateException("can't inject to " + this, e);
|
| 157 | 154 |
}
|
| 158 |
- this.activityCache = CacheBuilder.newBuilder().build(
|
|
| 159 |
- new CacheLoader<String, Activity>() {
|
|
| 160 |
- @Override
|
|
| 161 |
- public Activity load(@SuppressWarnings("NullableProblems") String key) {
|
|
| 162 |
- return activityDAO.forTopiaIdEquals(Objects.requireNonNull(key)).findUnique();
|
|
| 163 |
- }
|
|
| 164 |
- });
|
|
| 165 | 155 |
T3TopiaPersistenceContext persistenceContext = action.getT3TopiaPersistenceContext().get();
|
| 156 |
+ this.activityCache = persistenceContext.newActivityCache();
|
|
| 166 | 157 |
this.weightCategoryCache = persistenceContext.newWeightCategoryTreatmentCache();
|
| 167 | 158 |
C configuration = action.getConfiguration();
|
| 168 | 159 |
configuration.setLocale(action.getLocale());
|
| ... | ... | @@ -216,7 +207,7 @@ public abstract class LevelInputContext<C extends LevelConfigurationWithStratum, |
| 216 | 207 |
return nbStratum;
|
| 217 | 208 |
}
|
| 218 | 209 |
|
| 219 |
- public LoadingCache<String, Activity> getActivityCache() {
|
|
| 210 |
+ public ActivityCache getActivityCache() {
|
|
| 220 | 211 |
return activityCache;
|
| 221 | 212 |
}
|
| 222 | 213 |
|
| ... | ... | @@ -281,6 +272,6 @@ public abstract class LevelInputContext<C extends LevelConfigurationWithStratum, |
| 281 | 272 |
}
|
| 282 | 273 |
|
| 283 | 274 |
public void clear() {
|
| 284 |
- activityCache.invalidateAll();
|
|
| 275 |
+ activityCache.clear();
|
|
| 285 | 276 |
}
|
| 286 | 277 |
}
|
| ... | ... | @@ -119,6 +119,8 @@ public abstract class LevelStratumConfigurationIterator<C extends LevelConfigura |
| 119 | 119 |
}
|
| 120 | 120 |
|
| 121 | 121 |
protected void nextSchoolType() {
|
| 122 |
+ // each time we change school type, we are sure to not cross again a previous loaded activity, so can clear cache
|
|
| 123 |
+ inputContext.getActivityCache().clear();
|
|
| 122 | 124 |
currentSchoolType = schoolTypeIterator.next();
|
| 123 | 125 |
currentZones = inputContext.getZones(currentSchoolType);
|
| 124 | 126 |
currentWeightCategoryTreatments = inputContext.getWeightCategoryTreatments(currentSchoolType);
|
| ... | ... | @@ -39,7 +39,6 @@ import java.util.List; |
| 39 | 39 |
import java.util.Map;
|
| 40 | 40 |
import java.util.Objects;
|
| 41 | 41 |
import java.util.Set;
|
| 42 |
-import java.util.concurrent.ExecutionException;
|
|
| 43 | 42 |
import java.util.function.BiConsumer;
|
| 44 | 43 |
import java.util.function.Function;
|
| 45 | 44 |
import java.util.function.Predicate;
|
| ... | ... | @@ -185,11 +184,7 @@ public class StratumConfiguration<C extends LevelConfigurationWithStratum, A ext |
| 185 | 184 |
}
|
| 186 | 185 |
|
| 187 | 186 |
public Activity getActivity(String activityId) {
|
| 188 |
- try {
|
|
| 189 |
- return inputContext.getActivityCache().get(activityId);
|
|
| 190 |
- } catch (ExecutionException e) {
|
|
| 191 |
- throw new RuntimeException(String.format("Could not obtain activity %s", activityId), e);
|
|
| 192 |
- }
|
|
| 187 |
+ return inputContext.getActivityCache().forId(activityId);
|
|
| 193 | 188 |
}
|
| 194 | 189 |
|
| 195 | 190 |
@Override
|
| ... | ... | @@ -21,6 +21,7 @@ package fr.ird.t3.entities; |
| 21 | 21 |
* #L%
|
| 22 | 22 |
*/
|
| 23 | 23 |
|
| 24 |
+import fr.ird.t3.entities.cache.ActivityCache;
|
|
| 24 | 25 |
import fr.ird.t3.entities.cache.LengthWeightConversionCache;
|
| 25 | 26 |
import fr.ird.t3.entities.cache.LengthWeightConversionWithContextCache;
|
| 26 | 27 |
import fr.ird.t3.entities.cache.WeightCategoryTreatmentCache;
|
| ... | ... | @@ -37,6 +38,10 @@ public class T3TopiaPersistenceContext extends AbstractT3TopiaPersistenceContext |
| 37 | 38 |
super(parameter);
|
| 38 | 39 |
}
|
| 39 | 40 |
|
| 41 |
+ public ActivityCache newActivityCache() {
|
|
| 42 |
+ return new ActivityCache(getActivityDao());
|
|
| 43 |
+ }
|
|
| 44 |
+ |
|
| 40 | 45 |
public WeightCategoryTreatmentCache newWeightCategoryTreatmentCache() {
|
| 41 | 46 |
return new WeightCategoryTreatmentCache(getWeightCategoryTreatmentDao());
|
| 42 | 47 |
}
|
| 1 |
+/*
|
|
| 2 |
+ * #%L
|
|
| 3 |
+ * T3 :: Domain
|
|
| 4 |
+ * %%
|
|
| 5 |
+ * Copyright (C) 2010 - 2018 IRD, Code Lutin, Ultreia.io
|
|
| 6 |
+ * %%
|
|
| 7 |
+ * This program is free software: you can redistribute it and/or modify
|
|
| 8 |
+ * it under the terms of the GNU Affero General Public License as published by
|
|
| 9 |
+ * the Free Software Foundation, either version 3 of the License, or
|
|
| 10 |
+ * (at your option) any later version.
|
|
| 11 |
+ *
|
|
| 12 |
+ * This program is distributed in the hope that it will be useful,
|
|
| 13 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
| 14 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
| 15 |
+ * GNU General Public License for more details.
|
|
| 16 |
+ *
|
|
| 17 |
+ * You should have received a copy of the GNU Affero General Public License
|
|
| 18 |
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
| 19 |
+ * #L%
|
|
| 20 |
+ */
|
|
| 21 |
+package fr.ird.t3.entities.cache;
|
|
| 22 |
+ |
|
| 23 |
+import fr.ird.t3.entities.data.Activity;
|
|
| 24 |
+import fr.ird.t3.entities.data.ActivityTopiaDao;
|
|
| 25 |
+import org.apache.commons.logging.Log;
|
|
| 26 |
+import org.apache.commons.logging.LogFactory;
|
|
| 27 |
+ |
|
| 28 |
+import java.util.Map;
|
|
| 29 |
+import java.util.TreeMap;
|
|
| 30 |
+ |
|
| 31 |
+/**
|
|
| 32 |
+ * Created by tchemit on 23/03/2018.
|
|
| 33 |
+ *
|
|
| 34 |
+ * @author Tony Chemit - dev@tchemit.fr
|
|
| 35 |
+ */
|
|
| 36 |
+public class ActivityCache {
|
|
| 37 |
+ |
|
| 38 |
+ private static final Log log = LogFactory.getLog(ActivityCache.class);
|
|
| 39 |
+ private final Map<String, Activity> cache;
|
|
| 40 |
+ private final ActivityTopiaDao activityDAO;
|
|
| 41 |
+ |
|
| 42 |
+ public ActivityCache(ActivityTopiaDao activityDAO) {
|
|
| 43 |
+ this.activityDAO = activityDAO;
|
|
| 44 |
+ this.cache = new TreeMap<>();
|
|
| 45 |
+ }
|
|
| 46 |
+ |
|
| 47 |
+ public Activity forId(String activityId) {
|
|
| 48 |
+ Activity result = cache.get(activityId);
|
|
| 49 |
+ if (result == null) {
|
|
| 50 |
+ log.info(String.format("Cache Activity [%d] for %s", cache.size(), activityId));
|
|
| 51 |
+ cache.put(activityId, result = activityDAO.forTopiaIdEquals(activityId).findUnique());
|
|
| 52 |
+ }
|
|
| 53 |
+ return result;
|
|
| 54 |
+ }
|
|
| 55 |
+ |
|
| 56 |
+ public void clear() {
|
|
| 57 |
+ cache.clear();
|
|
| 58 |
+ }
|
|
| 59 |
+}
|