From c9dcc77b53c38a3beae853f5f22a9ef8bfffd8bc Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Wed, 3 Oct 2018 16:49:46 +0200 Subject: [PATCH] reuse existing PdbFiles --- .../lucares/performance/db/TagsToFile.java | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/performanceDb/src/main/java/org/lucares/performance/db/TagsToFile.java b/performanceDb/src/main/java/org/lucares/performance/db/TagsToFile.java index 6c9c434..37a6b8f 100644 --- a/performanceDb/src/main/java/org/lucares/performance/db/TagsToFile.java +++ b/performanceDb/src/main/java/org/lucares/performance/db/TagsToFile.java @@ -93,7 +93,7 @@ public class TagsToFile implements AutoCloseable { final CacheConfigurationBuilder cacheConfiguration = CacheConfigurationBuilder .newCacheConfigurationBuilder(CacheKey.class, PdbWriter.class, ResourcePoolsBuilder.heap(1000)) - .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMinutes(1))) + .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(10))) .add(cacheEventListenerConfiguration); final CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() @@ -131,18 +131,31 @@ public class TagsToFile implements AutoCloseable { public PdbWriter getWriter(final long dateAsEpochMilli, final Tags tags) throws ReadException, WriteException { final CacheKey cacheKey = new CacheKey(tags); - PdbWriter cachedWriter = writerCache.get(cacheKey); - if (cachedWriter == null) { + PdbWriter writer = writerCache.get(cacheKey); + if (writer == null) { synchronized (this) { - cachedWriter = writerCache.get(cacheKey); - if (cachedWriter == null) { - cachedWriter = newPdbWriter(tags); - writerCache.put(cacheKey, cachedWriter); + writer = writerCache.get(cacheKey); + if (writer == null) { + + LOGGER.info("getByTags({})", tags); + final List docsForTags = db.getByTags(tags); + if (docsForTags.size() > 0) { + try { + final Doc doc = docsForTags.get(0); + final PdbFile pdbFile = new PdbFile(doc.getRootBlockNumber(), tags); + writer = new PdbWriter(pdbFile, db.getDiskStorage()); + } catch (final IOException e) { + throw new ReadException(e); + } + } else { + writer = newPdbWriter(tags); + } + writerCache.put(cacheKey, writer); } } } - return cachedWriter; + return writer; } public void clearWriterCache() { @@ -159,7 +172,6 @@ public class TagsToFile implements AutoCloseable { METRICS_LOGGER_NEW_WRITER.debug("newPdbWriter took {}ms tags: {}", (System.nanoTime() - start) / 1_000_000.0, tags); - return result; } catch (final IOException e) { throw new WriteException(e);