From 43bed5110e75497fb2ded96912a87c4a9d0841fd Mon Sep 17 00:00:00 2001 From: Jeronimo Backes Date: Tue, 14 May 2019 15:58:14 -0300 Subject: [PATCH] fixed github issue #326 --- .../univocity/parsers/common/StringCache.java | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/univocity/parsers/common/StringCache.java b/src/main/java/com/univocity/parsers/common/StringCache.java index e889f87e..819faba7 100644 --- a/src/main/java/com/univocity/parsers/common/StringCache.java +++ b/src/main/java/com/univocity/parsers/common/StringCache.java @@ -20,15 +20,22 @@ import java.util.concurrent.*; /** - * A simple cache of values associated with strings. + * A simple cache of values associated with strings. It is built to simply prevent generating the same value over + * and over again over a short period of time. Once its size limit is reached, the cache will be fully cleared. + * Do not use this as a general purpose caching solution. This meant for storing values that can be cheaply produced + * and re-generating them every now and then won't incur in any major performance impact. + * * @param the type of entry to be stored in the cache */ public abstract class StringCache { + private static final int DEFAULT_SIZE_LIMIT = 16384; private final Map> stringCache = new ConcurrentHashMap>(); + private int sizeLimit = DEFAULT_SIZE_LIMIT; /** * Converts a given string to a value + * * @param input the input to be converted and stored in the cache * @return the value generated from the given string/ */ @@ -36,30 +43,59 @@ public abstract class StringCache { /** * Tests whether the cache contains the given key + * * @param input a string that might have a value associated to it. * @return {@code true} if the cache contains (or contained) a value associated with the given key. */ - public boolean containsKey(String input){ + public boolean containsKey(String input) { return stringCache.containsKey(input); } + /** + * Returns the size limit of this string cache. Defaults to 16,384. For simplicity, when + * this limit is reached, the entire cache is cleared. + * + * @return the maximum number of entries that can be stored in this string cache. + */ + public int getSizeLimit() { + return sizeLimit; + } + + /** + * Defines the size limit of this string cache (16,384 by default). For simplicity, when + * this limit is reached, the entire cache is cleared. + * + * @param sizeLimit the maximum number of entries that can be stored in this string cache. + */ + public void setSizeLimit(int sizeLimit) { + if (sizeLimit <= 0) { + sizeLimit = DEFAULT_SIZE_LIMIT; + } + this.sizeLimit = sizeLimit; + } + /** * Associates a value to a string + * * @param input the string to be associated with a given value * @param value the value associated with the given string */ public void put(String input, T value) { + if (stringCache.size() >= sizeLimit) { + stringCache.clear(); + } stringCache.put(input, new SoftReference(value)); } /** * Returns the value associated with the given string. If it doesn't exist, * or if it has been evicted, a value will be populated using {@link #process(String)} + * * @param input the string whose associated value will be returned * @return the value associated with the given string. */ public T get(String input) { - if(input == null){ + if (input == null) { return null; } SoftReference ref = stringCache.get(input); @@ -73,4 +109,8 @@ public T get(String input) { } return out; } + + public void clear() { + stringCache.clear(); + } }