You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yacy_search_server/source/net/yacy/kelondro/util/EventTracker.java

123 lines
4.4 KiB

// EventTracker.java
// (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
// first published 17.11.2007 on http://yacy.net
//
// This is a part of YaCy, a peer-to-peer based web search engine
//
// $LastChangedDate: 2009-11-05 21:28:37 +0100 (Do, 05 Nov 2009) $
// $LastChangedRevision: 6458 $
// $LastChangedBy: orbiter $
//
// LICENSE
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package net.yacy.kelondro.util;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
public class EventTracker {
private final static Map<String, ConcurrentLinkedQueue<Event>> historyMaps = new ConcurrentHashMap<String, ConcurrentLinkedQueue<Event>>();
private final static Map<String, Long> eventAccess = new ConcurrentHashMap<String, Long>(); // value: last time when this was accessed
public final static void update(
final String eventName,
final Object eventPayload,
boolean useProtection,
int maxQueueSize,
long maxQueueAge) {
// check protection against too heavy access
if (useProtection) {
Long lastAcc = eventAccess.get(eventName);
if (lastAcc == null) {
eventAccess.put(eventName, Long.valueOf(System.currentTimeMillis()));
} else {
long time = System.currentTimeMillis();
if (time - lastAcc.longValue() < 1000) {
return; // protect against too heavy load
}
eventAccess.put(eventName, Long.valueOf(time));
}
}
// get event history container
ConcurrentLinkedQueue<Event> history = historyMaps.get(eventName);
// create history
if (history == null) {
history = new ConcurrentLinkedQueue<Event>();
// update entry
history.offer(new Event(eventPayload));
// store map
historyMaps.put(eventName, history);
return;
}
// update history
history.offer(new Event(eventPayload));
// clean up too old entries
int tp = history.size() - maxQueueSize;
while (tp-- > 0) history.poll();
if (history.size() % 10 == 0) { // reduce number of System.currentTimeMillis() calls
synchronized (history) {
if (history.size() % 10 == 0) { // check again
Event e;
final long now = System.currentTimeMillis();
while (history.size() > 0) {
e = history.peek();
if (now - e.time < maxQueueAge) break;
history.poll();
}
}
}
}
}
public final static Iterator<Event> getHistory(final String eventName) {
ConcurrentLinkedQueue<Event> list = historyMaps.get(eventName);
if (list == null) return null;
return list.iterator();
}
public final static int countEvents(final String eventName, long time) {
Iterator<Event> event = getHistory(eventName);
if (event == null) return 0;
long now = System.currentTimeMillis();
int count = 0;
while (event.hasNext()) {
if (now - event.next().time < time) count++;
}
return count;
}
public final static class Event {
public Object payload;
public long time;
public Event(final Object payload) {
this.payload = payload;
this.time = System.currentTimeMillis();
}
}
}