Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out duplicated defined custom-entries and only take the custom one #1921

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/main/java/sirius/biz/codelists/CustomLookupTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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 {

Expand Down Expand Up @@ -90,22 +95,31 @@ protected Stream<LookupTableEntry> 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()
idlira marked this conversation as resolved.
Show resolved Hide resolved
.stream();
}

@Override
protected Stream<LookupTableEntry> performSearch(String searchTerm, Limit limit, String language) {
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))
ymo-sci marked this conversation as resolved.
Show resolved Hide resolved
.values()
.stream();
}

@Override
public Stream<LookupTableEntry> 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
Expand All @@ -116,6 +130,13 @@ public int count() {
@Override
protected Stream<LookupTableEntry> 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<LookupTableEntry, ?, Map<String, LookupTableEntry>> filterCustomDuplicateCollector() {
ymo-sci marked this conversation as resolved.
Show resolved Hide resolved
return Collectors.toMap(LookupTableEntry::getCode, Function.identity(), (a, b) -> a);
}
}