Remove context clone and add trace ranks to experiment location
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / TmfExperiment.java
index 3fc0ffe532516d35e322a238b24142b09a737276..674b3dc9097e551d1ab41e08823b0b75ecba6649 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2010, 2012 Ericsson
+ * Copyright (c) 2009, 2010, 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
@@ -9,6 +9,8 @@
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
  *   Francois Chouinard - Updated as per TMF Trace Model 1.0
+ *   Patrick Tasse - Updated for removal of context clone
+ *   Patrick Tasse - Updated for ranks in experiment location
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.trace;
@@ -27,13 +29,9 @@ import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
 import org.eclipse.linuxtools.tmf.core.signal.TmfClearExperimentSignal;
-import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal;
-import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentDisposedSignal;
-import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentRangeUpdatedSignal;
-import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
-import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentUpdatedSignal;
 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
-import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
+import org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal;
+import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
 
 /**
  * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
@@ -57,11 +55,6 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // Attributes
     // ------------------------------------------------------------------------
 
-    /**
-     * The currently selected experiment (null if none)
-     */
-    protected static TmfExperiment fCurrentExperiment = null;
-
     /**
      * The set of traces that constitute the experiment
      */
@@ -116,13 +109,6 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     @Override
     public synchronized void dispose() {
 
-        final TmfExperimentDisposedSignal signal = new TmfExperimentDisposedSignal(this, this);
-        broadcast(signal);
-
-        if (fCurrentExperiment == this) {
-            fCurrentExperiment = null;
-        }
-
         // Clean up the index if applicable
         if (getIndexer() != null) {
             getIndexer().dispose();
@@ -169,25 +155,6 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // Accessors
     // ------------------------------------------------------------------------
 
-    /**
-     * Selects the current, framework-wide, experiment
-     *
-     * @param experiment das experiment
-     */
-    public static void setCurrentExperiment(final TmfExperiment experiment) {
-        if (fCurrentExperiment != null && fCurrentExperiment != experiment) {
-            fCurrentExperiment.dispose();
-        }
-        fCurrentExperiment = experiment;
-    }
-
-    /**
-     * @return das experiment
-     */
-    public static TmfExperiment getCurrentExperiment() {
-        return fCurrentExperiment;
-    }
-
     /**
      * Get the list of traces. Handle with care...
      *
@@ -233,11 +200,11 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // Request management
     // ------------------------------------------------------------------------
 
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
+    /**
+     * @since 2.0
      */
     @Override
-    protected synchronized ITmfContext armRequest(final ITmfDataRequest request) {
+    public synchronized ITmfContext armRequest(final ITmfDataRequest request) {
 
         // Make sure we have something to read from
         if (fTraces == null) {
@@ -279,32 +246,33 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
             return null;
         }
 
+        // Initialize the location array if necessary
+        TmfLocationArray locationArray = ((location == null) ?
+                new TmfLocationArray(fTraces.length) :
+                ((TmfExperimentLocation) location).getLocationInfo());
+
+        ITmfLocation[] locations = locationArray.getLocations();
+        long[] ranks = locationArray.getRanks();
+
         // Create and populate the context's traces contexts
-        final TmfExperimentContext context = new TmfExperimentContext(new ITmfContext[fTraces.length]);
-        ITmfLocation[] expLocations = new ITmfLocation[fTraces.length];
-        if (location != null) {
-            TmfExperimentLocation locations = (TmfExperimentLocation) location;
-            int index = 0;
-            ITmfLocation l = locations.getLocationInfo().getLocation(index);
-            while (index < expLocations.length && l != null) {
-                expLocations[index] = l;
-                l = locations.getLocationInfo().getLocation(++index);
-            }
-        }
+        final TmfExperimentContext context = new TmfExperimentContext(fTraces.length);
 
         // Position the traces
+        long rank = 0;
         for (int i = 0; i < fTraces.length; i++) {
             // Get the relevant trace attributes
-            final ITmfLocation trcLocation = expLocations[i];
-            context.getContexts()[i] = fTraces[i].seekEvent(trcLocation);
-            expLocations[i] = context.getContexts()[i].getLocation();
-            context.getEvents()[i] = fTraces[i].getNext(context.getContexts()[i]);
+            final ITmfContext traceContext = fTraces[i].seekEvent(locations[i]);
+            context.getContexts()[i] = traceContext;
+            traceContext.setRank(ranks[i]);
+            locations[i] = traceContext.getLocation(); // update location after seek
+            context.getEvents()[i] = fTraces[i].getNext(traceContext);
+            rank += ranks[i];
         }
 
         // Finalize context
-        context.setLocation(new TmfExperimentLocation(new TmfLocationArray(expLocations)));
+        context.setLocation(new TmfExperimentLocation(new TmfLocationArray(locations, ranks)));
         context.setLastTrace(TmfExperimentContext.NO_TRACE);
-        context.setRank((location == null) ? 0 : ITmfContext.UNKNOWN_RANK);
+        context.setRank(rank);
 
         return context;
     }
@@ -328,7 +296,12 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     @Override
     public double getLocationRatio(final ITmfLocation location) {
         if (location instanceof TmfExperimentLocation) {
-            return (double) seekEvent(location).getRank() / getNbEvents();
+            long rank = 0;
+            TmfLocationArray locationArray = ((TmfExperimentLocation) location).getLocationInfo();
+            for (int i = 0; i < locationArray.size(); i++) {
+                rank += locationArray.getRank(i);
+            }
+            return (double) rank / getNbEvents();
         }
         return 0.0;
     }
@@ -338,11 +311,8 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
      */
     @Override
     public ITmfLocation getCurrentLocation() {
-        ITmfLocation[] locations = new ITmfLocation[fTraces.length];
-        for (int i = 0; i < fTraces.length; i++) {
-            locations[i] = fTraces[i].getCurrentLocation();
-        }
-        return new TmfExperimentLocation(new TmfLocationArray(locations));
+        // never used
+        return null;
     }
 
     // ------------------------------------------------------------------------
@@ -354,8 +324,8 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
      */
     @Override
     public synchronized ITmfEvent parseEvent(final ITmfContext context) {
-        final ITmfContext savedContext = context.clone();
-        final ITmfEvent event = getNext(savedContext);
+        final ITmfContext tmpContext = seekEvent(context.getLocation());
+        final ITmfEvent event = getNext(tmpContext);
         return event;
     }
 
@@ -408,9 +378,11 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
                 expContext.setLastTrace(trace);
                 final ITmfContext traceContext = expContext.getContexts()[trace];
 
-                expContext.setLocation(new TmfExperimentLocation(
-                        (TmfExperimentLocation) expContext.getLocation(),
-                        trace, traceContext.getLocation()));
+                // Update the experiment location
+                TmfLocationArray locationArray = new TmfLocationArray(
+                        ((TmfExperimentLocation) expContext.getLocation()).getLocationInfo(),
+                        trace, traceContext.getLocation(), traceContext.getRank());
+                expContext.setLocation(new TmfExperimentLocation(locationArray));
 
                 processEvent(event);
             }
@@ -419,6 +391,28 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
         return event;
     }
 
+    /* (non-Javadoc)
+     * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#getInitialRangeOffset()
+     */
+    /**
+     * @since 2.0
+     */
+    @Override
+    public ITmfTimestamp getInitialRangeOffset() {
+        if ((fTraces == null) || (fTraces.length == 0)) {
+            return super.getInitialRangeOffset();
+        }
+
+        ITmfTimestamp initTs = TmfTimestamp.BIG_CRUNCH;
+        for (int i = 0; i < fTraces.length; i++) {
+            ITmfTimestamp ts = fTraces[i].getInitialRangeOffset();
+            if (ts.compareTo(initTs) < 0) {
+                initTs = ts;
+            }
+        }
+        return initTs;
+    }
+
     /* (non-Javadoc)
      * @see java.lang.Object#toString()
      */
@@ -447,7 +441,7 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
                 return;
             }
             final TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp(), TmfTimestamp.BIG_CRUNCH);
-            final TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(this, this, timeRange);
+            final TmfTraceRangeUpdatedSignal signal = new TmfTraceRangeUpdatedSignal(this, this, timeRange);
 
             // Broadcast in separate thread to prevent deadlock
             new Thread() {
@@ -486,8 +480,8 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
                         }
                         safeTimestamp = endTimestamp;
                         if (timeRange != null) {
-                            final TmfExperimentRangeUpdatedSignal signal =
-                                    new TmfExperimentRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
+                            final TmfTraceRangeUpdatedSignal signal =
+                                    new TmfTraceRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
                             broadcast(signal);
                         }
                     }
@@ -518,56 +512,14 @@ public class TmfExperiment extends TmfTrace implements ITmfEventParser {
     // Signal handlers
     // ------------------------------------------------------------------------
 
-    private Integer fEndSynchReference;
-
-    /**
-     * Signal handler for the TmfExperimentSelectedSignal signal
-     *
-     * @param signal The incoming signal
-     */
-    @TmfSignalHandler
-    public void experimentSelected(final TmfExperimentSelectedSignal signal) {
-        final TmfExperiment experiment = signal.getExperiment();
-        if (experiment == this) {
-            setCurrentExperiment(experiment);
-            fEndSynchReference = Integer.valueOf(signal.getReference());
-        }
-    }
-
-    /**
-     * Signal handler for the TmfEndSynchSignal signal
-     *
-     * @param signal The incoming signal
-     */
-    @TmfSignalHandler
-    public void endSync(final TmfEndSynchSignal signal) {
-        if (fEndSynchReference != null && fEndSynchReference.intValue() == signal.getReference()) {
-            fEndSynchReference = null;
-            initializeStreamingMonitor();
-        }
-    }
-
-    /**
-     * Signal handler for the TmfTraceUpdatedSignal signal
-     *
-     * @param signal The incoming signal
+    /* (non-Javadoc)
+     * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#traceOpened(org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal)
      */
+    @Override
     @TmfSignalHandler
-    public void traceUpdated(final TmfTraceUpdatedSignal signal) {
+    public void traceOpened(TmfTraceOpenedSignal signal) {
         if (signal.getTrace() == this) {
-            broadcast(new TmfExperimentUpdatedSignal(this, this));
-        }
-    }
-
-    /**
-     * Signal handler for the TmfExperimentRangeUpdatedSignal signal
-     *
-     * @param signal The incoming signal
-     */
-    @TmfSignalHandler
-    public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal) {
-        if (signal.getExperiment() == this) {
-            getIndexer().buildIndex(getNbEvents(), signal.getRange(), false);
+            initializeStreamingMonitor();
         }
     }
 
This page took 0.028602 seconds and 5 git commands to generate.