Skip to content

Commit

Permalink
HSEARCH-4253 Align IndexSettings#merge on AbstractTypeMapping#merge
Browse files Browse the repository at this point in the history
It's easier to implement AbstractTypeMapping#merge if we consider `this`
takes precedence over the argument to `merge()`; for
`IndexSettings#merge`, both approaches are equally easy to implement.

So, let's be consistent and consider `this` takes precedence over the
argument to `merge()` in both cases.
  • Loading branch information
yrodiere authored and fax4ever committed Oct 14, 2021
1 parent ef540b5 commit 67c3e23
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,17 @@ private IndexSettings buildSettings() {
getAnalysis( settings ).setCharFilters( analysisDefinitionRegistry.getCharFilterDefinitions() );
}

// if customSettings are present, merge them with the ones created by Search
settings.merge( customIndexSettings );
if ( customIndexSettings == null ) {
return settings;
}

return settings;
// If customIndexSettings are present, merge them with the ones created by Search.
// Avoid side effects: we copy the settings before modifying them.
IndexSettings customIndexSettingsCopy =
GsonUtils.deepCopy( gsonProvider.getGsonNoSerializeNulls(), IndexSettings.class, customIndexSettings );
// The custom settings take precedence over the Hibernate Search ones.
customIndexSettingsCopy.merge( settings );
return customIndexSettingsCopy;
}

private RootTypeMapping buildMapping() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,41 @@ public String toString() {
}

/**
* Merge this analysis with another one.
* Any conflict of definition will be solved in favour of the other analysis.
* {@link #extraAttributes} will be always overridden.
* Merge these (custom) analysis settings with analysis settings generated by Hibernate Search.
* <p>
* Any conflict of definition will be solved in favor of {@code this}.
*
* @param overridingAnalysis The other analysis definition
* @param other The other analysis settings
*/
public void merge(Analysis overridingAnalysis) {
if ( overridingAnalysis == null ) {
public void merge(Analysis other) {
if ( other == null ) {
// nothing to do
return;
}

analyzers = merge( analyzers, overridingAnalysis.analyzers );
normalizers = merge( normalizers, overridingAnalysis.normalizers );
tokenizers = merge( tokenizers, overridingAnalysis.tokenizers );
tokenFilters = merge( tokenFilters, overridingAnalysis.tokenFilters );
charFilters = merge( charFilters, overridingAnalysis.charFilters );
analyzers = merge( analyzers, other.analyzers );
normalizers = merge( normalizers, other.normalizers );
tokenizers = merge( tokenizers, other.tokenizers );
tokenFilters = merge( tokenFilters, other.tokenFilters );
charFilters = merge( charFilters, other.charFilters );

extraAttributes = overridingAnalysis.extraAttributes;
if ( extraAttributes == null ) {
extraAttributes = other.extraAttributes;
}
}

private static <K> Map<String, K> merge(Map<String, K> originalImmutableMap, Map<String, K> overridingMap) {
if ( overridingMap == null || overridingMap.isEmpty() ) {
private static <K> Map<String, K> merge(Map<String, K> originalImmutableMap, Map<String, K> otherMap) {
if ( otherMap == null || otherMap.isEmpty() ) {
return originalImmutableMap;
}

if ( originalImmutableMap == null || originalImmutableMap.isEmpty() ) {
return overridingMap;
return otherMap;
}

// let's merge
HashMap<String, K> result = new HashMap<>( originalImmutableMap );
result.putAll( overridingMap );
HashMap<String, K> result = new HashMap<>( otherMap );
result.putAll( originalImmutableMap );
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,32 @@ public String toString() {
}

/**
* Merge these settings with other settings.
* Any conflict of definition will be solved in favour of the other settings.
* {@link #extraAttributes} will be always overridden.
* Merge these (custom) settings with settings generated by Hibernate Search.
* <p>
* Any conflict of definition will be solved in favor of {@code this}.
*
* @param overridingIndexSettings The other index settings
* @param other The other index settings
*/
public void merge(IndexSettings overridingIndexSettings) {
if ( overridingIndexSettings == null ) {
public void merge(IndexSettings other) {
if ( other == null ) {
// nothing to do
return;
}

if ( analysis == null ) {
analysis = overridingIndexSettings.analysis;
analysis = other.analysis;
}
else {
analysis.merge( overridingIndexSettings.analysis );
analysis.merge( other.analysis );
}

if ( overridingIndexSettings.maxResultWindow != null ) {
maxResultWindow = overridingIndexSettings.maxResultWindow;
if ( maxResultWindow == null ) {
maxResultWindow = other.maxResultWindow;
}

extraAttributes = overridingIndexSettings.extraAttributes;
if ( extraAttributes == null ) {
extraAttributes = other.extraAttributes;
}
}

/**
Expand Down

0 comments on commit 67c3e23

Please sign in to comment.