tmf: Make CustomText/Xml test wait until wizard shell is active
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / internal / tmf / ctf / core / trace / iterator / CtfIterator.java
CommitLineData
b1baa808 1/*******************************************************************************
fab7b404 2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
b1baa808
MK
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
5b020488
AM
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
fab7b404 11 * Florian Wininger - Performance improvements
b1baa808 12 *******************************************************************************/
5b020488 13
fe71057b 14package org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator;
a3fc8213 15
c052c341
MK
16import static org.eclipse.tracecompass.common.core.NonNullUtils.equalsNullable;
17
680f9173 18import org.eclipse.tracecompass.ctf.core.CTFException;
90cefe9f 19import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
f357bcd4 20import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader;
fe71057b 21import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
f357bcd4 22import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
2bdf0193
AM
23import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator;
24import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
25import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
9722e5d7
AM
26import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocation;
27import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocationInfo;
28import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
9722e5d7 29import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
a3fc8213 30
b1baa808 31/**
d09f973b 32 * The CTF trace reader iterator.
3eaea7e6 33 *
db8e8f7d
AM
34 * It doesn't reserve a file handle, so many iterators can be used without
35 * worries of I/O errors or resource exhaustion.
3eaea7e6 36 *
d09f973b 37 * @author Matthew Khouzam
b1baa808 38 */
5b020488
AM
39public class CtfIterator extends CTFTraceReader
40 implements ITmfContext, Comparable<CtfIterator> {
a3fc8213 41
5b020488 42 /** An invalid location */
db8e8f7d 43 public static final CtfLocation NULL_LOCATION = new CtfLocation(CtfLocation.INVALID_LOCATION);
132a02b0 44
5b020488
AM
45 private final CtfTmfTrace fTrace;
46
47 private CtfLocation fCurLocation;
48 private long fCurRank;
49
fab7b404
FW
50 private CtfLocation fPreviousLocation;
51 private CtfTmfEvent fPreviousEvent;
52
5b020488
AM
53 // ------------------------------------------------------------------------
54 // Constructors
55 // ------------------------------------------------------------------------
a3fc8213
AM
56
57 /**
58 * Create a new CTF trace iterator, which initially points at the first
59 * event in the trace.
60 *
fe71057b
AM
61 * @param ctfTrace
62 * The {@link CTFTrace} linked to the trace. It should be
63 * provided by the corresponding 'ctfTmfTrace'.
64 *
65 * @param ctfTmfTrace
66 * The {@link CtfTmfTrace} to iterate over
680f9173 67 * @throws CTFException
5b020488
AM
68 * If the iterator couldn't not be instantiated, probably due to
69 * a read error.
a3fc8213 70 */
680f9173 71 public CtfIterator(CTFTrace ctfTrace, CtfTmfTrace ctfTmfTrace) throws CTFException {
fe71057b
AM
72 super(ctfTrace);
73 fTrace = ctfTmfTrace;
9d819ac7 74 if (hasMoreEvents()) {
fe71057b 75 fCurLocation = new CtfLocation(ctfTmfTrace.getStartTime());
5b020488 76 fCurRank = 0;
57c073c5
MK
77 } else {
78 setUnknownLocation();
79 }
a3fc8213
AM
80 }
81
b1baa808 82 /**
5b020488
AM
83 * Create a new CTF trace iterator, which will initially point to the given
84 * location/rank.
132a02b0 85 *
fe71057b
AM
86 * @param ctfTrace
87 * The {@link CTFTrace} linked to the trace. It should be
88 * provided by the corresponding 'ctfTmfTrace'.
89 * @param ctfTmfTrace
90 * The {@link CtfTmfTrace} to iterate over
132a02b0 91 * @param ctfLocationData
5b020488 92 * The initial timestamp the iterator will be pointing to
132a02b0 93 * @param rank
5b020488 94 * The initial rank
680f9173 95 * @throws CTFException
5b020488
AM
96 * If the iterator couldn't not be instantiated, probably due to
97 * a read error.
b1baa808 98 */
fe71057b 99 public CtfIterator(CTFTrace ctfTrace, CtfTmfTrace ctfTmfTrace, CtfLocationInfo ctfLocationData, long rank)
680f9173 100 throws CTFException {
fe71057b 101 super(ctfTrace);
57c073c5 102
fe71057b 103 this.fTrace = ctfTmfTrace;
57c073c5 104 if (this.hasMoreEvents()) {
5b020488 105 this.fCurLocation = new CtfLocation(ctfLocationData);
58f3bc52 106 if (this.getCurrentEvent().getTimestamp().getValue() != ctfLocationData.getTimestamp()) {
132a02b0 107 this.seek(ctfLocationData);
5b020488 108 this.fCurRank = rank;
57c073c5
MK
109 }
110 } else {
111 setUnknownLocation();
112 }
a3fc8213
AM
113 }
114
dd9752d5
AM
115 @Override
116 public void dispose() {
117 close();
118 }
119
5b020488
AM
120 private void setUnknownLocation() {
121 fCurLocation = NULL_LOCATION;
122 fCurRank = UNKNOWN_RANK;
123 }
124
125 // ------------------------------------------------------------------------
126 // Accessors
127 // ------------------------------------------------------------------------
128
b1baa808 129 /**
5b020488 130 * Return this iterator's trace.
db8e8f7d 131 *
5b020488 132 * @return CtfTmfTrace The iterator's trace
b1baa808 133 */
a3fc8213 134 public CtfTmfTrace getCtfTmfTrace() {
5b020488 135 return fTrace;
a3fc8213
AM
136 }
137
b1baa808 138 /**
5b020488 139 * Return the current event pointed to by the iterator.
db8e8f7d 140 *
5b020488 141 * @return CtfTmfEvent The current event
b1baa808 142 */
fab7b404 143 public synchronized CtfTmfEvent getCurrentEvent() {
d84419e1 144 final CTFStreamInputReader top = super.getPrio().peek();
57c073c5 145 if (top != null) {
fab7b404
FW
146 if (!fCurLocation.equals(fPreviousLocation)) {
147 fPreviousLocation = fCurLocation;
8e376474 148 fPreviousEvent = fTrace.getEventFactory().createEvent(fTrace, top.getCurrentEvent(), top.getFilename());
fab7b404
FW
149 }
150 return fPreviousEvent;
57c073c5 151 }
a3fc8213
AM
152 return null;
153 }
154
962fb72f 155 /**
c052c341
MK
156 * Return the current timestamp location pointed to by the iterator. This is
157 * the timestamp for use in CtfLocation, not the event timestamp.
962fb72f
PT
158 *
159 * @return long The current timestamp location
160 */
161 public synchronized long getCurrentTimestamp() {
162 final CTFStreamInputReader top = super.getPrio().peek();
163 if (top != null) {
90cefe9f
MK
164 EventDefinition currentEvent = top.getCurrentEvent();
165 if (currentEvent != null) {
166 long ts = currentEvent.getTimestamp();
167 return fTrace.timestampCyclesToNanos(ts);
168 }
962fb72f
PT
169 }
170 return 0;
171 }
172
b1baa808 173 /**
132a02b0
MK
174 * Seek this iterator to a given location.
175 *
176 * @param ctfLocationData
177 * The LocationData representing the position to seek to
db8e8f7d
AM
178 * @return boolean True if the seek was successful, false if there was an
179 * error seeking.
b1baa808 180 */
5b020488 181 public synchronized boolean seek(CtfLocationInfo ctfLocationData) {
a3fc8213 182 boolean ret = false;
21924e03
MAL
183 if (ctfLocationData.equals(CtfLocation.INVALID_LOCATION)) {
184 fCurLocation = NULL_LOCATION;
185 return false;
186 }
132a02b0 187
92d542eb 188 /* Avoid the cost of seeking at the current location. */
5b020488 189 if (fCurLocation.getLocationInfo().equals(ctfLocationData)) {
92d542eb
EB
190 return super.hasMoreEvents();
191 }
5b17357d
PT
192 /* Update location to make sure the current event is updated */
193 fCurLocation = new CtfLocation(ctfLocationData);
92d542eb 194
132a02b0
MK
195 /* Adjust the timestamp depending on the trace's offset */
196 long currTimestamp = ctfLocationData.getTimestamp();
fe71057b 197 final long offsetTimestamp = this.getCtfTmfTrace().timestampNanoToCycles(currTimestamp);
db8e8f7d
AM
198 try {
199 if (offsetTimestamp < 0) {
200 ret = super.seek(0L);
201 } else {
202 ret = super.seek(offsetTimestamp);
203 }
680f9173 204 } catch (CTFException e) {
91e7f946 205 Activator.getDefault().logError(e.getMessage(), e);
db8e8f7d 206 return false;
57c073c5 207 }
132a02b0
MK
208 /*
209 * Check if there is already one or more events for that timestamp, and
210 * assign the location index correctly
211 */
132a02b0 212 long index = 0;
b6220b93
MK
213 final CtfTmfEvent currentEvent = this.getCurrentEvent();
214 if (currentEvent != null) {
215 currTimestamp = currentEvent.getTimestamp().getValue();
77ef700d
MK
216
217 for (long i = 0; i < ctfLocationData.getIndex(); i++) {
b6220b93 218 if (currTimestamp == currentEvent.getTimestamp().getValue()) {
77ef700d
MK
219 index++;
220 } else {
221 index = 0;
222 }
223 this.advance();
132a02b0 224 }
77ef700d 225 } else {
ecb12461 226 ret = false;
132a02b0 227 }
132a02b0 228 /* Seek the current location accordingly */
57c073c5 229 if (ret) {
5b020488 230 fCurLocation = new CtfLocation(new CtfLocationInfo(getCurrentEvent().getTimestamp().getValue(), index));
f474d36b 231 } else {
5b020488 232 fCurLocation = NULL_LOCATION;
57c073c5 233 }
ecb12461 234
ce2388e0
FC
235 return ret;
236 }
237
5b020488
AM
238 // ------------------------------------------------------------------------
239 // CTFTraceReader
240 // ------------------------------------------------------------------------
a3fc8213
AM
241
242 @Override
5b020488
AM
243 public boolean seek(long timestamp) {
244 return seek(new CtfLocationInfo(timestamp, 0));
a3fc8213
AM
245 }
246
a3fc8213 247 @Override
5b020488 248 public synchronized boolean advance() {
5b020488 249 boolean ret = false;
db8e8f7d 250 try {
5b020488 251 ret = super.advance();
680f9173 252 } catch (CTFException e) {
91e7f946 253 Activator.getDefault().logError(e.getMessage(), e);
db8e8f7d 254 }
a3fc8213 255
5b020488 256 if (ret) {
962fb72f
PT
257 long timestamp = fCurLocation.getLocationInfo().getTimestamp();
258 final long timestampValue = getCurrentTimestamp();
5b020488 259 if (timestamp == timestampValue) {
962fb72f 260 long index = fCurLocation.getLocationInfo().getIndex();
5b020488
AM
261 fCurLocation = new CtfLocation(timestampValue, index + 1);
262 } else {
263 fCurLocation = new CtfLocation(timestampValue, 0L);
264 }
265 } else {
266 fCurLocation = NULL_LOCATION;
267 }
268 return ret;
a3fc8213
AM
269 }
270
5b020488
AM
271 // ------------------------------------------------------------------------
272 // ITmfContext
273 // ------------------------------------------------------------------------
274
a3fc8213 275 @Override
5b020488
AM
276 public long getRank() {
277 return fCurRank;
a3fc8213
AM
278 }
279
280 @Override
5b020488
AM
281 public void setRank(long rank) {
282 fCurRank = rank;
a3fc8213
AM
283 }
284
285 @Override
cbdacf03 286 public void increaseRank() {
4a110860 287 /* Only increase the rank if it's valid */
ecb12461 288 if (hasValidRank()) {
5b020488 289 fCurRank++;
4a110860 290 }
a3fc8213
AM
291 }
292
293 @Override
cbdacf03 294 public boolean hasValidRank() {
bcbea6a6 295 return (getRank() >= 0);
a3fc8213
AM
296 }
297
298 @Override
5b020488
AM
299 public void setLocation(ITmfLocation location) {
300 // FIXME alex: isn't there a cleaner way than a cast here?
301 fCurLocation = (CtfLocation) location;
302 seek(((CtfLocation) location).getLocationInfo());
303 }
132a02b0 304
5b020488
AM
305 @Override
306 public CtfLocation getLocation() {
307 return fCurLocation;
a3fc8213
AM
308 }
309
5b020488
AM
310 // ------------------------------------------------------------------------
311 // Comparable
312 // ------------------------------------------------------------------------
313
a3fc8213 314 @Override
ce2388e0 315 public int compareTo(final CtfIterator o) {
9d819ac7 316 if (getRank() < o.getRank()) {
a3fc8213 317 return -1;
9d819ac7 318 } else if (getRank() > o.getRank()) {
a3fc8213 319 return 1;
57c073c5 320 }
a3fc8213
AM
321 return 0;
322 }
788ddcbc 323
5b020488
AM
324 // ------------------------------------------------------------------------
325 // Object
326 // ------------------------------------------------------------------------
327
b1baa808
MK
328 @Override
329 public int hashCode() {
330 final int prime = 31;
331 int result = super.hashCode();
332 result = (prime * result)
5b020488 333 + ((fTrace == null) ? 0 : fTrace.hashCode());
b1baa808 334 result = (prime * result)
5b020488
AM
335 + ((fCurLocation == null) ? 0 : fCurLocation.hashCode());
336 result = (prime * result) + (int) (fCurRank ^ (fCurRank >>> 32));
b1baa808
MK
337 return result;
338 }
a3fc8213 339
b1baa808
MK
340 @Override
341 public boolean equals(Object obj) {
342 if (this == obj) {
343 return true;
344 }
345 if (!super.equals(obj)) {
346 return false;
347 }
348 if (!(obj instanceof CtfIterator)) {
349 return false;
350 }
351 CtfIterator other = (CtfIterator) obj;
c052c341 352 if (!equalsNullable(fTrace, other.fTrace)) {
b1baa808
MK
353 return false;
354 }
c052c341 355 if (!equalsNullable(fCurLocation, other.fCurLocation)) {
b1baa808
MK
356 return false;
357 }
5b020488 358 if (fCurRank != other.fCurRank) {
b1baa808
MK
359 return false;
360 }
361 return true;
362 }
ce2388e0 363}
This page took 0.100837 seconds and 5 git commands to generate.