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