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().getTimestampValue() != 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 /**
113 * Seek this iterator to a given location.
114 *
115 * @param ctfLocationData
116 * The LocationData representing the position to seek to
117 * @return boolean
118 * @since 2.0
119 */
120 public boolean seek(final CtfLocationData ctfLocationData) {
121 boolean ret = false;
122
123 /* Adjust the timestamp depending on the trace's offset */
124 long currTimestamp = ctfLocationData.getTimestamp();
125 final long offsetTimestamp = this.getCtfTmfTrace().getCTFTrace().timestampNanoToCycles(currTimestamp);
126 if (offsetTimestamp < 0) {
127 ret = super.seek(0L);
128 } else {
129 ret = super.seek(offsetTimestamp);
130 }
131
132 /*
133 * Check if there is already one or more events for that timestamp, and
134 * assign the location index correctly
135 */
136 long index = 0;
137 if (this.getCurrentEvent() != null) {
138 currTimestamp = this.getCurrentEvent().getTimestampValue();
139
140 for (long i = 0; i < ctfLocationData.getIndex(); i++) {
141 if (currTimestamp == this.getCurrentEvent().getTimestampValue()) {
142 index++;
143 } else {
144 index = 0;
145 }
146 this.advance();
147 }
148 } else {
149 ret= false;
150 }
151 /* Seek the current location accordingly */
152 if (ret) {
153 curLocation.setLocation(new CtfLocationData(getCurrentEvent().getTimestampValue(), index));
154 } else {
155 curLocation = NULL_LOCATION;
156 }
157 return ret;
158 }
159
160 /**
161 * Method getRank.
162 * @return long
163 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getRank()
164 */
165 @Override
166 public long getRank() {
167 return curRank;
168 }
169
170 /**
171 * Method setRank.
172 * @param rank long
173 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#setRank(long)
174 */
175 @Override
176 public void setRank(final long rank) {
177 curRank = rank;
178 }
179
180 /*
181 * (non-Javadoc)
182 *
183 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
184 */
185 @Override
186 public CtfIterator clone() {
187 CtfIterator clone = null;
188 clone = new CtfIterator(ctfTmfTrace, this.getLocation().getLocation(), curRank);
189 return clone;
190 }
191
192 /**
193 * Method dispose.
194 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#dispose()
195 */
196 @Override
197 public void dispose() {
198 // FIXME add dispose() stuff to CTFTrace and call it here...
199
200 }
201
202 /**
203 * Method setLocation.
204 * @param location ITmfLocation<?>
205 */
206 @Override
207 public void setLocation(final ITmfLocation<?> location) {
208 // FIXME alex: isn't there a cleaner way than a cast here?
209 this.curLocation = (CtfLocation) location;
210 seek(((CtfLocation) location).getLocation());
211 }
212
213 /**
214 * Method getLocation.
215 * @return CtfLocation
216 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#getLocation()
217 */
218 @Override
219 public CtfLocation getLocation() {
220 return curLocation;
221 }
222
223 /**
224 * Method increaseRank.
225 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#increaseRank()
226 */
227 @Override
228 public void increaseRank() {
229 /* Only increase the rank if it's valid */
230 if(hasValidRank()) {
231 curRank++;
232 }
233 }
234
235 /**
236 * Method hasValidRank, if the iterator is valid
237 * @return boolean
238 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfContext#hasValidRank()
239 */
240 @Override
241 public boolean hasValidRank() {
242 return (getRank() >= 0);
243 }
244
245 /**
246 * Method advance go to the next event
247 * @return boolean successful or not
248 */
249 @Override
250 public boolean advance() {
251 long index = curLocation.getLocation().getIndex();
252 long timestamp = curLocation.getLocation().getTimestamp();
253 boolean ret = super.advance();
254
255 if (ret) {
256 final long timestampValue = getCurrentEvent().getTimestampValue();
257 if (timestamp == timestampValue) {
258 curLocation.setLocation(timestampValue, index + 1);
259 } else {
260 curLocation.setLocation(timestampValue, 0L);
261 }
262 } else {
263 curLocation = NULL_LOCATION;
264 }
265 return ret;
266 }
267
268 /**
269 * Method compareTo.
270 * @param o CtfIterator
271 * @return int -1, 0, 1
272 */
273 @Override
274 public int compareTo(final CtfIterator o) {
275 if (this.getRank() < o.getRank()) {
276 return -1;
277 } else if (this.getRank() > o.getRank()) {
278 return 1;
279 }
280 return 0;
281 }
282
283 /* (non-Javadoc)
284 * @see java.lang.Object#hashCode()
285 */
286 @Override
287 public int hashCode() {
288 final int prime = 31;
289 int result = super.hashCode();
290 result = (prime * result)
291 + ((ctfTmfTrace == null) ? 0 : ctfTmfTrace.hashCode());
292 result = (prime * result)
293 + ((curLocation == null) ? 0 : curLocation.hashCode());
294 result = (prime * result) + (int) (curRank ^ (curRank >>> 32));
295 return result;
296 }
297
298 /* (non-Javadoc)
299 * @see java.lang.Object#equals(java.lang.Object)
300 */
301 @Override
302 public boolean equals(Object obj) {
303 if (this == obj) {
304 return true;
305 }
306 if (!super.equals(obj)) {
307 return false;
308 }
309 if (!(obj instanceof CtfIterator)) {
310 return false;
311 }
312 CtfIterator other = (CtfIterator) obj;
313 if (ctfTmfTrace == null) {
314 if (other.ctfTmfTrace != null) {
315 return false;
316 }
317 } else if (!ctfTmfTrace.equals(other.ctfTmfTrace)) {
318 return false;
319 }
320 if (curLocation == null) {
321 if (other.curLocation != null) {
322 return false;
323 }
324 } else if (!curLocation.equals(other.curLocation)) {
325 return false;
326 }
327 if (curRank != other.curRank) {
328 return false;
329 }
330 return true;
331 }
332 }
This page took 0.047785 seconds and 6 git commands to generate.