1
1
package io .apicurio .registry .ccompat .rest .v7 .impl ;
2
2
3
3
4
+ import java .util .Collections ;
5
+ import java .util .List ;
6
+ import java .util .Map ;
7
+ import java .util .function .Function ;
8
+ import java .util .stream .Collectors ;
9
+
10
+ import org .apache .avro .AvroTypeException ;
11
+ import org .apache .avro .SchemaParseException ;
12
+ import org .apache .commons .codec .digest .DigestUtils ;
13
+ import org .slf4j .Logger ;
14
+
4
15
import io .apicurio .registry .ccompat .dto .SchemaReference ;
5
16
import io .apicurio .registry .ccompat .rest .error .ConflictException ;
6
17
import io .apicurio .registry .ccompat .rest .error .UnprocessableEntityException ;
7
18
import io .apicurio .registry .content .ContentHandle ;
19
+ import io .apicurio .registry .model .BranchId ;
20
+ import io .apicurio .registry .model .GA ;
21
+ import io .apicurio .registry .model .GAV ;
8
22
import io .apicurio .registry .rest .v3 .beans .ArtifactReference ;
9
23
import io .apicurio .registry .rules .RuleApplicationType ;
10
24
import io .apicurio .registry .rules .RuleViolationException ;
11
25
import io .apicurio .registry .rules .RulesService ;
12
26
import io .apicurio .registry .storage .RegistryStorage ;
13
- import io .apicurio .registry .storage .dto . ArtifactMetaDataDto ;
27
+ import io .apicurio .registry .storage .RegistryStorage . ArtifactRetrievalBehavior ;
14
28
import io .apicurio .registry .storage .dto .ArtifactReferenceDto ;
15
29
import io .apicurio .registry .storage .dto .ArtifactVersionMetaDataDto ;
16
- import io .apicurio .registry .storage .dto .StoredArtifactDto ;
30
+ import io .apicurio .registry .storage .dto .StoredArtifactVersionDto ;
17
31
import io .apicurio .registry .storage .error .ArtifactNotFoundException ;
18
32
import io .apicurio .registry .storage .error .RuleNotFoundException ;
19
33
import io .apicurio .registry .storage .error .VersionNotFoundException ;
20
- import io .apicurio .registry .types .ArtifactState ;
21
34
import io .apicurio .registry .types .ArtifactType ;
22
35
import io .apicurio .registry .types .Current ;
23
36
import io .apicurio .registry .types .RuleType ;
37
+ import io .apicurio .registry .types .VersionState ;
24
38
import io .apicurio .registry .types .provider .ArtifactTypeUtilProvider ;
25
39
import io .apicurio .registry .types .provider .ArtifactTypeUtilProviderFactory ;
26
- import org .apache .avro .AvroTypeException ;
27
- import org .apache .avro .SchemaParseException ;
28
- import org .apache .commons .codec .digest .DigestUtils ;
29
- import org .slf4j .Logger ;
30
-
31
40
import jakarta .inject .Inject ;
32
41
33
- import java .util .Collections ;
34
- import java .util .List ;
35
- import java .util .Map ;
36
- import java .util .function .Function ;
37
- import java .util .stream .Collectors ;
38
-
39
- import static io .apicurio .registry .storage .RegistryStorage .ArtifactRetrievalBehavior .DEFAULT ;
40
-
41
42
public abstract class AbstractResource {
42
43
43
44
@ Inject
@@ -59,8 +60,8 @@ public abstract class AbstractResource {
59
60
@ Inject
60
61
ArtifactTypeUtilProviderFactory factory ;
61
62
62
- protected ArtifactMetaDataDto createOrUpdateArtifact (String subject , String schema , String artifactType , List <SchemaReference > references , String groupId ) {
63
- ArtifactMetaDataDto res ;
63
+ protected ArtifactVersionMetaDataDto createOrUpdateArtifact (String subject , String schema , String artifactType , List <SchemaReference > references , String groupId ) {
64
+ ArtifactVersionMetaDataDto res ;
64
65
final List <ArtifactReferenceDto > parsedReferences = parseReferences (references , groupId );
65
66
final List <ArtifactReference > artifactReferences = parsedReferences .stream ().map (dto -> ArtifactReference .builder ().name (dto .getName ()).groupId (dto .getGroupId ()).artifactId (dto .getArtifactId ()).version (dto .getVersion ()).build ()).collect (Collectors .toList ());
66
67
final Map <String , ContentHandle > resolvedReferences = storage .resolveReferences (parsedReferences );
@@ -73,7 +74,7 @@ protected ArtifactMetaDataDto createOrUpdateArtifact(String subject, String sche
73
74
res = storage .createArtifact (groupId , subject , null , artifactType , schemaContent , parsedReferences );
74
75
} else {
75
76
rulesService .applyRules (groupId , subject , artifactType , schemaContent , RuleApplicationType .UPDATE , artifactReferences , resolvedReferences );
76
- res = storage .updateArtifact (groupId , subject , null , artifactType , schemaContent , parsedReferences );
77
+ res = storage .createArtifactVersion (groupId , subject , null , artifactType , schemaContent , parsedReferences );
77
78
}
78
79
} catch (RuleViolationException ex ) {
79
80
if (ex .getRuleType () == RuleType .VALIDITY ) {
@@ -95,15 +96,15 @@ protected ArtifactVersionMetaDataDto lookupSchema(String groupId, String subject
95
96
96
97
if (cconfig .canonicalHashModeEnabled .get () || normalize ) {
97
98
try {
98
- amd = storage .getArtifactVersionMetaData (groupId , subject , true , ContentHandle .create (schema ), artifactReferences );
99
+ amd = storage .getArtifactVersionMetaDataByContent (groupId , subject , true , ContentHandle .create (schema ), artifactReferences );
99
100
} catch (ArtifactNotFoundException ex ) {
100
101
if (type .equals (ArtifactType .AVRO )) {
101
102
//When comparing using content, sometimes the references might be inlined into the content, try to dereference the existing content and compare as a fallback. See https://github.com/Apicurio/apicurio-registry/issues/3588 for more information.
102
103
//If using this method there is no matching content either, just re-throw the exception.
103
104
//This approach only works for schema types with dereference support (for now, only Avro in the ccompat API).
104
105
amd = storage .getArtifactVersions (groupId , subject )
105
106
.stream ().filter (version -> {
106
- StoredArtifactDto artifactVersion = storage .getArtifactVersion (groupId , subject , version );
107
+ StoredArtifactVersionDto artifactVersion = storage .getArtifactVersionContent (groupId , subject , version );
107
108
Map <String , ContentHandle > artifactVersionReferences = storage .resolveReferences (artifactVersion .getReferences ());
108
109
String dereferencedExistingContentSha = DigestUtils .sha256Hex (artifactTypeProvider .getContentDereferencer ().dereference (artifactVersion .getContent (), artifactVersionReferences ).content ());
109
110
return dereferencedExistingContentSha .equals (DigestUtils .sha256Hex (schema ));
@@ -117,7 +118,7 @@ protected ArtifactVersionMetaDataDto lookupSchema(String groupId, String subject
117
118
}
118
119
119
120
} else {
120
- amd = storage .getArtifactVersionMetaData (groupId , subject , false , ContentHandle .create (schema ), artifactReferences );
121
+ amd = storage .getArtifactVersionMetaDataByContent (groupId , subject , false , ContentHandle .create (schema ), artifactReferences );
121
122
}
122
123
123
124
return amd ;
@@ -150,43 +151,38 @@ protected Map<String, ContentHandle> resolveReferences(List<SchemaReference> ref
150
151
return resolvedReferences ;
151
152
}
152
153
153
- protected boolean isArtifactActive (String subject , String groupId , RegistryStorage . ArtifactRetrievalBehavior retrievalBehavior ) {
154
- final ArtifactState state = storage .getArtifactMetaData (groupId , subject , retrievalBehavior ). getState ( );
155
- return storage . isArtifactExists ( groupId , subject ) && ( state . equals ( ArtifactState . ENABLED ) || state . equals ( ArtifactState . DEPRECATED )) ;
154
+ protected boolean isArtifactActive (String subject , String groupId ) {
155
+ long count = storage .countActiveArtifactVersions (groupId , subject );
156
+ return count > 0 ;
156
157
}
157
158
158
159
protected String getLatestArtifactVersionForSubject (String subject , String groupId ) {
159
160
try {
160
- ArtifactMetaDataDto latest = storage .getArtifactMetaData ( groupId , subject );
161
- return latest . getVersion ();
161
+ GAV latestGAV = storage .getArtifactBranchTip ( new GA ( groupId , subject ), BranchId . LATEST , ArtifactRetrievalBehavior . SKIP_DISABLED_LATEST );
162
+ return latestGAV . getRawVersionId ();
162
163
} catch (ArtifactNotFoundException ex ) {
163
164
throw new VersionNotFoundException (groupId , subject , "latest" );
164
165
}
165
166
}
166
167
167
- protected boolean shouldFilterState (boolean deleted , ArtifactState state ) {
168
+ protected boolean shouldFilterState (boolean deleted , VersionState state ) {
168
169
if (deleted ) {
169
170
//if deleted is enabled, just return all states
170
171
return true ;
171
172
} else {
172
- return state .equals (ArtifactState .ENABLED );
173
+ return state .equals (VersionState .ENABLED );
173
174
}
174
175
}
175
176
176
177
protected boolean areAllSchemasDisabled (List <Long > globalIds ) {
177
178
return globalIds .stream ().anyMatch (globalId -> {
178
- ArtifactState state = storage .getArtifactMetaData (globalId ).getState ();
179
- return state .equals (ArtifactState .DISABLED );
179
+ VersionState state = storage .getArtifactVersionMetaData (globalId ).getState ();
180
+ return state .equals (VersionState .DISABLED );
180
181
});
181
182
}
182
183
183
184
protected boolean doesArtifactExist (String artifactId , String groupId ) {
184
- try {
185
- storage .getArtifact (groupId , artifactId , DEFAULT );
186
- return true ;
187
- } catch (ArtifactNotFoundException ignored ) {
188
- return false ;
189
- }
185
+ return storage .isArtifactExists (groupId , artifactId );
190
186
}
191
187
192
188
protected boolean doesArtifactRuleExist (String artifactId , RuleType type , String groupId ) {
0 commit comments