1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 * Alexandre Montplaisir - Merge with TmfCoalescedDataRequest
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.request
;
16 import java
.util
.ArrayList
;
17 import java
.util
.List
;
19 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.TmfCoreTracer
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
27 * The TMF coalesced event request
29 * @author Francois Chouinard
32 public class TmfCoalescedEventRequest
extends TmfEventRequest
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 /** The list of coalesced requests */
39 private final List
<ITmfEventRequest
> fRequests
= new ArrayList
<>();
42 * We do not use super.fRange, because in the case of coalesced requests,
43 * the global range can be modified as sub-request are added.
45 private TmfTimeRange fRange
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 * Request 'n' events of a given type for the given time range (given
53 * priority). Events are returned in blocks of the given size.
56 * The requested data type
58 * The range of the request. You can use
59 * {@link TmfTimeRange#ETERNITY} to request all events.
61 * The index of the first event to retrieve. Use '0' to start at
64 * The number of events requested. You can use
65 * {@link TmfEventRequest#ALL_DATA} to request all events.
67 * The requested execution priority
69 public TmfCoalescedEventRequest(Class
<?
extends ITmfEvent
> dataType
,
73 ExecutionType priority
) {
74 super(ITmfEvent
.class, null, index
, nbRequested
, priority
);
77 if (TmfCoreTracer
.isRequestTraced()) {
78 String type
= getClass().getName();
79 type
= type
.substring(type
.lastIndexOf('.') + 1);
80 @SuppressWarnings("nls")
81 String message
= "CREATED "
82 + (getExecType() == ITmfEventRequest
.ExecutionType
.BACKGROUND ?
"(BG)" : "(FG)")
83 + " Type=" + type
+ " Index=" + getIndex() + " NbReq=" + getNbRequested()
84 + " Range=" + getRange()
85 + " DataType=" + getDataType().getSimpleName();
86 TmfCoreTracer
.traceRequest(this, message
);
91 public TmfTimeRange
getRange() {
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
100 * Add a request to this one.
105 public void addRequest(ITmfEventRequest request
) {
106 fRequests
.add(request
);
111 * Check if a request is compatible with the current coalesced one
114 * The request to verify
115 * @return If the request is compatible, true or false
117 public boolean isCompatible(ITmfEventRequest request
) {
118 if (request
.getExecType() == getExecType() &&
119 ranksOverlap(request
) &&
120 timeRangesOverlap(request
)) {
126 private boolean ranksOverlap(ITmfEventRequest request
) {
127 long start
= request
.getIndex();
128 long end
= start
+ request
.getNbRequested();
130 // Return true if either the start or end index falls within
131 // the coalesced request boundaries
132 return (start
<= (fIndex
+ fNbRequested
+ 1) && (end
>= fIndex
- 1));
135 private boolean timeRangesOverlap(ITmfEventRequest request
) {
136 ITmfTimestamp startTime
= request
.getRange().getStartTime();
137 ITmfTimestamp endTime
= request
.getRange().getEndTime();
138 return (startTime
.compareTo(endTime
) <= 0) &&
139 (fRange
.getStartTime().compareTo(fRange
.getEndTime()) <= 0);
142 private void merge(ITmfEventRequest request
) {
143 long start
= request
.getIndex();
144 long end
= Math
.min(start
+ request
.getNbRequested(), ITmfEventRequest
.ALL_DATA
);
146 if (start
< fIndex
) {
147 if (fNbRequested
!= ITmfEventRequest
.ALL_DATA
) {
148 fNbRequested
+= (fIndex
- start
);
152 if ((request
.getNbRequested() == ITmfEventRequest
.ALL_DATA
) ||
153 (fNbRequested
== ITmfEventRequest
.ALL_DATA
)) {
154 fNbRequested
= ITmfEventRequest
.ALL_DATA
;
156 fNbRequested
= (int) Math
.max(end
- fIndex
, fNbRequested
);
159 ITmfTimestamp startTime
= request
.getRange().getStartTime();
160 ITmfTimestamp endTime
= request
.getRange().getEndTime();
161 if (!fRange
.contains(startTime
) && fRange
.getStartTime().compareTo(startTime
) > 0) {
162 fRange
= new TmfTimeRange(startTime
, fRange
.getEndTime());
164 if (!fRange
.contains(endTime
) && fRange
.getEndTime().compareTo(endTime
) < 0) {
165 fRange
= new TmfTimeRange(fRange
.getStartTime(), endTime
);
170 * @return The list of IDs of the sub-requests
172 @SuppressWarnings("nls")
173 public String
getSubRequestIds() {
174 StringBuffer result
= new StringBuffer("[");
175 for (int i
= 0; i
< fRequests
.size(); i
++) {
179 result
.append(fRequests
.get(i
).getRequestId());
182 return result
.toString();
185 // ------------------------------------------------------------------------
187 // ------------------------------------------------------------------------
190 public void handleData(ITmfEvent data
) {
191 super.handleData(data
);
192 long index
= getIndex() + getNbRead() - 1;
193 for (ITmfEventRequest request
: fRequests
) {
195 request
.handleData(null);
197 long start
= request
.getIndex();
198 if (!request
.isCompleted() && index
>= start
&& request
.getNbRead() < request
.getNbRequested()) {
199 ITmfTimestamp ts
= data
.getTimestamp();
200 if (request
.getRange().contains(ts
)) {
201 if (request
.getDataType().isInstance(data
)) {
202 request
.handleData(data
);
211 public void start() {
212 for (ITmfEventRequest request
: fRequests
) {
213 if (!request
.isCompleted()) {
222 for (ITmfEventRequest request
: fRequests
) {
223 if (!request
.isCompleted()) {
232 for (ITmfEventRequest request
: fRequests
) {
239 public void cancel() {
240 for (ITmfEventRequest request
: fRequests
) {
241 if (!request
.isCompleted()) {
249 public synchronized boolean isCompleted() {
250 // Firstly, check if coalescing request is completed
251 if (super.isCompleted()) {
255 // Secondly, check if all sub-requests are finished
256 if (fRequests
.size() > 0) {
257 // If all sub requests are completed the coalesced request is
258 // treated as completed, too.
259 for (ITmfEventRequest request
: fRequests
) {
260 if (!request
.isCompleted()) {
267 // Coalescing request is not finished if there are no sub-requests
272 public synchronized boolean isCancelled() {
273 // Firstly, check if coalescing request is canceled
274 if (super.isCancelled()) {
278 // Secondly, check if all sub-requests are canceled
279 if (fRequests
.size() > 0) {
280 // If all sub requests are canceled the coalesced request is
281 // treated as completed, too.
282 for (ITmfEventRequest request
: fRequests
) {
283 if (!request
.isCancelled()) {
290 // Coalescing request is not canceled if there are no sub-requests
295 // ------------------------------------------------------------------------
297 // ------------------------------------------------------------------------
300 // All requests have a unique id
301 public int hashCode() {
302 return super.hashCode();
306 public boolean equals(Object other
) {
307 if (other
instanceof TmfCoalescedEventRequest
) {
308 TmfCoalescedEventRequest request
= (TmfCoalescedEventRequest
) other
;
309 return (request
.getDataType() == getDataType()) &&
310 (request
.getIndex() == getIndex()) &&
311 (request
.getNbRequested() == getNbRequested()) &&
312 (request
.getRange().equals(fRange
));
318 @SuppressWarnings("nls")
319 public String
toString() {
320 return "[TmfCoalescedEventRequest(" + getRequestId() + "," + getDataType().getSimpleName()
321 + "," + getExecType() + "," + getRange() + "," + getIndex() + "," + getNbRequested()
322 + ", " + fRequests
.toString() + ")]";