|
9 | 9 |
|
10 | 10 | package org.elasticsearch.gradle.internal.transport; |
11 | 11 |
|
12 | | -import com.google.common.collect.Streams; |
13 | | -import org.elasticsearch.gradle.VersionProperties; |
14 | 12 | import org.gradle.api.DefaultTask; |
15 | 13 | import org.gradle.api.file.RegularFileProperty; |
16 | 14 | import org.gradle.api.provider.ListProperty; |
|
22 | 20 |
|
23 | 21 | import java.io.IOException; |
24 | 22 | import java.nio.file.Path; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.Comparator; |
| 25 | +import java.util.HashSet; |
25 | 26 | import java.util.List; |
26 | 27 | import java.util.Objects; |
27 | 28 | import java.util.function.Function; |
28 | | -import java.util.stream.Stream; |
29 | 29 |
|
30 | 30 | import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.IdIncrement; |
31 | 31 | import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.getDefinedFile; |
32 | | -import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.getLatestFile; |
| 32 | +import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.getLatestId; |
| 33 | +import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.getPriorLatestId; |
33 | 34 | import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.updateLatestFile; |
34 | 35 | import static org.elasticsearch.gradle.internal.transport.TransportVersionUtils.writeDefinitionFile; |
35 | 36 |
|
@@ -78,37 +79,54 @@ public abstract class GenerateTransportVersionDataTask extends DefaultTask { |
78 | 79 | @Input |
79 | 80 | public abstract Property<Function<String, IdIncrement>> getIdIncrementSupplier(); |
80 | 81 |
|
81 | | - |
82 | 82 | @TaskAction |
83 | 83 | public void generateTransportVersionData() throws IOException { |
84 | 84 | final Path tvDataDir = Objects.requireNonNull(getDataFileDirectory().getAsFile().get()).toPath(); |
85 | 85 | final var tvName = Objects.requireNonNull(getTVName().get()); |
86 | 86 | final var forMinorVersions = Objects.requireNonNull(getMinorVersionsForTV().get()); |
| 87 | + final var idIncrementSupplier = Objects.requireNonNull(getIdIncrementSupplier().get()); |
87 | 88 |
|
88 | 89 | // TODO |
89 | | - // - do we need to validate that the minorVersions don't contain duplicates? |
90 | | - // - is there an order we need to apply? ( I don't think so) |
91 | | - |
| 90 | + // - do we need to also validate that the minorVersions don't contain duplicates here? How do we enforce idempotency if we don't? |
| 91 | + // - is there an order we need to apply? ( I don't think so) |
| 92 | + // - Do we need to run this iteratively for backport construction, rather than accepting a list like this? (I don't think so) |
| 93 | + // - also parse args if run alone |
| 94 | + // - check that duplicate tags don't come in too |
| 95 | + |
| 96 | + // Load the tvSetData for the specified name, if it exists |
| 97 | + final var tvDefinition = getDefinedFile(tvDataDir, tvName); |
| 98 | + boolean tvDefinitionExists = tvDefinition != null; |
| 99 | + final List<Integer> definitionIds = tvDefinitionExists ? tvDefinition.ids() : List.of(); |
| 100 | + |
| 101 | + var seenIds = new HashSet<Integer>(); |
| 102 | + var ids = new ArrayList<>(definitionIds); |
92 | 103 | for (var forMinorVersion : forMinorVersions) { |
93 | 104 | // Get the latest transport version data for the specified minor version. |
94 | | - final var latestTV = getLatestFile(tvDataDir, forMinorVersion); |
| 105 | + final int latestTV = getLatestId(tvDataDir, forMinorVersion); |
95 | 106 |
|
96 | 107 | // Create the new version |
| 108 | + final int newVersion = idIncrementSupplier.apply(forMinorVersion).bumpVersionNumber(latestTV); |
97 | 109 |
|
98 | | - // TODO |
99 | | - |
100 | | - // Load the tvSetData for the specified name, if it exists |
101 | | - final var tvSetDataFromFile = getDefinedFile(tvDataDir, tvName); |
102 | | - final var tvSetFileExists = tvSetDataFromFile != null; |
103 | | - |
104 | | - // Write the definition file. |
105 | | - final var ids = tvSetFileExists |
106 | | - ? Streams.concat(tvSetDataFromFile.ids().stream(), Stream.of(newVersion)).sorted().toList().reversed() |
107 | | - : List.of(newVersion); |
108 | | - writeDefinitionFile(tvDataDir, tvName, ids); |
| 110 | + // Check that we don't already have an ID for this minor version |
| 111 | + var priorLatestID = getPriorLatestId(tvDataDir, forMinorVersion); |
| 112 | + if (containsValueInRange(priorLatestID, newVersion, ids)) { |
| 113 | + // TODO: Should we log something here? |
| 114 | + continue; |
| 115 | + } |
109 | 116 |
|
110 | 117 | // Update the LATEST file. |
111 | 118 | updateLatestFile(tvDataDir, forMinorVersion, tvName, newVersion); |
112 | 119 | } |
| 120 | + |
| 121 | + writeDefinitionFile(tvDataDir, tvName, ids.stream().sorted(Comparator.reverseOrder()).toList()); |
| 122 | + } |
| 123 | + |
| 124 | + private boolean containsValueInRange(int lowerExclusive, int upperInclusive, List<Integer> ids) { |
| 125 | + for (var id : ids) { |
| 126 | + if (lowerExclusive < id && id <= upperInclusive) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + } |
| 130 | + return false; |
113 | 131 | } |
114 | 132 | } |
0 commit comments