rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / trace / CtfTmfTrace.java
index 2329e007e7bccf81f0b724330d954084d41d0cc2..e2ba2258253e2a84ece5c387e0acbfcc7ce98a3e 100644 (file)
@@ -39,6 +39,7 @@ import org.eclipse.tracecompass.ctf.core.event.CTFClock;
 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
+import org.eclipse.tracecompass.ctf.core.trace.Metadata;
 import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator;
 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator;
 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIteratorManager;
@@ -54,6 +55,7 @@ import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceProperties;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceWithPreDefinedEvents;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
+import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
 import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
@@ -110,6 +112,7 @@ public class CtfTmfTrace extends TmfTrace
      */
     private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
     private static final int CONFIDENCE = 10;
+    private static final int MIN_CONFIDENCE = 1;
 
     // -------------------------------------------
     // Fields
@@ -215,30 +218,52 @@ public class CtfTmfTrace extends TmfTrace
     /**
      * {@inheritDoc}
      * <p>
-     * The default implementation sets the confidence to 10 if the trace is a
-     * valid CTF trace.
+     * The default implementation of a CTF trace.
+     *
+     * Firstly a weak validation of the metadata is done to determine if the
+     * path is actually for a CTF trace. After that a full validation is done.
+     *
+     * If the weak and full validation are successful the confidence is set
+     * to 10.
+     *
+     * If the weak validation was successful, but the full validation fails
+     * a TraceValidationStatus with severity warning and confidence of 1 is
+     * returned.
+     *
+     * If both weak and full validation fails an error status is returned.
      */
     @Override
     public IStatus validate(final IProject project, final String path) {
+        boolean isMetadataFile = false;
         try {
-            final CTFTrace trace = new CTFTrace(path);
-            if (!trace.majorIsSet()) {
-                return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
-            }
-            try (CTFTraceReader ctfTraceReader = new CTFTraceReader(trace)) {
-                if (!ctfTraceReader.hasMoreEvents()) {
-                    // TODO: This will need an additional check when we
-                    // support live traces
-                    // because having no event is valid for a live trace
-                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_NoEvent);
+            isMetadataFile = Metadata.preValidate(path);
+        } catch (final CTFException e) {
+            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
+        }
+
+        if (isMetadataFile) {
+            // Trace is pre-validated, continue will full validation
+            try {
+                final CTFTrace trace = new CTFTrace(path);
+                if (!trace.majorIsSet()) {
+                    if (isMetadataFile) {
+                        return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet, null);
+                    }
+                    return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
                 }
+
+                // Validate using reader initialization
+                try (CTFTraceReader ctfTraceReader = new CTFTraceReader(trace)) {}
+
+                // Trace is validated, return with confidence
+                return new CtfTraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID, trace.getEnvironment());
+
+            } catch (final CTFException | BufferOverflowException e ) {
+                // return warning since it's a CTF trace but with errors in it
+                return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
             }
-            return new CtfTraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID, trace.getEnvironment());
-        } catch (final CTFException e) {
-            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString()); //$NON-NLS-1$
-        } catch (final BufferOverflowException e) {
-            return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + Messages.CtfTmfTrace_BufferOverflowErrorMessage); //$NON-NLS-1$
         }
+        return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError);
     }
 
     @Override
@@ -260,14 +285,10 @@ public class CtfTmfTrace extends TmfTrace
     @Override
     public double getLocationRatio(ITmfLocation location) {
         final CtfLocation curLocation = (CtfLocation) location;
-        final CtfTmfContext context = new CtfTmfContext(this);
-        context.setLocation(curLocation);
-        context.seek(curLocation.getLocationInfo());
-        final CtfLocationInfo currentTime = ((CtfLocationInfo) context.getLocation().getLocationInfo());
-        final long startTime = fIteratorManager.getIterator(context).getStartTime();
-        final long endTime = fIteratorManager.getIterator(context).getEndTime();
-        return ((double) currentTime.getTimestamp() - startTime)
-                / (endTime - startTime);
+        final long startTime = getStartTime().getValue();
+        final double diff = curLocation.getLocationInfo().getTimestamp() - startTime;
+        final double total = getEndTime().getValue() - startTime;
+        return Math.max(0.0, Math.min(1.0, diff / total));
     }
 
     /**
@@ -294,18 +315,14 @@ public class CtfTmfTrace extends TmfTrace
         if (currentLocation == null) {
             currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
             context.setRank(0);
-        }
-        if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
-            currentLocation = new CtfLocation(fTrace.getCurrentEndTime() + 1, 0L);
-        }
-        context.setLocation(currentLocation);
-        if (location == null) {
-            long timestamp = fIteratorManager.getIterator(context).getCurrentTimestamp();
-            currentLocation = new CtfLocation(timestamp, 0);
-        }
-        if (context.getRank() != 0) {
+        } else {
             context.setRank(ITmfContext.UNKNOWN_RANK);
+            if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
+                currentLocation = new CtfLocation(fTrace.getCurrentEndTime() + 1, 0L);
+            }
         }
+        /* This will seek and update the location after the seek */
+        context.setLocation(currentLocation);
         return context;
     }
 
@@ -317,8 +334,8 @@ public class CtfTmfTrace extends TmfTrace
             context.setRank(ITmfContext.UNKNOWN_RANK);
             return context;
         }
-        final long end = fTrace.getCurrentEndTime();
-        final long start = fTrace.getCurrentStartTime();
+        final long end = getEndTime().getValue();
+        final long start = getStartTime().getValue();
         final long diff = end - start;
         final long ratioTs = Math.round(diff * ratio) + start;
         context.seek(ratioTs);
This page took 0.026295 seconds and 5 git commands to generate.