You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@roller.apache.org by ag...@apache.org on 2006/11/03 02:29:01 UTC
svn commit: r470627 - in
/incubator/roller/trunk/src/org/apache/roller/business: HitCountQueue.java
runnable/HitCountProcessingJob.java
Author: agilliland
Date: Thu Nov 2 17:29:01 2006
New Revision: 470627
URL: http://svn.apache.org/viewvc?view=rev&rev=470627
Log:
updating the hit count processing code to use a process which is easier and less likely to introduce synchronization issues. counts are now simply shoved in a running list by the HitCountQueue and the processing job now grabs the list and tallies the hits before saving them to the db.
Modified:
incubator/roller/trunk/src/org/apache/roller/business/HitCountQueue.java
incubator/roller/trunk/src/org/apache/roller/business/runnable/HitCountProcessingJob.java
Modified: incubator/roller/trunk/src/org/apache/roller/business/HitCountQueue.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/business/HitCountQueue.java?view=diff&rev=470627&r1=470626&r2=470627
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/business/HitCountQueue.java (original)
+++ incubator/roller/trunk/src/org/apache/roller/business/HitCountQueue.java Thu Nov 2 17:29:01 2006
@@ -20,10 +20,7 @@
import java.util.ArrayList;
import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
import java.util.List;
-import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.roller.business.runnable.ContinuousWorkerThread;
@@ -38,10 +35,10 @@
* an asynchronous manner at give intervals.
*
* We also start up a single thread which runs continously to take the queued
- * hit counts and record them into the db.
+ * hit counts, tally them, and record them into the db.
*
* TODO: we may want to make this an interface that is pluggable if there is
- * some indication that users may want to override this implementation.
+ * some indication that users want to override this implementation.
*/
public class HitCountQueue {
@@ -52,7 +49,7 @@
private int numWorkers = 1;
private int sleepTime = 180000;
private WorkerThread worker = null;
- private Map queue = null;
+ private List queue = null;
static {
@@ -73,7 +70,7 @@
}
// create the hits queue
- this.queue = Collections.synchronizedMap(new HashMap());
+ this.queue = Collections.synchronizedList(new ArrayList());
// start up a worker to process the hits at intervals
HitCountProcessingJob job = new HitCountProcessingJob();
@@ -89,19 +86,16 @@
public void processHit(WebsiteData weblog, String url, String referrer) {
- // just update the count for the weblog
- Long count = (Long) this.queue.get(weblog.getHandle());
- if(count == null) {
- count = new Long(1);
- } else {
- count = new Long(count.longValue()+1);
+ // if the weblog isn't null then just drop it's handle in the queue
+ // each entry in the queue is a weblog handle and indicates a single hit
+ if(weblog != null) {
+ this.queue.add(weblog.getHandle());
}
- this.queue.put(weblog.getHandle(), count);
}
- public Map getHits() {
- return new HashMap(this.queue);
+ public List getHits() {
+ return new ArrayList(this.queue);
}
@@ -109,7 +103,7 @@
* Reset the queued hits.
*/
public synchronized void resetHits() {
- this.queue = Collections.synchronizedMap(new HashMap());
+ this.queue = Collections.synchronizedList(new ArrayList());
}
Modified: incubator/roller/trunk/src/org/apache/roller/business/runnable/HitCountProcessingJob.java
URL: http://svn.apache.org/viewvc/incubator/roller/trunk/src/org/apache/roller/business/runnable/HitCountProcessingJob.java?view=diff&rev=470627&r1=470626&r2=470627
==============================================================================
--- incubator/roller/trunk/src/org/apache/roller/business/runnable/HitCountProcessingJob.java (original)
+++ incubator/roller/trunk/src/org/apache/roller/business/runnable/HitCountProcessingJob.java Thu Nov 2 17:29:01 2006
@@ -18,7 +18,9 @@
package org.apache.roller.business.runnable;
+import java.util.HashMap;
import java.util.Iterator;
+import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -61,34 +63,48 @@
return;
}
- HitCountQueue counter = HitCountQueue.getInstance();
- HitCountQueue hitCounter = (HitCountQueue) counter;
+ HitCountQueue hitCounter = HitCountQueue.getInstance();
// first get the current set of hits
- Map currentHits = hitCounter.getHits();
+ List currentHits = hitCounter.getHits();
// now reset the queued hits
hitCounter.resetHits();
- // iterate over the hits and store them in the db
+ // tally the counts, grouped by weblog handle
+ Map hitsTally = new HashMap();
+ String weblogHandle = null;
+ for(int i=0; i < currentHits.size(); i++) {
+ weblogHandle = (String) currentHits.get(i);
+
+ Long count = (Long) hitsTally.get(weblogHandle);
+ if(count == null) {
+ count = new Long(1);
+ } else {
+ count = new Long(count.longValue()+1);
+ }
+ hitsTally.put(weblogHandle, count);
+ }
+
+ // iterate over the tallied hits and store them in the db
try {
long startTime = System.currentTimeMillis();
WebsiteData weblog = null;
String key = null;
- Iterator it = currentHits.keySet().iterator();
+ Iterator it = hitsTally.keySet().iterator();
while(it.hasNext()) {
key = (String) it.next();
try {
weblog = umgr.getWebsiteByHandle(key);
- wmgr.incrementHitCount(weblog, ((Long)currentHits.get(key)).intValue());
+ wmgr.incrementHitCount(weblog, ((Long)hitsTally.get(key)).intValue());
} catch (RollerException ex) {
log.error(ex);
}
}
- // make sure and flush the results
+ // flush the results to the db
RollerFactory.getRoller().flush();
long endTime = System.currentTimeMillis();