From: <one...@us...> - 2003-02-15 08:58:00
|
Update of /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/impl In directory sc8-pr-cvs1:/tmp/cvs-serv10780/hibernate/impl Modified Files: SessionFactoryImpl.java Log Message: query cache now keeps strong references to MRU queries Index: SessionFactoryImpl.java =================================================================== RCS file: /cvsroot/hibernate/Hibernate2/src/net/sf/hibernate/impl/SessionFactoryImpl.java,v retrieving revision 1.9 retrieving revision 1.10 diff -C2 -d -r1.9 -r1.10 *** SessionFactoryImpl.java 9 Feb 2003 06:28:15 -0000 1.9 --- SessionFactoryImpl.java 15 Feb 2003 08:57:57 -0000 1.10 *************** *** 22,25 **** --- 22,26 ---- import java.util.Map; import java.util.Properties; + import java.util.Collections; import javax.naming.NamingException; *************** *** 31,34 **** --- 32,37 ---- import javax.xml.transform.stream.StreamSource; + import net.sf.cglib.KeyFactory; + import net.sf.hibernate.AssertionFailure; import net.sf.hibernate.Databinder; *************** *** 283,288 **** } ! private transient final ReferenceMap queryCache = new ReferenceMap(ReferenceMap.SOFT, ReferenceMap.SOFT); public QueryTranslator getQuery(String query) throws QueryException, MappingException { --- 286,320 ---- } + + // Emulates constant time LRU/MRU algorythms for cache + // It is better to hold strong refernces on some (LRU/MRU) queries + private transient final int MAX_STRONG_REF_COUNT = 128; //TODO: configurable? + private transient final Object strongRefs[] = new Object[MAX_STRONG_REF_COUNT]; //strong reference to MRU queries + private transient int strongRefIndex = 0; + private transient final Map softQueryCache = new ReferenceMap(ReferenceMap.HARD, ReferenceMap.SOFT) ; ! private static QueryCacheKeyFactory keyFactory = (QueryCacheKeyFactory) KeyFactory.create( ! QueryCacheKeyFactory.class, QueryCacheKeyFactory.class.getClassLoader() ! ); ! ! //retuns generated class instance ! interface QueryCacheKeyFactory { ! //Will not recalculate hashKey for constant queries ! Object newInstance(String query, boolean scalar); ! } ! ! //TODO: this stuff can be implemented in separate class to reuse soft MRU/LRU caching ! private synchronized Object get(Object key) { ! Object result = softQueryCache.get(key); ! if( result != null ) { ! strongRefs[ ++strongRefIndex % MAX_STRONG_REF_COUNT ] = result; ! } ! return result; ! } ! ! private synchronized void put(Object key, Object value) { ! softQueryCache.put(key, value); ! strongRefs[ ++strongRefIndex % MAX_STRONG_REF_COUNT ] = value; ! } public QueryTranslator getQuery(String query) throws QueryException, MappingException { *************** *** 297,301 **** private QueryTranslator getQuery(String query, boolean shallow) throws QueryException, MappingException { ! String cacheKey = (shallow?'$':'%') + query; //a but expedient, i know.... // have to be careful to ensure that if the JVM does out-of-order execution --- 329,333 ---- private QueryTranslator getQuery(String query, boolean shallow) throws QueryException, MappingException { ! Object cacheKey = keyFactory.newInstance(query, shallow); // have to be careful to ensure that if the JVM does out-of-order execution *************** *** 304,311 **** // a query while another query is being compiled ! QueryTranslator q = (QueryTranslator) queryCache.get(cacheKey); if ( q==null ) { q = new QueryTranslator(); ! queryCache.put(cacheKey, q); } q.compile(this, query, querySubstitutions, shallow); --- 336,343 ---- // a query while another query is being compiled ! QueryTranslator q = (QueryTranslator) get(cacheKey); if ( q==null ) { q = new QueryTranslator(); ! put(cacheKey, q); } q.compile(this, query, querySubstitutions, shallow); *************** *** 316,326 **** public FilterTranslator getFilter(String query, String collectionRole, boolean scalar) throws QueryException, MappingException { ! String cacheKey = (scalar?'#':'*') + query; //a but expedient, i know.... ! ! FilterTranslator q = (FilterTranslator) queryCache.get(cacheKey); if ( q==null ) { q = new FilterTranslator(); ! queryCache.put(cacheKey, q); } q.compile(collectionRole, this, query, querySubstitutions, scalar); --- 348,358 ---- public FilterTranslator getFilter(String query, String collectionRole, boolean scalar) throws QueryException, MappingException { + + Object cacheKey = keyFactory.newInstance(query, scalar); ! FilterTranslator q = (FilterTranslator) get(cacheKey); if ( q==null ) { q = new FilterTranslator(); ! put(cacheKey, q); } q.compile(collectionRole, this, query, querySubstitutions, scalar); |