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

Vector Tiles: add support for array serialization in meta layer #72136

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
31 changes: 25 additions & 6 deletions server/src/main/java/org/elasticsearch/common/util/Maps.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TreeMap;
Expand Down Expand Up @@ -127,23 +128,41 @@ public static <K, V> boolean deepEquals(Map<K, V> left, Map<K, V> right) {
.allMatch(e -> right.containsKey(e.getKey()) && Objects.deepEquals(e.getValue(), right.get(e.getKey())));
}

public static Map<String, Object> flatten(Map<String, Object> map, boolean ordered) {
return flatten(map, ordered, null);
public static Map<String, Object> flatten(Map<String, Object> map, boolean flattenArrays, boolean ordered) {
return flatten(map, flattenArrays, ordered, null);
}

@SuppressWarnings("unchecked")
private static Map<String, Object> flatten(Map<String, Object> map, boolean ordered, String parentPath) {
Map<String, Object> flatMap = ordered ? new TreeMap() : new HashMap<>();
private static Map<String, Object> flatten(Map<String, Object> map, boolean flattenArrays, boolean ordered, String parentPath) {
Map<String, Object> flatMap = ordered ? new TreeMap<>() : new HashMap<>();
String prefix = parentPath != null ? parentPath + "." : "";

for (Map.Entry<String, Object> entry : map.entrySet()) {
if (entry.getValue() instanceof Map) {
flatMap.putAll(flatten((Map<String, Object>) entry.getValue(), ordered, prefix + entry.getKey()));
flatMap.putAll(flatten((Map<String, Object>) entry.getValue(), flattenArrays, ordered, prefix + entry.getKey()));
} else if (flattenArrays && entry.getValue() instanceof List) {
flatMap.putAll(flatten((List<Object>) entry.getValue(), ordered, prefix + entry.getKey()));
} else {
flatMap.put(prefix + entry.getKey(), entry.getValue());
}
}
return flatMap;
}

@SuppressWarnings("unchecked")
private static Map<String, Object> flatten(List<Object> list, boolean ordered, String parentPath) {
Map<String, Object> flatMap = ordered ? new TreeMap<>() : new HashMap<>();
String prefix = parentPath != null ? parentPath + "." : "";
for (int i = 0; i < list.size(); i++) {
Object cur = list.get(i);
if (cur instanceof Map) {
flatMap.putAll(flatten((Map<String, Object>) cur, true, ordered, prefix + i));
}
if (cur instanceof List) {
flatMap.putAll(flatten((List<Object>) cur, ordered, prefix + i));
} else {
flatMap.put(prefix + i, cur);
}
}
return flatMap;
}
}
65 changes: 65 additions & 0 deletions server/src/test/java/org/elasticsearch/common/util/MapsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -122,6 +123,70 @@ public void testDeepEquals() {
assertFalse(Maps.deepEquals(map, mapModified));
}

public void testFlatten() {
Map<String, Object> map = randomNestedMap(10);
Map<String, Object> flatten = Maps.flatten(map, true, true);
assertThat(flatten.size(), equalTo(deepCount(map.values())));
for (Map.Entry<String, Object> entry : flatten.entrySet()) {
assertThat(entry.getKey(), entry.getValue(), equalTo(deepGet(entry.getKey(), map)));
}
}

@SuppressWarnings("unchecked")
private static Object deepGet(String path, Object obj) {
Object cur = obj;
String[] keys = path.split("\\.");
for (String key : keys) {
if (Character.isDigit(key.charAt(0))) {
List<Object> list = (List<Object>) cur;
cur = list.get(Integer.parseInt(key));
} else {
Map<String, Object> map = (Map<String, Object>) cur;
cur = map.get(key);
}
}
return cur;
}

@SuppressWarnings("unchecked")
private int deepCount(Collection<Object> map) {
int sum = 0;
for (Object val : map) {
if (val instanceof Map) {
sum += deepCount(((Map<String, Object>) val).values());
} else if (val instanceof List) {
sum += deepCount((List<Object>) val);
} else {
sum ++;
}
}
return sum;
}

private Map<String, Object> randomNestedMap(int level) {
final Supplier<String> keyGenerator = () -> randomAlphaOfLengthBetween(1, 5);
final Supplier<Object> arrayValueGenerator = () -> random().ints(randomInt(5))
.boxed()
.map(s -> (Object) s)
.collect(Collectors.toList());

final Supplier<Object> mapSupplier;
if (level > 0) {
mapSupplier = () -> randomNestedMap(level - 1);
} else {
mapSupplier = ESTestCase::randomLong;
}
final Supplier<Supplier<Object>> valueSupplier = () -> randomFrom(
ESTestCase::randomBoolean,
ESTestCase::randomDouble,
ESTestCase::randomLong,
arrayValueGenerator,
mapSupplier
);
final Supplier<Object> valueGenerator = () -> valueSupplier.get().get();
return randomMap(randomInt(5), keyGenerator, valueGenerator);
}

private void assertMapEntries(final Map<String, String> map, final Collection<Map.Entry<String, String>> entries) {
for (var entry : entries) {
assertThat("map [" + map + "] does not contain key [" + entry.getKey() + "]", map.keySet(), hasItem(entry.getKey()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public static void addToXContentToFeature(VectorTile.Tile.Feature.Builder featur
throws IOException {
final Map<String, Object> map = Maps.flatten(
XContentHelper.convertToMap(XContentHelper.toXContent(toXContent, XContentType.CBOR, false), true, XContentType.CBOR).v2(),
true,
true
);
for (Map.Entry<String, Object> entry : map.entrySet()) {
Expand Down