tmf: Move timestamps to their own package
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
index dcf21ed4dfc6e6600c0ff57ff94a69f637501536..c7981f683a866afbfaa3062619cf6f1adc0d164f 100644 (file)
@@ -1,27 +1,26 @@
 /*******************************************************************************
- * 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
+ *   Patrick Tasse - Updated for removal of context clone
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.CoreException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
-import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType;
-import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
-import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
-import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
+import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
+import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
@@ -33,7 +32,7 @@ import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
  * @version 1.0
  * @author Matthew khouzam
  */
-public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
+public class CtfTmfTrace extends TmfTrace implements ITmfEventParser {
 
 
     //-------------------------------------------
@@ -48,9 +47,6 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
     //        Fields
     //-------------------------------------------
 
-    /** Reference to the state system assigned to this trace */
-    protected IStateSystemQuerier ss = null;
-
     /* Reference to the CTF Trace */
     private CTFTrace fTrace;
 
@@ -79,7 +75,6 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
          * because the super needs to know the cache size.
          */
         setCacheSize();
-        super.initTrace(resource, path, eventType);
 
         @SuppressWarnings("unused")
         CtfTmfEventType type;
@@ -87,11 +82,11 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
         try {
             this.fTrace = new CTFTrace(path);
             CtfIteratorManager.addTrace(this);
-            CtfTmfLightweightContext ctx;
+            CtfTmfContext ctx;
             /* Set the start and (current) end times for this trace */
-            ctx = (CtfTmfLightweightContext) seekEvent(0L);
+            ctx = (CtfTmfContext) seekEvent(0L);
             CtfTmfEvent event = getNext(ctx);
-            if(ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) {
+            if((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
                 /* Handle the case where the trace is empty */
                 this.setStartTime(TmfTimestamp.BIG_BANG);
             } else {
@@ -109,17 +104,7 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
             throw new TmfTraceException(e.getMessage(), e);
         }
 
-        //FIXME This should be called via the ExperimentUpdated signal
-        buildStateSystem();
-
-        /* Refresh the project, so it can pick up new files that got created. */
-        if ( resource != null) {
-            try {
-                resource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
-            } catch (CoreException e) {
-                throw new TmfTraceException(e.getMessage(), e);
-            }
-        }
+        super.initTrace(resource, path, eventType);
     }
 
     /* (non-Javadoc)
@@ -128,6 +113,10 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
     @Override
     public synchronized void dispose() {
         CtfIteratorManager.removeTrace(this);
+        if (fTrace != null) {
+            fTrace.dispose();
+            fTrace = null;
+        }
         super.dispose();
     }
 
@@ -142,7 +131,9 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
     public boolean validate(final IProject project, final String path) {
         try {
             final CTFTrace temp = new CTFTrace(path);
-            return temp.majortIsSet(); // random test
+            boolean valid = temp.majortIsSet(); // random test
+            temp.dispose();
+            return valid;
         } catch (final CTFReaderException e) {
             /* Nope, not a CTF trace we can read */
             return false;
@@ -155,65 +146,56 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
      * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
      */
     @Override
-    public ITmfLocation<?> getCurrentLocation() {
+    public ITmfLocation getCurrentLocation() {
         return null;
     }
 
-
-
     @Override
-    public double getLocationRatio(ITmfLocation<?> location) {
+    public double getLocationRatio(ITmfLocation location) {
         final CtfLocation curLocation = (CtfLocation) location;
-        final CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
+        final CtfTmfContext context = new CtfTmfContext(this);
         context.setLocation(curLocation);
-        context.seek(curLocation.getLocation());
-        final CtfLocationData currentTime = ((CtfLocationData)context.getLocation().getLocation());
+        context.seek(curLocation.getLocationInfo());
+        final CtfLocationInfo currentTime = ((CtfLocationInfo)context.getLocation().getLocationInfo());
         final long startTime = getIterator(this, context).getStartTime();
         final long endTime = getIterator(this, context).getEndTime();
         return ((double) currentTime.getTimestamp() - startTime)
                 / (endTime - startTime);
     }
 
-
-
-
-
-    /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
-     */
-    @Override
-    public synchronized ITmfContext seekEvent(ITmfTimestamp timestamp) {
-        if( timestamp instanceof CtfTmfTimestamp){
-            CtfTmfLightweightContext iter = new CtfTmfLightweightContext(this);
-            iter.seek(timestamp.getValue());
-            return iter;
-        }
-        return super.seekEvent(timestamp);
-    }
-
     /**
      * Method seekEvent.
      * @param location ITmfLocation<?>
      * @return ITmfContext
      */
     @Override
-    public ITmfContext seekEvent(final ITmfLocation<?> location) {
+    public synchronized ITmfContext seekEvent(final ITmfLocation location) {
         CtfLocation currentLocation = (CtfLocation) location;
-        CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
+        CtfTmfContext context = new CtfTmfContext(this);
+        if (fTrace == null) {
+            context.setLocation(null);
+            context.setRank(ITmfContext.UNKNOWN_RANK);
+            return context;
+        }
         /*
          * The rank is set to 0 if the iterator seeks the beginning. If not, it
          * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
          * by rank for now.
          */
         if (currentLocation == null) {
-            currentLocation = new CtfLocation(new CtfLocationData(0L, 0L));
+            currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
             context.setRank(0);
         }
-        if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
-            ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
-            currentLocation.setLocation(getEndTime().getValue() + 1, 0L);
+        if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
+            currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L);
         }
         context.setLocation(currentLocation);
+        if (location == null) {
+            CtfTmfEvent event = getIterator(this, context).getCurrentEvent();
+            if (event != null) {
+                currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0);
+            }
+        }
         if(context.getRank() != 0) {
             context.setRank(ITmfContext.UNKNOWN_RANK);
         }
@@ -222,9 +204,18 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
 
 
     @Override
-    public ITmfContext seekEvent(double ratio) {
-        CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
-        context.seek((long) (this.getNbEvents() * ratio));
+    public synchronized ITmfContext seekEvent(double ratio) {
+        CtfTmfContext context = new CtfTmfContext(this);
+        if (fTrace == null) {
+            context.setLocation(null);
+            context.setRank(ITmfContext.UNKNOWN_RANK);
+            return context;
+        }
+        final long end = this.getEndTime().getValue();
+        final long start = this.getStartTime().getValue();
+        final long diff = end - start;
+        final long ratioTs = Math.round(diff * ratio) + start;
+        context.seek(ratioTs);
         context.setRank(ITmfContext.UNKNOWN_RANK);
         return context;
     }
@@ -237,12 +228,15 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
      */
     @Override
     public synchronized CtfTmfEvent getNext(final ITmfContext context) {
+        if (fTrace == null) {
+            return null;
+        }
         CtfTmfEvent event = null;
-        if (context instanceof CtfTmfLightweightContext) {
-            if (CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocation())) {
+        if (context instanceof CtfTmfContext) {
+            if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
                 return null;
             }
-            CtfTmfLightweightContext ctfContext = (CtfTmfLightweightContext) context;
+            CtfTmfContext ctfContext = (CtfTmfContext) context;
             event = ctfContext.getCurrentEvent();
 
             if (event != null) {
@@ -255,30 +249,6 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
         return event;
     }
 
-    /**
-     * Suppressing the warning, because the 'throws' will usually happen in
-     * sub-classes.
-     *
-     * @throws TmfTraceException
-     */
-    @SuppressWarnings("unused")
-    protected void buildStateSystem() throws TmfTraceException {
-        /*
-         * Nothing is done in the basic implementation, please specify
-         * how/if to build a state system in derived classes.
-         */
-        return;
-    }
-
-    /**
-     * Method getStateSystem.
-     *
-     * @return IStateSystemQuerier
-     */
-    public IStateSystemQuerier getStateSystem() {
-        return this.ss;
-    }
-
     /**
      * gets the CTFtrace that this is wrapping
      * @return the CTF trace
@@ -343,9 +313,9 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
     @Override
     public CtfTmfEvent parseEvent(ITmfContext context) {
         CtfTmfEvent event = null;
-        if( context instanceof CtfTmfLightweightContext ){
-            CtfTmfLightweightContext itt = (CtfTmfLightweightContext) context.clone();
-            event = itt.getCurrentEvent();
+        if (context instanceof CtfTmfContext) {
+            final ITmfContext tmpContext = seekEvent(context.getLocation());
+            event = getNext(tmpContext);
         }
         return event;
     }
@@ -361,7 +331,17 @@ public class CtfTmfTrace extends TmfTrace implements ITmfEventParser{
     //          Helpers
     //-------------------------------------------
 
-    private static CtfIterator getIterator(CtfTmfTrace trace,  CtfTmfLightweightContext context) {
+    private static CtfIterator getIterator(CtfTmfTrace trace,  CtfTmfContext context) {
         return CtfIteratorManager.getIterator(trace, context);
     }
+
+    /**
+     * Get an iterator to the trace
+     *
+     * @return an iterator to the trace
+     * @since 2.0
+     */
+    public CtfIterator createIterator(){
+        return new CtfIterator(this);
+    }
 }
This page took 0.029236 seconds and 5 git commands to generate.