CleanRepositoryCache implements Job { @Override public void execute(JobContext context) { log.trace("Starting CleanRepositoryCache Job"); long startTime = System.currentTimeMillis(); Repository cacheRepo = repositoryManager.getRepository(repoId) .orElseThrow(() -> new IllegalStateException("Ontology Cache Repository" + repoId + " must exist")); try (RepositoryConnection conn = cacheRepo.getConnection()) { List<Statement> statements = RepositoryResults.asList( conn.getStatements(null, vf.createIRI(OntologyDatasets.TIMESTAMP_IRI_STRING), null)); OffsetDateTime now = OffsetDateTime.now(); statements.forEach(statement -> { if (now.isAfter(OffsetDateTime.parse(statement.getObject().stringValue()).plusSeconds(expirySeconds))) { log.debug("Evicting expired dataset: " + statement.getSubject().stringValue()); datasetManager.safeDeleteDataset(statement.getSubject(), repoId, false); } }); } log.trace("CleanRepositoryCache Job complete in " + (System.currentTimeMillis() - startTime) + " ms"); }  @Reference void setDatasetManager(DatasetManager datasetManager); @Reference void setRepositoryManager(RepositoryManager repositoryManager); @Reference void setValueFactory(ValueFactory vf); @Activate void start(Map<String, Object> props); @Override void execute(JobContext context);  }
@Test public void executeNothingExpiredTest() { try (RepositoryConnection conn = repo.getConnection()) { conn.add(dataset1, vf.createIRI(OntologyDatasets.TIMESTAMP_IRI_STRING), vf.createIRI(OffsetDateTime.now().plusSeconds(10000).toString())); conn.add(dataset2, vf.createIRI(OntologyDatasets.TIMESTAMP_IRI_STRING), vf.createIRI(OffsetDateTime.now().plusSeconds(10000).toString())); } cleanJob.execute(jobContext); verify(repoManager).getRepository("ontologyCache"); verify(datasetManager, never()).safeDeleteDataset(dataset1, "ontologyCache", false); verify(datasetManager, never()).safeDeleteDataset(dataset2, "ontologyCache", false); }
@Test public void executeOneExpiredTest() { try (RepositoryConnection conn = repo.getConnection()) { conn.add(dataset1, vf.createIRI(OntologyDatasets.TIMESTAMP_IRI_STRING), vf.createIRI(OffsetDateTime.now().minusSeconds(10000).toString())); conn.add(dataset2, vf.createIRI(OntologyDatasets.TIMESTAMP_IRI_STRING), vf.createIRI(OffsetDateTime.now().plusSeconds(10000).toString())); } cleanJob.execute(jobContext); verify(repoManager).getRepository("ontologyCache"); verify(datasetManager).safeDeleteDataset(dataset1, "ontologyCache", false); verify(datasetManager, never()).safeDeleteDataset(dataset2, "ontologyCache", false); }
@Test public void executeAllExpiredTest() { try (RepositoryConnection conn = repo.getConnection()) { conn.add(dataset1, vf.createIRI(OntologyDatasets.TIMESTAMP_IRI_STRING), vf.createIRI(OffsetDateTime.now().minusSeconds(10000).toString())); conn.add(dataset2, vf.createIRI(OntologyDatasets.TIMESTAMP_IRI_STRING), vf.createIRI(OffsetDateTime.now().minusSeconds(10000).toString())); } cleanJob.execute(jobContext); verify(repoManager).getRepository("ontologyCache"); verify(datasetManager).safeDeleteDataset(dataset1, "ontologyCache", false); verify(datasetManager).safeDeleteDataset(dataset2, "ontologyCache", false); }
@Test public void executeEmptyRepoTest() { cleanJob.execute(jobContext); verify(repoManager).getRepository("ontologyCache"); verify(datasetManager, never()).safeDeleteDataset(any(Resource.class), anyString(), anyBoolean()); }