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