VanillaDataCache implements Closeable { public synchronized VanillaMappedBytes dataFor(int cycle, int threadId, int dataCount, boolean forWrite) throws IOException { key.cycle = cycle; key.threadId = threadId; key.dataCount = dataCount; VanillaMappedBytes vmb = this.cache.get(key); if(vmb == null) { vmb = this.cache.put( key.clone(), VanillaChronicleUtils.mkFiles( basePath, dateCache.formatFor(cycle), FILE_NAME_PREFIX + threadId + "-" + dataCount, forWrite), 1L << blockBits, dataCount); } vmb.reserve(); return vmb; } VanillaDataCache(@NotNull String basePath, int blockBits, @NotNull DateCache dateCache);  VanillaDataCache(@NotNull String basePath, int blockBits, @NotNull DateCache dateCache, @NotNull VanillaChronicleConfig config);  VanillaDataCache(@NotNull String basePath, int blockBits, @NotNull DateCache dateCache, int capacity);  VanillaDataCache(@NotNull String basePath, int blockBits, @NotNull DateCache dateCache, int capacity, boolean cleanupOnClose); File fileFor(int cycle, int threadId, int dataCount, boolean forWrite); File fileFor(int cycle, int threadId); synchronized VanillaMappedBytes dataFor(int cycle, int threadId, int dataCount, boolean forWrite); @Override synchronized void close(); int findNextDataCount(int cycle, int threadId); void checkCounts(int min, int max);  }
@Test public void testDataFor() throws Exception { final String baseDir = getTestPath(); assertNotNull(baseDir); final DateCache dateCache = new DateCache("yyyyMMddHHmmss", 1000); final VanillaDataCache cache = new VanillaDataCache(baseDir, 10 + 6, dateCache); try { int cycle = (int) (System.currentTimeMillis() / 1000); VanillaMappedBytes vanillaBuffer0 = cache.dataFor(cycle, AffinitySupport.getThreadId(), 0, true); vanillaBuffer0.writeLong(0, 0x12345678); File file0 = cache.fileFor(cycle, AffinitySupport.getThreadId(), 0, true); assertEquals(64 << 10, file0.length()); assertEquals(0x12345678L, vanillaBuffer0.readLong(0)); vanillaBuffer0.release(); VanillaMappedBytes vanillaBuffer1 = cache.dataFor(cycle, AffinitySupport.getThreadId(), 1, true); File file1 = cache.fileFor(cycle, AffinitySupport.getThreadId(), 1, true); assertEquals(64 << 10, file1.length()); vanillaBuffer1.release(); assertNotEquals(file1, file0); VanillaMappedBytes vanillaBuffer2 = cache.dataFor(cycle, AffinitySupport.getThreadId(), 2, true); File file2 = cache.fileFor(cycle, AffinitySupport.getThreadId(), 2, true); assertEquals(64 << 10, file2.length()); vanillaBuffer2.release(); assertNotEquals(file2, file0); assertNotEquals(file2, file1); cache.close(); assertEquals(0, vanillaBuffer0.refCount()); assertEquals(0, vanillaBuffer1.refCount()); assertEquals(0, vanillaBuffer2.refCount()); assertTrue(file0.delete()); assertTrue(file1.delete()); assertTrue(file2.delete()); assertTrue(file0.getParentFile().delete()); cache.checkCounts(1, 1); } finally { cache.close(); IOTools.deleteDir(baseDir); assertFalse(new File(baseDir).exists()); } }