tmf: Bug 421475: Inaccurate seek for traces with timestamp transform
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ctf.core / src / org / eclipse / linuxtools / tmf / ctf / core / CtfTmfContext.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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:
10 * Matthew Khouzam - Initial API and implementation
11 * Simon Delisle - Remove the iterator in dispose()
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ctf.core;
15
16 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
17 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
18
19 /**
20 * Lightweight Context for CtfTmf traces. Should only use 3 references, 1 ref to
21 * a boxed Long, a long and an int.
22 *
23 * @author Matthew Khouzam
24 * @version 1.0
25 * @since 2.0
26 */
27 public class CtfTmfContext implements ITmfContext {
28
29 // -------------------------------------------
30 // Fields
31 // -------------------------------------------
32
33 private CtfLocation fCurLocation;
34 private long fCurRank;
35
36 private final CtfTmfTrace fTrace;
37
38 // -------------------------------------------
39 // Constructor
40 // -------------------------------------------
41
42 /**
43 * Constructor
44 *
45 * @param ctfTmfTrace
46 * the parent trace
47 * @since 1.1
48 */
49 public CtfTmfContext(CtfTmfTrace ctfTmfTrace) {
50 fTrace = ctfTmfTrace;
51 fCurLocation = new CtfLocation(new CtfLocationInfo(0, 0));
52 }
53
54 // -------------------------------------------
55 // TmfContext Overrides
56 // -------------------------------------------
57
58 @Override
59 public long getRank() {
60 return fCurRank;
61 }
62
63 /**
64 * @since 3.0
65 */
66 @Override
67 public synchronized ITmfLocation getLocation() {
68 return fCurLocation;
69 }
70
71 @Override
72 public boolean hasValidRank() {
73 return fCurRank != CtfLocation.INVALID_LOCATION.getTimestamp();
74 }
75
76 /**
77 * @since 3.0
78 */
79 @Override
80 public synchronized void setLocation(ITmfLocation location) {
81 fCurLocation = (CtfLocation) location;
82 if (fCurLocation != null) {
83 getIterator().seek(fCurLocation.getLocationInfo());
84 }
85 }
86
87 @Override
88 public void setRank(long rank) {
89 fCurRank = rank;
90
91 }
92
93 @Override
94 public void increaseRank() {
95 if (hasValidRank()) {
96 fCurRank++;
97 }
98 }
99
100 // -------------------------------------------
101 // CtfTmfTrace Helpers
102 // -------------------------------------------
103
104 /**
105 * Gets the trace of this context.
106 *
107 * @return The trace of this context
108 */
109 public CtfTmfTrace getTrace() {
110 return fTrace;
111 }
112
113 /**
114 * Gets the current event. Wrapper to help CtfTmfTrace
115 *
116 * @return The event or null
117 */
118 public synchronized CtfTmfEvent getCurrentEvent() {
119 return getIterator().getCurrentEvent();
120 }
121
122 /**
123 * Advances to a the next event. Wrapper to help CtfTmfTrace
124 *
125 * @return success or not
126 */
127 public synchronized boolean advance() {
128 final CtfLocationInfo curLocationData = fCurLocation.getLocationInfo();
129 CtfIterator iterator = getIterator();
130 boolean retVal = iterator.advance();
131 CtfTmfEvent currentEvent = iterator.getCurrentEvent();
132
133 if (currentEvent != null) {
134 final long timestampValue = iterator.getCurrentTimestamp();
135 if (curLocationData.getTimestamp() == timestampValue) {
136 fCurLocation = new CtfLocation(timestampValue, curLocationData.getIndex() + 1);
137 } else {
138 fCurLocation = new CtfLocation(timestampValue, 0L);
139 }
140 } else {
141 fCurLocation = new CtfLocation(CtfLocation.INVALID_LOCATION);
142 }
143
144 return retVal;
145 }
146
147 @Override
148 public void dispose() {
149 CtfIteratorManager.removeIterator(fTrace, this);
150 }
151
152 /**
153 * Seeks to a given timestamp. Wrapper to help CtfTmfTrace
154 *
155 * @param timestamp
156 * desired timestamp
157 * @return success or not
158 */
159 public synchronized boolean seek(final long timestamp) {
160 fCurLocation = new CtfLocation(timestamp, 0);
161 return getIterator().seek(timestamp);
162 }
163
164 /**
165 * Seeks to a given location. Wrapper to help CtfTmfTrace
166 * @param location
167 * unique location to find the event.
168 *
169 * @return success or not
170 * @since 2.0
171 */
172 public synchronized boolean seek(final CtfLocationInfo location) {
173 fCurLocation = new CtfLocation(location);
174 return getIterator().seek(location);
175 }
176
177 @Override
178 public CtfTmfContext clone() {
179 CtfTmfContext ret = null;
180 try {
181 ret = (CtfTmfContext) super.clone();
182 /* Fields are immutable, no need to deep-copy them */
183 } catch (CloneNotSupportedException e) {
184 /* Should not happen, we're calling Object.clone() */
185 }
186 return ret;
187 }
188
189 // -------------------------------------------
190 // Private helpers
191 // -------------------------------------------
192
193 /**
194 * Get iterator, called every time to get an iterator, no local copy is
195 * stored so that there is no need to "update"
196 *
197 * @return an iterator
198 */
199 private CtfIterator getIterator() {
200 return CtfIteratorManager.getIterator(fTrace, this);
201 }
202 }
This page took 0.052626 seconds and 6 git commands to generate.