Add incremental indexing support Bug 380952
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputReader.java
CommitLineData
866e5b51
FC
1/*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
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 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.ctf.core.trace;
14
ce2388e0 15import java.nio.ByteOrder;
866e5b51
FC
16
17import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
18import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
ce2388e0
FC
19import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput;
20import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
866e5b51
FC
21
22/**
23 * <b><u>StreamInputReader</u></b>
24 * <p>
25 * Reads the events of a trace file.
26 */
27public class StreamInputReader {
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32
33 /**
34 * The StreamInput we are reading.
35 */
36 private final StreamInput streamInput;
37
38 /**
39 * The packet reader used to read packets from this trace file.
40 */
41 private final StreamInputPacketReader packetReader;
42
43 /**
44 * Iterator on the packet index
45 */
bfe038ff 46 private int packetIndex;
866e5b51
FC
47
48 /**
49 * Reference to the current event of this trace file (iow, the last on that
50 * was read, the next one to be returned)
51 */
52 private EventDefinition currentEvent = null;
53
54 private int name;
55
bfe038ff
MK
56 private CTFTraceReader parent;
57
58 private final long prevIndex;
59
60 private final boolean isthisthefirsttimewerereadingthisgivenstream;
61
866e5b51
FC
62 // ------------------------------------------------------------------------
63 // Constructors
64 // ------------------------------------------------------------------------
65
66 /**
67 * Constructs a StreamInputReader that reads a StreamInput.
68 *
69 * @param streamInput
70 * The StreamInput to read.
71 */
72 public StreamInputReader(StreamInput streamInput) {
73 this.streamInput = streamInput;
74 this.packetReader = new StreamInputPacketReader(this);
bfe038ff 75 this.isthisthefirsttimewerereadingthisgivenstream = true;
866e5b51
FC
76 /*
77 * Get the iterator on the packet index.
78 */
bfe038ff 79 this.packetIndex = 0;
866e5b51 80
bfe038ff 81 this.prevIndex = 0;
866e5b51
FC
82 /*
83 * Make first packet the current one.
84 */
85 goToNextPacket();
86 }
87
88 // ------------------------------------------------------------------------
89 // Getters/Setters/Predicates
90 // ------------------------------------------------------------------------
91
92 public EventDefinition getCurrentEvent() {
93 return this.currentEvent;
94 }
95
96 public StructDefinition getCurrentPacketContext() {
97 return this.packetReader.getStreamPacketContextDef();
98 }
99
ce2388e0
FC
100 public ByteOrder getByteOrder() {
101 return streamInput.getStream().getTrace().getByteOrder();
866e5b51
FC
102 }
103
104 public int getName() {
105 return this.name;
106 }
107
108 public void setName(int name) {
109 this.name = name;
110 }
111
112 public int getCPU() {
113 return this.packetReader.getCPU();
114 }
115
ce2388e0
FC
116 public String getFilename() {
117 return streamInput.getFilename();
118 }
119
120 /*
121 * for internal use only
122 */
123 StreamInput getStreamInput() {
124 return streamInput;
125 }
126
866e5b51
FC
127 // ------------------------------------------------------------------------
128 // Operations
129 // ------------------------------------------------------------------------
130 /**
131 * Reads the next event in the current event variable.
132 *
133 * @return If an event has been successfully read.
134 */
135 public boolean readNextEvent() {
bfe038ff
MK
136
137
866e5b51
FC
138 /*
139 * Change packet if needed
140 */
141 if (!this.packetReader.hasMoreEvents()) {
bfe038ff
MK
142 final StreamInputPacketIndexEntry prevPacket = this.packetReader
143 .getCurrentPacket();
144 if (prevPacket != null) {
145 goToNextPacket();
146 final StreamInputPacketIndexEntry currentPacket = this.packetReader
147 .getCurrentPacket();
148 }
866e5b51
FC
149 }
150
151 /*
152 * If an event is available, read it.
153 */
154 if (this.packetReader.hasMoreEvents()) {
155 try {
156 this.setCurrentEvent(this.packetReader.readNextEvent());
157 } catch (CTFReaderException e) {
bfe038ff
MK
158 /*
159 * Some problem happened, we'll assume that there are no more
160 * events
161 */
866e5b51
FC
162 return false;
163 }
164 return true;
165 }
bfe038ff
MK
166 final StreamInputPacketIndexEntry currentPacket = this
167 .getPacketReader().getCurrentPacket();
866e5b51
FC
168 this.setCurrentEvent(null);
169 return false;
170 }
171
172 /**
173 * Change the current packet of the packet reader to the next one.
174 */
175 private void goToNextPacket() {
bfe038ff
MK
176 packetIndex++;
177 if (getPacketSize() >= (packetIndex + 1)) {
178 this.packetReader.setCurrentPacket(getPacket());
866e5b51 179 } else {
bfe038ff
MK
180 try {
181 if (this.streamInput.addPacketHeaderIndex()) {
182 packetIndex = getPacketSize() - 1;
183 this.packetReader.setCurrentPacket(getPacket());
184
185 } else {
186 this.packetReader.setCurrentPacket(null);
187 }
188
189 } catch (CTFReaderException e) {
190 this.packetReader.setCurrentPacket(null);
191 }
866e5b51
FC
192 }
193 }
194
bfe038ff
MK
195 /**
196 * @return
197 */
198 private int getPacketSize() {
199 return streamInput.getIndex().getEntries().size();
200 }
201
866e5b51
FC
202 /**
203 * Changes the location of the trace file reader so that the current event
204 * is the first event with a timestamp greater than the given timestamp.
205 *
206 * @param timestamp
207 * The timestamp to seek to.
208 */
ce2388e0
FC
209 public long seek(long timestamp) {
210 long offset = 0;
866e5b51
FC
211 /*
212 * Search in the index for the packet to search in.
213 */
bfe038ff
MK
214 this.packetIndex = this.streamInput.getIndex().search(timestamp)
215 .previousIndex();
866e5b51
FC
216
217 /*
218 * Switch to this packet.
219 */
220 goToNextPacket();
221
222 /*
223 * Advance until A. we reached the end of the trace file (which means
224 * the given timestamp is after the last event), or B. we found the
225 * first event with a timestamp greater than the given timestamp.
226 */
227 readNextEvent();
228 boolean done = (this.getCurrentEvent() == null);
aa572e22 229 while (!done && (this.getCurrentEvent().getTimestamp() < timestamp)) {
866e5b51
FC
230 readNextEvent();
231 done = (this.getCurrentEvent() == null);
ce2388e0 232 offset++;
866e5b51 233 }
ce2388e0
FC
234 return offset;
235 }
236
bfe038ff 237 public void goToLastEvent() {
866e5b51
FC
238 /*
239 * Search in the index for the packet to search in.
240 */
ec6f5beb
MK
241 final int len = this.streamInput.getIndex().getEntries().size();
242
243 StreamInputPacketIndexEntry entry = null;
866e5b51 244 /*
ec6f5beb 245 * Go to beginning of trace.
866e5b51 246 */
ec6f5beb
MK
247 seek(0);
248 /*
249 * if the trace is empty.
250 */
bfe038ff 251 if ((len == 0) || (this.packetReader.hasMoreEvents() == false)) {
ec6f5beb
MK
252 /*
253 * This means the trace is empty. abort.
254 */
255 return;
ce2388e0 256 }
ec6f5beb
MK
257 /*
258 * Go to the last packet that contains events.
259 */
bfe038ff
MK
260 for (int pos = len - 1; pos > 0; pos--) {
261 packetIndex = pos;
262 this.packetReader.setCurrentPacket(getPacket());
263 if (this.packetReader.hasMoreEvents()) {
ec6f5beb
MK
264 break;
265 }
866e5b51 266 }
ec6f5beb
MK
267
268 /*
269 * Go until the end of that packet
270 */
271 EventDefinition prevEvent = null;
272 while (this.currentEvent != null) {
273 prevEvent = this.currentEvent;
274 this.readNextEvent();
275 }
276 /*
277 * Go back to the previous event
278 */
279 this.setCurrentEvent(prevEvent);
866e5b51
FC
280 }
281
bfe038ff
MK
282 /**
283 * @return the parent
284 */
285 public CTFTraceReader getParent() {
286 return parent;
287 }
288
289 /**
290 * @param parent
291 * the parent to set
292 */
293 public void setParent(CTFTraceReader parent) {
294 this.parent = parent;
295 }
296
866e5b51
FC
297 public void setCurrentEvent(EventDefinition currentEvent) {
298 this.currentEvent = currentEvent;
299 }
300
ce2388e0
FC
301 /**
302 * @return the packetIndexIt
303 */
bfe038ff
MK
304 private int getPacketIndex() {
305 return packetIndex;
306 }
307
308 private StreamInputPacketIndexEntry getPacket() {
309 return streamInput.getIndex().getEntries().get(getPacketIndex());
ce2388e0
FC
310 }
311
312 /**
313 * @return the packetReader
314 */
315 public StreamInputPacketReader getPacketReader() {
316 return packetReader;
317 }
318
bfe038ff
MK
319 /*
320 * (non-Javadoc)
321 *
81c8e6f7
MK
322 * @see java.lang.Object#hashCode()
323 */
324 @Override
325 public int hashCode() {
326 final int prime = 31;
327 int result = 1;
328 result = (prime * result) + name;
329 result = (prime * result)
330 + ((streamInput == null) ? 0 : streamInput.hashCode());
331 return result;
332 }
333
bfe038ff
MK
334 /*
335 * (non-Javadoc)
336 *
81c8e6f7
MK
337 * @see java.lang.Object#equals(java.lang.Object)
338 */
339 @Override
340 public boolean equals(Object obj) {
341 if (this == obj) {
342 return true;
343 }
344 if (obj == null) {
345 return false;
346 }
347 if (!(obj instanceof StreamInputReader)) {
348 return false;
349 }
350 StreamInputReader other = (StreamInputReader) obj;
351 if (name != other.name) {
352 return false;
353 }
354 if (streamInput == null) {
355 if (other.streamInput != null) {
356 return false;
357 }
358 } else if (!streamInput.equals(other.streamInput)) {
359 return false;
360 }
361 return true;
362 }
363
866e5b51 364}
This page took 0.067259 seconds and 5 git commands to generate.