forked from oras-project/oras-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIndex.java
More file actions
178 lines (159 loc) · 5.16 KB
/
Index.java
File metadata and controls
178 lines (159 loc) · 5.16 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
/*-
* =LICENSE=
* ORAS Java SDK
* ===
* Copyright (C) 2024 - 2025 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 java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import land.oras.utils.Const;
import land.oras.utils.JsonUtils;
/**
* Index from an OCI layout
*/
public final class Index extends Descriptor {
private final int schemaVersion;
private final List<ManifestDescriptor> manifests;
/**
* Original json
*/
private transient String json;
/**
* The manifest descriptor
*/
private final transient ManifestDescriptor descriptor;
private Index(
int schemaVersion,
String mediaType,
String artifactType,
List<ManifestDescriptor> manifests,
ManifestDescriptor descriptor,
String json) {
super(null, null, mediaType, Map.of(), artifactType);
this.schemaVersion = schemaVersion;
this.descriptor = descriptor;
this.manifests = manifests;
this.json = json;
}
/**
* 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;
}
/**
* 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, descriptor, json);
}
/**
* Get the descriptor
* @return The descriptor
*/
public ManifestDescriptor getDescriptor() {
return descriptor;
}
/**
* 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, descriptor, json);
}
/**
* Return same instance but with original JSON
* @param json The original JSON
* @return The index
*/
private Index withJson(String json) {
this.json = json;
return this;
}
/**
* Return the original JSON
* @return The original JSON
*/
public String getJson() {
return 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);
}
}