From 57fdfb34db236e843b00407e251f96486af3c8b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yannick=20Mo=CC=88ller?= Date: Mon, 15 Jan 2024 14:52:54 +0100 Subject: [PATCH 1/4] Filter out duplicated defined custom-entries and only take the custom one This is needed to "override" base defined entries in a custom file and not duplicate it in the system (where the difference is not easily visible) - fixes: SE-13228 --- .../biz/codelists/CustomLookupTable.java | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/src/main/java/sirius/biz/codelists/CustomLookupTable.java b/src/main/java/sirius/biz/codelists/CustomLookupTable.java index 3f32cc261..0d3e3a3e3 100644 --- a/src/main/java/sirius/biz/codelists/CustomLookupTable.java +++ b/src/main/java/sirius/biz/codelists/CustomLookupTable.java @@ -13,13 +13,18 @@ import sirius.kernel.settings.Extension; import javax.annotation.Nonnull; +import java.util.Map; import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collector; +import java.util.stream.Collectors; import java.util.stream.Stream; /** * Combines two {@link LookupTable lookup tables} into a single lookup table. *

* Using this approach, the custom table is always queried first and then complemented by the base table. + * If there is a custom entry with the same key as in the base table, the custom entry is used. */ class CustomLookupTable extends LookupTable { @@ -90,7 +95,10 @@ protected Stream performSuggest(Limit limit, String searchTerm return Stream.concat(customTable.performSuggest(Limit.UNLIMITED, searchTerm, language), baseTable.performSuggest(Limit.UNLIMITED, searchTerm, language)) .skip(limit.getItemsToSkip()) - .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()); + .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()) + .collect(filterCustomDuplicateCollector()) + .values() + .stream(); } @Override @@ -98,14 +106,20 @@ protected Stream performSearch(String searchTerm, Limit limit, return Stream.concat(customTable.performSearch(searchTerm, Limit.UNLIMITED, language), baseTable.performSearch(searchTerm, Limit.UNLIMITED, language)) .skip(limit.getItemsToSkip()) - .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()); + .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()) + .collect(Collectors.toMap(LookupTableEntry::getCode, Function.identity(), (a, b) -> a)) + .values() + .stream(); } @Override public Stream scan(String language, Limit limit) { return Stream.concat(customTable.scan(language, Limit.UNLIMITED), baseTable.scan(language, Limit.UNLIMITED)) .skip(limit.getItemsToSkip()) - .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()); + .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()) + .collect(filterCustomDuplicateCollector()) + .values() + .stream(); } @Override @@ -116,6 +130,13 @@ public int count() { @Override protected Stream performQuery(String language, String lookupPath, String lookupValue) { return Stream.concat(customTable.performQuery(language, lookupPath, lookupValue), - baseTable.performQuery(language, lookupPath, lookupValue)); + baseTable.performQuery(language, lookupPath, lookupValue)) + .collect(filterCustomDuplicateCollector()) + .values() + .stream(); + } + + private Collector> filterCustomDuplicateCollector() { + return Collectors.toMap(LookupTableEntry::getCode, Function.identity(), (a, b) -> a); } } From 73c8ef0fa8a963a1466ed0f59a20c96b8ff27de5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yannick=20Mo=CC=88ller?= Date: Mon, 15 Jan 2024 15:08:55 +0100 Subject: [PATCH 2/4] fix a missing refactoring and also use introduced method - fixes: SE-13228 --- src/main/java/sirius/biz/codelists/CustomLookupTable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/sirius/biz/codelists/CustomLookupTable.java b/src/main/java/sirius/biz/codelists/CustomLookupTable.java index 0d3e3a3e3..56dac75eb 100644 --- a/src/main/java/sirius/biz/codelists/CustomLookupTable.java +++ b/src/main/java/sirius/biz/codelists/CustomLookupTable.java @@ -107,7 +107,7 @@ protected Stream performSearch(String searchTerm, Limit limit, baseTable.performSearch(searchTerm, Limit.UNLIMITED, language)) .skip(limit.getItemsToSkip()) .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()) - .collect(Collectors.toMap(LookupTableEntry::getCode, Function.identity(), (a, b) -> a)) + .collect(filterCustomDuplicateCollector()) .values() .stream(); } From 9881d24732e338904fc564f0adff58f0541862ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yannick=20Mo=CC=88ller?= Date: Mon, 15 Jan 2024 15:29:38 +0100 Subject: [PATCH 3/4] move .limit to the correct place - fixes: SE-13228 --- .../java/sirius/biz/codelists/CustomLookupTable.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/sirius/biz/codelists/CustomLookupTable.java b/src/main/java/sirius/biz/codelists/CustomLookupTable.java index 56dac75eb..586751ade 100644 --- a/src/main/java/sirius/biz/codelists/CustomLookupTable.java +++ b/src/main/java/sirius/biz/codelists/CustomLookupTable.java @@ -95,10 +95,10 @@ protected Stream performSuggest(Limit limit, String searchTerm return Stream.concat(customTable.performSuggest(Limit.UNLIMITED, searchTerm, language), baseTable.performSuggest(Limit.UNLIMITED, searchTerm, language)) .skip(limit.getItemsToSkip()) - .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()) .collect(filterCustomDuplicateCollector()) .values() - .stream(); + .stream() + .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()); } @Override @@ -106,20 +106,20 @@ protected Stream performSearch(String searchTerm, Limit limit, return Stream.concat(customTable.performSearch(searchTerm, Limit.UNLIMITED, language), baseTable.performSearch(searchTerm, Limit.UNLIMITED, language)) .skip(limit.getItemsToSkip()) - .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()) .collect(filterCustomDuplicateCollector()) .values() - .stream(); + .stream() + .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()); } @Override public Stream scan(String language, Limit limit) { return Stream.concat(customTable.scan(language, Limit.UNLIMITED), baseTable.scan(language, Limit.UNLIMITED)) .skip(limit.getItemsToSkip()) - .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()) .collect(filterCustomDuplicateCollector()) .values() - .stream(); + .stream() + .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()); } @Override From cdd3f5a531567de9e90b73962cab554dc2157fd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yannick=20Mo=CC=88ller?= Date: Tue, 16 Jan 2024 08:52:34 +0100 Subject: [PATCH 4/4] use temp set to filter out duplicates and move the filter to the right place - fixes: SE-13228 --- .../biz/codelists/CustomLookupTable.java | 30 +++++++------------ 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/main/java/sirius/biz/codelists/CustomLookupTable.java b/src/main/java/sirius/biz/codelists/CustomLookupTable.java index 586751ade..8f23afb37 100644 --- a/src/main/java/sirius/biz/codelists/CustomLookupTable.java +++ b/src/main/java/sirius/biz/codelists/CustomLookupTable.java @@ -13,11 +13,9 @@ import sirius.kernel.settings.Extension; import javax.annotation.Nonnull; -import java.util.Map; +import java.util.HashSet; import java.util.Optional; -import java.util.function.Function; -import java.util.stream.Collector; -import java.util.stream.Collectors; +import java.util.Set; import java.util.stream.Stream; /** @@ -92,33 +90,30 @@ protected Optional performFetchObject(Class type, String code, boolean @Override protected Stream performSuggest(Limit limit, String searchTerm, String language) { + Set codes = new HashSet<>(); return Stream.concat(customTable.performSuggest(Limit.UNLIMITED, searchTerm, language), baseTable.performSuggest(Limit.UNLIMITED, searchTerm, language)) + .filter(entry -> codes.add(entry.getCode())) .skip(limit.getItemsToSkip()) - .collect(filterCustomDuplicateCollector()) - .values() - .stream() .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()); } @Override protected Stream performSearch(String searchTerm, Limit limit, String language) { + Set codes = new HashSet<>(); return Stream.concat(customTable.performSearch(searchTerm, Limit.UNLIMITED, language), baseTable.performSearch(searchTerm, Limit.UNLIMITED, language)) + .filter(entry -> codes.add(entry.getCode())) .skip(limit.getItemsToSkip()) - .collect(filterCustomDuplicateCollector()) - .values() - .stream() .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()); } @Override public Stream scan(String language, Limit limit) { + Set codes = new HashSet<>(); return Stream.concat(customTable.scan(language, Limit.UNLIMITED), baseTable.scan(language, Limit.UNLIMITED)) + .filter(entry -> codes.add(entry.getCode())) .skip(limit.getItemsToSkip()) - .collect(filterCustomDuplicateCollector()) - .values() - .stream() .limit(limit.getMaxItems() == 0 ? Long.MAX_VALUE : limit.getMaxItems()); } @@ -129,14 +124,9 @@ public int count() { @Override protected Stream performQuery(String language, String lookupPath, String lookupValue) { + Set codes = new HashSet<>(); return Stream.concat(customTable.performQuery(language, lookupPath, lookupValue), baseTable.performQuery(language, lookupPath, lookupValue)) - .collect(filterCustomDuplicateCollector()) - .values() - .stream(); - } - - private Collector> filterCustomDuplicateCollector() { - return Collectors.toMap(LookupTableEntry::getCode, Function.identity(), (a, b) -> a); + .filter(entry -> codes.add(entry.getCode())); } }