From 339833e2d6f4f85757f7fdb84c2006f17d766f38 Mon Sep 17 00:00:00 2001 From: deiflaender Date: Fri, 17 Mar 2023 15:48:16 +0100 Subject: [PATCH] RED-6224: Fixed pr findings --- .../redaction/service/DictionaryService.java | 99 ++++++++++++------- .../settings/RedactionServiceSettings.java | 4 + 2 files changed, 65 insertions(+), 38 deletions(-) diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DictionaryService.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DictionaryService.java index c0308ef6..f90750e3 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DictionaryService.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/redaction/service/DictionaryService.java @@ -8,7 +8,6 @@ import java.util.List; import java.util.Locale; import java.util.Optional; import java.util.Set; -import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @@ -16,7 +15,6 @@ import javax.annotation.PostConstruct; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.SerializationUtils; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import com.google.common.cache.CacheBuilder; @@ -34,6 +32,7 @@ import com.iqser.red.service.redaction.v1.server.redaction.model.DictionaryModel import com.iqser.red.service.redaction.v1.server.redaction.model.DictionaryRepresentation; import com.iqser.red.service.redaction.v1.server.redaction.model.DictionaryVersion; import com.iqser.red.service.redaction.v1.server.redaction.model.TenantDictionary; +import com.iqser.red.service.redaction.v1.server.settings.RedactionServiceSettings; import feign.FeignException; import io.micrometer.core.annotation.Timed; @@ -49,11 +48,7 @@ public class DictionaryService { private static final String DEFAULT_COLOR = "#cccccc"; private final DictionaryClient dictionaryClient; - @Value("${multitenancy.dictionary-cache.maximumSize:100}") - private Long maximumSize; - - @Value("${multitenancy.dictionary-cache.expireAfterAccess:3}") - private Integer expireAfterAccess; + private final RedactionServiceSettings settings; private LoadingCache tenantDictionaryCache; @@ -61,12 +56,15 @@ public class DictionaryService { @PostConstruct protected void createCache() { - tenantDictionaryCache = CacheBuilder.newBuilder().maximumSize(maximumSize).expireAfterAccess(expireAfterAccess, TimeUnit.DAYS).build(new CacheLoader<>() { - public TenantDictionary load(String key) { + tenantDictionaryCache = CacheBuilder.newBuilder() + .maximumSize(settings.getDictionaryCacheMaximumSize()) + .expireAfterAccess(settings.getDictionaryCacheExpireAfterAccessDays(), TimeUnit.DAYS) + .build(new CacheLoader<>() { + public TenantDictionary load(String key) { - return new TenantDictionary(); - } - }); + return new TenantDictionary(); + } + }); } @@ -76,13 +74,13 @@ public class DictionaryService { log.info("Updating dictionary data for dossierTemplate {} and dossier {}", dossierTemplateId, dossierId); long dossierTemplateDictionaryVersion = dictionaryClient.getVersion(dossierTemplateId); - var dossierTemplateDictionary = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId); + var dossierTemplateDictionary = getDossierTemplateDictionary(dossierTemplateId); if (dossierTemplateDictionary == null || dossierTemplateDictionaryVersion > dossierTemplateDictionary.getDictionaryVersion()) { updateDictionaryEntry(dossierTemplateId, dossierTemplateDictionaryVersion, getVersion(dossierTemplateDictionary), null); } long dossierDictionaryVersion = dictionaryClient.getVersionForDossier(dossierId); - var dossierDictionary = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId); + var dossierDictionary = getDossierDictionary(dossierId); if (dossierDictionary == null || dossierDictionaryVersion > dossierDictionary.getDictionaryVersion()) { updateDictionaryEntry(dossierTemplateId, dossierDictionaryVersion, getVersion(dossierDictionary), dossierId); } @@ -98,7 +96,7 @@ public class DictionaryService { DictionaryVersion version = updateDictionary(dossierTemplateId, dossierId); Set newValues = new HashSet<>(); - List dictionaryModels = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getDictionary(); + List dictionaryModels = getDossierTemplateDictionary(dossierTemplateId).getDictionary(); dictionaryModels.forEach(dictionaryModel -> { dictionaryModel.getEntries().forEach(dictionaryEntry -> { if (dictionaryEntry.getVersion() > fromVersion.getDossierTemplateVersion()) { @@ -117,8 +115,8 @@ public class DictionaryService { }); }); - if (tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().containsKey(dossierId)) { - dictionaryModels = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId).getDictionary(); + if (dossierDictionaryExists(dossierId)) { + dictionaryModels = getDossierDictionary(dossierId).getDictionary(); dictionaryModels.forEach(dictionaryModel -> { dictionaryModel.getEntries().forEach(dictionaryEntry -> { if (dictionaryEntry.getVersion() > fromVersion.getDossierVersion()) { @@ -156,20 +154,10 @@ public class DictionaryService { Optional oldModel; if (dossierId == null) { - DictionaryRepresentation representation = null; - try { - representation = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId); - } catch (ExecutionException e) { - throw new RuntimeException("Failed to load Dictionary cache for tenant: " + TenantContext.getTenantId()); - } + var representation = getDossierTemplateDictionary(dossierTemplateId); oldModel = representation != null ? representation.getDictionary().stream().filter(f -> f.getType().equals(t.getType())).findAny() : Optional.empty(); } else { - DictionaryRepresentation representation = null; - try { - representation = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId); - } catch (ExecutionException e) { - throw new RuntimeException("Failed to load Dictionary cache for tenant: " + TenantContext.getTenantId()); - } + var representation = getDossierDictionary(dossierId); oldModel = representation != null ? representation.getDictionary().stream().filter(f -> f.getType().equals(t.getType())).findAny() : Optional.empty(); } @@ -229,9 +217,9 @@ public class DictionaryService { dictionaryRepresentation.setDictionary(dictionary); if (dossierId == null) { - tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().put(dossierTemplateId, dictionaryRepresentation); + addDictionaryRepresentationForDossierTemplate(dossierTemplateId, dictionaryRepresentation); } else { - tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().put(dossierId, dictionaryRepresentation); + addDictionaryRepresentationForDossier(dossierId, dictionaryRepresentation); } } } catch (FeignException e) { @@ -273,18 +261,18 @@ public class DictionaryService { @SneakyThrows public float[] getColor(String type, String dossierTemplateId) { - DictionaryModel model = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getLocalAccessMap().get(type); + DictionaryModel model = getDossierTemplateDictionary(dossierTemplateId).getLocalAccessMap().get(type); if (model != null) { return model.getColor(); } - return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getDefaultColor(); + return getDossierTemplateDictionary(dossierTemplateId).getDefaultColor(); } @SneakyThrows public boolean isHint(String type, String dossierTemplateId) { - DictionaryModel model = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getLocalAccessMap().get(type); + DictionaryModel model = getDossierTemplateDictionary(dossierTemplateId).getLocalAccessMap().get(type); if (model != null) { return model.isHint(); } @@ -298,15 +286,15 @@ public class DictionaryService { List copy = new ArrayList<>(); - var dossierTemplateRepresentation = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId); + var dossierTemplateRepresentation = getDossierTemplateDictionary(dossierTemplateId); dossierTemplateRepresentation.getDictionary().forEach(dm -> { copy.add(SerializationUtils.clone(dm)); }); //TODO merge dictionaries if they have same names long dossierDictionaryVersion = -1; - if (tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().containsKey(dossierId)) { - var dossierRepresentation = tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId); + if (dossierDictionaryExists(dossierId)) { + var dossierRepresentation = getDossierDictionary(dossierId); dossierRepresentation.getDictionary().forEach(dm -> { copy.add(SerializationUtils.clone(dm)); }); @@ -321,7 +309,42 @@ public class DictionaryService { @SneakyThrows public float[] getNotRedactedColor(String dossierTemplateId) { - return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId).getNotRedactedColor(); + return getDossierTemplateDictionary(dossierTemplateId).getNotRedactedColor(); + } + + + @SneakyThrows + private void addDictionaryRepresentationForDossierTemplate(String dossierTemplateId, DictionaryRepresentation dictionaryRepresentation) { + + tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().put(dossierTemplateId, dictionaryRepresentation); + } + + + @SneakyThrows + private void addDictionaryRepresentationForDossier(String dossierId, DictionaryRepresentation dictionaryRepresentation) { + + tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().put(dossierId, dictionaryRepresentation); + } + + + @SneakyThrows + private DictionaryRepresentation getDossierTemplateDictionary(String dossierTemplateId) { + + return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossierTemplate().get(dossierTemplateId); + } + + + @SneakyThrows + private DictionaryRepresentation getDossierDictionary(String dossierId) { + + return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().get(dossierId); + } + + + @SneakyThrows + private boolean dossierDictionaryExists(String dossierId) { + + return tenantDictionaryCache.get(TenantContext.getTenantId()).getDictionariesByDossier().containsKey(dossierId); } diff --git a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/settings/RedactionServiceSettings.java b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/settings/RedactionServiceSettings.java index 1c35d9b0..110ac5dd 100644 --- a/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/settings/RedactionServiceSettings.java +++ b/redaction-service-v1/redaction-service-server-v1/src/main/java/com/iqser/red/service/redaction/v1/server/settings/RedactionServiceSettings.java @@ -24,4 +24,8 @@ public class RedactionServiceSettings { private boolean priorityMode; + private long dictionaryCacheMaximumSize = 100; + + private int dictionaryCacheExpireAfterAccessDays = 3; + }