/*******************************************************************************
- * Copyright (c) 2012 Ericsson
+ * Copyright (c) 2012, 2013 Ericsson
*
* All rights reserved. This program and the accompanying materials are made
* available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
- * Contributors: Matthew Khouzam - Initial API and implementation
+ * Contributors:
+ * Matthew Khouzam - Initial API and implementation
+ * Simon Delisle - Added a method to remove the iterator
*******************************************************************************/
package org.eclipse.linuxtools.tmf.core.ctfadaptor;
* the trace to register.
*/
public static synchronized void removeTrace(final CtfTmfTrace trace) {
- map.remove(trace);
+ CtfTraceManager mgr = map.remove(trace);
+ if (mgr != null) {
+ mgr.clear();
+ }
}
/**
* @param ctx
* the context
* @return the iterator
+ * @since 2.0
*/
public static synchronized CtfIterator getIterator(final CtfTmfTrace trace,
- final CtfTmfLightweightContext ctx) {
+ final CtfTmfContext ctx) {
return map.get(trace).getIterator(ctx);
}
+
+ /**
+ * Remove an iterator for a given trace and context
+ *
+ * @param trace
+ * the trace
+ * @param ctx
+ * the context
+ * @since 2.1
+ */
+ public static synchronized void removeIterator(final CtfTmfTrace trace, final CtfTmfContext ctx) {
+ CtfTraceManager traceManager = map.get(trace);
+ if (traceManager != null) {
+ traceManager.removeIterator(ctx);
+ }
+ }
}
/**
/*
* The map of the cache.
*/
- private final HashMap<CtfTmfLightweightContext, CtfIterator> fMap;
+ private final HashMap<CtfTmfContext, CtfIterator> fMap;
/*
* An array pointing to the same cache. this allows fast "random" accesses.
*/
- private final ArrayList<CtfTmfLightweightContext> fRandomAccess;
+ private final ArrayList<CtfTmfContext> fRandomAccess;
/*
* The parent trace
*/
private final Random fRnd;
public CtfTraceManager(CtfTmfTrace trace) {
- fMap = new HashMap<CtfTmfLightweightContext, CtfIterator>();
- fRandomAccess = new ArrayList<CtfTmfLightweightContext>();
+ fMap = new HashMap<CtfTmfContext, CtfIterator>();
+ fRandomAccess = new ArrayList<CtfTmfContext>();
fRnd = new Random(System.nanoTime());
fTrace = trace;
}
* the context to look up
* @return the iterator refering to the context
*/
- public CtfIterator getIterator(final CtfTmfLightweightContext context) {
+ public CtfIterator getIterator(final CtfTmfContext context) {
/*
* if the element is in the map, we don't need to do anything else.
*/
/*
* if we're not full yet, just add an element.
*/
- retVal = new CtfIterator(fTrace);
+ retVal = fTrace.createIterator();
addElement(context, retVal);
} else {
*/
retVal = replaceRandomElement(context);
}
- final CtfLocationData location = (CtfLocationData) context.getLocation().getLocation();
- retVal.seek( location);
+ if (context.getLocation() != null) {
+ final CtfLocationInfo location = (CtfLocationInfo) context.getLocation().getLocationInfo();
+ retVal.seek(location);
+ }
}
return retVal;
}
+ public void removeIterator(CtfTmfContext context) {
+ fMap.remove(context);
+ fRandomAccess.remove(context);
+ }
+
/**
* Add a pair of context and element to the hashmap and the arraylist.
*
* @param elem
* the iterator
*/
- private void addElement(final CtfTmfLightweightContext context,
+ private void addElement(final CtfTmfContext context,
final CtfIterator elem) {
fMap.put(context, elem);
fRandomAccess.add(context);
* @return the iterator of the removed elements.
*/
private CtfIterator replaceRandomElement(
- final CtfTmfLightweightContext context) {
+ final CtfTmfContext context) {
/*
* This needs some explanation too: We need to select a random victim
* and remove it. The order of the elements is not important, so instead
*/
final int size = fRandomAccess.size();
final int pos = fRnd.nextInt(size);
- final CtfTmfLightweightContext victim = fRandomAccess.get(pos);
+ final CtfTmfContext victim = fRandomAccess.get(pos);
fRandomAccess.set(pos, context);
final CtfIterator elem = fMap.remove(victim);
fMap.put(context, elem);
return elem;
}
+ void clear() {
+ for (CtfIterator iterator : fMap.values()) {
+ iterator.dispose();
+ }
+ fMap.clear();
+ fRandomAccess.clear();
+ }
}