Refactor TmfTrace and dependencies - introduce ITmfTraceIndexer
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / trace / ITmfTrace.java
index 9ab76c4216a5299ebbbce8e6895d53c42e0a157c..15666917f8a66a8b5c3c4bf91fb734efdb478631 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2009, 2011 Ericsson
+ * Copyright (c) 2009, 2011, 2012 Ericsson
  * 
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -8,6 +8,7 @@
  * 
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
+ *   Francois Chouinard - Updated as per TMF Trace Model 1.0
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.trace;
@@ -15,128 +16,250 @@ package org.eclipse.linuxtools.tmf.core.trace;
 import java.io.FileNotFoundException;
 
 import org.eclipse.core.resources.IProject;
-import org.eclipse.linuxtools.tmf.core.component.ITmfComponent;
-import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider;
+import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
+import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
-import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
 
 /**
  * <b><u>ITmfTrace</u></b>
  * <p>
+ * The event stream structure in TMF. In its basic form, a trace has:
+ * <ul>
+ * <li> the associated Eclipse resource
+ * <li> the path to its location on the file system
+ * <li> the type of the events it contains
+ * <li> the number of events it contains
+ * <li> the time range (span) of the events it contains
+ * </ul>
+ * Concrete ITmfTrace classes have to provide a parameter-less constructor and
+ * an initialization method (initTace())if they are to be opened from the
+ * Project View. Also, a validation (validate()) method has to be provided to
+ * ensure that the trace is of the correct type.
+ * <p>
+ * A trace can be accessed simultaneously from multiple threads by various
+ * application components. To avoid obvious multi-threading issues, the trace
+ * uses an ITmfContext as a synchronization aid for its read operations.
+ * <p>
+ * A proper ITmfContext can be obtained by performing a seek operation on the
+ * trace. Seek operations can be performed for a particular event (by rank or
+ * timestamp) or for a plain trace location.
+ * <p>
+ * <b>Example 1</b>: Read a whole trace
+ * <pre>
+ * ITmfContext context = trace.seekLocationt(null);
+ * ITmfEvent event = trace.getEvent(context);
+ * while (event != null) {
+ *     // Do something ...
+ *     event = trace.getEvent(context);
+ * }
+ * </pre>
+ * <b>Example 2</b>: Process 50 events starting from the 1000th event
+ * <pre>
+ * int nbEventsRead = 0;
+ * ITmfContext context = trace.seekEvent(1000);
+ * ITmfEvent event = trace.getEvent(context);
+ * while (event != null && nbEventsRead < 50) {
+ *     nbEventsRead++;
+ *     // Do something ...
+ *     event = trace.getEvent(context);
+ * }
+ * </pre>
+ * <b>Example 3</b>: Process the events between 2 timestamps (inclusive)
+ * <pre>
+ * ITmfTimestamp startTime = ...;
+ * ITmfTimestamp endTime = ...;
+ * ITmfContext context = trace.seekEvent(startTime);
+ * ITmfEvent event = trace.getEvent(context);
+ * while (event != null && event.getTimestamp().compareTo(endTime) <= 0) {
+ *     // Do something ...
+ *     event = trace.getEvent(context);
+ * }
+ * </pre>
  */
-public interface ITmfTrace<T extends TmfEvent> extends ITmfComponent {
-
-    // initTrace variants
-    public void initTrace(String path, Class<T> eventType) throws FileNotFoundException;
-
-    public void initTrace(String path, Class<T> eventType, int cacheSize) throws FileNotFoundException;
-
-    public void initTrace(String path, Class<T> eventType, boolean indexTrace) throws FileNotFoundException;
+public interface ITmfTrace<T extends ITmfEvent> extends ITmfDataProvider<T> {
 
-    public void initTrace(String path, Class<T> eventType, int cacheSize, boolean indexTrace) throws FileNotFoundException;
+    // ------------------------------------------------------------------------
+    // Initializers
+    // ------------------------------------------------------------------------
 
-    public void initTrace(String path, Class<T> eventType, int cacheSize, boolean indexTrace, String name) throws FileNotFoundException;
+    /**
+     * Initialize a newly instantiated "empty" trace object. This is used to
+     * properly parameterize an ITmfTrace instantiated with its parameterless
+     * constructor.
+     * 
+     * Typically, the parameterless constructor will provide the block size
+     * and its associated parser and indexer.
+     * 
+     * @param resource the trace resource
+     * @param path the trace path
+     * @param type the trace event type
+     * @throws FileNotFoundException
+     */
+    public void initTrace(IResource resource, String path, Class<T> type) throws FileNotFoundException;
 
-    // Trace type validation
+    /**
+     * Validate that the trace is of the correct type.
+     * 
+     * @param project the eclipse project
+     * @param path the trace path
+     * 
+     * @return true if trace is valid
+     */
     public boolean validate(IProject project, String path);
 
-    public ITmfTrace<T> copy();
+    // ------------------------------------------------------------------------
+    // Basic getters
+    // ------------------------------------------------------------------------
 
     /**
-     * @return the trace path
+     * @return the trace event type
      */
-    public String getPath();
+    public Class<T> getType();
 
     /**
-     * @return the trace name
+     * @return the associated trace resource
      */
-    @Override
-    public String getName();
+    public IResource getResource();
 
     /**
-     * @return the cache size
+     * @return the trace path
+     */
+    public String getPath();
+
+    /**
+     * @return the trace cache size
      */
     public int getCacheSize();
 
+    // ------------------------------------------------------------------------
+    // Trace characteristics getters
+    // ------------------------------------------------------------------------
+
     /**
      * @return the number of events in the trace
      */
     public long getNbEvents();
 
     /**
-     * Trace time range accesses
+     * @return the trace time range
      */
     public TmfTimeRange getTimeRange();
 
-    public TmfTimestamp getStartTime();
+    /**
+     * @return the timestamp of the first trace event
+     */
+    public ITmfTimestamp getStartTime();
+
+    /**
+     * @return the timestamp of the last trace event
+     */
+    public ITmfTimestamp getEndTime();
 
-    public TmfTimestamp getEndTime();
+    // ------------------------------------------------------------------------
+    // Trace positioning getters
+    // ------------------------------------------------------------------------
 
     /**
-     * @return the streaming interval in ms (0 if not streaming)
+     * @return the current trace location
      */
-    public long getStreamingInterval();
+    public ITmfLocation<?> getCurrentLocation();
 
     /**
-     * Positions the trace at the first event with the specified timestamp or index (i.e. the nth event in the trace).
-     * 
-     * Returns a context which can later be used to read the event.
+     * Returns the ratio (proportion) corresponding to the specified location.
      * 
-     * @param location
-     * @param timestamp
-     * @param rank
-     * @return a context object for subsequent reads
+     * @param location a trace specific location
+     * @return a floating-point number between 0.0 (beginning) and 1.0 (end)
      */
-    public TmfContext seekLocation(ITmfLocation<?> location);
+    public double getLocationRatio(ITmfLocation<?> location);
 
-    public TmfContext seekEvent(TmfTimestamp timestamp);
+    // ------------------------------------------------------------------------
+    // Seek operations (returning a reading context)
+    // ------------------------------------------------------------------------
 
-    public TmfContext seekEvent(long rank);
+    /**
+     * Position the trace at the specified location. The null location
+     * is used to indicate that the first trace event is requested.
+     * <p>
+     * <ul>
+     * <li> a <b>null</b> location returns the context of the first event
+     * <li> an invalid location, including beyond the last event, returns a null context
+     * </ul>
+     * <p>
+     * @param location the trace specific location (null for 1st event)
+     * @return a context which can later be used to read the corresponding event
+     */
+    public ITmfContext seekLocation(ITmfLocation<?> location);
 
     /**
-     * Positions the trace at the event located at the specified ratio.
+     * Position the trace at the event located at the specified ratio in the
+     * trace file.
+     * <p>
+     * The notion of ratio (0.0 <= r <= 1.0) is trace specific and left
+     * voluntarily vague. Typically, it would refer to the event proportional
+     * rank (arguably more intuitive) or timestamp in the trace file.
      * 
-     * Returns a context which can later be used to read the event.
+     * @param ratio the proportional 'rank' in the trace
+     * @return a context which can later be used to read the corresponding event
+     */
+    public ITmfContext seekLocation(double ratio);
+
+    /**
+     * Position the trace at the first event with the specified timestamp. If
+     * there is no event with the requested timestamp, a context pointing to
+     * the next chronological event is returned.
+     * <p>
+     * A null timestamp is interpreted as seeking for the first event of the
+     * trace.
+     * <p>
+     * If the requested timestamp is beyond the last trace event, the context
+     * returned will yield a null event if used in a subsequent read.
      * 
-     * @param ratio
-     *            a floating-point number between 0.0 (beginning) and 1.0 (end)
-     * @return a context object for subsequent reads
+     * @param timestamp the timestamp of desired event
+     * @return a context which can later be used to read the corresponding event
      */
-    public TmfContext seekLocation(double ratio);
+    public ITmfContext seekEvent(ITmfTimestamp timestamp);
 
     /**
-     * Returns the ratio corresponding to the specified location.
+     * Position the trace at the 'rank'th event in the trace.
+     * <p>
+     * If the requested rank is beyond the last trace event, the context
+     * returned will yield a null event if used in a subsequent read.
      * 
-     * @param location
-     *            a trace location
-     * @return a floating-point number between 0.0 (beginning) and 1.0 (end)
+     * @param rank the event rank
+     * @return a context which can later be used to read the corresponding event
      */
-    public double getLocationRatio(ITmfLocation<?> location);
+    public ITmfContext seekEvent(long rank);
 
-    public ITmfLocation<?> getCurrentLocation();
+    // ------------------------------------------------------------------------
+    // Read operations (returning an actual event)
+    // ------------------------------------------------------------------------
 
     /**
-     * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
-     * (if any).
+     * Return the event pointed by the supplied context (or null if no event
+     * left) and updates the context to point the next event.
      * 
-     * @param timestamp
-     * @return
+     * @param context the read context (will be updated)
+     * @return the event pointed to by the context
      */
-    public long getRank(TmfTimestamp timestamp);
+    public ITmfEvent getNextEvent(ITmfContext context);
 
     /**
-     * Return the event pointed by the supplied context (or null if no event left) and updates the context to the next
-     * event.
+     * Return the event pointed by the supplied context (or null if no event
+     * left) and *does not* update the context.
      * 
+     * @param context the read context
      * @return the next event in the stream
      */
-    public TmfEvent getNextEvent(TmfContext context);
+    public ITmfEvent parseEvent(ITmfContext context);
+
+    // ------------------------------------------------------------------------
+    // ------------------------------------------------------------------------
 
     /**
-     * Return the event pointed by the supplied context (or null if no event left) and *does not* update the context.
-     * 
-     * @return the next event in the stream
+     * @return the streaming interval in ms (0 if not a streaming trace)
      */
-    public TmfEvent parseEvent(TmfContext context);
+    public long getStreamingInterval();
 
 }
This page took 0.029572 seconds and 5 git commands to generate.