Use a more compact layers.idx format
Update the `layers.idx` format so that it is more compact and can be parsed by third-parties as YAML. Closes gh-20860
This commit is contained in:
@@ -21,23 +21,46 @@ import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
/**
|
||||
* Index describing the layer to which each entry in a jar belongs.
|
||||
* Index describing the layer to which each entry in a jar belongs. Index files are simple
|
||||
* text files that should be read from top to bottom. Each file defines the layers and
|
||||
* their content. Layers names are written as quoted strings prefixed by a dash space
|
||||
* ({@code "- "}) and with a colon ({@code ":"}) suffix. Layer content is either a file or
|
||||
* folder name written as a quoted string prefixed by space space dash space
|
||||
* ({@code " - "}). A folder name ends with {@code /}, a file name does not. When a
|
||||
* folder name is used it means that all files inside that folder are in the same layer.
|
||||
* <p>
|
||||
* Index files are designed to be compatible with YAML and may be read into a list of
|
||||
* `Map<String, List<String>>` instances.
|
||||
*
|
||||
* @author Madhura Bhave
|
||||
* @author Andy Wilkinson
|
||||
* @author Phillip Webb
|
||||
* @since 2.3.0
|
||||
*/
|
||||
public class LayersIndex {
|
||||
|
||||
private final Iterable<Layer> layers;
|
||||
|
||||
private final MultiValueMap<Layer, String> index = new LinkedMultiValueMap<>();
|
||||
private final Node root = new Node();
|
||||
|
||||
/**
|
||||
* Create a new {@link LayersIndex} backed by the given layers.
|
||||
* @param layers the layers in the index
|
||||
*/
|
||||
public LayersIndex(Layer... layers) {
|
||||
this(Arrays.asList(layers));
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new {@link LayersIndex} backed by the given layers.
|
||||
@@ -53,7 +76,12 @@ public class LayersIndex {
|
||||
* @param name the name of the item
|
||||
*/
|
||||
public void add(Layer layer, String name) {
|
||||
this.index.add(layer, name);
|
||||
String[] segments = name.split("/");
|
||||
Node node = this.root;
|
||||
for (int i = 0; i < segments.length; i++) {
|
||||
boolean isFolder = i < (segments.length - 1);
|
||||
node = node.updateOrAddNode(segments[i], isFolder, layer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -62,19 +90,67 @@ public class LayersIndex {
|
||||
* @throws IOException on IO error
|
||||
*/
|
||||
public void writeTo(OutputStream out) throws IOException {
|
||||
MultiValueMap<Layer, String> index = new LinkedMultiValueMap<>();
|
||||
this.root.buildIndex("", index);
|
||||
index.values().forEach(Collections::sort);
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
|
||||
for (Layer layer : this.layers) {
|
||||
List<String> names = this.index.get(layer);
|
||||
if (names != null) {
|
||||
List<String> names = index.get(layer);
|
||||
if (names != null && !names.isEmpty()) {
|
||||
writer.write("- \"" + layer + "\":\n");
|
||||
for (String name : names) {
|
||||
writer.write(layer.toString());
|
||||
writer.write(" ");
|
||||
writer.write(name);
|
||||
writer.write("\n");
|
||||
writer.write(" - \"" + name + "\"\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* A node within the index represeting a single path segment.
|
||||
*/
|
||||
private static class Node {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final Set<Layer> layers;
|
||||
|
||||
private final List<Node> children = new ArrayList<>();
|
||||
|
||||
Node() {
|
||||
this.name = "";
|
||||
this.layers = new HashSet<>();
|
||||
}
|
||||
|
||||
Node(String name, Layer layer, Node parent) {
|
||||
this.name = name;
|
||||
this.layers = new HashSet<>(Collections.singleton(layer));
|
||||
}
|
||||
|
||||
Node updateOrAddNode(String segment, boolean isFolder, Layer layer) {
|
||||
String name = segment + (isFolder ? "/" : "");
|
||||
for (Node child : this.children) {
|
||||
if (name.equals(child.name)) {
|
||||
child.layers.add(layer);
|
||||
return child;
|
||||
}
|
||||
}
|
||||
Node child = new Node(name, layer, this);
|
||||
this.children.add(child);
|
||||
return child;
|
||||
}
|
||||
|
||||
void buildIndex(String path, MultiValueMap<Layer, String> index) {
|
||||
String name = path + this.name;
|
||||
if (this.layers.size() == 1) {
|
||||
index.add(this.layers.iterator().next(), name);
|
||||
}
|
||||
else {
|
||||
for (Node child : this.children) {
|
||||
child.buildIndex(name, index);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user