Refactor TmfTrace and dependencies - minor changes
authorFrancois Chouinard <fchouinard@gmail.com>
Tue, 24 Apr 2012 18:07:13 +0000 (14:07 -0400)
committerFrancois Chouinard <fchouinard@gmail.com>
Tue, 24 Apr 2012 18:07:13 +0000 (14:07 -0400)
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/AllTests.java
org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java [new file with mode: 0644]
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/experiment/TmfExperiment.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/ITmfTrace.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java

index 21669eac3df841d288029a1cccf5a260827a4eef..e5d304222d82db2e34be50e08f03d816e9f844d6 100644 (file)
@@ -31,7 +31,7 @@ public class AllTests {
                suite.addTestSuite(TmfLocationTest.class);
         suite.addTestSuite(TmfContextTest.class);
                suite.addTestSuite(TmfCheckpointTest.class);
-//        suite.addTestSuite(TmfCheckpointIndexTest.class);
+        suite.addTestSuite(TmfCheckpointIndexTest.class);
                suite.addTestSuite(TmfTraceTest.class);
                //$JUnit-END$
                return suite;
diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfCheckpointIndexTest.java
new file mode 100644 (file)
index 0000000..9511b7b
--- /dev/null
@@ -0,0 +1,144 @@
+/*******************************************************************************
+ * Copyright (c) 2009, 2010, 20112 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:
+ *   Francois Chouinard - Initial API and implementation
+ *   Francois Chouinard - Adapted for TMF Trace Model 1.0
+ *******************************************************************************/
+
+package org.eclipse.linuxtools.tmf.core.tests.trace;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.Vector;
+
+import junit.framework.TestCase;
+
+import org.eclipse.core.runtime.FileLocator;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
+import org.eclipse.linuxtools.tmf.core.tests.TmfCoreTestPlugin;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint;
+import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpointIndexer;
+import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
+import org.eclipse.linuxtools.tmf.tests.stubs.trace.TmfTraceStub;
+
+/**
+ * <b><u>TmfTraceTest</u></b>
+ * <p>
+ * Test suite for the TmfTrace class.
+ */
+@SuppressWarnings("nls")
+public class TmfCheckpointIndexTest extends TestCase {
+
+    // ------------------------------------------------------------------------
+    // Variables
+    // ------------------------------------------------------------------------
+
+    private static final String DIRECTORY   = "testfiles";
+    private static final String TEST_STREAM = "A-Test-10K";
+    private static final int    BLOCK_SIZE  = 500;
+    private static final int    NB_EVENTS   = 10000;
+    private static TestTrace    fTrace      = null;
+
+    // ------------------------------------------------------------------------
+    // Housekeeping
+    // ------------------------------------------------------------------------
+
+    public TmfCheckpointIndexTest(final String name) throws Exception {
+        super(name);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        fTrace = setupTrace(DIRECTORY + File.separator + TEST_STREAM);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        fTrace.dispose();
+        fTrace = null;
+    }
+
+    // ------------------------------------------------------------------------
+    // Helper classes
+    // ------------------------------------------------------------------------
+
+    private class TestIndexer extends TmfCheckpointIndexer<ITmfTrace<ITmfEvent>> {
+        @SuppressWarnings({ "unchecked", "rawtypes" })
+        public TestIndexer(TestTrace testTrace) {
+            super((ITmfTrace) testTrace);
+        }
+        public Vector<TmfCheckpoint> getCheckpoints() {
+            return fTraceIndex;
+        }
+    }
+
+    private class TestTrace extends TmfTraceStub {
+        public TestTrace(String path, int blockSize) throws FileNotFoundException {
+            super(path, blockSize);
+            fIndexer = new TestIndexer(this);
+        }
+        public TestIndexer getIndexer() {
+            return (TestIndexer) fIndexer;
+        }
+    }
+
+    // ------------------------------------------------------------------------
+    // Helper functions
+    // ------------------------------------------------------------------------
+
+    private TestTrace setupTrace(final String path) {
+        if (fTrace == null) {
+            try {
+                final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(path), null);
+                final File test = new File(FileLocator.toFileURL(location).toURI());
+                fTrace = new TestTrace(test.toURI().getPath(), BLOCK_SIZE);
+                fTrace.indexTrace();
+            } catch (final URISyntaxException e) {
+                e.printStackTrace();
+            } catch (final IOException e) {
+                e.printStackTrace();
+            }
+        }
+        return fTrace;
+    }
+
+    // ------------------------------------------------------------------------
+    // Verify checkpoints
+    // ------------------------------------------------------------------------
+
+    public void testTmfTraceIndexing() throws Exception {
+        assertEquals("getCacheSize",   BLOCK_SIZE, fTrace.getCacheSize());
+        assertEquals("getTraceSize",   NB_EVENTS,  fTrace.getNbEvents());
+        assertEquals("getRange-start", 1,          fTrace.getTimeRange().getStartTime().getValue());
+        assertEquals("getRange-end",   NB_EVENTS,  fTrace.getTimeRange().getEndTime().getValue());
+        assertEquals("getStartTime",   1,          fTrace.getStartTime().getValue());
+        assertEquals("getEndTime",     NB_EVENTS,  fTrace.getEndTime().getValue());
+
+        Vector<TmfCheckpoint> checkpoints = fTrace.getIndexer().getCheckpoints();
+        int pageSize = fTrace.getCacheSize();
+        assertTrue("Checkpoints exist",  checkpoints != null);
+
+        // Validate that each checkpoint points to the right event
+        for (int i = 0; i < checkpoints.size(); i++) {
+            TmfCheckpoint checkpoint = checkpoints.get(i);
+            TmfContext context = new TmfContext(checkpoint.getLocation(), i * pageSize);
+            ITmfEvent event = fTrace.parseEvent(context);
+            assertTrue(context.getRank() == i * pageSize);
+            assertTrue((checkpoint.getTimestamp().compareTo(event.getTimestamp(), false) == 0));
+        }
+    }
+
+}
\ No newline at end of file
index 1a57e6e5fb879c7f1fd44581673d21c1ebfa71bd..faff4ad4a3927f351cb57ffa3d844bece4b0f338 100644 (file)
@@ -121,7 +121,7 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
      * @return the trace path
      */
     @Override
-    public Class<CtfTmfEvent> getType() {
+    public Class<CtfTmfEvent> getEventType() {
         return fType;
     }
 
index 4b4cb226392732876501579b0d555daf603600ae..5c179244c8cdd9209a9ee1aba86ad8434c83745a 100644 (file)
@@ -183,7 +183,7 @@ public class TmfExperiment<T extends ITmfEvent> extends TmfEventProvider<T> impl
     // ------------------------------------------------------------------------
 
     @Override
-    public Class<T> getType() {
+    public Class<T> getEventType() {
         return fType;
     }
 
index af9e741576271b1a5f1d3e7dcc21ef44adf45ac2..190306d7f92e3c7bda845c3988dfeb2800314012 100644 (file)
@@ -144,7 +144,7 @@ public interface ITmfTrace<T extends ITmfEvent> extends ITmfDataProvider<T> {
     /**
      * @return the trace event type
      */
-    public Class<T> getType();
+    public Class<T> getEventType();
 
     /**
      * @return the associated trace resource
@@ -185,6 +185,11 @@ public interface ITmfTrace<T extends ITmfEvent> extends ITmfDataProvider<T> {
      */
     public ITmfTimestamp getEndTime();
 
+    /**
+     * @return the streaming interval in ms (0 if not a streaming trace)
+     */
+    public long getStreamingInterval();
+
     // ------------------------------------------------------------------------
     // Trace positioning getters
     // ------------------------------------------------------------------------
@@ -276,13 +281,4 @@ public interface ITmfTrace<T extends ITmfEvent> extends ITmfDataProvider<T> {
      */
     public ITmfEvent readEvent(ITmfContext context);
 
-    // ------------------------------------------------------------------------
-    //
-    // ------------------------------------------------------------------------
-
-    /**
-     * @return the streaming interval in ms (0 if not a streaming trace)
-     */
-    public long getStreamingInterval();
-
 }
index 75891a7a0a71d00415832d2351295338a249c727..5ea35f60a701bfb27f75a06010849dcc24520365 100644 (file)
@@ -32,7 +32,21 @@ import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
  * <p>
  * Abstract implementation of ITmfTrace.
  * <p>
- * Document me...
+ * Since the concept of 'location' is trace specific, the concrete classes have
+ * to provide the related methods, namely:
+ * <ul>
+ * <li> public ITmfLocation<?> getCurrentLocation()
+ * <li> public double getLocationRatio(ITmfLocation<?> location)
+ * <li> public ITmfContext seekEvent(ITmfLocation<?> location)
+ * <li> public ITmfContext seekEvent(double ratio)
+ * </ul>
+ * A concrete trace must provide its corresponding parser. A common way to
+ * accomplish this is by making the concrete class extend TmfTrace and
+ * implement ITmfEventParser.
+ * <p>
+ * The concrete class can either specify its own indexer or use the provided
+ * TmfCheckpointIndexer (default). In this case, the trace cache size will be
+ * used as checkpoint interval.
  */
 public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T> {
 
@@ -98,7 +112,8 @@ public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T>
     }
 
     /**
-     * The standard constructor (non-live trace)
+     * The standard constructor (non-live trace). Applicable when the trace
+     * implements its own parser and if at checkpoint-based index is OK. 
      * 
      * @param resource the resource associated to the trace
      * @param type the trace event type
@@ -111,7 +126,8 @@ public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T>
     }
 
     /**
-     * The standard constructor (live trace)
+     * The standard constructor (live trace). Applicable when the trace
+     * implements its own parser and if at checkpoint-based index is OK.
      * 
      * @param resource the resource associated to the trace
      * @param type the trace event type
@@ -125,7 +141,8 @@ public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T>
     }
 
     /**
-     * The full constructor
+     * The 'non-default indexer' constructor. Allows to provide a trace
+     * specific indexer.
      * 
      * @param resource the resource associated to the trace
      * @param type the trace event type
@@ -134,13 +151,30 @@ public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T>
      * @param indexer the trace indexer
      * @throws FileNotFoundException
      */
-    @SuppressWarnings({ "unchecked", "rawtypes" })
     protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize,
             final long interval, final ITmfTraceIndexer<?> indexer) throws FileNotFoundException {
+        this(resource, type, path, cacheSize, interval, null, null);
+    }
+
+    /**
+     * The full constructor where trace specific indexer/parser are provided. 
+     * 
+     * @param resource the resource associated to the trace
+     * @param type the trace event type
+     * @param path the trace path
+     * @param cacheSize the trace cache size
+     * @param indexer the trace indexer
+     * @param parser the trace event parser
+     * @throws FileNotFoundException
+     */
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize,
+            final long interval, final ITmfTraceIndexer<?> indexer, final ITmfEventParser<ITmfEvent> parser) throws FileNotFoundException {
         super();
         fCacheSize = (cacheSize > 0) ? cacheSize : DEFAULT_TRACE_CACHE_SIZE;
         fStreamingInterval = interval;
         fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize);
+        fParser = parser;
         initialize(resource, path, type);
     }
 
@@ -157,7 +191,8 @@ public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T>
         fCacheSize = trace.getCacheSize();
         fStreamingInterval = trace.getStreamingInterval();
         fIndexer = new TmfCheckpointIndexer(this);
-        initialize(trace.getResource(), trace.getPath(), trace.getType());
+        fParser = trace.fParser;
+        initialize(trace.getResource(), trace.getPath(), trace.getEventType());
     }
 
     // ------------------------------------------------------------------------
@@ -212,11 +247,11 @@ public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T>
     // ------------------------------------------------------------------------
 
     /* (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getType()
+     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
      */
     @Override
     @SuppressWarnings("unchecked")
-    public Class<T> getType() {
+    public Class<T> getEventType() {
         return (Class<T>) super.getType();
     }
 
This page took 0.031631 seconds and 5 git commands to generate.