FileSystemDataSetMetadataRepository extends ObjectDataSetMetadataRepository { @Override public void save(DataSetMetadata metadata) { String id = metadata.getId(); ReentrantReadWriteLock lock = locks.getLock(id); final File file = getFile(id); lock.writeLock().lock(); try (GZIPOutputStream output = new GZIPOutputStream(new FileOutputStream(file))) { mapper.writer().writeValue(output, metadata); } catch (IOException e) { LOG.error("Error saving {}", metadata, e); throw new TDPException(DataSetErrorCodes.UNABLE_TO_STORE_DATASET_METADATA, e, ExceptionContext.build().put("id", metadata.getId())); } finally { lock.writeLock().unlock(); } }  @Override void save(DataSetMetadata metadata); @Override DataSetMetadata get(String id); @Override void remove(String id); @Override Stream<DataSetMetadata> source();  }
@Test public void match() throws Exception { repository.save(getMetadata("456789")); final boolean match = repository.exist("id = 456789"); final boolean noMatch = repository.exist("id = 1234"); assertTrue(match); assertFalse(noMatch); }
@Test public void matchOnNested() throws Exception { final DataSetMetadata metadata = getMetadata("456789"); metadata.getLifecycle().setImporting(true); repository.save(metadata); final boolean match = repository.exist("lifecycle.importing = true"); final boolean noMatch = repository.exist("lifecycle.importing = false"); assertTrue(match); assertFalse(noMatch); }
@Test public void shouldList() throws IOException { final Iterator<DataSetMetadata> emptyList = repository.list().iterator(); assertFalse(emptyList.hasNext()); int expected = 26; for (int i = 1; i <= expected; i++) { repository.save(getMetadata(String.valueOf(i))); } final Iterator<DataSetMetadata> actual = repository.list().iterator(); final AtomicInteger count = new AtomicInteger(0); actual.forEachRemaining(dataSetMetadata -> { assertTrue(Integer.valueOf(dataSetMetadata.getId()) <= expected); count.addAndGet(1); }); assertEquals(expected, count.intValue()); }
@Test public void shouldClear() throws IOException { assertEquals(0, repository.size()); int count = 11; for (int i = 1; i <= count; i++) { repository.save(getMetadata(String.valueOf(i))); } assertEquals(count, repository.size()); repository.clear(); assertEquals(0, repository.size()); }
@Test public void testCountAllDataSetsSizeWhenRepoContainsMultipleElements() throws Exception { for (int i = 1; i <= 3; i++) { repository.save(getMetadata(String.valueOf(i))); } DataSetMetadata metadataWithDifferentDataSetSize = getMetadata("4"); metadataWithDifferentDataSetSize.setDataSetSize(5127892); repository.save(metadataWithDifferentDataSetSize); long totalSize = repository.countAllDataSetsSize(); assertThat(totalSize, is(5488927L)); }