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