-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement option to get and apply index settings (#21054)
* implement option to get and apply index settings * fix license header * fix review findings
- Loading branch information
Showing
8 changed files
with
240 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
graylog2-server/src/main/java/org/graylog2/indexer/indices/IndexSettingsHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
package org.graylog2.indexer.indices; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class IndexSettingsHelper { | ||
|
||
/** | ||
* This method is copied from {@link org.graylog.shaded.opensearch2.org.opensearch.common.settings.Settings} and adapted. | ||
*/ | ||
public static Map<String, Object> getAsStructuredMap(Map<String, Object> settings) { | ||
Map<String, Object> map = new HashMap<>(2); | ||
for (Map.Entry<String, Object> entry : settings.entrySet()) { | ||
processSetting(map, "", entry.getKey(), entry.getValue()); | ||
} | ||
for (Map.Entry<String, Object> entry : map.entrySet()) { | ||
if (entry.getValue() instanceof Map) { | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> valMap = (Map<String, Object>) entry.getValue(); | ||
entry.setValue(convertMapsToArrays(valMap)); | ||
} | ||
} | ||
|
||
return map; | ||
} | ||
|
||
private static Object convertMapsToArrays(Map<String, Object> map) { | ||
if (map.isEmpty()) { | ||
return map; | ||
} | ||
boolean isArray = true; | ||
int maxIndex = -1; | ||
for (Map.Entry<String, Object> entry : map.entrySet()) { | ||
if (isArray) { | ||
try { | ||
int index = Integer.parseInt(entry.getKey()); | ||
if (index >= 0) { | ||
maxIndex = Math.max(maxIndex, index); | ||
} else { | ||
isArray = false; | ||
} | ||
} catch (NumberFormatException ex) { | ||
isArray = false; | ||
} | ||
} | ||
if (entry.getValue() instanceof Map) { | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> valMap = (Map<String, Object>) entry.getValue(); | ||
entry.setValue(convertMapsToArrays(valMap)); | ||
} | ||
} | ||
if (isArray && (maxIndex + 1) == map.size()) { | ||
ArrayList<Object> newValue = new ArrayList<>(maxIndex + 1); | ||
for (int i = 0; i <= maxIndex; i++) { | ||
Object obj = map.get(Integer.toString(i)); | ||
if (obj == null) { | ||
// Something went wrong. Different format? | ||
// Bailout! | ||
return map; | ||
} | ||
newValue.add(obj); | ||
} | ||
return newValue; | ||
} | ||
return map; | ||
} | ||
|
||
|
||
private static void processSetting(Map<String, Object> map, String prefix, String setting, Object value) { | ||
int prefixLength = setting.indexOf('.'); | ||
if (prefixLength == -1) { | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> innerMap = (Map<String, Object>) map.get(prefix + setting); | ||
if (innerMap != null) { | ||
// It supposed to be a value, but we already have a map stored, need to convert this map to "." notation | ||
for (Map.Entry<String, Object> entry : innerMap.entrySet()) { | ||
map.put(prefix + setting + "." + entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
map.put(prefix + setting, value); | ||
} else { | ||
String key = setting.substring(0, prefixLength); | ||
String rest = setting.substring(prefixLength + 1); | ||
Object existingValue = map.get(prefix + key); | ||
if (existingValue == null) { | ||
Map<String, Object> newMap = new HashMap<>(2); | ||
processSetting(newMap, "", rest, value); | ||
map.put(prefix + key, newMap); | ||
} else { | ||
if (existingValue instanceof Map) { | ||
@SuppressWarnings("unchecked") | ||
Map<String, Object> innerMap = (Map<String, Object>) existingValue; | ||
processSetting(innerMap, "", rest, value); | ||
map.put(prefix + key, innerMap); | ||
} else { | ||
// It supposed to be a map, but we already have a value stored, which is not a map | ||
// fall back to "." notation | ||
processSetting(map, prefix + key + ".", rest, value); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.