ctf/tmf: allow multiple traces to be open with name clashing events
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
index 700b0a9d7d228c807e0bbe948349375249192527..66d53cd203dcdf925bb415de9a315cd0db73b5f9 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2013 Ericsson
+ * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are made
  * available under the terms of the Eclipse Public License v1.0 which
@@ -9,17 +9,20 @@
  * Contributors:
  *   Matthew Khouzam - Initial API and implementation
  *   Patrick Tasse - Updated for removal of context clone
+ *   Geneviève Bastien - Added the createTimestamp function
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
 
-import java.util.Collections;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
 import java.util.Map;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.linuxtools.ctf.core.event.CTFClock;
 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
@@ -31,9 +34,15 @@ 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;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceProperties;
 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TraceValidationStatus;
+import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
+import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
+import org.eclipse.linuxtools.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
+import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
+import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
+import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
 
 /**
  * The CTf trace handler
@@ -42,7 +51,7 @@ import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
  * @author Matthew khouzam
  */
 public class CtfTmfTrace extends TmfTrace
-        implements ITmfEventParser, ITmfTraceProperties {
+        implements ITmfEventParser, ITmfTraceProperties, ITmfPersistentlyIndexable {
 
     // -------------------------------------------
     // Constants
@@ -52,6 +61,12 @@ public class CtfTmfTrace extends TmfTrace
      */
     protected static final int DEFAULT_CACHE_SIZE = 50000;
 
+    /*
+     * The Ctf clock unique identifier field
+     */
+    private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
+    private static final int CONFIDENCE = 10;
+
     // -------------------------------------------
     // Fields
     // -------------------------------------------
@@ -72,7 +87,7 @@ public class CtfTmfTrace extends TmfTrace
      * @param eventType
      *            The type of events that will be read from this trace
      * @throws TmfTraceException
-     *             If something when wrong while reading the trace
+     *             If something went wrong while reading the trace
      */
     @Override
     public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
@@ -85,9 +100,6 @@ public class CtfTmfTrace extends TmfTrace
 
         super.initTrace(resource, path, eventType);
 
-        @SuppressWarnings("unused")
-        CtfTmfEventType type;
-
         try {
             this.fTrace = new CTFTrace(path);
             CtfIteratorManager.addTrace(this);
@@ -125,22 +137,17 @@ public class CtfTmfTrace extends TmfTrace
     }
 
     /**
-     * Method validate.
-     *
-     * @param project
-     *            IProject
-     * @param path
-     *            String
-     * @return IStatus IStatus.error or Status.OK_STATUS
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
-     * @since 2.0
+     * {@inheritDoc}
+     * <p>
+     * The default implementation sets the confidence to 10 if the trace is a
+     * valid CTF trace.
      */
     @Override
     public IStatus validate(final IProject project, final String path) {
-        IStatus validTrace = Status.OK_STATUS;
+        IStatus validTrace = new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
         try {
             final CTFTrace temp = new CTFTrace(path);
-            if (!temp.majortIsSet()) {
+            if (!temp.majorIsSet()) {
                 validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
             } else {
                 CTFTraceReader ctfTraceReader = new CTFTraceReader(temp);
@@ -154,7 +161,10 @@ public class CtfTmfTrace extends TmfTrace
             temp.dispose();
         } catch (final CTFReaderException e) {
             validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError +": " + e.toString()); //$NON-NLS-1$
+        } catch (final BufferOverflowException e){
+            validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError +": " + Messages.CtfTmfTrace_BufferOverflowErrorMessage); //$NON-NLS-1$
         }
+
         return validTrace;
     }
 
@@ -163,12 +173,16 @@ public class CtfTmfTrace extends TmfTrace
      *
      * @return null, since the trace has no knowledge of the current location
      * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
+     * @since 3.0
      */
     @Override
     public ITmfLocation getCurrentLocation() {
         return null;
     }
 
+    /**
+     * @since 3.0
+     */
     @Override
     public double getLocationRatio(ITmfLocation location) {
         final CtfLocation curLocation = (CtfLocation) location;
@@ -188,6 +202,7 @@ public class CtfTmfTrace extends TmfTrace
      * @param location
      *            ITmfLocation<?>
      * @return ITmfContext
+     * @since 3.0
      */
     @Override
     public synchronized ITmfContext seekEvent(final ITmfLocation location) {
@@ -280,6 +295,25 @@ public class CtfTmfTrace extends TmfTrace
         return fTrace;
     }
 
+    /**
+     * Ctf traces have a clock with a unique uuid that will be used to identify
+     * the host. Traces with the same clock uuid will be known to have been made
+     * on the same machine.
+     *
+     * Note: uuid is an optional field, it may not be there for a clock.
+     */
+    @Override
+    public String getHostId() {
+        CTFClock clock = getCTFTrace().getClock();
+        if (clock != null) {
+            String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY);
+            if (clockHost != null) {
+                return clockHost;
+            }
+        }
+        return super.getHostId();
+    }
+
     // -------------------------------------------
     // ITmfTraceProperties
     // -------------------------------------------
@@ -289,7 +323,7 @@ public class CtfTmfTrace extends TmfTrace
      */
     @Override
     public Map<String, String> getTraceProperties() {
-        return Collections.unmodifiableMap(fTrace.getEnvironment());
+        return fTrace.getEnvironment();
     }
 
     // -------------------------------------------
@@ -401,6 +435,54 @@ public class CtfTmfTrace extends TmfTrace
      * @since 2.0
      */
     public CtfIterator createIterator() {
-        return new CtfIterator(this);
+        try {
+            return new CtfIterator(this);
+        } catch (CTFReaderException e) {
+            Activator.logError(e.getMessage(), e);
+        }
+        return null;
+    }
+
+    // ------------------------------------------------------------------------
+    // Timestamp transformation functions
+    // ------------------------------------------------------------------------
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public CtfTmfTimestamp createTimestamp(long ts) {
+        return new CtfTmfTimestamp(getTimestampTransform().transform(ts));
+    }
+
+    private static int fCheckpointSize = -1;
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public synchronized int getCheckpointSize() {
+        if (fCheckpointSize == -1) {
+            TmfCheckpoint c = new TmfCheckpoint(new CtfTmfTimestamp(0), new CtfLocation(0, 0), 0);
+            ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
+            b.clear();
+            c.serialize(b);
+            fCheckpointSize = b.position();
+        }
+
+        return fCheckpointSize;
+    }
+
+    @Override
+    protected ITmfTraceIndexer createIndexer(int interval) {
+        return new TmfBTreeTraceIndexer(this, interval);
+    }
+
+    /**
+     * @since 3.0
+     */
+    @Override
+    public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
+        return new CtfLocation(bufferIn);
     }
 }
This page took 0.025782 seconds and 5 git commands to generate.