jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From alexparvule...@apache.org
Subject svn commit: r1205199 - in /jackrabbit/trunk: jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/ jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/ jackrabbit-core/src/mai...
Date Tue, 22 Nov 2011 22:31:03 GMT
Author: alexparvulescu
Date: Tue Nov 22 22:31:01 2011
New Revision: 1205199

URL: http://svn.apache.org/viewvc?rev=1205199&view=rev
Log:
JCR-3154 Stats for Queries continued

Added:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoOccurrenceComparator.java
  (with props)
Modified:
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java
    jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java

Modified: jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java?rev=1205199&r1=1205198&r2=1205199&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java
(original)
+++ jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/jmx/QueryStatManagerMBean.java
Tue Nov 22 22:31:01 2011
@@ -28,23 +28,52 @@ public interface QueryStatManagerMBean {
 
     String NAME = "org.apache.jackrabbit:type=QueryStats";
 
-    TabularData getQueries();
+    /**
+     * @return a sorted array containing the top
+     *         {@link #getSlowQueriesQueueSize()} slowest queries
+     */
+    TabularData getSlowQueries();
+
+    /**
+     * @return a sorted array containing the
+     *         {@link #getPopularQueriesQueueSize()} most popular queries
+     */
+    TabularData getPopularQueries();
+
+    /**
+     * @return size of the <b>Slow</b> queue
+     */
+    int getSlowQueriesQueueSize();
+
+    /**
+     * Change the size of the <b>Slow</b> queue
+     * 
+     * @param size
+     *            the new size
+     */
+    void setSlowQueriesQueueSize(int size);
 
     /**
-     * @return how big the <b>Top X</b> queue is
+     * clears the <b>Slow</b> queue
      */
-    int getQueueSize();
+    void clearSlowQueriesQueue();
 
     /**
-     * Change the <b>Top X</b> queue size
+     * @return size of the <b>Popular</b> queue
+     */
+    int getPopularQueriesQueueSize();
+
+    /**
+     * Change the size of the <b>Popular</b> queue
      * 
      * @param size
      *            the new size
      */
-    void setQueueSize(int size);
+    void setPopularQueriesQueueSize(int size);
 
     /**
-     * clears the queue
+     * clears the <b>Popular</b> queue
      */
-    void clearQueue();
+    void clearPopularQueriesQueue();
+
 }

Modified: jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java?rev=1205199&r1=1205198&r2=1205199&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java
(original)
+++ jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStat.java
Tue Nov 22 22:31:01 2011
@@ -22,17 +22,19 @@ package org.apache.jackrabbit.api.stats;
  */
 public interface QueryStat {
 
-    /** Slowest Queries */
-
+    /**
+     * @return a sorted array containing the top
+     *         {@link #getSlowQueriesQueueSize()} slowest queries
+     */
     QueryStatDto[] getSlowQueries();
 
     /**
-     * @return how big the <b>Top X</b> queue is
+     * @return size of the <b>Slow</b> queue
      */
     int getSlowQueriesQueueSize();
 
     /**
-     * Change the <b>Top X</b> queue size
+     * Change the size of the <b>Slow</b> queue
      * 
      * @param size
      *            the new size
@@ -40,10 +42,34 @@ public interface QueryStat {
     void setSlowQueriesQueueSize(int size);
 
     /**
-     * clears the queue
+     * clears the <b>Slow</b> queue
      */
     void clearSlowQueriesQueue();
 
+    /**
+     * @return a sorted array containing the
+     *         {@link #getPopularQueriesQueueSize()} most popular queries
+     */
+    QueryStatDto[] getPopularQueries();
+
+    /**
+     * @return size of the <b>Popular</b> queue
+     */
+    int getPopularQueriesQueueSize();
+
+    /**
+     * Change the size of the <b>Popular</b> queue
+     * 
+     * @param size
+     *            the new size
+     */
+    void setPopularQueriesQueueSize(int size);
+
+    /**
+     * clears the <b>Popular</b> queue
+     */
+    void clearPopularQueriesQueue();
+
     /** -- GENERAL OPS -- **/
 
     /**

Modified: jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java?rev=1205199&r1=1205198&r2=1205199&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java
(original)
+++ jackrabbit/trunk/jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/QueryStatDto.java
Tue Nov 22 22:31:01 2011
@@ -32,6 +32,8 @@ public interface QueryStatDto extends Se
 
     String getCreationTime();
 
+    int getOccurrenceCount();
+
     long getPosition();
 
     void setPosition(long position);

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java?rev=1205199&r1=1205198&r2=1205199&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/jmx/QueryStatManager.java
Tue Nov 22 22:31:01 2011
@@ -16,10 +16,6 @@
  */
 package org.apache.jackrabbit.core.jmx;
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Comparator;
-
 import javax.management.openmbean.CompositeDataSupport;
 import javax.management.openmbean.CompositeType;
 import javax.management.openmbean.OpenDataException;
@@ -32,7 +28,6 @@ import javax.management.openmbean.Tabula
 import org.apache.jackrabbit.api.jmx.QueryStatManagerMBean;
 import org.apache.jackrabbit.api.stats.QueryStat;
 import org.apache.jackrabbit.api.stats.QueryStatDto;
-import org.apache.jackrabbit.core.stats.QueryStatDtoComparator;
 
 /**
  * The QueryStatManagerMBean default implementation
@@ -42,9 +37,6 @@ public class QueryStatManager implements
 
     private final QueryStat queryStat;
 
-    private final static Comparator<QueryStatDto> comparatorRev = Collections
-            .reverseOrder(new QueryStatDtoComparator());
-
     public QueryStatManager(final QueryStat queryStat) {
         this.queryStat = queryStat;
     }
@@ -65,28 +57,39 @@ public class QueryStatManager implements
         this.queryStat.reset();
     }
 
-    public int getQueueSize() {
+    public int getSlowQueriesQueueSize() {
         return queryStat.getSlowQueriesQueueSize();
     }
 
-    public void setQueueSize(int size) {
+    public void setSlowQueriesQueueSize(int size) {
         this.queryStat.setSlowQueriesQueueSize(size);
     }
 
-    public void clearQueue() {
+    public void clearSlowQueriesQueue() {
         this.queryStat.clearSlowQueriesQueue();
     }
 
-    public QueryStatDto[] getTopQueries() {
-        QueryStatDto[] top = this.queryStat.getSlowQueries();
-        Arrays.sort(top, comparatorRev);
-        for (int i = 0; i < top.length; i++) {
-            top[i].setPosition(i + 1);
-        }
-        return top;
+    public int getPopularQueriesQueueSize() {
+        return queryStat.getPopularQueriesQueueSize();
+    }
+
+    public void setPopularQueriesQueueSize(int size) {
+        queryStat.setPopularQueriesQueueSize(size);
+    }
+
+    public void clearPopularQueriesQueue() {
+        queryStat.clearPopularQueriesQueue();
+    }
+
+    public TabularData getSlowQueries() {
+        return asTabularData(queryStat.getSlowQueries());
     }
 
-    public TabularData getQueries() {
+    public TabularData getPopularQueries() {
+        return asTabularData(queryStat.getPopularQueries());
+    }
+
+    private TabularData asTabularData(QueryStatDto[] data) {
         TabularDataSupport tds = null;
         try {
             CompositeType ct = QueryStatCompositeTypeFactory.getCompositeType();
@@ -95,7 +98,7 @@ public class QueryStatManager implements
                     "Query History", ct, QueryStatCompositeTypeFactory.index);
             tds = new TabularDataSupport(tt);
 
-            for (QueryStatDto q : getTopQueries()) {
+            for (QueryStatDto q : data) {
                 tds.put(new CompositeDataSupport(ct,
                         QueryStatCompositeTypeFactory.names,
                         QueryStatCompositeTypeFactory.getValues(q)));
@@ -112,14 +115,14 @@ public class QueryStatManager implements
         private final static String[] index = { "position" };
 
         private final static String[] names = { "position", "duration",
-                "language", "statement", "creationTime" };
+                "occurrenceCount", "language", "statement", "creationTime" };
 
         private final static String[] descriptions = { "position", "duration",
-                "language", "statement", "creationTime" };
+                "occurrenceCount", "language", "statement", "creationTime" };
 
         private final static OpenType[] types = { SimpleType.LONG,
-                SimpleType.LONG, SimpleType.STRING, SimpleType.STRING,
-                SimpleType.STRING };
+                SimpleType.LONG, SimpleType.INTEGER, SimpleType.STRING,
+                SimpleType.STRING, SimpleType.STRING };
 
         public static CompositeType getCompositeType() throws OpenDataException {
             return new CompositeType(QueryStat.class.getName(),
@@ -128,7 +131,9 @@ public class QueryStatManager implements
 
         public static Object[] getValues(QueryStatDto q) {
             return new Object[] { q.getPosition(), q.getDuration(),
-                    q.getLanguage(), q.getStatement(), q.getCreationTime() };
+                    q.getOccurrenceCount(), q.getLanguage(), q.getStatement(),
+                    q.getCreationTime() };
         }
     }
+
 }

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java?rev=1205199&r1=1205198&r2=1205199&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryImpl.java
Tue Nov 22 22:31:01 2011
@@ -137,12 +137,12 @@ public class QueryImpl extends AbstractQ
                     }
                 });
         time = System.nanoTime() - time;
-        final long timeMs = time / 1000;
+        final long timeMs = time / 1000000;
         log.debug("executed in {} ms. ({})", timeMs, statement);
         RepositoryStatisticsImpl statistics = sessionContext
                 .getRepositoryContext().getRepositoryStatistics();
         statistics.getCounter(Type.QUERY_COUNT).incrementAndGet();
-        statistics.getCounter(Type.QUERY_DURATION).addAndGet(time);
+        statistics.getCounter(Type.QUERY_DURATION).addAndGet(timeMs);
         sessionContext.getRepositoryContext().getStatManager().getQueryStat()
                 .logQuery(language, statement, timeMs);
         return result;

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java?rev=1205199&r1=1205198&r2=1205199&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/QueryObjectModelImpl.java
Tue Nov 22 22:31:01 2011
@@ -135,13 +135,14 @@ public class QueryObjectModelImpl extend
                     }
                 });
         time = System.nanoTime() - time;
-        log.debug("executed in {} ms. ({})", time / 1000, statement);
+        final long timeMs = time / 1000000;
+        log.debug("executed in {} ms. ({})", timeMs, statement);
         RepositoryStatisticsImpl statistics = sessionContext
                 .getRepositoryContext().getRepositoryStatistics();
         statistics.getCounter(Type.QUERY_COUNT).incrementAndGet();
-        statistics.getCounter(Type.QUERY_DURATION).addAndGet(time);
+        statistics.getCounter(Type.QUERY_DURATION).addAndGet(timeMs);
         sessionContext.getRepositoryContext().getStatManager().getQueryStat()
-                .logQuery(language, statement, time);
+                .logQuery(language, statement, timeMs);
         return result;
     }
 

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java?rev=1205199&r1=1205198&r2=1205199&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoImpl.java
Tue Nov 22 22:31:01 2011
@@ -54,6 +54,11 @@ public class QueryStatDtoImpl implements
      */
     private final String statement;
 
+    /**
+     * used in popular queries list
+     */
+    private int occurrenceCount = 1;
+
     public QueryStatDtoImpl(final String language, final String statement,
             long durationMs) {
         this.durationMs = durationMs;
@@ -95,4 +100,46 @@ public class QueryStatDtoImpl implements
                 + durationMs + ", language=" + language + ", statement="
                 + statement + "]";
     }
+
+    public int getOccurrenceCount() {
+        return occurrenceCount;
+    }
+
+    public void setOccurrenceCount(int occurrenceCount) {
+        this.occurrenceCount = occurrenceCount;
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result
+                + ((language == null) ? 0 : language.hashCode());
+        result = prime * result
+                + ((statement == null) ? 0 : statement.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
+            return true;
+        if (obj == null)
+            return false;
+        if (getClass() != obj.getClass())
+            return false;
+        QueryStatDtoImpl other = (QueryStatDtoImpl) obj;
+        if (language == null) {
+            if (other.language != null)
+                return false;
+        } else if (!language.equals(other.language))
+            return false;
+        if (statement == null) {
+            if (other.statement != null)
+                return false;
+        } else if (!statement.equals(other.statement))
+            return false;
+        return true;
+    }
+
 }

Added: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoOccurrenceComparator.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoOccurrenceComparator.java?rev=1205199&view=auto
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoOccurrenceComparator.java
(added)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoOccurrenceComparator.java
Tue Nov 22 22:31:01 2011
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.core.stats;
+
+import java.util.Comparator;
+
+/**
+ * QueryStatDto comparator by occurrence count
+ * 
+ * used by the popular queries queue
+ * 
+ */
+public class QueryStatDtoOccurrenceComparator implements
+        Comparator<QueryStatDtoImpl> {
+    public int compare(QueryStatDtoImpl o1, QueryStatDtoImpl o2) {
+        return new Integer(o1.getOccurrenceCount()).compareTo(o2
+                .getOccurrenceCount());
+    }
+}

Propchange: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatDtoOccurrenceComparator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java?rev=1205199&r1=1205198&r2=1205199&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/QueryStatImpl.java
Tue Nov 22 22:31:01 2011
@@ -16,8 +16,11 @@
  */
 package org.apache.jackrabbit.core.stats;
 
+import java.util.Arrays;
+import java.util.Collections;
 import java.util.Comparator;
-import java.util.PriorityQueue;
+import java.util.Iterator;
+import java.util.concurrent.PriorityBlockingQueue;
 
 import org.apache.jackrabbit.api.stats.QueryStatDto;
 
@@ -29,10 +32,59 @@ public class QueryStatImpl implements Qu
 
     private final static Comparator<QueryStatDto> comparator = new QueryStatDtoComparator();
 
-    private int queueSize = 15;
+    private final BoundedPriorityBlockingQueue<QueryStatDto> slowQueries = new BoundedPriorityBlockingQueue<QueryStatDto>(
+            15, comparator);
 
-    private PriorityQueue<QueryStatDto> queries = new PriorityQueue<QueryStatDto>(
-            queueSize + 1, comparator);
+    private final static Comparator<QueryStatDtoImpl> comparatorOccurrence = new QueryStatDtoOccurrenceComparator();
+
+    /**
+     * the real queue size will be bigger than the desired number of popular
+     * queries by POPULAR_QUEUE_MULTIPLIER times
+     */
+    private static final int POPULAR_QUEUE_MULTIPLIER = 5;
+
+    private final BoundedPriorityBlockingQueue<QueryStatDtoImpl> popularQueries = new
BoundedPriorityBlockingQueue<QueryStatDtoImpl>(
+            15 * POPULAR_QUEUE_MULTIPLIER, comparatorOccurrence);
+
+    private static final class BoundedPriorityBlockingQueue<E> extends
+            PriorityBlockingQueue<E> {
+
+        private static final long serialVersionUID = 1L;
+        private int maxSize;
+
+        public BoundedPriorityBlockingQueue(int maxSize,
+                Comparator<? super E> comparator) {
+            super(maxSize + 1, comparator);
+            this.maxSize = maxSize;
+        }
+
+        @Override
+        public boolean offer(E e) {
+            boolean s = super.offer(e);
+            if (!s) {
+                return false;
+            }
+            if (size() > maxSize) {
+                poll();
+            }
+            return true;
+        }
+
+        public synchronized void setMaxSize(int maxSize) {
+            if (maxSize < this.maxSize) {
+                // shrink the queue
+                int delta = this.maxSize - maxSize;
+                for (int i = 0; i < delta; i++) {
+                    poll();
+                }
+            }
+            this.maxSize = maxSize;
+        }
+
+        public int getMaxSize() {
+            return maxSize;
+        }
+    }
 
     private boolean enabled = false;
 
@@ -40,27 +92,19 @@ public class QueryStatImpl implements Qu
     }
 
     public int getSlowQueriesQueueSize() {
-        return queueSize;
+        return slowQueries.getMaxSize();
     }
 
     public void setSlowQueriesQueueSize(int size) {
-        synchronized (queries) {
-            this.queueSize = size;
-            this.queries = new PriorityQueue<QueryStatDto>(this.queueSize + 1,
-                    comparator);
-        }
+        slowQueries.setMaxSize(size);
     }
 
     public boolean isEnabled() {
         return enabled;
     }
 
-    public void setEnabled(boolean enabled) {
-        synchronized (queries) {
-            this.enabled = enabled;
-            this.queries = new PriorityQueue<QueryStatDto>(this.queueSize + 1,
-                    comparator);
-        }
+    public synchronized void setEnabled(boolean enabled) {
+        this.enabled = enabled;
     }
 
     public void logQuery(final String language, final String statement,
@@ -68,25 +112,63 @@ public class QueryStatImpl implements Qu
         if (!enabled) {
             return;
         }
-        synchronized (queries) {
-            queries.add(new QueryStatDtoImpl(language, statement, durationMs));
-            if (queries.size() > queueSize) {
-                queries.remove();
+        final QueryStatDtoImpl qs = new QueryStatDtoImpl(language, statement,
+                durationMs);
+        slowQueries.offer(qs);
+        Iterator<QueryStatDtoImpl> iterator = popularQueries.iterator();
+        while (iterator.hasNext()) {
+            QueryStatDtoImpl qsdi = iterator.next();
+            if (qsdi.equals(qs)) {
+                qs.setOccurrenceCount(qsdi.getOccurrenceCount() + 1);
+                iterator.remove();
+                break;
             }
         }
+        popularQueries.offer(qs);
     }
 
     public void clearSlowQueriesQueue() {
-        synchronized (queries) {
-            queries.clear();
+        slowQueries.clear();
+    }
+
+    public QueryStatDto[] getSlowQueries() {
+        QueryStatDto[] top = slowQueries.toArray(new QueryStatDto[slowQueries
+                .size()]);
+        Arrays.sort(top, Collections.reverseOrder(comparator));
+        for (int i = 0; i < top.length; i++) {
+            top[i].setPosition(i + 1);
         }
+        return top;
     }
 
-    public void reset() {
-        clearSlowQueriesQueue();
+    public QueryStatDto[] getPopularQueries() {
+        QueryStatDtoImpl[] top = popularQueries
+                .toArray(new QueryStatDtoImpl[popularQueries.size()]);
+        Arrays.sort(top, Collections.reverseOrder(comparatorOccurrence));
+        int retSize = Math.min(popularQueries.size(),
+                popularQueries.getMaxSize() / POPULAR_QUEUE_MULTIPLIER);
+        QueryStatDto[] retval = new QueryStatDto[retSize];
+        for (int i = 0; i < retSize; i++) {
+            top[i].setPosition(i + 1);
+            retval[i] = top[i];
+        }
+        return top;
     }
 
-    public QueryStatDto[] getSlowQueries() {
-        return queries.toArray(new QueryStatDto[queries.size()]);
+    public int getPopularQueriesQueueSize() {
+        return popularQueries.getMaxSize();
+    }
+
+    public void setPopularQueriesQueueSize(int size) {
+        popularQueries.setMaxSize(size * POPULAR_QUEUE_MULTIPLIER);
+    }
+
+    public void clearPopularQueriesQueue() {
+        popularQueries.clear();
+    }
+
+    public void reset() {
+        clearSlowQueriesQueue();
+        clearPopularQueriesQueue();
     }
 }



Mime
View raw message