From 1703b53604f4b716022796d027b5658d0a696983 Mon Sep 17 00:00:00 2001 From: Francois Chouinard Date: Fri, 20 Apr 2012 13:10:57 -0400 Subject: [PATCH] Refactor TmfTrace and dependencies - finalize ITmfTraceIndexer --- .../core/tests/event/TmfTimestampTest.java | 31 +-- .../tmf/core/tests/trace/TmfTraceTest.java | 172 ++++++++++++++--- .../tmf/tests/stubs/trace/TmfTraceStub.java | 23 ++- .../tmf/core/event/TmfTimestamp.java | 3 + .../linuxtools/tmf/core/trace/TmfTrace.java | 177 +++--------------- .../tmf/core/trace/TmfTraceIndexer.java | 117 ++++++------ 6 files changed, 276 insertions(+), 247 deletions(-) diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfTimestampTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfTimestampTest.java index 0d56dc77ff..e60b94c3b5 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfTimestampTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/event/TmfTimestampTest.java @@ -441,7 +441,7 @@ public class TmfTimestampTest extends TestCase { assertTrue("compareTo", ts0.compareTo(ts0, false) == 0); assertTrue("compareTo", ts0.compareTo(ts0a, false) == 0); assertTrue("compareTo", ts0.compareTo(ts0b, false) == 0); - assertTrue("compareTo", ts0.compareTo(ts0c, false)== -1); + assertTrue("compareTo", ts0.compareTo(ts0c, false) == -1); assertTrue("compareTo", ts0.compareTo(ts0d, false) == -1); } @@ -450,14 +450,14 @@ public class TmfTimestampTest extends TestCase { final ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE); final ITmfTimestamp ts0c = new TmfTimestamp(Long.MAX_VALUE, Integer.MAX_VALUE); - assertTrue("compareTo", ts0a.compareTo(ts0b, false)== 1); - assertTrue("compareTo", ts0a.compareTo(ts0c, false)== -1); + assertTrue("compareTo", ts0a.compareTo(ts0b, false) == 1); + assertTrue("compareTo", ts0a.compareTo(ts0c, false) == -1); - assertTrue("compareTo", ts0b.compareTo(ts0a, false)== -1); - assertTrue("compareTo", ts0b.compareTo(ts0c, false)== -1); + assertTrue("compareTo", ts0b.compareTo(ts0a, false) == -1); + assertTrue("compareTo", ts0b.compareTo(ts0c, false) == -1); - assertTrue("compareTo", ts0c.compareTo(ts0a, false)== 1); - assertTrue("compareTo", ts0c.compareTo(ts0b, false)== 1); + assertTrue("compareTo", ts0c.compareTo(ts0a, false) == 1); + assertTrue("compareTo", ts0c.compareTo(ts0b, false) == 1); } public void testCompareToCornerCases3() throws Exception { @@ -465,14 +465,19 @@ public class TmfTimestampTest extends TestCase { final ITmfTimestamp ts0b = new TmfTimestamp(0, Integer.MAX_VALUE); final ITmfTimestamp ts0c = new TmfTimestamp(Long.MIN_VALUE, Integer.MAX_VALUE); - assertTrue("compareTo", ts0a.compareTo(ts0b, false)== -1); - assertTrue("compareTo", ts0a.compareTo(ts0c, false)== 1); + assertTrue("compareTo", ts0a.compareTo(ts0b, false) == -1); + assertTrue("compareTo", ts0a.compareTo(ts0c, false) == 1); - assertTrue("compareTo", ts0b.compareTo(ts0a, false)== 1); - assertTrue("compareTo", ts0b.compareTo(ts0c, false)== 1); + assertTrue("compareTo", ts0b.compareTo(ts0a, false) == 1); + assertTrue("compareTo", ts0b.compareTo(ts0c, false) == 1); - assertTrue("compareTo", ts0c.compareTo(ts0a, false)== -1); - assertTrue("compareTo", ts0c.compareTo(ts0b, false)== -1); + assertTrue("compareTo", ts0c.compareTo(ts0a, false) == -1); + assertTrue("compareTo", ts0c.compareTo(ts0b, false) == -1); + } + + public void testCompareToCornerCases4() throws Exception { + assertTrue("compareTo", ts0.compareTo(null, false) == 1); + assertTrue("compareTo", ts0.compareTo(null, true) == 1); } public void testCompareToSameScale() throws Exception { diff --git a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfTraceTest.java b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfTraceTest.java index d79f02d214..a2a58ec2cd 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfTraceTest.java +++ b/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/trace/TmfTraceTest.java @@ -14,6 +14,7 @@ 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; @@ -214,6 +215,36 @@ public class TmfTraceTest extends TestCase { assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue()); } + public void testLiveTraceConstructor() throws Exception { + TmfTraceStub trace = null; + File testfile = null; + final long interval = 100; + try { + final URL location = FileLocator.find(TmfCoreTestPlugin.getDefault().getBundle(), new Path(DIRECTORY + File.separator + TEST_STREAM), null); + testfile = new File(FileLocator.toFileURL(location).toURI()); + trace = new TmfTraceStub(testfile.toURI().getPath(), BLOCK_SIZE, interval); + trace.indexTrace(); + } catch (final URISyntaxException e) { + fail("URISyntaxException"); + } catch (final IOException e) { + fail("IOException"); + } + + assertFalse ("Open trace", trace == null); + assertEquals("getType", TmfEvent.class, trace.getType()); + assertNull ("getResource", trace.getResource()); + assertEquals("getPath", testfile.toURI().getPath(), trace.getPath()); + assertEquals("getCacheSize", BLOCK_SIZE, trace.getCacheSize()); + assertEquals("getStreamingInterval", interval, trace.getStreamingInterval()); + assertEquals("getName", TEST_STREAM, trace.getName()); + + assertEquals("getNbEvents", NB_EVENTS, trace.getNbEvents()); + assertEquals("getRange-start", 1, trace.getTimeRange().getStartTime().getValue()); + assertEquals("getRange-end", NB_EVENTS, trace.getTimeRange().getEndTime().getValue()); + assertEquals("getStartTime", 1, trace.getStartTime().getValue()); + assertEquals("getEndTime", NB_EVENTS, trace.getEndTime().getValue()); + } + @SuppressWarnings({ "rawtypes", "unchecked" }) public void testCopyConstructor() throws Exception { TmfTraceStub original = null; @@ -257,33 +288,80 @@ public class TmfTraceTest extends TestCase { } // ------------------------------------------------------------------------ - // toString + // Trace initialization // ------------------------------------------------------------------------ - public void testDefaultTmfTraceStub() throws Exception { - assertFalse ("Open trace", fTrace == null); - assertEquals("getType", TmfEvent.class, fTrace.getType()); - assertNull ("getResource", fTrace.getResource()); - assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize()); - assertEquals("getStreamingInterval", 0, fTrace.getStreamingInterval()); - assertEquals("getName", TEST_STREAM, fTrace.getName()); + public void testInitializeNullPath() throws Exception { - assertEquals("getNbEvents", 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()); + // Instantiate an "empty" trace + final TmfTraceStub trace = new TmfTraceStub(); - String expected = "TmfTrace [fPath=" + fTrace.getPath() + ", fCacheSize=" + fTrace.getCacheSize() + - ", fNbEvents=" + fTrace.getNbEvents() + ", fStartTime=" + fTrace.getStartTime() + - ", fEndTime=" + fTrace.getEndTime() + ", fStreamingInterval=" + fTrace.getStreamingInterval() + - "]"; - assertEquals("toString", expected, fTrace.toString()); + try { + trace.initialize(null, null, TmfEvent.class); + fail("TmfTrace.initialize() - no exception thrown"); + } catch (FileNotFoundException e) { + // Success + } catch (Exception e) { + fail("TmfTrace.initialize() - wrong exception thrown"); + } } + + public void testInitializeSimplePath() throws Exception { - // ------------------------------------------------------------------------ - // Trace initialization - // ------------------------------------------------------------------------ + // Instantiate an "empty" trace + final TmfTraceStub trace = new TmfTraceStub(); + + // Path == trace name + String path = "TraceName"; + try { + trace.initialize(null, path, TmfEvent.class); + } catch (Exception e) { + fail("TmfTrace.initialize() - Exception thrown"); + } + + assertFalse ("Open trace", trace == null); + assertEquals("getType", TmfEvent.class, trace.getType()); + assertNull ("getResource", trace.getResource()); + assertEquals("getPath", path, trace.getPath()); + assertEquals("getCacheSize", TmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); + assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); + assertEquals("getName", path, trace.getName()); + + assertEquals("getNbEvents", 0, trace.getNbEvents()); + assertEquals("getRange-start", Long.MAX_VALUE, trace.getTimeRange().getStartTime().getValue()); + assertEquals("getRange-end", Long.MIN_VALUE, trace.getTimeRange().getEndTime().getValue()); + assertEquals("getStartTime", Long.MAX_VALUE, trace.getStartTime().getValue()); + assertEquals("getEndTime", Long.MIN_VALUE, trace.getEndTime().getValue()); + } + + public void testInitializeNormalPath() throws Exception { + + // Instantiate an "empty" trace + final TmfTraceStub trace = new TmfTraceStub(); + + // Path == trace name + String name = "TraceName"; + String path = "/my/trace/path/" + name; + try { + trace.initialize(null, path, TmfEvent.class); + } catch (Exception e) { + fail("TmfTrace.initialize() - Exception thrown"); + } + + assertFalse ("Open trace", trace == null); + assertEquals("getType", TmfEvent.class, trace.getType()); + assertNull ("getResource", trace.getResource()); + assertEquals("getPath", path, trace.getPath()); + assertEquals("getCacheSize", TmfTrace.DEFAULT_TRACE_CACHE_SIZE, trace.getCacheSize()); + assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); + assertEquals("getName", name, trace.getName()); + + assertEquals("getNbEvents", 0, trace.getNbEvents()); + assertEquals("getRange-start", Long.MAX_VALUE, trace.getTimeRange().getStartTime().getValue()); + assertEquals("getRange-end", Long.MIN_VALUE, trace.getTimeRange().getEndTime().getValue()); + assertEquals("getStartTime", Long.MAX_VALUE, trace.getStartTime().getValue()); + assertEquals("getEndTime", Long.MIN_VALUE, trace.getEndTime().getValue()); + } public void testInitTrace() throws Exception { @@ -334,7 +412,32 @@ public class TmfTraceTest extends TestCase { } // ------------------------------------------------------------------------ - // Get/Set time range + // Set/Get streaming interval + // ------------------------------------------------------------------------ + + public void testSetStreamingInterval() throws Exception { + final TmfTraceStub trace = new TmfTraceStub(fTrace); + + long interval = 0; + assertEquals("getStreamingInterval", interval, trace.getStreamingInterval()); + + interval = 100; + trace.setStreamingInterval(interval); + assertEquals("getStreamingInterval", interval, trace.getStreamingInterval()); + + interval = -1; + trace.setStreamingInterval(interval); + assertEquals("getStreamingInterval", 0, trace.getStreamingInterval()); + + interval = 0; + trace.setStreamingInterval(interval); + assertEquals("getStreamingInterval", interval, trace.getStreamingInterval()); + + trace.dispose(); + } + + // ------------------------------------------------------------------------ + // Set/Get time range // ------------------------------------------------------------------------ public void testSetTimeRange() throws Exception { @@ -827,6 +930,31 @@ public class TmfTraceTest extends TestCase { assertTrue("isCancelled", request.isCancelled()); } + // ------------------------------------------------------------------------ + // toString + // ------------------------------------------------------------------------ + + public void testDefaultTmfTraceStub() throws Exception { + assertFalse ("Open trace", fTrace == null); + assertEquals("getType", TmfEvent.class, fTrace.getType()); + assertNull ("getResource", fTrace.getResource()); + assertEquals("getCacheSize", BLOCK_SIZE, fTrace.getCacheSize()); + assertEquals("getStreamingInterval", 0, fTrace.getStreamingInterval()); + assertEquals("getName", TEST_STREAM, fTrace.getName()); + + assertEquals("getNbEvents", 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()); + + String expected = "TmfTrace [fPath=" + fTrace.getPath() + ", fCacheSize=" + fTrace.getCacheSize() + + ", fNbEvents=" + fTrace.getNbEvents() + ", fStartTime=" + fTrace.getStartTime() + + ", fEndTime=" + fTrace.getEndTime() + ", fStreamingInterval=" + fTrace.getStreamingInterval() + + "]"; + assertEquals("toString", expected, fTrace.toString()); + } + // // ------------------------------------------------------------------------ // // seekLocation diff --git a/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java b/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java index b2bced5b67..c9d032d4e6 100644 --- a/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java +++ b/org.eclipse.linuxtools.tmf.core.tests/stubs/org/eclipse/linuxtools/tmf/tests/stubs/trace/TmfTraceStub.java @@ -81,6 +81,17 @@ public class TmfTraceStub extends TmfTrace { this(path, cacheSize, false); } + /** + * @param path + * @param cacheSize + * @throws FileNotFoundException + */ + public TmfTraceStub(final String path, final int cacheSize, final long interval) throws FileNotFoundException { + super(null, TmfEvent.class, path, cacheSize, interval); + fTrace = new RandomAccessFile(path, "r"); + fParser = new TmfEventParserStub(); + } + /** * @param path * @param cacheSize @@ -106,7 +117,6 @@ public class TmfTraceStub extends TmfTrace { * @throws FileNotFoundException */ public TmfTraceStub(final String path, final int cacheSize, final boolean waitForCompletion) throws FileNotFoundException { - // this(path, cacheSize, waitForCompletion, null); super(null, TmfEvent.class, path, cacheSize); fTrace = new RandomAccessFile(path, "r"); fParser = new TmfEventParserStub(); @@ -119,7 +129,6 @@ public class TmfTraceStub extends TmfTrace { * @throws FileNotFoundException */ public TmfTraceStub(final IResource resource, final String path, final int cacheSize, final boolean waitForCompletion) throws FileNotFoundException { - // this(path, cacheSize, waitForCompletion, null); super(resource, TmfEvent.class, path, cacheSize); fTrace = new RandomAccessFile(path, "r"); fParser = new TmfEventParserStub(); @@ -159,6 +168,11 @@ public class TmfTraceStub extends TmfTrace { super.initTrace(resource, path, type); } + @Override + public void initialize(final IResource resource, final String path, final Class type) throws FileNotFoundException { + super.initialize(resource, path, type); + } + // ------------------------------------------------------------------------ // Accessors // ------------------------------------------------------------------------ @@ -289,6 +303,11 @@ public class TmfTraceStub extends TmfTrace { super.setEndTime(endTime); } + @Override + public void setStreamingInterval(final long interval) { + super.setStreamingInterval(interval); + } + @Override public void dispose() { fLock.lock(); diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfTimestamp.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfTimestamp.java index 436fcd668c..b5aee95c7e 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfTimestamp.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/event/TmfTimestamp.java @@ -212,6 +212,9 @@ public class TmfTimestamp implements ITmfTimestamp { @Override public int compareTo(final ITmfTimestamp ts, final boolean withinPrecision) { + if (ts == null) + return 1; + // Check the corner cases (we can't use equals() because it uses compareTo()...) if (this == ts || (fValue == ts.getValue() && fScale == ts.getScale())) return 0; diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java index d50c2b1a46..d7b8e62eaa 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTrace.java @@ -15,7 +15,6 @@ package org.eclipse.linuxtools.tmf.core.trace; import java.io.File; import java.io.FileNotFoundException; -import java.util.Collections; import java.util.Vector; import org.eclipse.core.resources.IProject; @@ -96,7 +95,7 @@ public abstract class TmfTrace extends TmfEventProvider } /** - * The standard constructor (non-streaming trace) + * The standard constructor (non-live trace) * * @param resource the resource associated to the trace * @param type the trace event type @@ -109,7 +108,7 @@ public abstract class TmfTrace extends TmfEventProvider } /** - * The standard constructor (streaming trace) + * The standard constructor (live trace) * * @param resource the resource associated to the trace * @param type the trace event type @@ -159,24 +158,25 @@ public abstract class TmfTrace extends TmfEventProvider } /** - * @param resource - * @param path - * @param type + * Initialize the trace common attributes and the base component. + * + * @param resource the Eclipse resource (trace) + * @param path the trace path + * @param type the trace event type + * * @throws FileNotFoundException */ protected void initialize(final IResource resource, final String path, final Class type) throws FileNotFoundException { - fResource = resource; + if (path == null) + throw new FileNotFoundException(); fPath = path; + fResource = resource; String traceName = (resource != null) ? resource.getName() : null; - // If no display name was provided, extract it from the trace path - if (traceName == null) - if (path != null) { - final int sep = path.lastIndexOf(Path.SEPARATOR); - traceName = (sep >= 0) ? path.substring(sep + 1) : path; - } - else { - traceName = ""; //$NON-NLS-1$ - } + // If no resource was provided, extract the display name the trace path + if (traceName == null) { + final int sep = path.lastIndexOf(Path.SEPARATOR); + traceName = (sep >= 0) ? path.substring(sep + 1) : path; + } super.init(traceName, type); } @@ -323,7 +323,7 @@ public abstract class TmfTrace extends TmfEventProvider * @param interval the new trace streaming interval */ protected void setStreamingInterval(final long interval) { - fStreamingInterval = interval; + fStreamingInterval = (interval > 0) ? interval : 0; } // ------------------------------------------------------------------------ @@ -334,23 +334,14 @@ public abstract class TmfTrace extends TmfEventProvider * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp) */ @Override - public ITmfContext seekEvent(final ITmfTimestamp timestamp) { + public synchronized ITmfContext seekEvent(final ITmfTimestamp timestamp) { // A null timestamp indicates to seek the first event if (timestamp == null) return seekLocation(null); - // Find the checkpoint at or before the requested timestamp. - // In the very likely event that the timestamp is not at a checkpoint - // boundary, bsearch will return index = (- (insertion point + 1)). - // It is then trivial to compute the index of the previous checkpoint. - int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null)); - if (index < 0) { - index = Math.max(0, -(index + 2)); - } - // Position the trace at the checkpoint - final ITmfContext context = seekCheckpoint(index); + final ITmfContext context = fIndexer.seekIndex(timestamp); // And locate the requested event context final ITmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype... @@ -367,15 +358,14 @@ public abstract class TmfTrace extends TmfEventProvider * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long) */ @Override - public ITmfContext seekEvent(final long rank) { + public synchronized ITmfContext seekEvent(final long rank) { // A rank <= 0 indicates to seek the first event if (rank <= 0) return seekLocation(null); - // Find the checkpoint at or before the requested rank. - final int index = (int) rank / fCacheSize; - final ITmfContext context = seekCheckpoint(index); + // Position the trace at the checkpoint + final ITmfContext context = fIndexer.seekIndex(rank); // And locate the requested event context long pos = context.getRank(); @@ -388,30 +378,6 @@ public abstract class TmfTrace extends TmfEventProvider return context; } - - /** - * Position the trace at the given checkpoint - * - * @param index the checkpoint index - * @return the corresponding context - */ - private ITmfContext seekCheckpoint(int index) { - ITmfLocation location; - synchronized (fCheckpoints) { - if (!fCheckpoints.isEmpty()) { - if (index >= fCheckpoints.size()) { - index = fCheckpoints.size() - 1; - } - location = fCheckpoints.elementAt(index).getLocation(); - } else { - location = null; - } - } - final ITmfContext context = seekLocation(location); - context.setRank(index * fCacheSize); - return context; - } - // ------------------------------------------------------------------------ // ITmfTrace - Read operations (returning an actual event) // ------------------------------------------------------------------------ @@ -467,105 +433,6 @@ public abstract class TmfTrace extends TmfEventProvider } } -// // ------------------------------------------------------------------------ -// // ITmfTrace - indexing -// // ------------------------------------------------------------------------ -// -// /* -// * The index is a list of contexts that point to events at regular interval -// * (rank-wise) in the trace. After it is built, the index can be used to -// * quickly access any event by rank or timestamp. -// * -// * fIndexPageSize holds the event interval (default INDEX_PAGE_SIZE). -// */ -// -// @SuppressWarnings({ "unchecked" }) -// protected void indexTrace(final boolean waitForCompletion) { -// -// // The monitoring job -// final Job job = new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$ -// -// @Override -// protected IStatus run(final IProgressMonitor monitor) { -// while (!monitor.isCanceled()) { -// try { -// Thread.sleep(100); -// } catch (final InterruptedException e) { -// return Status.OK_STATUS; -// } -// } -// monitor.done(); -// return Status.OK_STATUS; -// } -// }; -// job.schedule(); -// -// // Clear the checkpoints -// fCheckpoints.clear(); -// -// // Build a background request for all the trace data. The index is -// // updated as we go by getNextEvent(). -// final ITmfEventRequest request = new TmfEventRequest(ITmfEvent.class, -// TmfTimeRange.ETERNITY, -// TmfDataRequest.ALL_DATA, fCacheSize, ITmfDataRequest.ExecutionType.BACKGROUND) -// { -// -// ITmfTimestamp startTime = null; -// ITmfTimestamp lastTime = null; -// -// @Override -// public void handleData(final ITmfEvent event) { -// super.handleData(event); -// if (event != null) { -// final ITmfTimestamp timestamp = event.getTimestamp(); -// if (startTime == null) { -// startTime = timestamp.clone(); -// } -// lastTime = timestamp.clone(); -// -// // Update the trace status at regular intervals -// if ((getNbRead() % fCacheSize) == 0) { -// updateTraceStatus(); -// } -// } -// } -// -// @Override -// public void handleSuccess() { -// updateTraceStatus(); -// } -// -// @Override -// public void handleCompleted() { -// job.cancel(); -// super.handleCompleted(); -// } -// -// private synchronized void updateTraceStatus() { -// final int nbRead = getNbRead(); -// if (nbRead != 0) { -// fStartTime = startTime; -// fEndTime = lastTime; -// fNbEvents = nbRead; -// notifyListeners(); -// } -// } -// }; -// -// // Submit the request and wait for completion if required -// sendRequest((ITmfDataRequest) request); -// if (waitForCompletion) { -// try { -// request.waitForCompletion(); -// } catch (final InterruptedException e) { -// } -// } -// } -// -// private void notifyListeners() { -// broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime, fEndTime))); -// } - // ------------------------------------------------------------------------ // TmfProvider // ------------------------------------------------------------------------ diff --git a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceIndexer.java b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceIndexer.java index 81d15a926d..8e35d1ab47 100644 --- a/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceIndexer.java +++ b/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/trace/TmfTraceIndexer.java @@ -22,7 +22,6 @@ import org.eclipse.core.runtime.jobs.Job; 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; import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; @@ -92,7 +91,7 @@ public class TmfTraceIndexer> implements ITmfTrac } // ------------------------------------------------------------------------ - // ITmfTraceIndexer + // ITmfTraceIndexer - buildIndex // ------------------------------------------------------------------------ /* (non-Javadoc) @@ -180,86 +179,94 @@ public class TmfTraceIndexer> implements ITmfTrac } } + private void notifyListeners(final ITmfTimestamp startTime, final ITmfTimestamp endTime) { + fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime))); + } + + // ------------------------------------------------------------------------ + // ITmfTraceIndexer - updateIndex + // ------------------------------------------------------------------------ + + @Override + public void updateIndex(final ITmfContext context, final long rank, final ITmfTimestamp timestamp) { + if ((rank % fCheckpointInterval) == 0) { + // Determine the table position + final long position = rank / fCheckpointInterval; + // Add new entry at proper location (if empty) + if (fTraceIndex.size() == position) { + final ITmfLocation location = context.getLocation().clone(); + fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), location)); + // System.out.println(getName() + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString()); + } + } + } + + // ------------------------------------------------------------------------ + // ITmfTraceIndexer - seekIndex + // ------------------------------------------------------------------------ /* (non-Javadoc) * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp) */ @Override - public ITmfContext seekIndex(final ITmfTimestamp timestamp) { - // Adjust the timestamp if needed - ITmfTimestamp ts = timestamp; - if (ts == null) { - ts = TmfTimestamp.BIG_BANG; - } + public synchronized ITmfContext seekIndex(final ITmfTimestamp timestamp) { - // First, find the right checkpoint - int index = Collections.binarySearch(fTraceIndex, new TmfCheckpoint(ts, null)); + // A null timestamp indicates to seek the first event + if (timestamp == null) + return fTrace.seekLocation(null); - // In the very likely case that the checkpoint was not found, bsearch - // returns its negated would-be location (not an offset...). From that - // index, we can then position the stream and get figure out the context. + // Find the checkpoint at or before the requested timestamp. + // In the very likely event that the timestamp is not at a checkpoint + // boundary, bsearch will return index = (- (insertion point + 1)). + // It is then trivial to compute the index of the previous checkpoint. + int index = Collections.binarySearch(fTraceIndex, new TmfCheckpoint(timestamp, null)); if (index < 0) { index = Math.max(0, -(index + 2)); } // Position the trace at the checkpoint - ITmfLocation location; - synchronized (fTraceIndex) { - if (!fTraceIndex.isEmpty()) { - if (index >= fTraceIndex.size()) { - index = fTraceIndex.size() - 1; - } - location = fTraceIndex.elementAt(index).getLocation(); - } else { - location = null; - } - } - final ITmfContext context = fTrace.seekLocation(location); - context.setRank(index * fCheckpointInterval); - - return context; + return seekCheckpoint(index); } + /* (non-Javadoc) + * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTraceIndexer#seekIndex(long) + */ @Override public ITmfContext seekIndex(final long rank) { - // Position the stream at the previous checkpoint - int index = (int) rank / fCheckpointInterval; + // A rank <= 0 indicates to seek the first event + if (rank <= 0) + return fTrace.seekLocation(null); + + // Find the checkpoint at or before the requested rank. + final int index = (int) rank / fCheckpointInterval; + + // Position the trace at the checkpoint + return seekCheckpoint(index); + } + + /** + * Position the trace at the given checkpoint + * + * @param index + * the checkpoint index + * @return the corresponding context + */ + private ITmfContext seekCheckpoint(int index) { ITmfLocation location; synchronized (fTraceIndex) { - if (fTraceIndex.isEmpty()) { - location = null; - } else { + if (!fTraceIndex.isEmpty()) { if (index >= fTraceIndex.size()) { index = fTraceIndex.size() - 1; } location = fTraceIndex.elementAt(index).getLocation(); + } else { + location = null; } } - final ITmfContext context = fTrace.seekLocation(location); - final long pos = index * fCheckpointInterval; - context.setRank(pos); - + context.setRank(index * fCheckpointInterval); return context; } - private void notifyListeners(final ITmfTimestamp startTime, final ITmfTimestamp endTime) { - fTrace.broadcast(new TmfTraceUpdatedSignal(fTrace, fTrace, new TmfTimeRange(startTime, endTime))); - } - - @Override - public void updateIndex(final ITmfContext context, final long rank, final ITmfTimestamp timestamp) { - if ((rank % fCheckpointInterval) == 0) { - // Determine the table position - final long position = rank / fCheckpointInterval; - // Add new entry at proper location (if empty) - if (fTraceIndex.size() == position) { - final ITmfLocation location = context.getLocation().clone(); - fTraceIndex.add(new TmfCheckpoint(timestamp.clone(), location)); - // System.out.println(getName() + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString()); - } - } - } - } -- 2.34.1