Commit | Line | Data |
---|---|---|
b1baa808 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012 Ericsson | |
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: Matthew Khouzam - Initial API and implementation | |
10 | *******************************************************************************/ | |
a3fc8213 AM |
11 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
12 | ||
13 | import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; | |
14 | import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; | |
15 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; | |
16 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; | |
17 | ||
b1baa808 | 18 | /** |
d09f973b | 19 | * The CTF trace reader iterator. |
3eaea7e6 | 20 | * |
d09f973b FC |
21 | * It doesn't reserve a file handle, so many iterators can be used without worries |
22 | * of I/O errors or resource exhaustion. | |
3eaea7e6 | 23 | * |
d09f973b FC |
24 | * @version 1.0 |
25 | * @author Matthew Khouzam | |
b1baa808 | 26 | */ |
132a02b0 MK |
27 | public class CtfIterator extends CTFTraceReader implements ITmfContext, |
28 | Comparable<CtfIterator>, Cloneable { | |
a3fc8213 AM |
29 | |
30 | private final CtfTmfTrace ctfTmfTrace; | |
31 | ||
9ac2eb62 MK |
32 | /** |
33 | * An invalid location | |
34 | */ | |
132a02b0 MK |
35 | final public static CtfLocation NULL_LOCATION = new CtfLocation(CtfLocation.INVALID_LOCATION); |
36 | ||
a3fc8213 AM |
37 | private CtfLocation curLocation; |
38 | private long curRank; | |
39 | ||
40 | /** | |
41 | * Create a new CTF trace iterator, which initially points at the first | |
42 | * event in the trace. | |
43 | * | |
132a02b0 MK |
44 | * @param trace |
45 | * the trace to iterate over | |
a3fc8213 | 46 | */ |
ce2388e0 | 47 | public CtfIterator(final CtfTmfTrace trace) { |
a3fc8213 AM |
48 | super(trace.getCTFTrace()); |
49 | this.ctfTmfTrace = trace; | |
57c073c5 | 50 | if (this.hasMoreEvents()) { |
57c073c5 MK |
51 | this.curLocation = new CtfLocation(trace.getStartTime()); |
52 | this.curRank = 0; | |
53 | } else { | |
54 | setUnknownLocation(); | |
55 | } | |
a3fc8213 AM |
56 | } |
57 | ||
57c073c5 | 58 | private void setUnknownLocation() { |
f474d36b | 59 | this.curLocation = NULL_LOCATION; |
57c073c5 MK |
60 | this.curRank = UNKNOWN_RANK; |
61 | } | |
62 | ||
b1baa808 MK |
63 | /** |
64 | * Constructor for CtfIterator. | |
132a02b0 MK |
65 | * |
66 | * @param trace | |
67 | * CtfTmfTrace the trace | |
68 | * @param ctfLocationData | |
69 | * long the timestamp in ns of the trace for positioning | |
70 | * @param rank | |
71 | * long the index of the trace for positioning | |
72 | * @since 2.0 | |
b1baa808 | 73 | */ |
132a02b0 MK |
74 | public CtfIterator(final CtfTmfTrace trace, |
75 | final CtfLocationData ctfLocationData, final long rank) { | |
a3fc8213 | 76 | super(trace.getCTFTrace()); |
57c073c5 | 77 | |
a3fc8213 | 78 | this.ctfTmfTrace = trace; |
57c073c5 | 79 | if (this.hasMoreEvents()) { |
132a02b0 MK |
80 | this.curLocation = new CtfLocation(ctfLocationData); |
81 | if (this.getCurrentEvent().getTimestampValue() != ctfLocationData.getTimestamp()) { | |
82 | this.seek(ctfLocationData); | |
57c073c5 MK |
83 | this.curRank = rank; |
84 | } | |
85 | } else { | |
86 | setUnknownLocation(); | |
87 | } | |
a3fc8213 | 88 | |
a3fc8213 AM |
89 | } |
90 | ||
b1baa808 MK |
91 | /** |
92 | * Method getCtfTmfTrace. gets a CtfTmfTrace | |
93 | * @return CtfTmfTrace | |
94 | */ | |
a3fc8213 AM |
95 | public CtfTmfTrace getCtfTmfTrace() { |
96 | return ctfTmfTrace; | |
97 | } | |
98 | ||
b1baa808 MK |
99 | /** |
100 | * Method getCurrentEvent. gets the current event | |
101 | * @return CtfTmfEvent | |
102 | */ | |
a3fc8213 | 103 | public CtfTmfEvent getCurrentEvent() { |
ce2388e0 | 104 | final StreamInputReader top = super.prio.peek(); |
57c073c5 MK |
105 | if (top != null) { |
106 | return new CtfTmfEvent(top.getCurrentEvent(), top.getFilename(), | |
107 | ctfTmfTrace); | |
108 | } | |
a3fc8213 AM |
109 | return null; |
110 | } | |
111 | ||
b1baa808 | 112 | /** |
132a02b0 MK |
113 | * Seek this iterator to a given location. |
114 | * | |
115 | * @param ctfLocationData | |
116 | * The LocationData representing the position to seek to | |
b1baa808 | 117 | * @return boolean |
132a02b0 | 118 | * @since 2.0 |
b1baa808 | 119 | */ |
132a02b0 | 120 | public boolean seek(final CtfLocationData ctfLocationData) { |
a3fc8213 | 121 | boolean ret = false; |
132a02b0 MK |
122 | |
123 | /* Adjust the timestamp depending on the trace's offset */ | |
124 | long currTimestamp = ctfLocationData.getTimestamp(); | |
125 | final long offsetTimestamp = currTimestamp - this.getTrace().getOffset(); | |
57c073c5 | 126 | if (offsetTimestamp < 0) { |
3eaea7e6 | 127 | ret = super.seek(0L); |
57c073c5 | 128 | } else { |
ce2388e0 | 129 | ret = super.seek(offsetTimestamp); |
57c073c5 | 130 | } |
a3fc8213 | 131 | |
132a02b0 MK |
132 | /* |
133 | * Check if there is already one or more events for that timestamp, and | |
134 | * assign the location index correctly | |
135 | */ | |
136 | currTimestamp = this.getCurrentEvent().getTimestampValue(); | |
137 | long index = 0; | |
138 | for (long i = 0; i < ctfLocationData.getIndex(); i++) { | |
139 | if (currTimestamp == this.getCurrentEvent().getTimestampValue()) { | |
140 | index++; | |
141 | } else { | |
142 | index = 0; | |
143 | } | |
144 | this.advance(); | |
145 | } | |
146 | ||
147 | /* Seek the current location accordingly */ | |
57c073c5 | 148 | if (ret) { |
132a02b0 | 149 | curLocation.setLocation(new CtfLocationData(getCurrentEvent().getTimestampValue(), index)); |
f474d36b PT |
150 | } else { |
151 | curLocation = NULL_LOCATION; | |
57c073c5 | 152 | } |
ce2388e0 FC |
153 | return ret; |
154 | } | |
155 | ||
b1baa808 MK |
156 | /** |
157 | * Method getRank. | |
158 | * @return long | |
159 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank() | |
160 | */ | |
a3fc8213 AM |
161 | @Override |
162 | public long getRank() { | |
f474d36b | 163 | return curRank; |
a3fc8213 AM |
164 | } |
165 | ||
b1baa808 MK |
166 | /** |
167 | * Method setRank. | |
168 | * @param rank long | |
169 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long) | |
170 | */ | |
a3fc8213 | 171 | @Override |
ce2388e0 | 172 | public void setRank(final long rank) { |
f474d36b | 173 | curRank = rank; |
a3fc8213 AM |
174 | } |
175 | ||
176 | /* | |
177 | * (non-Javadoc) | |
178 | * | |
179 | * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone() | |
180 | */ | |
181 | @Override | |
182 | public CtfIterator clone() { | |
183 | CtfIterator clone = null; | |
132a02b0 | 184 | clone = new CtfIterator(ctfTmfTrace, this.getLocation().getLocation(), curRank); |
a3fc8213 AM |
185 | return clone; |
186 | } | |
187 | ||
b1baa808 MK |
188 | /** |
189 | * Method dispose. | |
190 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose() | |
191 | */ | |
a3fc8213 AM |
192 | @Override |
193 | public void dispose() { | |
194 | // FIXME add dispose() stuff to CTFTrace and call it here... | |
195 | ||
196 | } | |
197 | ||
b1baa808 MK |
198 | /** |
199 | * Method setLocation. | |
200 | * @param location ITmfLocation<?> | |
b1baa808 | 201 | */ |
a3fc8213 | 202 | @Override |
ce2388e0 | 203 | public void setLocation(final ITmfLocation<?> location) { |
a3fc8213 AM |
204 | // FIXME alex: isn't there a cleaner way than a cast here? |
205 | this.curLocation = (CtfLocation) location; | |
57c073c5 | 206 | seek(((CtfLocation) location).getLocation()); |
a3fc8213 AM |
207 | } |
208 | ||
b1baa808 MK |
209 | /** |
210 | * Method getLocation. | |
211 | * @return CtfLocation | |
212 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation() | |
213 | */ | |
a3fc8213 AM |
214 | @Override |
215 | public CtfLocation getLocation() { | |
216 | return curLocation; | |
217 | } | |
218 | ||
b1baa808 MK |
219 | /** |
220 | * Method increaseRank. | |
221 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank() | |
222 | */ | |
a3fc8213 | 223 | @Override |
cbdacf03 | 224 | public void increaseRank() { |
4a110860 AM |
225 | /* Only increase the rank if it's valid */ |
226 | if(hasValidRank()) { | |
227 | curRank++; | |
228 | } | |
a3fc8213 AM |
229 | } |
230 | ||
b1baa808 MK |
231 | /** |
232 | * Method hasValidRank, if the iterator is valid | |
233 | * @return boolean | |
234 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank() | |
235 | */ | |
a3fc8213 | 236 | @Override |
cbdacf03 | 237 | public boolean hasValidRank() { |
bcbea6a6 | 238 | return (getRank() >= 0); |
a3fc8213 AM |
239 | } |
240 | ||
b1baa808 MK |
241 | /** |
242 | * Method advance go to the next event | |
243 | * @return boolean successful or not | |
244 | */ | |
a3fc8213 AM |
245 | @Override |
246 | public boolean advance() { | |
132a02b0 MK |
247 | long index = curLocation.getLocation().getIndex(); |
248 | long timestamp = curLocation.getLocation().getTimestamp(); | |
f474d36b | 249 | boolean ret = super.advance(); |
132a02b0 | 250 | |
f474d36b | 251 | if (ret) { |
132a02b0 MK |
252 | final long timestampValue = getCurrentEvent().getTimestampValue(); |
253 | if (timestamp == timestampValue) { | |
254 | curLocation.setLocation(timestampValue, index + 1); | |
255 | } else { | |
256 | curLocation.setLocation(timestampValue, 0L); | |
257 | } | |
f474d36b PT |
258 | } else { |
259 | curLocation = NULL_LOCATION; | |
260 | } | |
261 | return ret; | |
a3fc8213 AM |
262 | } |
263 | ||
b1baa808 MK |
264 | /** |
265 | * Method compareTo. | |
266 | * @param o CtfIterator | |
267 | * @return int -1, 0, 1 | |
268 | */ | |
a3fc8213 | 269 | @Override |
ce2388e0 | 270 | public int compareTo(final CtfIterator o) { |
57c073c5 | 271 | if (this.getRank() < o.getRank()) { |
a3fc8213 | 272 | return -1; |
57c073c5 | 273 | } else if (this.getRank() > o.getRank()) { |
a3fc8213 | 274 | return 1; |
57c073c5 | 275 | } |
a3fc8213 AM |
276 | return 0; |
277 | } | |
788ddcbc | 278 | |
b1baa808 MK |
279 | /* (non-Javadoc) |
280 | * @see java.lang.Object#hashCode() | |
281 | */ | |
282 | @Override | |
283 | public int hashCode() { | |
284 | final int prime = 31; | |
285 | int result = super.hashCode(); | |
286 | result = (prime * result) | |
287 | + ((ctfTmfTrace == null) ? 0 : ctfTmfTrace.hashCode()); | |
288 | result = (prime * result) | |
289 | + ((curLocation == null) ? 0 : curLocation.hashCode()); | |
290 | result = (prime * result) + (int) (curRank ^ (curRank >>> 32)); | |
291 | return result; | |
292 | } | |
a3fc8213 | 293 | |
b1baa808 MK |
294 | /* (non-Javadoc) |
295 | * @see java.lang.Object#equals(java.lang.Object) | |
296 | */ | |
297 | @Override | |
298 | public boolean equals(Object obj) { | |
299 | if (this == obj) { | |
300 | return true; | |
301 | } | |
302 | if (!super.equals(obj)) { | |
303 | return false; | |
304 | } | |
305 | if (!(obj instanceof CtfIterator)) { | |
306 | return false; | |
307 | } | |
308 | CtfIterator other = (CtfIterator) obj; | |
309 | if (ctfTmfTrace == null) { | |
310 | if (other.ctfTmfTrace != null) { | |
311 | return false; | |
312 | } | |
313 | } else if (!ctfTmfTrace.equals(other.ctfTmfTrace)) { | |
314 | return false; | |
315 | } | |
316 | if (curLocation == null) { | |
317 | if (other.curLocation != null) { | |
318 | return false; | |
319 | } | |
320 | } else if (!curLocation.equals(other.curLocation)) { | |
321 | return false; | |
322 | } | |
323 | if (curRank != other.curRank) { | |
324 | return false; | |
325 | } | |
326 | return true; | |
327 | } | |
ce2388e0 | 328 | } |