Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
e31e01e8 | 2 | * Copyright (c) 2009, 2010 Ericsson |
8c8bf09f ASL |
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.tmf.request; | |
14 | ||
e31e01e8 | 15 | import org.eclipse.linuxtools.tmf.event.TmfData; |
8c8bf09f ASL |
16 | |
17 | /** | |
18 | * <b><u>TmfDataRequest</u></b> | |
19 | * <p> | |
20 | * TmfDataRequests are used to obtain blocks of contiguous data from a data | |
e31e01e8 | 21 | * provider. Open ranges can be used, especially for continuous streaming. |
8c8bf09f | 22 | * <p> |
e31e01e8 FC |
23 | * The request is processed asynchronously by a TmfProvider and, as blocks |
24 | * of data become available, handleData() is invoked synchronously for each | |
25 | * block. Upon return, the data instances go out of scope and become eligible | |
26 | * for gc. It is is thus the responsibility of the requester to either clone | |
27 | * or keep a reference to the data it wishes to track specifically. | |
8c8bf09f | 28 | * <p> |
e31e01e8 FC |
29 | * This data block approach is used to avoid busting the heap for very |
30 | * large trace files. The block size is configurable. | |
8c8bf09f | 31 | * <p> |
e31e01e8 FC |
32 | * The TmfProvider indicates that the request is completed by calling done(). |
33 | * The request can be canceled at any time with cancel(). | |
8c8bf09f ASL |
34 | * <p> |
35 | * Typical usage: | |
36 | *<pre><code><i>TmfTimeWindow range = new TmfTimewindow(...); | |
e31e01e8 | 37 | *TmfDataRequest<DataType[]> request = new TmfDataRequest<DataType[]>(DataType.class, 0, NB_EVENTS, BLOCK_SIZE) { |
0ab46cd3 | 38 | * public void handleData() { |
8c8bf09f ASL |
39 | * DataType[] data = request.getData(); |
40 | * for (DataType e : data) { | |
41 | * // do something | |
42 | * } | |
43 | * } | |
0ab46cd3 FC |
44 | * public void handleSuccess() { |
45 | * // do something | |
46 | * } | |
47 | * } | |
48 | * public void handleFailure() { | |
49 | * // do something | |
50 | * } | |
51 | * } | |
52 | * public void handleCancel() { | |
53 | * // do something | |
54 | * } | |
55 | * } | |
8c8bf09f ASL |
56 | *}; |
57 | *fProcessor.process(request, true); | |
58 | *</i></code></pre> | |
59 | * | |
e31e01e8 FC |
60 | * TODO: Consider decoupling from "time range", "rank", etc and for the more |
61 | * generic notion of "criteria". This would allow to extend for "time range", etc | |
62 | * instead of providing specialized constructors. This also means removing the | |
63 | * criteria info from the data structure (with the possible exception of fNbRequested). | |
64 | * The nice thing about it is that it would prepare us well for the coming generation | |
65 | * of analysis tools. | |
0ab46cd3 FC |
66 | * |
67 | * TODO: Implement request failures (codes, etc...) | |
8c8bf09f | 68 | */ |
e31e01e8 | 69 | public class TmfDataRequest<T extends TmfData> { |
8c8bf09f | 70 | |
e31e01e8 | 71 | // ------------------------------------------------------------------------ |
8c8bf09f | 72 | // Constants |
e31e01e8 | 73 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
74 | |
75 | // The default maximum number of events per chunk | |
76 | public static final int DEFAULT_BLOCK_SIZE = 1000; | |
77 | ||
78 | // The request count for all the events | |
e31e01e8 | 79 | public static final int ALL_DATA = Integer.MAX_VALUE; |
8c8bf09f | 80 | |
e31e01e8 | 81 | // ------------------------------------------------------------------------ |
8c8bf09f | 82 | // Attributes |
e31e01e8 | 83 | // ------------------------------------------------------------------------ |
8c8bf09f | 84 | |
e31e01e8 FC |
85 | private final Class<? extends TmfData> fDataType; |
86 | private final int fIndex; // The index (rank) of the requested event | |
87 | private final int fNbRequested; // The number of requested events (ALL_DATA for all) | |
88 | private final int fBlockSize; // The maximum number of events per chunk | |
89 | private int fNbRead; // The number of reads so far | |
8c8bf09f | 90 | |
e31e01e8 | 91 | private Object lock = new Object(); |
9aae0442 | 92 | private boolean fRequestCompleted = false; |
0ab46cd3 | 93 | private boolean fRequestFailed = false; |
9aae0442 | 94 | private boolean fRequestCanceled = false; |
8c8bf09f | 95 | |
e31e01e8 | 96 | private T[] fData; // Data object |
8c8bf09f | 97 | |
e31e01e8 | 98 | // ------------------------------------------------------------------------ |
8c8bf09f | 99 | // Constructors |
e31e01e8 | 100 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
101 | |
102 | /** | |
e31e01e8 | 103 | * Default constructor |
8c8bf09f | 104 | */ |
e31e01e8 FC |
105 | public TmfDataRequest(Class<? extends TmfData> dataType) { |
106 | this(dataType, 0, ALL_DATA, DEFAULT_BLOCK_SIZE); | |
8c8bf09f ASL |
107 | } |
108 | ||
109 | /** | |
e31e01e8 | 110 | * @param nbRequested |
8c8bf09f | 111 | */ |
e31e01e8 FC |
112 | public TmfDataRequest(Class<? extends TmfData> dataType, int index) { |
113 | this(dataType, index, ALL_DATA, DEFAULT_BLOCK_SIZE); | |
8c8bf09f ASL |
114 | } |
115 | ||
116 | /** | |
e31e01e8 FC |
117 | * @param index |
118 | * @param nbRequested | |
8c8bf09f | 119 | */ |
e31e01e8 FC |
120 | public TmfDataRequest(Class<? extends TmfData> dataType, int index, int nbRequested) { |
121 | this(dataType, index, nbRequested, DEFAULT_BLOCK_SIZE); | |
8c8bf09f ASL |
122 | } |
123 | ||
124 | /** | |
165c977c | 125 | * @param index |
e31e01e8 FC |
126 | * @param nbRequested |
127 | * @param blockSize | |
8c8bf09f | 128 | */ |
e31e01e8 FC |
129 | public TmfDataRequest(Class<? extends TmfData> dataType, int index, int nbRequested, int blockSize) { |
130 | fDataType = dataType; | |
131 | fIndex = index; | |
132 | fNbRequested = nbRequested; | |
133 | fBlockSize = blockSize; | |
134 | fNbRead = 0; | |
8c8bf09f | 135 | } |
e31e01e8 FC |
136 | |
137 | // ------------------------------------------------------------------------ | |
165c977c | 138 | // Accessors |
e31e01e8 | 139 | // ------------------------------------------------------------------------ |
8c8bf09f | 140 | |
28b94d61 FC |
141 | /** |
142 | * @return the index | |
143 | */ | |
144 | public int getIndex() { | |
145 | return fIndex; | |
146 | } | |
147 | ||
8c8bf09f | 148 | /** |
e31e01e8 | 149 | * @return the number of requested events (ALL_DATA = all) |
8c8bf09f | 150 | */ |
e31e01e8 FC |
151 | public int getNbRequested() { |
152 | return fNbRequested; | |
8c8bf09f ASL |
153 | } |
154 | ||
9aae0442 | 155 | /** |
165c977c | 156 | * @return the block size |
9aae0442 | 157 | */ |
165c977c FC |
158 | public int getBlockize() { |
159 | return fBlockSize; | |
9aae0442 ASL |
160 | } |
161 | ||
660c9e60 FC |
162 | /** |
163 | * @return the number of events read so far | |
164 | */ | |
e31e01e8 FC |
165 | public int getNbRead() { |
166 | return fNbRead; | |
660c9e60 FC |
167 | } |
168 | ||
8c8bf09f ASL |
169 | /** |
170 | * @return indicates if the request is completed | |
171 | */ | |
172 | public boolean isCompleted() { | |
173 | return fRequestCompleted; | |
174 | } | |
175 | ||
0ab46cd3 FC |
176 | /** |
177 | * @return indicates if the request is canceled | |
178 | */ | |
179 | public boolean isFailed() { | |
180 | return fRequestFailed; | |
181 | } | |
182 | ||
8c8bf09f ASL |
183 | /** |
184 | * @return indicates if the request is canceled | |
185 | */ | |
186 | public boolean isCancelled() { | |
187 | return fRequestCanceled; | |
188 | } | |
189 | ||
e31e01e8 FC |
190 | /** |
191 | * @return the requested data type | |
192 | */ | |
193 | public Class<?> getDataType() { | |
194 | return fDataType; | |
195 | } | |
196 | ||
197 | // ------------------------------------------------------------------------ | |
8c8bf09f | 198 | // Operators |
e31e01e8 | 199 | // ------------------------------------------------------------------------ |
8c8bf09f ASL |
200 | |
201 | /** | |
660c9e60 | 202 | * Sets the data object to specified value. To be called by the |
8c8bf09f ASL |
203 | * asynchronous method implementor. |
204 | * @param data Data value to set. | |
205 | */ | |
e31e01e8 FC |
206 | public synchronized void setData(T[] data) { |
207 | fNbRead += data.length; | |
8c8bf09f ASL |
208 | fData = data; |
209 | } | |
210 | ||
211 | /** | |
212 | * Returns the data value, null if not set. | |
213 | */ | |
e31e01e8 | 214 | public synchronized T[] getData() { |
8c8bf09f ASL |
215 | return fData; |
216 | } | |
217 | ||
218 | /** | |
0ab46cd3 FC |
219 | * Handle a block of incoming data. This method is called every time |
220 | * a block of data becomes available. | |
8c8bf09f ASL |
221 | * |
222 | * - Data items are received in the order they appear in the stream. | |
223 | * - Called by the request processor, in its execution thread, every time a | |
224 | * block of data becomes available. | |
225 | * - Request processor performs a synchronous call to handlePartialResult() | |
226 | * i.e. its execution threads holds until handlePartialData() returns. | |
227 | * - Original data items are disposed of on return i.e. keep a reference | |
228 | * (or a copy) if some persistence is needed between invocations. | |
229 | * - When there is no more data, done() is called. | |
230 | * | |
231 | * @param events - an array of events | |
232 | */ | |
0ab46cd3 | 233 | public void handleData() { |
9aae0442 ASL |
234 | } |
235 | ||
0ab46cd3 FC |
236 | /** |
237 | * Handle the completion of the request. It is called when there is no more | |
238 | * data available either because: | |
239 | * - the request completed normally | |
240 | * - the request failed | |
241 | * - the request was canceled | |
242 | * | |
243 | * As a convenience, handleXXXX methods are provided. They are meant to be | |
244 | * overridden by the application if it needs to handle these conditions. | |
245 | */ | |
8c8bf09f | 246 | public void handleCompleted() { |
0ab46cd3 FC |
247 | if (fRequestFailed) { |
248 | handleFailure(); | |
249 | } | |
250 | else if (fRequestCanceled) { | |
251 | handleCancel(); | |
252 | } | |
37c8b509 FC |
253 | else { |
254 | handleSuccess(); | |
255 | } | |
0ab46cd3 FC |
256 | } |
257 | ||
258 | public void handleSuccess() { | |
259 | } | |
260 | ||
261 | public void handleFailure() { | |
262 | } | |
263 | ||
264 | public void handleCancel() { | |
8c8bf09f ASL |
265 | } |
266 | ||
267 | /** | |
268 | * To suspend the client thread until the request completes (or is | |
269 | * canceled). | |
270 | * | |
271 | * @throws InterruptedException | |
272 | */ | |
165c977c | 273 | public void waitForCompletion() { |
8c8bf09f ASL |
274 | synchronized (lock) { |
275 | while (!fRequestCompleted) | |
165c977c FC |
276 | try { |
277 | lock.wait(); | |
278 | } catch (InterruptedException e) { | |
279 | e.printStackTrace(); | |
280 | } | |
9aae0442 | 281 | } |
9aae0442 ASL |
282 | } |
283 | ||
8c8bf09f | 284 | /** |
0ab46cd3 | 285 | * Called by the request processor upon completion. |
8c8bf09f ASL |
286 | */ |
287 | public void done() { | |
288 | synchronized(lock) { | |
289 | fRequestCompleted = true; | |
290 | lock.notify(); | |
291 | } | |
292 | handleCompleted(); | |
293 | } | |
294 | ||
295 | /** | |
0ab46cd3 FC |
296 | * Called by the request processor upon failure. |
297 | */ | |
298 | public void fail() { | |
299 | synchronized(lock) { | |
300 | fRequestFailed = true; | |
301 | done(); | |
302 | } | |
303 | } | |
304 | ||
305 | /** | |
306 | * Called by the request processor upon cancellation. | |
8c8bf09f ASL |
307 | */ |
308 | public void cancel() { | |
309 | synchronized(lock) { | |
310 | fRequestCanceled = true; | |
0ab46cd3 | 311 | done(); |
8c8bf09f ASL |
312 | } |
313 | } | |
314 | ||
8c8bf09f | 315 | } |