add fallback for seeking by unindexed traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfIterator.java
1 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
2
3 import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
4 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
5 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
6 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
7
8 public class CtfIterator extends CTFTraceReader implements ITmfContext,
9 Comparable<CtfIterator> {
10
11 private final CtfTmfTrace ctfTmfTrace;
12
13 private CtfLocation curLocation;
14 private final long curRank;
15
16 /**
17 * Create a new CTF trace iterator, which initially points at the first
18 * event in the trace.
19 *
20 * @param trace
21 */
22 public CtfIterator(CtfTmfTrace trace) {
23 super(trace.getCTFTrace());
24 this.ctfTmfTrace = trace;
25
26 // FIXME put the real stuff here...
27 this.curLocation = new CtfLocation(trace.getStartTime());
28 this.curRank = 0;
29 }
30
31 public CtfIterator(CtfTmfTrace trace, long timestampValue, long rank) {
32 super(trace.getCTFTrace());
33 this.ctfTmfTrace = trace;
34 this.curLocation = (new CtfLocation(this.getCurrentEvent()
35 .getTimestampValue()));
36 if (this.getCurrentEvent().getTimestampValue() != timestampValue) {
37 this.seek(timestampValue);
38 }
39
40 this.curRank = rank;
41 }
42
43 public CtfTmfTrace getCtfTmfTrace() {
44 return ctfTmfTrace;
45 }
46
47 public CtfTmfEvent getCurrentEvent() {
48 StreamInputReader top = super.prio.peek();
49 if (top != null) {
50 return new CtfTmfEvent(top.getCurrentEvent(), top, ctfTmfTrace);
51 }
52 return null;
53 }
54
55 @Override
56 public boolean seek(long timestamp) {
57 boolean ret = false;
58 long offsetTimestamp = timestamp - this.getCtfTmfTrace().getCTFTrace().getOffset();
59 if( offsetTimestamp < 0 ) {
60 ret = super.seek(timestamp);
61 } else {
62 ret = super.seek(offsetTimestamp);
63 }
64
65 if (ret) {
66 curLocation.setLocation(getCurrentEvent().getTimestampValue());
67 }
68 return ret;
69 }
70
71 public boolean seekRank(long rank) {
72 boolean ret = false;
73 ret = super.seekIndex(rank);
74
75 if (ret) {
76 curLocation.setLocation(getCurrentEvent().getTimestampValue());
77 }
78 return ret;
79 }
80
81 @Override
82 public long getRank() {
83 return super.getIndex();
84 }
85
86 @Override
87 public void setRank(long rank) {
88 seekRank(rank);
89 }
90
91 /*
92 * (non-Javadoc)
93 *
94 * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone()
95 */
96 @Override
97 public CtfIterator clone() {
98 CtfIterator clone = null;
99 clone = new CtfIterator(ctfTmfTrace, this.getCurrentEvent()
100 .getTimestampValue(), curRank);
101 return clone;
102 }
103
104 @Override
105 public void dispose() {
106 // FIXME add dispose() stuff to CTFTrace and call it here...
107
108 }
109
110 @Override
111 public void setLocation(ITmfLocation<?> location) {
112 // FIXME alex: isn't there a cleaner way than a cast here?
113 this.curLocation = (CtfLocation) location;
114 seek(((CtfLocation)location).getLocation());
115 }
116
117 @Override
118 public CtfLocation getLocation() {
119 return curLocation;
120 }
121
122 @Override
123 public void updateRank(int rank) {
124 // not needed I think
125 }
126
127 @Override
128 public boolean isValidRank() {
129 return (getRank() > -1);
130 }
131
132 @Override
133 public boolean advance() {
134 return super.advance();
135 }
136
137 @Override
138 public int compareTo(CtfIterator o) {
139 if (this.getRank() < o.getRank()) {
140 return -1;
141 } else if (this.getRank() > o.getRank()) {
142 return 1;
143 }
144 return 0;
145 }
146
147 }
This page took 0.034531 seconds and 6 git commands to generate.