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