Skip to content

Commit

Permalink
Handle disposable table sources
Browse files Browse the repository at this point in the history
  • Loading branch information
ymarcon committed Jan 24, 2023
1 parent 2dbc669 commit dd227ef
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.RemovalListener;
import org.apache.commons.math3.util.Pair;
import org.obiba.magma.NoSuchValueTableException;
import org.obiba.magma.support.Disposables;
import org.obiba.mica.core.source.ExcelTableSource;
import org.obiba.mica.core.source.OpalTableSource;
import org.obiba.mica.dataset.domain.StudyDataset;
Expand All @@ -29,6 +31,8 @@
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import java.io.InputStream;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -56,12 +60,28 @@ public class StudyTableSourceServiceRegistry {
@Inject
private FileStoreService fileStoreService;

private Cache<String, StudyTableSource> sourcesCache = CacheBuilder.newBuilder().maximumSize(1000).expireAfterWrite(1, TimeUnit.MINUTES).build();
private Cache<String, StudyTableSource> sourcesCache;

@PostConstruct
public void initialize() {
sourcesCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(1, TimeUnit.MINUTES)
.removalListener((RemovalListener<String, StudyTableSource>) notification -> {
Disposables.silentlyDispose(notification.getValue());
})
.build();
}

@PreDestroy
public void close() {
sourcesCache.cleanUp();
}

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

String cacheKey = String.format("%s::%s", study.getId(), source);
String cacheKey = String.format("%s::%s::%s", dataset.getId(), study.getId(), source);
try {
return sourcesCache.get(cacheKey, () -> makeStudyTableSourceInternal(context, source));
} catch (ExecutionException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.obiba.magma.NoSuchValueTableException;
import org.obiba.magma.NoSuchVariableException;
import org.obiba.magma.ValueTable;
import org.obiba.magma.support.Disposables;
import org.obiba.mica.NoSuchEntityException;
import org.obiba.mica.core.domain.AbstractGitPersistable;
import org.obiba.mica.core.domain.PublishCascadingScope;
Expand Down Expand Up @@ -431,14 +432,18 @@ public DatasetVariable getDatasetVariable(StudyDataset dataset, String variableN
public Mica.DatasetVariableAggregationDto getVariableSummary(@NotNull StudyDataset dataset, String variableName) {
log.info("Caching variable summary {} {}", dataset.getId(), variableName);
StudyTableSource tableSource = getStudyTableSource(dataset, dataset.getSafeStudyTable());
return tableSource.providesVariableSummary() ? tableSource.getVariableSummary(variableName) : null;
Mica.DatasetVariableAggregationDto summary = tableSource.providesVariableSummary() ? tableSource.getVariableSummary(variableName) : null;
Disposables.silentlyDispose(tableSource);
return summary;
}

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

StudyTableSource tableSource = getStudyTableSource(dataset, dataset.getSafeStudyTable());
return tableSource.providesContingency() ? tableSource.getContingency(variable, crossVariable) : null;
Mica.DatasetVariableContingencyDto results = tableSource.providesContingency() ? tableSource.getContingency(variable, crossVariable) : null;
Disposables.silentlyDispose(tableSource);
return results;
}

public void delete(String id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;
import org.obiba.magma.*;
import org.obiba.magma.support.Disposables;
import org.obiba.mica.NoSuchEntityException;
import org.obiba.mica.core.domain.BaseStudyTable;
import org.obiba.mica.core.domain.PublishCascadingScope;
Expand Down Expand Up @@ -383,7 +384,9 @@ public Mica.DatasetVariableAggregationDto getVariableSummary(@NotNull Harmonizat
if(baseTable.isFor(studyId, source)) {
log.info("Caching variable summary {} {} {} {}", dataset.getId(), variableName, studyId, source);
StudyTableSource tableSource = getStudyTableSource(dataset, baseTable);
return tableSource.providesVariableSummary() ? tableSource.getVariableSummary(variableName) : null;
Mica.DatasetVariableAggregationDto summary = tableSource.providesVariableSummary() ? tableSource.getVariableSummary(variableName) : null;
Disposables.silentlyDispose(tableSource);
return summary;
}
}

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

@Override
Expand Down

0 comments on commit dd227ef

Please sign in to comment.