Fix for bug 385432: Missed events in coalesced requests.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / request / TmfCoalescedEventRequest.java
CommitLineData
8c8bf09f
ASL
1/*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
9b749023 3 *
8c8bf09f
ASL
4 * All rights reserved. This program and the accompanying materials are
5 * made 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
9b749023 8 *
8c8bf09f
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
8fd82db5 13package org.eclipse.linuxtools.internal.tmf.core.request;
8c8bf09f 14
4cf201de 15import org.eclipse.linuxtools.internal.tmf.core.Tracer;
72f1e62a 16import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
4df4581d 17import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
6c13869b 18import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
8fd82db5
FC
19import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
20import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
21import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
22import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
8c8bf09f
ASL
23
24/**
8fd82db5 25 * The TMF coalesced event request
9b749023 26 *
8fd82db5
FC
27 * @version 1.0
28 * @author Francois Chouinard
8c8bf09f 29 */
72f1e62a 30public class TmfCoalescedEventRequest<T extends ITmfEvent> extends TmfCoalescedDataRequest<T> implements ITmfEventRequest<T> {
8c8bf09f
ASL
31
32 // ------------------------------------------------------------------------
33 // Attributes
34 // ------------------------------------------------------------------------
35
550d787e 36 private TmfTimeRange fRange; // The requested events time range
8c8bf09f
ASL
37
38 // ------------------------------------------------------------------------
39 // Constructor
40 // ------------------------------------------------------------------------
41
42 /**
0d9a6d76
FC
43 * Request all the events of a given type (high priority)
44 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
9b749023 45 *
0d9a6d76 46 * @param dataType the requested data type
8c8bf09f
ASL
47 */
48 public TmfCoalescedEventRequest(Class<T> dataType) {
a4115405 49 this(dataType, TmfTimeRange.ETERNITY, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
cb866e08
FC
50 }
51
0d9a6d76
FC
52 /**
53 * Request all the events of a given type (given priority)
54 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
9b749023 55 *
0d9a6d76
FC
56 * @param dataType the requested data type
57 * @param priority the requested execution priority
58 */
59 public TmfCoalescedEventRequest(Class<T> dataType, ExecutionType priority) {
60 this(dataType, TmfTimeRange.ETERNITY, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
8c8bf09f
ASL
61 }
62
63 /**
0d9a6d76
FC
64 * Request all the events of a given type for the given time range (high priority)
65 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
9b749023 66 *
0d9a6d76
FC
67 * @param dataType the requested data type
68 * @param range the time range of the requested events
8c8bf09f
ASL
69 */
70 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range) {
f6b14ce2 71 this(dataType, range, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
cb866e08
FC
72 }
73
0d9a6d76
FC
74 /**
75 * Request all the events of a given type for the given time range (given priority)
76 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
9b749023 77 *
0d9a6d76
FC
78 * @param dataType the requested data type
79 * @param range the time range of the requested events
80 * @param priority the requested execution priority
81 */
82 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, ExecutionType priority) {
83 this(dataType, range, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
8c8bf09f
ASL
84 }
85
86 /**
0d9a6d76
FC
87 * Request 'n' events of a given type from the given time range (high priority)
88 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
9b749023 89 *
0d9a6d76
FC
90 * @param dataType the requested data type
91 * @param range the time range of the requested events
92 * @param nbRequested the number of events requested
8c8bf09f
ASL
93 */
94 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested) {
f6b14ce2 95 this(dataType, range, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
cb866e08 96 }
0d9a6d76
FC
97
98 /**
99 * Request 'n' events of a given type for the given time range (given priority)
100 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
9b749023 101 *
0d9a6d76
FC
102 * @param dataType the requested data type
103 * @param range the time range of the requested events
104 * @param nbRequested the number of events requested
105 * @param priority the requested execution priority
106 */
107 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested, ExecutionType priority) {
108 this(dataType, range, nbRequested, DEFAULT_BLOCK_SIZE, priority);
8c8bf09f 109 }
0d9a6d76 110
8c8bf09f 111 /**
0d9a6d76
FC
112 * Request 'n' events of a given type for the given time range (high priority).
113 * Events are returned in blocks of the given size.
9b749023 114 *
0d9a6d76
FC
115 * @param dataType the requested data type
116 * @param range the time range of the requested events
117 * @param nbRequested the number of events requested
118 * @param blockSize the number of events per block
8c8bf09f
ASL
119 */
120 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested, int blockSize) {
4cf201de 121 this(dataType, range, 0, nbRequested, blockSize, ExecutionType.FOREGROUND);
cb866e08
FC
122 }
123
0d9a6d76
FC
124 /**
125 * Request 'n' events of a given type for the given time range (given priority).
126 * Events are returned in blocks of the given size.
9b749023 127 *
0d9a6d76
FC
128 * @param dataType the requested data type
129 * @param range the time range of the requested events
130 * @param nbRequested the number of events requested
131 * @param blockSize the number of events per block
132 * @param priority the requested execution priority
133 */
134 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, int nbRequested, int blockSize, ExecutionType priority) {
4cf201de 135 this(dataType, range, 0, nbRequested, blockSize, priority);
8c8bf09f
ASL
136 }
137
0d9a6d76
FC
138 /**
139 * Request 'n' events of a given type for the given time range (given priority).
140 * Events are returned in blocks of the given size.
9b749023 141 *
0d9a6d76
FC
142 * @param dataType the requested data type
143 * @param range the time range of the requested events
144 * @param index the index of the first event to retrieve
145 * @param nbRequested the number of events requested
146 * @param blockSize the number of events per block
147 * @param priority the requested execution priority
148 */
9e0640dc 149 public TmfCoalescedEventRequest(Class<T> dataType, TmfTimeRange range, long index, int nbRequested, int blockSize, ExecutionType priority) {
0d9a6d76
FC
150 super(dataType, index, nbRequested, blockSize, priority);
151 fRange = range;
4cf201de
FC
152
153 if (Tracer.isRequestTraced()) {
154 String type = getClass().getName();
155 type = type.substring(type.lastIndexOf('.') + 1);
156 @SuppressWarnings("nls")
9b749023
AM
157 String message = "CREATED "
158 + (getExecType() == ITmfDataRequest.ExecutionType.BACKGROUND ? "(BG)" : "(FG)")
159 + " Type=" + type + " Index=" + getIndex() + " NbReq=" + getNbRequested()
4cf201de
FC
160 + " Range=" + getRange()
161 + " DataType=" + getDataType().getSimpleName();
162 Tracer.traceRequest(this, message);
163 }
a79913eb
FC
164 }
165
8c8bf09f
ASL
166 // ------------------------------------------------------------------------
167 // Management
168 // ------------------------------------------------------------------------
169
923b8517
FC
170 @Override
171 public void addRequest(ITmfDataRequest<T> request) {
172 super.addRequest(request);
173 if (request instanceof ITmfEventRequest<?>) {
174 merge((ITmfEventRequest<T>) request);
175 }
176 }
177
8c8bf09f 178 @Override
2fb2eb37 179 public boolean isCompatible(ITmfDataRequest<T> request) {
4cf201de 180 if (request instanceof ITmfEventRequest<?>) {
923b8517
FC
181 if (super.isCompatible(request)) {
182 return overlaps((ITmfEventRequest<T>) request);
4cf201de 183 }
4cf201de
FC
184 }
185 return false;
8c8bf09f
ASL
186 }
187
923b8517 188 private boolean overlaps(ITmfEventRequest<T> request) {
9b749023
AM
189 ITmfTimestamp startTime = request.getRange().getStartTime();
190 ITmfTimestamp endTime = request.getRange().getEndTime();
923b8517
FC
191 return (startTime.compareTo(endTime) <= 0) && (fRange.getStartTime().compareTo(fRange.getEndTime()) <= 0);
192 }
193
194 private void merge(ITmfEventRequest<T> request) {
9b749023
AM
195 ITmfTimestamp startTime = request.getRange().getStartTime();
196 ITmfTimestamp endTime = request.getRange().getEndTime();
923b8517
FC
197 if (!fRange.contains(startTime) && fRange.getStartTime().compareTo(startTime) > 0) {
198 fRange = new TmfTimeRange(startTime, fRange.getEndTime());
199 }
200 if (!fRange.contains(endTime) && fRange.getEndTime().compareTo(endTime) < 0) {
201 fRange = new TmfTimeRange(fRange.getStartTime(), endTime);
202 }
203 }
204
550d787e
FC
205 // ------------------------------------------------------------------------
206 // ITmfDataRequest
207 // ------------------------------------------------------------------------
208
209 @Override
1b70b6dc
PT
210 public void handleData(T data) {
211 super.handleData(data);
212 for (ITmfDataRequest<T> request : fRequests) {
213 if (data == null) {
214 request.handleData(null);
215 } else {
216 if (request instanceof TmfEventRequest<?>) {
217 TmfEventRequest<T> req = (TmfEventRequest<T>) request;
ad4f74f5 218 if (!req.isCompleted() && (getIndex() + getNbRead() > request.getIndex())) {
dfee01ae 219 ITmfTimestamp ts = data.getTimestamp();
1b70b6dc
PT
220 if (req.getRange().contains(ts)) {
221 if (req.getDataType().isInstance(data)) {
222 req.handleData(data);
223 }
224 }
225 }
226 }
227 else {
228 TmfDataRequest<T> req = (TmfDataRequest<T>) request;
229 if (!req.isCompleted()) {
230 if (req.getDataType().isInstance(data)) {
231 req.handleData(data);
232 }
233 }
234 }
235 }
236 }
550d787e
FC
237 }
238
8c8bf09f
ASL
239 // ------------------------------------------------------------------------
240 // ITmfEventRequest
241 // ------------------------------------------------------------------------
242
d4011df2 243 @Override
8c8bf09f
ASL
244 public TmfTimeRange getRange() {
245 return fRange;
246 }
9b749023 247
a79913eb
FC
248 @Override
249 public void setStartIndex(int index) {
250 setIndex(index);
251 }
9b749023 252
2fb2eb37
FC
253 // ------------------------------------------------------------------------
254 // Object
255 // ------------------------------------------------------------------------
256
257 @Override
258 // All requests have a unique id
259 public int hashCode() {
260 return super.hashCode();
261 }
262
263 @Override
264 public boolean equals(Object other) {
265 if (other instanceof TmfCoalescedEventRequest<?>) {
266 TmfCoalescedEventRequest<?> request = (TmfCoalescedEventRequest<?>) other;
267 return (request.getDataType() == getDataType()) &&
268 (request.getIndex() == getIndex()) &&
269 (request.getNbRequested() == getNbRequested()) &&
270 (request.getRange().equals(getRange()));
271 }
272 if (other instanceof TmfCoalescedDataRequest<?>) {
273 return super.equals(other);
274 }
275 return false;
276 }
277
278 @Override
3b38ea61 279 @SuppressWarnings("nls")
2fb2eb37 280 public String toString() {
9b749023 281 return "[TmfCoalescedEventRequest(" + getRequestId() + "," + getDataType().getSimpleName()
a79913eb 282 + "," + getRange() + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
2fb2eb37
FC
283 }
284
8c8bf09f 285}
This page took 0.053556 seconds and 5 git commands to generate.