[Schmitzm-commits] r2025 - in trunk/schmitzm-core: . src/main/java/de/schmitzm/lang src/main/java/de/schmitzm/regex

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Sun May 27 14:48:55 CEST 2012


Author: alfonx
Date: 2012-05-27 14:48:55 +0200 (Sun, 27 May 2012)
New Revision: 2025

Modified:
   trunk/schmitzm-core/pom.xml
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LimitedConcurrentHashMap.java
   trunk/schmitzm-core/src/main/java/de/schmitzm/regex/RegexCache.java
Log:
Evaluiere Apache Lucene ConcurrentLFUCache  als alternative zu einer selbstgebauten concurrent-Versino von LimitedhashMap...

Modified: trunk/schmitzm-core/pom.xml
===================================================================
--- trunk/schmitzm-core/pom.xml	2012-05-27 00:39:52 UTC (rev 2024)
+++ trunk/schmitzm-core/pom.xml	2012-05-27 12:48:55 UTC (rev 2025)
@@ -59,11 +59,11 @@
 			<scope>compile</scope>
 		</dependency>
 
-<!-- 		<dependency> -->
-<!-- 			<groupId>org.supercsv</groupId> -->
-<!-- 			<artifactId>supercsv</artifactId> -->
-<!-- 			<version>1.52</version> -->
-<!-- 		</dependency> -->
+		<!-- <dependency> -->
+		<!-- <groupId>org.supercsv</groupId> -->
+		<!-- <artifactId>supercsv</artifactId> -->
+		<!-- <version>1.52</version> -->
+		<!-- </dependency> -->
 
 		<dependency>
 			<groupId>com.toedter</groupId>
@@ -72,7 +72,7 @@
 			<type>jar</type>
 			<scope>compile</scope>
 		</dependency>
-		
+
 		<dependency>
 			<groupId>net.jini</groupId>
 			<artifactId>jini-ext</artifactId>
@@ -105,6 +105,12 @@
 			<type>jar</type>
 			<scope>compile</scope>
 		</dependency>
+		<dependency>
+			<groupId>org.apache.solr</groupId>
+			<artifactId>solr-core</artifactId>
+			<version>3.6.0</version>
+		</dependency>
+
 	</dependencies>
 
 	<build>
@@ -125,7 +131,8 @@
 
 		<pluginManagement>
 			<plugins>
-				<!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
+				<!--This plugin's configuration is used to store Eclipse m2e settings 
+					only. It has no influence on the Maven build itself. -->
 				<plugin>
 					<groupId>org.eclipse.m2e</groupId>
 					<artifactId>lifecycle-mapping</artifactId>

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LimitedConcurrentHashMap.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LimitedConcurrentHashMap.java	2012-05-27 00:39:52 UTC (rev 2024)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LimitedConcurrentHashMap.java	2012-05-27 12:48:55 UTC (rev 2025)
@@ -33,6 +33,8 @@
 import java.util.Vector;
 import java.util.concurrent.ConcurrentHashMap;
 
+import org.apache.solr.util.ConcurrentLFUCache;
+
 import de.schmitzm.data.event.AbstractObjectTraceable;
 import de.schmitzm.data.event.GeneralObjectChangeEvent;
 import de.schmitzm.data.event.Invoker;
@@ -49,6 +51,10 @@
  * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
  *         (University of Bonn/Germany)
  * @version 1.0
+ * 
+ * @deprecated
+ * 
+ * @see ConcurrentLFUCache
  */
 public class LimitedConcurrentHashMap<K, V> extends ConcurrentHashMap<K, V> implements
 		ObjectTraceable {

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/regex/RegexCache.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/regex/RegexCache.java	2012-05-27 00:39:52 UTC (rev 2024)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/regex/RegexCache.java	2012-05-27 12:48:55 UTC (rev 2025)
@@ -1,11 +1,12 @@
 package de.schmitzm.regex;
 
-
-import java.util.concurrent.ConcurrentHashMap;
 import java.util.regex.MatchResult;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.solr.util.ConcurrentLFUCache;
+import org.apache.solr.util.ConcurrentLFUCache.Stats;
+
 /**
  * Cached compilierte Pattern und auch Ergebnisse von RegExes. Use the matchers methods to obtain cached result and add
  * regex to cache automatically.
@@ -13,19 +14,20 @@
  * @author stranger
  * @author stefan.tzeggai
  * 
- * TODO ConcurrentLFUCache von solr einbauen um unendlichen Speicherverbrauch vorzubeugen
+ *         TODO ConcurrentLFUCache von solr einbauen um unendlichen Speicherverbrauch vorzubeugen
  */
+
 public class RegexCache {
 	/**
 	 * Matcher-Cache für Regex+Value verwenden? Durch die methode mather.reset wird pro Thread ein Matcher
 	 * wiederverwendet
 	 */
-	public static boolean matcherCache = false;
-	
+	public static boolean matcherCacheEnabled = false;
+
 	/**
-	 * Regex-Auswertungen komplett cachen  
+	 * Regex-Auswertungen komplett cachen
 	 */
-	public static boolean resultsCache = true;
+	public static boolean resultsCacheEnabled = true;
 
 	/**
 	 * TODO Ein String der länger ist als die hier angegebene Anzahl Zeichen wir nicht gecached. Die Wahrscheinlichkeit,
@@ -38,21 +40,20 @@
 	 **/
 	private static final int CACHE_VALUES_TO_RESULT_MIN_VALUE_LENGTH = 10;
 
+	private final ConcurrentLFUCache<String, ThreadLocal<Matcher>> matchersCachedThreadLocalPerRegex = new ConcurrentLFUCache<String, ThreadLocal<Matcher>>(
+			200000, 150000);
 
-	private final ConcurrentHashMap<String, ThreadLocal<Matcher>> matchersCachedThreadLocalPerRegex = new ConcurrentHashMap<String, ThreadLocal<Matcher>>();
-
-
 	private static RegexCache singletonInstance;
 
-	private final ConcurrentHashMap<String, Pattern> patterns = new ConcurrentHashMap<String, Pattern>(1000);
+	private final ConcurrentLFUCache<String, Pattern> patternCache = new ConcurrentLFUCache<String, Pattern>(50000,
+			35000);
 
 	public static RegexCache getInstance() {
 		return singletonInstance != null ? singletonInstance : new RegexCache();
 	}
 
-	// TODO
-	// http://lucene.apache.org/solr/api/org/apache/solr/util/ConcurrentLRUCache.html#ConcurrentLRUCache%28int,%20int%29
-	private final ConcurrentHashMap<Pattern, ConcurrentHashMap<String, MyMatchResult>> matcherResults = new ConcurrentHashMap<Pattern, ConcurrentHashMap<String, MyMatchResult>>();
+	private final ConcurrentLFUCache<Pattern, ConcurrentLFUCache<String, MyMatchResult>> resultsCache = new ConcurrentLFUCache<Pattern, ConcurrentLFUCache<String, MyMatchResult>>(
+			150000, 100000);
 
 	private RegexCache() {
 	}
@@ -66,7 +67,7 @@
 	 */
 	public final Matcher getMatcher(final String regex, final String text) {
 
-		if (!matcherCache) {
+		if (!matcherCacheEnabled) {
 			return getPattern(regex).matcher(text);
 		}
 
@@ -81,7 +82,7 @@
 					return getPattern(regex).matcher("");
 				}
 			};
-			matchersCachedThreadLocalPerRegex.putIfAbsent(regex, threadLocal);
+			matchersCachedThreadLocalPerRegex.put(regex, threadLocal);
 		}
 
 		return threadLocal.get().reset(text);
@@ -94,10 +95,10 @@
 		if (regex == null)
 			return null;
 		// synchronized (regex) {
-		Pattern p = patterns.get(regex);
+		Pattern p = patternCache.get(regex);
 		if (p == null) {
 			p = Pattern.compile(regex);
-			patterns.putIfAbsent(regex, p);
+			patternCache.put(regex, p);
 		}
 		return p;
 		// }
@@ -131,13 +132,13 @@
 	 *            may not be null
 	 * @return <code>null</code> if the pattern didn't match. A {@link MatchResult} otherwise which contains the groups
 	 *         etc.
-	 *         
+	 * 
 	 *         TODO Der cache sollte eine Hit-Quote halten, die irgendwann den Cache für eine Regex blockt
 	 */
 	public final MatchResult result(final String regex, final String value) {
 
 		// Wenn der value-String zu lang oder zu kurz ist, dann einfach so machten
-		if (!resultsCache || value.length() < CACHE_VALUES_TO_RESULT_MIN_VALUE_LENGTH
+		if (!resultsCacheEnabled || value.length() < CACHE_VALUES_TO_RESULT_MIN_VALUE_LENGTH
 				|| value.length() > CACHE_VALUES_TO_RESULT_MAX_VALUE_LENGTH) {
 			final Matcher m = getMatcher(regex, value);
 			final boolean found = m.find();
@@ -146,10 +147,10 @@
 
 		final Pattern pattern = getPattern(regex);
 
-		ConcurrentHashMap<String, MyMatchResult> m;
-		m = matcherResults.get(pattern);
+		ConcurrentLFUCache<String, MyMatchResult> m;
+		m = resultsCache.get(pattern);
 		if (m == null) {
-			matcherResults.put(pattern, m = new ConcurrentHashMap<String, MyMatchResult>());
+			resultsCache.put(pattern, m = new ConcurrentLFUCache<String, MyMatchResult>(50, 20));
 		}
 
 		MyMatchResult mResult;
@@ -171,6 +172,27 @@
 	}
 
 	/**
+	 * Liefert die aktuellen Statistiken des ResultsCache
+	 */
+	public Stats getResultsCacheStats() {
+		return resultsCache.getStats();
+	}
+
+	/**
+	 * Liefert die aktuellen Statistiken des ResultsCache
+	 */
+	public Stats getMatcherCacheStats() {
+		return matchersCachedThreadLocalPerRegex.getStats();
+	}
+
+	/**
+	 * Liefert die aktuellen Statistiken des PatternCache
+	 */
+	public Stats getPatternCacheStats() {
+		return patternCache.getStats();
+	}
+
+	/**
 	 * Diese Klasse dienst dazu, einen Eintrag im cache liegen zu haben, auch wenn keine match gefunden wurde. In dem
 	 * Falle ist das Feld {@link #matcherResult} dann <code>null</code>
 	 * 



More information about the Schmitzm-commits mailing list