Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
stop revoked cert sequence from incrementing on every upsert
Browse files Browse the repository at this point in the history
  • Loading branch information
ubhaller committed Jul 22, 2021
1 parent 0a00ba1 commit 99e9bf3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
import ch.admin.bag.covidcertificate.backend.verifier.data.mapper.RevokedCertRowMapper;
import ch.admin.bag.covidcertificate.backend.verifier.model.DbRevokedCert;
import ch.admin.bag.covidcertificate.backend.verifier.model.cert.db.RevokedCertsUpdateResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.transaction.annotation.Transactional;

public class JdbcRevokedCertDataServiceImpl implements RevokedCertDataService {
Expand All @@ -31,30 +32,38 @@ public class JdbcRevokedCertDataServiceImpl implements RevokedCertDataService {

private final int revokedCertBatchSize;
private final NamedParameterJdbcTemplate jt;
private final SimpleJdbcInsert revokedCertInsert;

public JdbcRevokedCertDataServiceImpl(DataSource dataSource, int revokedCertBatchSize) {
this.jt = new NamedParameterJdbcTemplate(dataSource);
this.revokedCertBatchSize = revokedCertBatchSize;
this.revokedCertInsert =
new SimpleJdbcInsert(dataSource)
.withTableName("t_revoked_cert")
.usingGeneratedKeyColumns("pk_revoked_cert_id");
}

@Transactional(readOnly = false)
@Override
public RevokedCertsUpdateResponse replaceRevokedCerts(List<String> revokedUvcis) {
int insertCount = upsertRevokedCerts(revokedUvcis);
int insertCount = insertNewRevokedCerts(revokedUvcis);
int removeCount = removeRevokedCertsNotIn(revokedUvcis);
return new RevokedCertsUpdateResponse(insertCount, removeCount);
}

private int upsertRevokedCerts(List<String> revokedUvcis) {
private int insertNewRevokedCerts(List<String> revokedUvcis) {
if (revokedUvcis != null && !revokedUvcis.isEmpty()) {
String sql =
"insert into t_revoked_cert"
+ " (uvci)"
+ " values (:uvci)"
+ " on conflict (uvci)"
+ " do nothing";
int[] updateCounts = jt.batchUpdate(sql, createParams(revokedUvcis));
return Arrays.stream(updateCounts).sum();
List<String> existingUvcis =
jt.queryForList(
"select uvci from t_revoked_cert",
new MapSqlParameterSource(),
String.class);
List<String> toInsert =
revokedUvcis.stream()
.filter(uvci -> !existingUvcis.contains(uvci))
.collect(Collectors.toList());
revokedCertInsert.executeBatch(createParams(toInsert));
return toInsert.size();
} else {
return 0;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Created by Ubique Innovation AG
* https://www.ubique.ch
* Copyright (c) 2021. All rights reserved.
*/

delete from t_revoked_cert;
select setval('t_revoked_cert_pk_revoked_cert_id_seq', 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/*
* Created by Ubique Innovation AG
* https://www.ubique.ch
* Copyright (c) 2021. All rights reserved.
*/

delete from t_revoked_cert;
select setval('t_revoked_cert_pk_revoked_cert_id_seq', 1);
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public SyncSchedulingBaseConfig(DgcSyncer dgcSyncer) {
@Scheduled(cron = "${dgc.sync.cron}")
@SchedulerLock(name = "DGC_download", lockAtLeastFor = "PT15S")
public void dgcSyncCron() {
LockAssert.assertLocked();
dgcSyncer.sync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ public SchedulingConfig(RevocationListSyncer revocationListSyncer) {
this.revocationListSyncer = revocationListSyncer;
}

// Sync revocation list on start up
@Scheduled(fixedDelay = Long.MAX_VALUE, initialDelay = 0)
@SchedulerLock(name = "revocation_list_sync", lockAtLeastFor = "PT15S")
public void syncRevocationListOnStartup() {
LockAssert.assertLocked();
revocationListSyncer.updateRevokedCerts();
}

// Sync revocation list every full hour (default)
@Scheduled(cron = "${revocationList.sync.cron:0 0 * ? * *}")
@SchedulerLock(name = "revocation_list_sync", lockAtLeastFor = "PT15S")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,15 @@
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("")
@Documentation(description = "mocks endpoints that are not available during local development")
public class DevController {

private static final String NEXT_SINCE_HEADER = "X-Next-Since";
private static final String UP_TO_DATE_HEADER = "up-to-date";

@GetMapping(value = "/v1/revocation-list")
public @ResponseBody ResponseEntity<List<String>> getMockRevokedCerts(
@RequestParam(required = false) String since) {
public @ResponseBody ResponseEntity<List<String>> getMockRevokedCerts() {
List<String> response = new ArrayList<>();
for (int i = 0; i < 10; i++) {
response.add("urn:uvci:01:CH:MOCK" + i);
Expand Down

0 comments on commit 99e9bf3

Please sign in to comment.