FileSystemFactoryStorageHistory { private void houseKeeping() { if (maxConfigurationHistory == Integer.MAX_VALUE) return; List<StoredDataMetadata> collect = getHistoryFactoryList().stream().collect(Collectors.toList()); int numToRemove = collect.size()-maxConfigurationHistory; if (numToRemove > 0) { collect.stream().sorted(Comparator.comparing(a -> a.creationTime)).limit(numToRemove).forEach(smd -> { if (!smd.isInitialFactory()) { try { Files.deleteIfExists(Paths.get(historyDirectory.toString() + "/" + smd.id + ".json")); Files.deleteIfExists(Paths.get(historyDirectory.toString() + "/" + smd.id + "_metadata.json")); cache.remove(smd.id); } catch (IOException ohno) { logger.warn("Could not remove configration files", ohno); } } }); } } FileSystemFactoryStorageHistory(Path basePath, MigrationManager<R> migrationManager);  FileSystemFactoryStorageHistory(Path basePath, MigrationManager<R> migrationManager, int maxConfigurationHistory); R getHistoryFactory(String id); Collection<StoredDataMetadata> getHistoryFactoryList(); void updateHistory(R factoryRoot, StoredDataMetadata metadata); void patchAll(DataStoragePatcher consumer, SimpleObjectMapper simpleObjectMapper);  }
@Test public void testHouseKeeping() throws IOException { Path tempDir = Paths.get("killme"); if (Files.exists(tempDir)) removeRecursive(tempDir); if (!Files.exists(tempDir)) Files.createDirectory(tempDir); try { FileSystemFactoryStorageHistory<RootFactory> history = new FileSystemFactoryStorageHistory<>(tempDir, new MigrationManager<>(RootFactory.class, ObjectMapperBuilder.build(), (root1, oldDataStorageMetadataDictionary) -> { }),2); RootFactory rf = new RootFactory().internal().finalise(); LocalDateTime time = LocalDateTime.of(2000,1,1,0,0,0); StoredDataMetadata md1 = randomMetadata(time, rf,"dummy1"); history.updateHistory(rf, md1); time = time.plusSeconds(1); StoredDataMetadata md2 = randomMetadata(time, rf,"dummy2"); history.updateHistory(rf, md2); Assertions.assertEquals(2,history.getHistoryFactoryList().size()); Assertions.assertNotNull(history.getHistoryFactory(md1.id)); Assertions.assertNotNull(history.getHistoryFactory(md2.id)); time = time.plusSeconds(1); StoredDataMetadata md3 = randomMetadata(time, rf,"dummy3"); history.updateHistory(rf, md3); Assertions.assertEquals(2,history.getHistoryFactoryList().size()); Assertions.assertNotNull(history.getHistoryFactory(md2.id)); Assertions.assertNotNull(history.getHistoryFactory(md3.id)); try { history.getHistoryFactory(md1.id); Assertions.fail("Expected exception"); } catch (Exception expected) { } Assertions.assertEquals(4,Files.list(tempDir.resolve("history")).filter(p->Files.isRegularFile(p)).count()); } finally { removeRecursive(tempDir); } }