Skip to content

Commit 66ed7d6

Browse files
committed
Use SQL upsert when adding or creating branches
1 parent 0204a37 commit 66ed7d6

File tree

5 files changed

+79
-28
lines changed

5 files changed

+79
-28
lines changed

app/src/main/java/io/apicurio/registry/storage/impl/sql/AbstractSqlRegistryStorage.java

+5-9
Original file line numberDiff line numberDiff line change
@@ -2989,7 +2989,7 @@ public BranchMetaDataDto createBranch(GA ga, BranchId branchId, String descripti
29892989
Date now = new Date();
29902990

29912991
handles.withHandle(handle -> {
2992-
// Insert a row into the groups table
2992+
// Insert a row into the branches table
29932993
handle.createUpdate(sqlStatements.insertBranch()).bind(0, ga.getRawGroupId())
29942994
.bind(1, ga.getRawArtifactId()).bind(2, branchId.getRawBranchId())
29952995
.bind(3, description).bind(4, false).bind(5, user).bind(6, now).bind(7, user)
@@ -3226,7 +3226,7 @@ public void appendVersionToBranch(GA ga, BranchId branchId, VersionId version) {
32263226

32273227
private void appendVersionToBranchRaw(Handle handle, GA ga, BranchId branchId, VersionId version) {
32283228
try {
3229-
// Insert a row into the groups table
3229+
// Insert a row into the branch_versions table
32303230
handle.createUpdate(sqlStatements.appendBranchVersion()).bind(0, ga.getRawGroupId())
32313231
.bind(1, ga.getRawArtifactId()).bind(2, branchId.getRawBranchId())
32323232
.bind(3, version.getRawVersionId()).bind(4, ga.getRawGroupId())
@@ -3276,18 +3276,14 @@ private void createOrUpdateBranchRaw(Handle handle, GAV gav, BranchId branchId,
32763276
String user = securityIdentity.getPrincipal().getName();
32773277
Date now = new Date();
32783278

3279-
handle.createUpdate(sqlStatements.insertBranch()).bind(0, gav.getRawGroupId())
3279+
handle.createUpdate(sqlStatements.upsertBranch()).bind(0, gav.getRawGroupId())
32803280
.bind(1, gav.getRawArtifactId()).bind(2, branchId.getRawBranchId()).bind(3, (String) null)
32813281
.bind(4, systemDefined).bind(5, user).bind(6, now).bind(7, user).bind(8, now).execute();
32823282
} catch (Exception ex) {
3283-
java.lang.System.out.println("================ createOrUpdateBranchRaw ======================");
3283+
// Only needed for H2, which doesn't support upsert (check this, it might now)
32843284
if (!sqlStatements.isPrimaryKeyViolation(ex)) {
3285-
java.lang.System.out.println(" RETHROW: " + ex.getMessage());
3286-
ex.printStackTrace();
32873285
throw ex;
32883286
}
3289-
java.lang.System.out.println(" !IGNORED!");
3290-
java.lang.System.out.println("===============================================================");
32913287
}
32923288

32933289
// Now add the version to it.
@@ -3358,7 +3354,7 @@ public void importBranch(BranchEntity entity) {
33583354
throw new ArtifactNotFoundException(ga.getRawGroupIdWithDefaultString(),
33593355
ga.getRawArtifactId());
33603356
}
3361-
handle.createUpdate(sqlStatements.insertBranch()).bind(0, ga.getRawGroupId())
3357+
handle.createUpdate(sqlStatements.importBranch()).bind(0, ga.getRawGroupId())
33623358
.bind(1, ga.getRawArtifactId()).bind(2, branchId.getRawBranchId())
33633359
.bind(3, entity.description).bind(4, entity.systemDefined).bind(5, entity.owner)
33643360
.bind(6, new Date(entity.createdOn)).bind(7, entity.modifiedBy)

app/src/main/java/io/apicurio/registry/storage/impl/sql/CommonSqlStatements.java

+12
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,18 @@ public String selectGAVByGlobalId() {
10351035

10361036
@Override
10371037
public String insertBranch() {
1038+
return "INSERT INTO branches (groupId, artifactId, branchId, description, systemDefined, owner, createdOn, modifiedBy, modifiedOn) "
1039+
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ";
1040+
}
1041+
1042+
@Override
1043+
public String upsertBranch() {
1044+
return "INSERT INTO branches (groupId, artifactId, branchId, description, systemDefined, owner, createdOn, modifiedBy, modifiedOn) "
1045+
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ";
1046+
}
1047+
1048+
@Override
1049+
public String importBranch() {
10381050
return "INSERT INTO branches (groupId, artifactId, branchId, description, systemDefined, owner, createdOn, modifiedBy, modifiedOn) "
10391051
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
10401052
}

app/src/main/java/io/apicurio/registry/storage/impl/sql/PostgreSQLSqlStatements.java

+9
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ public String upsertContentReference() {
7676
return "INSERT INTO content_references (contentId, groupId, artifactId, version, name) VALUES (?, ?, ?, ?, ?) ON CONFLICT (contentId, name) DO NOTHING";
7777
}
7878

79+
@Override
80+
public String upsertBranch() {
81+
return """
82+
INSERT INTO branches (groupId, artifactId, branchId, description, systemDefined, owner, createdOn, modifiedBy, modifiedOn)
83+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
84+
ON CONFLICT (groupId, artifactId, branchId) DO NOTHING
85+
""";
86+
}
87+
7988
@Override
8089
public String createDataSnapshot() {
8190
throw new IllegalStateException("Snapshot creation is not supported for Postgresql storage");

app/src/main/java/io/apicurio/registry/storage/impl/sql/SQLServerSqlStatements.java

+49-19
Original file line numberDiff line numberDiff line change
@@ -44,51 +44,81 @@ public String isDatabaseInitialized() {
4444
return "SELECT count(*) AS count FROM information_schema.tables WHERE table_name = 'artifacts'";
4545
}
4646

47+
@Override
48+
public String upsertBranch() {
49+
return """
50+
MERGE INTO branches AS target
51+
USING (VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)) AS source (groupId, artifactId, branchId, description, systemDefined, owner, createdOn, modifiedBy, modifiedOn)
52+
ON (target.groupId = source.groupId AND target.artifactId = source.artifactId AND target.branchId = source.branchId)
53+
WHEN NOT MATCHED THEN
54+
INSERT (groupId, artifactId, branchId, description, systemDefined, owner, createdOn, modifiedBy, modifiedOn)
55+
VALUES (source.groupId, source.artifactId, source.branchId, source.description, source.systemDefined, source.owner, source.createdOn, source.modifiedBy, source.modifiedOn)
56+
""";
57+
}
58+
4759
/**
4860
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#upsertContent()
4961
*/
5062
@Override
5163
public String upsertContent() {
52-
return String.join(" ", "MERGE INTO content AS target",
53-
"USING (VALUES (?, ?, ?, ?, ?, ?)) AS source (contentId, canonicalHash, contentHash, contentType, content, refs)",
54-
"ON (target.contentHash = source.contentHash)", "WHEN NOT MATCHED THEN",
55-
"INSERT (contentId, canonicalHash, contentHash, contentType, content, refs)",
56-
"VALUES (source.contentId, source.canonicalHash, source.contentHash, source.contentType, source.content, source.refs);");
64+
return """
65+
MERGE INTO content AS target
66+
USING (VALUES (?, ?, ?, ?, ?, ?)) AS source (contentId, canonicalHash, contentHash, contentType, content, refs)
67+
ON (target.contentHash = source.contentHash)
68+
WHEN NOT MATCHED THEN
69+
INSERT (contentId, canonicalHash, contentHash, contentType, content, refs)
70+
VALUES (source.contentId, source.canonicalHash, source.contentHash, source.contentType, source.content, source.refs)
71+
""";
5772
}
5873

5974
/**
6075
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#getNextSequenceValue()
6176
*/
6277
@Override
6378
public String getNextSequenceValue() {
64-
return String.join(" ", "MERGE INTO sequences AS target", "USING (VALUES (?)) AS source (seqName)",
65-
"ON (target.seqName = source.seqName)", "WHEN MATCHED THEN",
66-
"UPDATE SET seqValue = target.seqValue + 1", "WHEN NOT MATCHED THEN",
67-
"INSERT (seqName, seqValue)", "VALUES (source.seqName, 1)", "OUTPUT INSERTED.seqValue;");
79+
return """
80+
MERGE INTO sequences AS target
81+
USING (VALUES (?)) AS source (seqName)
82+
ON (target.seqName = source.seqName)
83+
WHEN MATCHED THEN
84+
UPDATE SET seqValue = target.seqValue + 1
85+
WHEN NOT MATCHED THEN
86+
INSERT (seqName, seqValue)
87+
VALUES (source.seqName, 1)
88+
OUTPUT INSERTED.seqValue
89+
""";
6890
}
6991

7092
/**
7193
* @see io.apicurio.registry.storage.impl.sql.SqlStatements#resetSequenceValue()
7294
*/
7395
@Override
7496
public String resetSequenceValue() {
75-
return String.join(" ", "MERGE INTO sequences AS target",
76-
"USING (VALUES (?, ?)) AS source (seqName, seqValue)", "ON (target.seqName = source.seqName)",
77-
"WHEN MATCHED THEN", "UPDATE SET seqValue = ?", "WHEN NOT MATCHED THEN",
78-
"INSERT (seqName, seqValue)", "VALUES (source.seqName, source.seqValue)",
79-
"OUTPUT INSERTED.seqValue;");
97+
return """
98+
MERGE INTO sequences AS target
99+
USING (VALUES (?, ?)) AS source (seqName, seqValue)
100+
ON (target.seqName = source.seqName)
101+
WHEN MATCHED THEN
102+
UPDATE SET seqValue = ?
103+
WHEN NOT MATCHED THEN
104+
INSERT (seqName, seqValue)
105+
VALUES (source.seqName, source.seqValue)
106+
OUTPUT INSERTED.seqValue
107+
""";
80108
}
81109

82110
/**
83111
* @see SqlStatements#upsertContentReference()
84112
*/
85113
@Override
86114
public String upsertContentReference() {
87-
return String.join(" ", "MERGE INTO content_references AS target",
88-
"USING (VALUES (?, ?, ?, ?, ?)) AS source (contentId, groupId, artifactId, version, name)",
89-
"ON (target.contentId = source.contentId AND target.name = source.name)",
90-
"WHEN NOT MATCHED THEN", "INSERT (contentId, groupId, artifactId, version, name)",
91-
"VALUES (source.contentId, source.groupId, source.artifactId, source.version, source.name);");
115+
return """
116+
MERGE INTO content_references AS target
117+
USING (VALUES (?, ?, ?, ?, ?)) AS source (contentId, groupId, artifactId, version, name)
118+
ON (target.contentId = source.contentId AND target.name = source.name)
119+
WHEN NOT MATCHED THEN", "INSERT (contentId, groupId, artifactId, version, name)
120+
VALUES (source.contentId, source.groupId, source.artifactId, source.version, source.name)
121+
""";
92122
}
93123

94124
/**

app/src/main/java/io/apicurio/registry/storage/impl/sql/SqlStatements.java

+4
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ public interface SqlStatements {
483483

484484
public String importGroup();
485485

486+
public String importBranch();
487+
486488
public String importGroupRule();
487489

488490
public String importArtifactRule();
@@ -587,6 +589,8 @@ public interface SqlStatements {
587589

588590
public String insertBranch();
589591

592+
public String upsertBranch();
593+
590594
public String updateBranch();
591595

592596
public String selectBranch();

0 commit comments

Comments
 (0)