reuse existing PdbFiles

This commit is contained in:
2018-10-03 16:49:46 +02:00
parent 60578b45ec
commit c9dcc77b53

View File

@@ -93,7 +93,7 @@ public class TagsToFile implements AutoCloseable {
final CacheConfigurationBuilder<CacheKey, PdbWriter> cacheConfiguration = CacheConfigurationBuilder final CacheConfigurationBuilder<CacheKey, PdbWriter> cacheConfiguration = CacheConfigurationBuilder
.newCacheConfigurationBuilder(CacheKey.class, PdbWriter.class, ResourcePoolsBuilder.heap(1000)) .newCacheConfigurationBuilder(CacheKey.class, PdbWriter.class, ResourcePoolsBuilder.heap(1000))
.withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMinutes(1))) .withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(10)))
.add(cacheEventListenerConfiguration); .add(cacheEventListenerConfiguration);
final CacheManager cacheManager = CacheManagerBuilder.newCacheManagerBuilder() 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 { public PdbWriter getWriter(final long dateAsEpochMilli, final Tags tags) throws ReadException, WriteException {
final CacheKey cacheKey = new CacheKey(tags); final CacheKey cacheKey = new CacheKey(tags);
PdbWriter cachedWriter = writerCache.get(cacheKey); PdbWriter writer = writerCache.get(cacheKey);
if (cachedWriter == null) { if (writer == null) {
synchronized (this) { synchronized (this) {
cachedWriter = writerCache.get(cacheKey); writer = writerCache.get(cacheKey);
if (cachedWriter == null) { if (writer == null) {
cachedWriter = newPdbWriter(tags);
writerCache.put(cacheKey, cachedWriter); LOGGER.info("getByTags({})", tags);
final List<Doc> 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() { public void clearWriterCache() {
@@ -159,7 +172,6 @@ public class TagsToFile implements AutoCloseable {
METRICS_LOGGER_NEW_WRITER.debug("newPdbWriter took {}ms tags: {}", METRICS_LOGGER_NEW_WRITER.debug("newPdbWriter took {}ms tags: {}",
(System.nanoTime() - start) / 1_000_000.0, tags); (System.nanoTime() - start) / 1_000_000.0, tags);
return result; return result;
} catch (final IOException e) { } catch (final IOException e) {
throw new WriteException(e); throw new WriteException(e);