Merge remote-tracking branch 'origin/master' into lttng-kepler
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / request / TmfCoalescedDataRequest.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.core.request;
14
15 import java.util.Vector;
16
17 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
18 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
19 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
20
21 /**
22 * The TMF coalesced data request
23 *
24 * @version 1.0
25 * @author Francois Chouinard
26 */
27 public class TmfCoalescedDataRequest extends TmfDataRequest {
28
29 // ------------------------------------------------------------------------
30 // Attributes
31 // ------------------------------------------------------------------------
32
33 /**
34 * The list of coalesced requests
35 */
36 protected Vector<ITmfDataRequest> fRequests = new Vector<ITmfDataRequest>();
37
38 // ------------------------------------------------------------------------
39 // Constructor
40 // ------------------------------------------------------------------------
41
42 /**
43 * Request all the events of a given type (high priority)
44 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
45 *
46 * @param dataType the requested data type
47 */
48 public TmfCoalescedDataRequest(Class<? extends ITmfEvent> dataType) {
49 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
50 }
51
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).
55 *
56 * @param dataType the requested data type
57 * @param priority the requested execution priority
58 */
59 public TmfCoalescedDataRequest(Class<? extends ITmfEvent> dataType, ExecutionType priority) {
60 this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
61 }
62
63 /**
64 * Request all the events of a given type from the given index (high priority)
65 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
66 *
67 * @param dataType the requested data type
68 * @param index the index of the first event to retrieve
69 */
70 public TmfCoalescedDataRequest(Class<? extends ITmfEvent> dataType, long index) {
71 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
72 }
73
74 /**
75 * Request all the events of a given type from the given index (given priority)
76 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
77 *
78 * @param dataType the requested data type
79 * @param index the index of the first event to retrieve
80 * @param priority the requested execution priority
81 */
82 public TmfCoalescedDataRequest(Class<? extends ITmfEvent> dataType, long index, ExecutionType priority) {
83 this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE, priority);
84 }
85
86 /**
87 * Request 'n' events of a given type from the given index (high priority)
88 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
89 *
90 * @param dataType the requested data type
91 * @param index the index of the first event to retrieve
92 * @param nbRequested the number of events requested
93 */
94 public TmfCoalescedDataRequest(Class<? extends ITmfEvent> dataType, long index, int nbRequested) {
95 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, ExecutionType.FOREGROUND);
96 }
97
98 /**
99 * Request 'n' events of a given type from the given index (given priority)
100 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
101 *
102 * @param dataType the requested data type
103 * @param index the index of the first event to retrieve
104 * @param nbRequested the number of events requested
105 * @param priority the requested execution priority
106 */
107 public TmfCoalescedDataRequest(Class<? extends ITmfEvent> dataType, long index, int nbRequested, ExecutionType priority) {
108 this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE, priority);
109 }
110
111 /**
112 * Request 'n' events of a given type from the given index (high priority).
113 * Events are returned in blocks of the given size.
114 *
115 * @param dataType the requested data type
116 * @param index the index of the first event to retrieve
117 * @param nbRequested the number of events requested
118 * @param blockSize the number of events per block
119 */
120 public TmfCoalescedDataRequest(Class<? extends ITmfEvent> dataType, long index, int nbRequested, int blockSize) {
121 super(ITmfEvent.class, index, nbRequested, blockSize, ExecutionType.FOREGROUND);
122 }
123
124 /**
125 * Request 'n' events of a given type from the given index (given priority).
126 * Events are returned in blocks of the given size.
127 *
128 * @param dataType the requested data type
129 * @param index the index of the first event to retrieve
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 TmfCoalescedDataRequest(Class<? extends ITmfEvent> dataType, long index, int nbRequested, int blockSize, ExecutionType priority) {
135 super(ITmfEvent.class, index, nbRequested, blockSize, priority);
136 }
137
138 // ------------------------------------------------------------------------
139 // Management
140 // ------------------------------------------------------------------------
141
142 /**
143 * Add a request to this one.
144 *
145 * @param request The request to add
146 */
147 public void addRequest(ITmfDataRequest request) {
148 fRequests.add(request);
149 merge(request);
150 }
151
152 /**
153 * Check if a request is compatible with the current coalesced one
154 *
155 * @param request
156 * The request to verify
157 * @return If the request is compatible, true or false
158 */
159 public boolean isCompatible(ITmfDataRequest request) {
160 if (request.getExecType() == getExecType()) {
161 return overlaps(request);
162 }
163 return false;
164 }
165
166 private boolean overlaps(ITmfDataRequest request) {
167 long start = request.getIndex();
168 long end = start + request.getNbRequested();
169
170 // Return true if either the start or end index falls within
171 // the coalesced request boundaries
172 return (start <= (fIndex + fNbRequested + 1) && (end >= fIndex - 1));
173 }
174
175 private void merge(ITmfDataRequest request) {
176 long start = request.getIndex();
177 long end = Math.min(start + request.getNbRequested(), TmfDataRequest.ALL_DATA);
178
179 if (start < fIndex) {
180 if (fNbRequested != TmfDataRequest.ALL_DATA) {
181 fNbRequested += (fIndex - start);
182 }
183 fIndex = start;
184 }
185 if ((request.getNbRequested() == TmfDataRequest.ALL_DATA) ||
186 (fNbRequested == TmfDataRequest.ALL_DATA))
187 {
188 fNbRequested = TmfDataRequest.ALL_DATA;
189 } else {
190 fNbRequested = (int) Math.max(end - fIndex, fNbRequested);
191 }
192 }
193
194 /**
195 * @return The list of IDs of the sub-requests
196 */
197 @SuppressWarnings("nls")
198 public String getSubRequestIds() {
199 StringBuffer result = new StringBuffer("[");
200 for (int i = 0; i < fRequests.size(); i++) {
201 if (i != 0) {
202 result.append(", ");
203 }
204 result.append(fRequests.get(i).getRequestId());
205 }
206 result.append("]");
207 return result.toString();
208 }
209
210 // ------------------------------------------------------------------------
211 // ITmfDataRequest
212 // ------------------------------------------------------------------------
213
214 @Override
215 public void handleData(ITmfEvent data) {
216 super.handleData(data);
217 // Don't call sub-requests handleData() unless this is a
218 // TmfCoalescedDataRequest; extended classes should call
219 // the sub-requests handleData().
220 if (getClass() == TmfCoalescedDataRequest.class) {
221 long index = getIndex() + getNbRead() - 1;
222 for (ITmfDataRequest request : fRequests) {
223 if (!request.isCompleted()) {
224 if (request.getDataType().isInstance(data)) {
225 long start = request.getIndex();
226 long end = start + request.getNbRequested();
227 if (index >= start && index < end) {
228 request.handleData(data);
229 }
230 }
231 }
232 }
233 }
234 }
235
236 @Override
237 public void start() {
238 for (ITmfDataRequest request : fRequests) {
239 if (!request.isCompleted()) {
240 request.start();
241 }
242 }
243 super.start();
244 }
245
246 @Override
247 public void done() {
248 for (ITmfDataRequest request : fRequests) {
249 if (!request.isCompleted()) {
250 request.done();
251 }
252 }
253 super.done();
254 }
255
256 @Override
257 public void fail() {
258 for (ITmfDataRequest request : fRequests) {
259 request.fail();
260 }
261 super.fail();
262 }
263
264 @Override
265 public void cancel() {
266 for (ITmfDataRequest request : fRequests) {
267 if (!request.isCompleted()) {
268 request.cancel();
269 }
270 }
271 super.cancel();
272 }
273
274 @Override
275 public synchronized boolean isCompleted() {
276 // Firstly, check if coalescing request is completed
277 if (super.isCompleted()) {
278 return true;
279 }
280
281 // Secondly, check if all sub-requests are finished
282 if (fRequests.size() > 0) {
283 // If all sub requests are completed the coalesced request is
284 // treated as completed, too.
285 for (ITmfDataRequest request : fRequests) {
286 if (!request.isCompleted()) {
287 return false;
288 }
289 }
290 return true;
291 }
292
293 // Coalescing request is not finished if there are no sub-requests
294 return false;
295 }
296
297 @Override
298 public synchronized boolean isCancelled() {
299 // Firstly, check if coalescing request is canceled
300 if (super.isCancelled()) {
301 return true;
302 }
303
304 // Secondly, check if all sub-requests are canceled
305 if (fRequests.size() > 0) {
306 // If all sub requests are canceled the coalesced request is
307 // treated as completed, too.
308 for (ITmfDataRequest request : fRequests) {
309 if (!request.isCancelled()) {
310 return false;
311 }
312 }
313 return true;
314 }
315
316 // Coalescing request is not canceled if there are no sub-requests
317 return false;
318
319 }
320
321
322 // ------------------------------------------------------------------------
323 // Object
324 // ------------------------------------------------------------------------
325
326 @Override
327 // All requests have a unique id
328 public int hashCode() {
329 return super.hashCode();
330 }
331
332 @Override
333 public boolean equals(Object other) {
334 if (other instanceof TmfCoalescedDataRequest) {
335 TmfCoalescedDataRequest request = (TmfCoalescedDataRequest) other;
336 return (request.getDataType() == getDataType()) &&
337 (request.getIndex() == getIndex()) &&
338 (request.getNbRequested() == getNbRequested() &&
339 (request.getExecType() == getExecType()));
340 }
341 return false;
342 }
343
344 @Override
345 @SuppressWarnings("nls")
346 public String toString() {
347 return "[TmfCoalescedDataRequest(" + getRequestId() + "," + getDataType().getSimpleName()
348 + "," + getIndex() + "," + getNbRequested() + "," + getBlockSize() + ")]";
349 }
350 }
This page took 0.038936 seconds and 6 git commands to generate.