- Move stat initialization, reduce number of rates
      - Add basic DOS prevention by not flooding if stores are too-frequent
This commit is contained in:
zzz
2010-01-24 02:36:42 +00:00
parent fdfbab850a
commit 087fd5a909
10 changed files with 101 additions and 30 deletions

View File

@@ -10,19 +10,28 @@ import java.util.concurrent.ConcurrentHashMap;
*/
public class ObjectCounter<K> {
private ConcurrentHashMap<K, Integer> _map;
private static final Integer ONE = Integer.valueOf(1);
public ObjectCounter() {
_map = new ConcurrentHashMap();
}
/**
* Add one.
* Not perfectly concurrent, new AtomicInteger(1) would be better,
* at the cost of some object churn.
* @return count after increment
*/
public void increment(K h) {
Integer i = _map.putIfAbsent(h, Integer.valueOf(1));
if (i != null)
_map.put(h, Integer.valueOf(i.intValue() + 1));
public int increment(K h) {
Integer i = _map.putIfAbsent(h, ONE);
if (i != null) {
int rv = i.intValue() + 1;
_map.put(h, Integer.valueOf(rv));
return rv;
}
return 1;
}
/**
* @return current count
*/
@@ -32,11 +41,20 @@ public class ObjectCounter<K> {
return i.intValue();
return 0;
}
/**
* @return set of objects with counts > 0
*/
public Set<K> objects() {
return _map.keySet();
}
/**
* start over
* @since 0.7.11
*/
public void clear() {
_map.clear();
}
}