[hibernate-commits] Hibernate SVN: r20861 - in search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search: util and 1 other directory.

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Mon Nov 8 05:16:04 EST 2010


Author: stliu
Date: 2010-11-08 05:16:04 -0500 (Mon, 08 Nov 2010)
New Revision: 20861

Added:
   search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/util/SoftLimitMRUCache.java
Modified:
   search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/filter/CachingWrapperFilter.java
   search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/filter/MRUFilterCachingStrategy.java
Log:
JBPAPP-5394 Backport HHH-5300 to EAP to fix memory leak in QueryPlanCache

Modified: search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/filter/CachingWrapperFilter.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/filter/CachingWrapperFilter.java	2010-11-08 10:14:48 UTC (rev 20860)
+++ search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/filter/CachingWrapperFilter.java	2010-11-08 10:16:04 UTC (rev 20861)
@@ -8,7 +8,7 @@
 import org.apache.lucene.search.DocIdSet;
 import org.apache.lucene.search.Filter;
 import org.hibernate.search.util.LoggerFactory;
-import org.hibernate.util.SoftLimitMRUCache;
+import org.hibernate.search.util.SoftLimitMRUCache;
 import org.slf4j.Logger;
 
 /**

Modified: search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/filter/MRUFilterCachingStrategy.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/filter/MRUFilterCachingStrategy.java	2010-11-08 10:14:48 UTC (rev 20860)
+++ search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/filter/MRUFilterCachingStrategy.java	2010-11-08 10:16:04 UTC (rev 20861)
@@ -6,7 +6,7 @@
 import org.apache.lucene.search.Filter;
 import org.hibernate.search.Environment;
 import org.hibernate.search.backend.configuration.ConfigurationParseHelper;
-import org.hibernate.util.SoftLimitMRUCache;
+import org.hibernate.search.util.SoftLimitMRUCache;
 
 /**
  * Keep the most recently used Filters in the cache

Added: search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/util/SoftLimitMRUCache.java
===================================================================
--- search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/util/SoftLimitMRUCache.java	                        (rev 0)
+++ search/branches/v3_1_1_GA_CP/src/main/java/org/hibernate/search/util/SoftLimitMRUCache.java	2010-11-08 10:16:04 UTC (rev 20861)
@@ -0,0 +1,107 @@
+/*
+ * Hibernate, Relational Persistence for Idiomatic Java
+ *
+ * Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
+ * indicated by the @author tags or express copyright attribution
+ * statements applied by the authors.  All third-party contributions are
+ * distributed under license by Red Hat Middleware LLC.
+ *
+ * This copyrighted material is made available to anyone wishing to use, modify,
+ * copy, or redistribute it subject to the terms and conditions of the GNU
+ * Lesser General Public License, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this distribution; if not, write to:
+ * Free Software Foundation, Inc.
+ * 51 Franklin Street, Fifth Floor
+ * Boston, MA  02110-1301  USA
+ *
+ */
+package org.hibernate.search.util;
+
+import java.io.IOException;
+import java.io.Serializable;
+
+import org.apache.commons.collections.map.LRUMap;
+import org.apache.commons.collections.map.ReferenceMap;
+
+/**
+ * Cache following a "Most Recently Used" (MRY) algorithm for maintaining a
+ * bounded in-memory size; the "Least Recently Used" (LRU) entry is the first
+ * available for removal from the cache.
+ * <p/>
+ * This implementation uses a "soft limit" to the in-memory size of the cache,
+ * meaning that all cache entries are kept within a completely
+ * {@link java.lang.ref.SoftReference}-based map with the most recently utilized
+ * entries additionally kept in a hard-reference manner to prevent those cache
+ * entries soft references from becoming enqueued by the garbage collector.
+ * Thus the actual size of this cache impl can actually grow beyond the stated
+ * max size bound as long as GC is not actively seeking soft references for
+ * enqueuement.
+ *
+ * @author Steve Ebersole
+ */
+public class SoftLimitMRUCache implements Serializable {
+
+	public static final int DEFAULT_STRONG_REF_COUNT = 128;
+
+	private final int strongReferenceCount;
+
+	// actual cache of the entries.  soft references are used for both the keys and the
+	// values here since the values pertaining to the MRU entries are kept in a
+	// separate hard reference cache (to avoid their enqueuement/garbage-collection).
+	private transient ReferenceMap softReferenceCache = new ReferenceMap( ReferenceMap.SOFT, ReferenceMap.SOFT );
+	// the MRU cache used to keep hard references to the most recently used query plans;
+	// note : LRU here is a bit of a misnomer, it indicates that LRU entries are removed, the
+	// actual kept entries are the MRU entries
+	private transient LRUMap strongReferenceCache;
+
+	public SoftLimitMRUCache() {
+		this( DEFAULT_STRONG_REF_COUNT );
+	}
+
+	public SoftLimitMRUCache(int strongRefCount) {
+		this.strongReferenceCount = strongRefCount;
+		init();
+	}
+
+	public synchronized Object get(Object key) {
+		Object result = softReferenceCache.get( key );
+		if ( result != null ) {
+			strongReferenceCache.put( key, result );
+		}
+		return result;
+	}
+
+	public synchronized Object put(Object key, Object value) {
+		softReferenceCache.put( key, value );
+		return strongReferenceCache.put( key, value );
+	}
+
+	public synchronized int size() {
+		return strongReferenceCache.size();
+	}
+
+	public synchronized int softSize() {
+		return softReferenceCache.size();
+	}
+
+	private void init() {
+		strongReferenceCache = new LRUMap( strongReferenceCount );
+	}
+
+	private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
+		in.defaultReadObject();
+		init();
+	}
+
+	public synchronized void clear() {
+		strongReferenceCache.clear();
+		softReferenceCache.clear();
+	}
+}



More information about the hibernate-commits mailing list