Index: jackrabbit-core/src/test/java/org/apache/jackrabbit/core/stats/RepositoryStatisticsImplTest.java =================================================================== --- jackrabbit-core/src/test/java/org/apache/jackrabbit/core/stats/RepositoryStatisticsImplTest.java (revision 0) +++ jackrabbit-core/src/test/java/org/apache/jackrabbit/core/stats/RepositoryStatisticsImplTest.java (revision 0) @@ -0,0 +1,60 @@ +/* + * 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.Iterator; +import java.util.Map.Entry; + +import junit.framework.TestCase; + +import org.apache.jackrabbit.api.stats.TimeSeries; + +public class RepositoryStatisticsImplTest extends TestCase { + + private static final int DEFAULT_NUMBER_OF_ELEMENTS = 17; + + public void testDefaultIterator() { + RepositoryStatisticsImpl repositoryStatistics = new RepositoryStatisticsImpl(); + + Iterator> iterator = repositoryStatistics.iterator(); + int count = 0; + while (iterator.hasNext()) { + count++; + iterator.next(); + } + assertEquals(DEFAULT_NUMBER_OF_ELEMENTS, count); + } + + public void testIteratorWithSingleCustomType() { + RepositoryStatisticsImpl repositoryStatistics = new RepositoryStatisticsImpl(); + String type = "customType"; + repositoryStatistics.getCounter(type, false); + + Iterator> iterator = repositoryStatistics.iterator(); + int count = 0; + boolean customTypeExists = false; + while (iterator.hasNext()) { + count++; + Entry entry = iterator.next(); + if (type.equals(entry.getKey())) { + customTypeExists = true; + } + } + assertEquals(DEFAULT_NUMBER_OF_ELEMENTS + 1, count); + assertTrue(customTypeExists); + } +} Property changes on: jackrabbit-core/src/test/java/org/apache/jackrabbit/core/stats/RepositoryStatisticsImplTest.java ___________________________________________________________________ Added: svn:eol-style + native Index: jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/RepositoryStatistics.java =================================================================== --- jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/RepositoryStatistics.java (revision 1291877) +++ jackrabbit-api/src/main/java/org/apache/jackrabbit/api/stats/RepositoryStatistics.java (working copy) @@ -53,10 +53,20 @@ this.resetValueEachSecond = resetValueEachSecond; } + public static Type getType(String type) { + Type realType = null; + try { + realType = Type.valueOf(type); + } catch (IllegalArgumentException ignore) {}; + return realType; + } + public boolean isResetValueEachSecond() { return resetValueEachSecond; } } TimeSeries getTimeSeries(Type type); + + TimeSeries getTimeSeries(String type, boolean resetValueEachSecond); } Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/RepositoryStatisticsImpl.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/RepositoryStatisticsImpl.java (revision 1291877) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/RepositoryStatisticsImpl.java (working copy) @@ -26,17 +26,16 @@ import java.util.concurrent.atomic.AtomicLong; import org.apache.jackrabbit.api.stats.RepositoryStatistics; -import org.apache.jackrabbit.api.stats.RepositoryStatistics.Type; import org.apache.jackrabbit.api.stats.TimeSeries; public class RepositoryStatisticsImpl implements - Iterable>, RepositoryStatistics { + Iterable>, RepositoryStatistics { - private final Map recorders = - new HashMap(); + private final Map recorders = + new HashMap(); - private final Map avg = - new HashMap(); + private final Map avg = + new HashMap(); public RepositoryStatisticsImpl() { getOrCreateRecorder(Type.SESSION_COUNT); @@ -55,7 +54,7 @@ } private void createAvg(Type count, Type duration, Type avgTs) { - avg.put(avgTs, new TimeSeriesAverage(getOrCreateRecorder(duration), + avg.put(avgTs.name(), new TimeSeriesAverage(getOrCreateRecorder(duration), getOrCreateRecorder(count))); } @@ -68,8 +67,8 @@ }, 1, 1, TimeUnit.SECONDS); } - public synchronized Iterator> iterator() { - Map map = new TreeMap(); + public synchronized Iterator> iterator() { + Map map = new TreeMap(); map.putAll(recorders); map.putAll(avg); return map.entrySet().iterator(); @@ -79,18 +78,29 @@ return getOrCreateRecorder(type).getCounter(); } + public AtomicLong getCounter(String type, boolean resetValueEachSecond) { + return getOrCreateRecorder(type, resetValueEachSecond).getCounter(); + } + public TimeSeries getTimeSeries(Type type) { + return getTimeSeries(type.name(), type.isResetValueEachSecond()); + } + + public TimeSeries getTimeSeries(String type, boolean resetValueEachSecond) { if (avg.containsKey(type)) { return avg.get(type); - } else { - return getOrCreateRecorder(type); } + return getOrCreateRecorder(type, resetValueEachSecond); } private synchronized TimeSeriesRecorder getOrCreateRecorder(Type type) { + return getOrCreateRecorder(type.name(), type.isResetValueEachSecond()); + } + + private synchronized TimeSeriesRecorder getOrCreateRecorder(String type, boolean resetValueEachSecond) { TimeSeriesRecorder recorder = recorders.get(type); if (recorder == null) { - recorder = new TimeSeriesRecorder(type); + recorder = new TimeSeriesRecorder(resetValueEachSecond); recorders.put(type, recorder); } return recorder; @@ -101,5 +111,4 @@ recorder.recordOneSecond(); } } - } Index: jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/TimeSeriesRecorder.java =================================================================== --- jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/TimeSeriesRecorder.java (revision 1291877) +++ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/stats/TimeSeriesRecorder.java (working copy) @@ -29,12 +29,12 @@ */ class TimeSeriesRecorder implements TimeSeries { - /** Type */ - private final Type type; - /** Value */ private final AtomicLong counter = new AtomicLong(); + /** Whether to reset value each second */ + private final boolean resetValueEachSecond; + /** Measured value per second over the last minute. */ private final long[] valuePerSecond = new long[60]; @@ -60,7 +60,11 @@ private int weeks = 0; public TimeSeriesRecorder(Type type) { - this.type = type; + this(type.isResetValueEachSecond()); + } + + public TimeSeriesRecorder(boolean resetValueEachSecond) { + this.resetValueEachSecond = resetValueEachSecond; } /** @@ -79,7 +83,7 @@ * second. */ public synchronized void recordOneSecond() { - if (type.isResetValueEachSecond()) { + if (resetValueEachSecond) { valuePerSecond[seconds++] = counter.getAndSet(0); } else { valuePerSecond[seconds++] = counter.get(); @@ -133,7 +137,7 @@ sum += array[i]; } - if (type.isResetValueEachSecond()) { + if (resetValueEachSecond) { return sum; } return sum / array.length; @@ -154,5 +158,4 @@ } return reverse; } - }