-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathIndex.java
More file actions
296 lines (267 loc) · 9.45 KB
/
Index.java
File metadata and controls
296 lines (267 loc) · 9.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*-
* =LICENSE=
* ORAS Java SDK
* ===
* Copyright (C) 2024 - 2026 ORAS
* ===
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =LICENSEEND=
*/
package land.oras;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import land.oras.utils.Const;
import land.oras.utils.JsonUtils;
import org.jspecify.annotations.Nullable;
/**
* Index from an OCI layout
*/
@OrasModel
@JsonPropertyOrder({
Const.JSON_PROPERTY_SCHEMA_VERSION,
Const.JSON_PROPERTY_MEDIA_TYPE,
Const.JSON_PROPERTY_ARTIFACT_TYPE,
Const.JSON_PROPERTY_DIGEST,
Const.JSON_PROPERTY_SIZE,
Const.JSON_PROPERTY_CONFIG,
Const.JSON_PROPERTY_SUBJECT,
Const.JSON_PROPERTY_ANNOTATIONS,
Const.JSON_PROPERTY_MANIFESTS,
})
@JsonInclude(JsonInclude.Include.NON_NULL) // We need to serialize empty list of manifests
public final class Index extends Descriptor implements Describable {
private final int schemaVersion;
private final List<ManifestDescriptor> manifests;
private final Subject subject;
/**
* The index descriptor
*/
private final ManifestDescriptor descriptor;
@JsonCreator
@SuppressWarnings("unused")
private Index(
@JsonProperty(Const.JSON_PROPERTY_SCHEMA_VERSION) int schemaVersion,
@JsonProperty(Const.JSON_PROPERTY_MEDIA_TYPE) String mediaType,
@JsonProperty(Const.JSON_PROPERTY_ARTIFACT_TYPE) String artifactType,
@JsonProperty(Const.JSON_PROPERTY_MANIFESTS) List<ManifestDescriptor> manifests,
@JsonProperty(Const.JSON_PROPERTY_ANNOTATIONS) Map<String, String> annotations,
@JsonProperty(Const.JSON_PROPERTY_SUBJECT) Subject subject) {
this(schemaVersion, mediaType, artifactType, manifests, annotations, subject, null, null, null);
}
private Index(
int schemaVersion,
String mediaType,
String artifactType,
List<ManifestDescriptor> manifests,
Map<String, String> annotations,
Subject subject,
ManifestDescriptor descriptor,
String registry,
String json) {
super(null, null, mediaType, annotations, artifactType, registry, json);
this.schemaVersion = schemaVersion;
this.descriptor = descriptor;
this.subject = subject;
this.manifests = manifests;
}
/**
* Get the schema version
* @return The schema version
*/
public int getSchemaVersion() {
return schemaVersion;
}
/**
* Get the list of manifests
* @return The list of manifests
*/
public List<ManifestDescriptor> getManifests() {
return manifests;
}
/**
* Filter the manifests by platform
* @param platform The platform
* @return The list of manifests that match the platform
*/
public List<ManifestDescriptor> filter(Platform platform) {
return getManifests().stream()
.filter(descriptor -> descriptor.getPlatform().equals(platform))
.toList();
}
/**
* Get the list of manifests that have unspecified platform
* @return The list of manifests that have unspecified platform
*/
public List<ManifestDescriptor> unspecifiedPlatforms() {
return getManifests().stream()
.filter(descriptor -> Platform.unspecified(descriptor.getPlatform()))
.toList();
}
/**
* Find a unique manifest by platform. If there are multiple manifests that match the platform, return null
* @param platform The platform
* @return The manifest that matches the platform, or null if there are multiple matches or no matches
*/
public @Nullable ManifestDescriptor findUnique(Platform platform) {
return getManifests().stream()
.filter(descriptor -> descriptor.getPlatform().equals(platform))
.findFirst()
.orElse(null);
}
/**
* Get the artifact type as string for JSON serialization
* @return The artifact type as string
*/
@JsonProperty(Const.JSON_PROPERTY_ARTIFACT_TYPE)
@SuppressWarnings("unused")
public String getArtifactTypeAsString() {
return artifactType;
}
/**
* Return a new index with new manifest added to index
* @param manifest The manifest
* @return The index
*/
public Index withNewManifests(ManifestDescriptor manifest) {
List<ManifestDescriptor> newManifests = new LinkedList<>();
for (ManifestDescriptor descriptor : manifests) {
// Ignore same digest
if (descriptor.getDigest().equals(manifest.getDigest())) {
continue;
}
// Move previous ref
if (descriptor.getAnnotations() != null
&& descriptor.getAnnotations().containsKey(Const.ANNOTATION_REF)
&& manifest.getAnnotations() != null
&& manifest.getAnnotations().containsKey(Const.ANNOTATION_REF)
&& descriptor
.getAnnotations()
.get(Const.ANNOTATION_REF)
.equals(manifest.getAnnotations().get(Const.ANNOTATION_REF))) {
Map<String, String> newAnnotations = new LinkedHashMap<>(descriptor.getAnnotations());
newAnnotations.remove(Const.ANNOTATION_REF);
if (newAnnotations.isEmpty()) {
newAnnotations = null;
}
newManifests.add(ManifestDescriptor.fromJson(
descriptor.withAnnotations(newAnnotations).toJson()));
continue;
}
newManifests.add(ManifestDescriptor.fromJson(descriptor.toJson()));
}
newManifests.add(manifest);
return new Index(
schemaVersion, mediaType, artifactType, newManifests, annotations, subject, descriptor, registry, json);
}
@Override
public ManifestDescriptor getDescriptor() {
return descriptor;
}
/**
* Get the annotations
* @return The annotations
*/
@Override
public @Nullable Map<String, String> getAnnotations() {
if (annotations != null && !annotations.isEmpty()) {
return annotations;
}
return null;
}
/**
* Return a new index with the given descriptor
* @param descriptor The descriptor
* @return The manifest
*/
public Index withDescriptor(ManifestDescriptor descriptor) {
return new Index(
schemaVersion, mediaType, artifactType, manifests, annotations, subject, descriptor, registry, json);
}
/**
* Return same instance but with original JSON
* @param json The original JSON
* @return The index
*/
protected Index withJson(String json) {
this.json = json;
return this;
}
@Override
public Subject getSubject() {
return subject;
}
/**
* Return a new index with the given artifact type
* @param artifactType The artifact type
* @return The index
*/
public Index withArtifactType(String artifactType) {
return new Index(
schemaVersion, mediaType, artifactType, manifests, annotations, subject, descriptor, registry, json);
}
/**
* Return a new index with the given subject
* @param subject The subject
* @return The index
*/
public Index withSubject(Subject subject) {
return new Index(
schemaVersion, mediaType, artifactType, manifests, annotations, subject, descriptor, registry, json);
}
/**
* Create an index from a JSON string
* @param json The JSON string
* @return The index
*/
public static Index fromJson(String json) {
return JsonUtils.fromJson(json, Index.class).withJson(json);
}
/**
* Create an index from a path
* @param path The path
* @return The index
*/
public static Index fromPath(Path path) {
return JsonUtils.fromJson(path, Index.class);
}
/**
* Create an index from a list of manifests
* @param descriptors The list of manifests
* @return The index
*/
public static Index fromManifests(List<ManifestDescriptor> descriptors) {
return new Index(2, Const.DEFAULT_INDEX_MEDIA_TYPE, null, descriptors, null, null, null, null, null);
}
@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;
Index index = (Index) o;
return Objects.equals(toJson(), index.toJson());
}
@Override
public int hashCode() {
return Objects.hash(toJson());
}
@Override
public String toString() {
return toJson();
}
}