Skip to content

Commit 0cd718a

Browse files
committed
Handle disposable table sources
1 parent 8856199 commit 0cd718a

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

mica-core/src/main/java/org/obiba/mica/core/service/StudyTableSourceServiceRegistry.java

+22-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212

1313
import com.google.common.cache.Cache;
1414
import com.google.common.cache.CacheBuilder;
15+
import com.google.common.cache.RemovalListener;
1516
import org.apache.commons.math3.util.Pair;
1617
import org.obiba.magma.NoSuchValueTableException;
18+
import org.obiba.magma.support.Disposables;
1719
import org.obiba.mica.core.source.ExcelTableSource;
1820
import org.obiba.mica.core.source.OpalTableSource;
1921
import org.obiba.mica.dataset.domain.StudyDataset;
@@ -29,6 +31,8 @@
2931
import org.slf4j.LoggerFactory;
3032
import org.springframework.stereotype.Service;
3133

34+
import javax.annotation.PostConstruct;
35+
import javax.annotation.PreDestroy;
3236
import javax.inject.Inject;
3337
import java.io.InputStream;
3438
import java.util.NoSuchElementException;
@@ -56,12 +60,28 @@ public class StudyTableSourceServiceRegistry {
5660
@Inject
5761
private FileStoreService fileStoreService;
5862

59-
private Cache<String, StudyTableSource> sourcesCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(1, TimeUnit.MINUTES).build();
63+
private Cache<String, StudyTableSource> sourcesCache;
64+
65+
@PostConstruct
66+
public void initialize() {
67+
sourcesCache = CacheBuilder.newBuilder()
68+
.maximumSize(1000)
69+
.expireAfterWrite(1, TimeUnit.MINUTES)
70+
.removalListener((RemovalListener<String, StudyTableSource>) notification -> {
71+
Disposables.silentlyDispose(notification.getValue());
72+
})
73+
.build();
74+
}
75+
76+
@PreDestroy
77+
public void close() {
78+
sourcesCache.cleanUp();
79+
}
6080

6181
public synchronized StudyTableSource makeStudyTableSource(IDataset dataset, IStudy study, String source) {
6282
StudyTableContext context = new StudyTableContext(dataset, study, micaConfigService.getConfig().getPrivacyThreshold());
6383

64-
String cacheKey = String.format("%s::%s", study.getId(), source);
84+
String cacheKey = String.format("%s::%s::%s", dataset.getId(), study.getId(), source);
6585
try {
6686
return sourcesCache.get(cacheKey, () -> makeStudyTableSourceInternal(context, source));
6787
} catch (ExecutionException e) {

mica-core/src/main/java/org/obiba/mica/dataset/service/CollectedDatasetService.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.obiba.magma.NoSuchValueTableException;
1919
import org.obiba.magma.NoSuchVariableException;
2020
import org.obiba.magma.ValueTable;
21+
import org.obiba.magma.support.Disposables;
2122
import org.obiba.mica.NoSuchEntityException;
2223
import org.obiba.mica.core.domain.AbstractGitPersistable;
2324
import org.obiba.mica.core.domain.PublishCascadingScope;
@@ -431,14 +432,18 @@ public DatasetVariable getDatasetVariable(StudyDataset dataset, String variableN
431432
public Mica.DatasetVariableAggregationDto getVariableSummary(@NotNull StudyDataset dataset, String variableName) {
432433
log.info("Caching variable summary {} {}", dataset.getId(), variableName);
433434
StudyTableSource tableSource = getStudyTableSource(dataset, dataset.getSafeStudyTable());
434-
return tableSource.providesVariableSummary() ? tableSource.getVariableSummary(variableName) : null;
435+
Mica.DatasetVariableAggregationDto summary = tableSource.providesVariableSummary() ? tableSource.getVariableSummary(variableName) : null;
436+
Disposables.silentlyDispose(tableSource);
437+
return summary;
435438
}
436439

437440
public Mica.DatasetVariableContingencyDto getContingencyTable(@NotNull StudyDataset dataset, DatasetVariable variable,
438441
DatasetVariable crossVariable) throws NoSuchValueTableException, NoSuchVariableException {
439442

440443
StudyTableSource tableSource = getStudyTableSource(dataset, dataset.getSafeStudyTable());
441-
return tableSource.providesContingency() ? tableSource.getContingency(variable, crossVariable) : null;
444+
Mica.DatasetVariableContingencyDto results = tableSource.providesContingency() ? tableSource.getContingency(variable, crossVariable) : null;
445+
Disposables.silentlyDispose(tableSource);
446+
return results;
442447
}
443448

444449
public void delete(String id) {

mica-core/src/main/java/org/obiba/mica/dataset/service/HarmonizedDatasetService.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.google.common.collect.Sets;
1717
import com.google.common.eventbus.EventBus;
1818
import org.obiba.magma.*;
19+
import org.obiba.magma.support.Disposables;
1920
import org.obiba.mica.NoSuchEntityException;
2021
import org.obiba.mica.core.domain.BaseStudyTable;
2122
import org.obiba.mica.core.domain.PublishCascadingScope;
@@ -383,7 +384,9 @@ public Mica.DatasetVariableAggregationDto getVariableSummary(@NotNull Harmonizat
383384
if(baseTable.isFor(studyId, source)) {
384385
log.info("Caching variable summary {} {} {} {}", dataset.getId(), variableName, studyId, source);
385386
StudyTableSource tableSource = getStudyTableSource(dataset, baseTable);
386-
return tableSource.providesVariableSummary() ? tableSource.getVariableSummary(variableName) : null;
387+
Mica.DatasetVariableAggregationDto summary = tableSource.providesVariableSummary() ? tableSource.getVariableSummary(variableName) : null;
388+
Disposables.silentlyDispose(tableSource);
389+
return summary;
387390
}
388391
}
389392

@@ -393,7 +396,9 @@ public Mica.DatasetVariableAggregationDto getVariableSummary(@NotNull Harmonizat
393396
public Mica.DatasetVariableContingencyDto getContingencyTable(@NotNull HarmonizationDataset dataset, @NotNull BaseStudyTable studyTable, DatasetVariable variable,
394397
DatasetVariable crossVariable) throws NoSuchStudyException, NoSuchValueTableException {
395398
StudyTableSource tableSource = getStudyTableSource(dataset, studyTable);
396-
return tableSource.providesContingency() ? tableSource.getContingency(variable, crossVariable) : null;
399+
Mica.DatasetVariableContingencyDto results = tableSource.providesContingency() ? tableSource.getContingency(variable, crossVariable) : null;
400+
Disposables.silentlyDispose(tableSource);
401+
return results;
397402
}
398403

399404
@Override

0 commit comments

Comments
 (0)