openjpa-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ppod...@apache.org
Subject svn commit: r935078 - in /openjpa/branches/2.0.x: ./ openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/ openjpa-kernel/src/main/java/org/apache/openjpa/kernel/ openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/
Date Fri, 16 Apr 2010 20:49:10 GMT
Author: ppoddar
Date: Fri Apr 16 20:49:09 2010
New Revision: 935078

URL: http://svn.apache.org/viewvc?rev=935078&view=rev
Log:
OPENJPA-703: Merge Query Statistics related changes

Added:
    openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestDefaultQueryStatistics.java
      - copied unchanged from r935074, openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestDefaultQueryStatistics.java
Modified:
    openjpa/branches/2.0.x/   (props changed)
    openjpa/branches/2.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
    openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQueryCache.java
    openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryStatistics.java
    openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java

Propchange: openjpa/branches/2.0.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Fri Apr 16 20:49:09 2010
@@ -1 +1,2 @@
 /openjpa/branches/1.0.x:736493
+/openjpa/trunk:934507,935033,935035,935073-935074

Modified: openjpa/branches/2.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java?rev=935078&r1=935077&r2=935078&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
(original)
+++ openjpa/branches/2.0.x/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/PreparedQueryCacheImpl.java
Fri Apr 16 20:49:09 2010
@@ -57,7 +57,8 @@ public class PreparedQueryCacheImpl impl
 	// Key: Query identifier Value: Reason why excluded
 	private final Map<String, Exclusion> _uncachables;
 	private final List<Exclusion> _exclusionPatterns;
-	private final QueryStatistics<String> _stats;
+	private QueryStatistics<String> _stats;
+	private boolean _statsEnabled;
 	private ReentrantLock _lock = new ReentrantLock();
 	private Log _log;
     private static Localizer _loc = Localizer.forPackage(PreparedQueryCacheImpl.class);
@@ -65,7 +66,6 @@ public class PreparedQueryCacheImpl impl
 	public PreparedQueryCacheImpl() {
 		_delegate = new CacheMap();
 		_uncachables = new CacheMap();
-		_stats = new QueryStatistics.Default<String>();
 		_exclusionPatterns = new ArrayList<Exclusion>();
 	}
 	
@@ -316,6 +316,14 @@ public class PreparedQueryCacheImpl impl
         _delegate.clear();
         _stats.clear();
     }
+    
+    public void setEnableStatistics(boolean enable){
+        _statsEnabled = enable;
+    }
+    
+    public boolean getEnableStatistics(){
+        return _statsEnabled;
+    }
         
 	//-------------------------------------------------------
 	// Configurable contract
@@ -328,6 +336,8 @@ public class PreparedQueryCacheImpl impl
     }
 
     public void endConfiguration() {
+        _stats = _statsEnabled ? new QueryStatistics.Default<String>() :
+                                 new QueryStatistics.None<String>();
     }
     
     /**

Modified: openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQueryCache.java?rev=935078&r1=935077&r2=935078&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQueryCache.java
(original)
+++ openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/PreparedQueryCache.java
Fri Apr 16 20:49:09 2010
@@ -196,7 +196,20 @@ public interface PreparedQueryCache exte
 	public void clear();
 	
 	/**
+	 * Enable/disable gathering of statistics.
+	 * 
+	 */
+    public void setEnableStatistics(boolean enable);
+    
+    /**
+     * Affirm if statistics is gathered.
+     * 
+     */
+    public boolean getEnableStatistics();
+    
+	/**
 	 * Gets the simple statistics for executed queries.
+	 * If the statistics gathering is disabled, an empty statistics is returned. 
 	 */
 	public QueryStatistics<String> getStatistics();
 	

Modified: openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryStatistics.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryStatistics.java?rev=935078&r1=935077&r2=935078&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryStatistics.java
(original)
+++ openjpa/branches/2.0.x/openjpa-kernel/src/main/java/org/apache/openjpa/kernel/QueryStatistics.java
Fri Apr 16 20:49:09 2010
@@ -20,11 +20,13 @@ package org.apache.openjpa.kernel;
 
 import java.io.PrintStream;
 import java.io.Serializable;
+import java.util.Collections;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 
+import org.apache.openjpa.lib.util.concurrent.SizedConcurrentHashMap;
+
 /**
  * Records query execution statistics.
  * 
@@ -119,17 +121,24 @@ public interface QueryStatistics<T> exte
 	
 	/**
 	 * A default implementation.
-	 *
+	 * 
+	 * Maintains statistics for only a fixed number of queries.
+	 * Statistical counts are approximate and not exact (to keep thread synchorization overhead
low).
+	 * 
 	 */
 	public static class Default<T> implements QueryStatistics<T> {
+	    private static final int FIXED_SIZE = 1000;
+	    private static final float LOAD_FACTOR = 0.75f;
+	    private static final int CONCURRENCY = 16;
+	    
 		private static final int ARRAY_SIZE = 2;
         private static final int READ  = 0;
         private static final int HIT   = 1;
         
 		private long[] astat = new long[ARRAY_SIZE];
 		private long[] stat  = new long[ARRAY_SIZE];
-		private Map<T, long[]> stats  = new HashMap<T, long[]>();
-		private Map<T, long[]> astats = new HashMap<T, long[]>();
+		private Map<T, long[]> stats  = new SizedConcurrentHashMap(FIXED_SIZE, LOAD_FACTOR,
CONCURRENCY);
+		private Map<T, long[]> astats = new SizedConcurrentHashMap(FIXED_SIZE, LOAD_FACTOR,
CONCURRENCY);
 		private Date start = new Date();
 		private Date since = start;
 		
@@ -182,17 +191,17 @@ public interface QueryStatistics<T> exte
 			return start;
 		}
 
-		public void reset() {
+		public synchronized void reset() {
 			stat = new long[ARRAY_SIZE];
 			stats.clear();
 			since = new Date();
 		}
 		
-	    public void clear() {
+	    public synchronized void clear() {
 	       astat = new long[ARRAY_SIZE];
 	       stat  = new long[ARRAY_SIZE];
-	       stats = new HashMap<T, long[]>();
-	       astats = new HashMap<T, long[]>();
+	       stats = new SizedConcurrentHashMap(FIXED_SIZE, LOAD_FACTOR, CONCURRENCY);
+	       astats = new SizedConcurrentHashMap(FIXED_SIZE, LOAD_FACTOR, CONCURRENCY);
 	       start  = new Date();
 	       since  = start;
 	    }
@@ -217,7 +226,7 @@ public interface QueryStatistics<T> exte
 		public void recordExecution(T query) {
 		    if (query == null)
 		        return;
-		    boolean cached = (astats.containsKey(query));
+		    boolean cached = astats.containsKey(query);
 			addSample(query, READ);
 			if (cached)
 				addSample(query, HIT);
@@ -244,8 +253,7 @@ public interface QueryStatistics<T> exte
                     out.println(i + ". \t" + toString(arow) + " \t" + key);
 				} else {
 					long[] row  = stats.get(key);
-                    out.println(i + ". \t" + toString(arow) + " \t"  
-					    + toString(row) + " \t\t" + key);
+                    out.println(i + ". \t" + toString(arow) + " \t"  + toString(row) + "
\t\t" + key);
 				}
 			}
 		}
@@ -257,8 +265,78 @@ public interface QueryStatistics<T> exte
 		}
 		
 		String toString(long[] row) {
-            return row[READ] + ":" + row[HIT] + "(" + pct(row[HIT], row[READ])
-			+ "%)";
+            return row[READ] + ":" + row[HIT] + "(" + pct(row[HIT], row[READ]) + "%)";
 		}
 	}
+	
+	/**
+	 * A do-nothing implementation.
+	 * 
+	 * @author Pinaki Poddar
+	 *
+	 * @param <T>
+	 */
+	public static class None<T> implements QueryStatistics<T> {
+        private Date start = new Date();
+        private Date since = start;
+
+        public void clear() {
+        }
+
+        public void dump(PrintStream out) {
+        }
+
+        public long getExecutionCount() {
+            return 0;
+        }
+
+        public long getExecutionCount(T query) {
+            return 0;
+        }
+
+        public long getHitCount() {
+            return 0;
+        }
+
+        public long getHitCount(T query) {
+            return 0;
+        }
+
+        public long getTotalExecutionCount() {
+            return 0;
+        }
+
+        public long getTotalExecutionCount(T query) {
+            return 0;
+        }
+
+        public long getTotalHitCount() {
+            return 0;
+        }
+
+        public long getTotalHitCount(T query) {
+            return 0;
+        }
+
+        public Set<T> keys() {
+            return Collections.emptySet();
+        }
+
+        public void recordExecution(T query) {
+        }
+
+        public void reset() {
+            start  = new Date();
+            since  = start;
+        }
+
+        public Date since() {
+            return since;
+        }
+
+        public Date start() {
+            return start;
+        }
+	}
 }
+

Modified: openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
URL: http://svn.apache.org/viewvc/openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java?rev=935078&r1=935077&r2=935078&view=diff
==============================================================================
--- openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
(original)
+++ openjpa/branches/2.0.x/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/sqlcache/TestPreparedQueryCache.java
Fri Apr 16 20:49:09 2010
@@ -109,7 +109,7 @@ public class TestPreparedQueryCache exte
             Properties config = new Properties();
             config.put("openjpa.jdbc.SynchronizeMappings", "buildSchema(ForeignKeys=true,SchemaAction='drop,add')");
             config.put("openjpa.jdbc.JDBCListeners", new JDBCListener[] { auditor = new SQLAuditor()});
-            config.put("openjpa.jdbc.QuerySQLCache", "true");
+            config.put("openjpa.jdbc.QuerySQLCache", "true(EnableStatistics=true)");
             config.put("openjpa.RuntimeUnenhancedClasses", "unsupported");
             config.put("openjpa.DynamicEnhancementAgent", "false");
             emf = (OpenJPAEntityManagerFactorySPI)OpenJPAPersistence.createEntityManagerFactory(



Mime
View raw message