1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.request
;
15 import org
.eclipse
.linuxtools
.tmf
.Tracer
;
16 import org
.eclipse
.linuxtools
.tmf
.event
.TmfData
;
19 * <b><u>TmfDataRequest</u></b>
21 * TmfDataRequests are used to obtain blocks of contiguous data from a data
22 * provider. Open ranges can be used, especially for continuous streaming.
24 * The request is processed asynchronously by a TmfProvider and, as blocks
25 * of data become available, handleData() is invoked synchronously for each
26 * block. Upon return, the data instances go out of scope and become eligible
27 * for gc. It is is thus the responsibility of the requester to either clone
28 * or keep a reference to the data it wishes to track specifically.
30 * This data block approach is used to avoid busting the heap for very
31 * large trace files. The block size is configurable.
33 * The TmfProvider indicates that the request is completed by calling done().
34 * The request can be canceled at any time with cancel().
37 *<pre><code><i>TmfTimeWindow range = new TmfTimewindow(...);
38 *TmfDataRequest<DataType[]> request = new TmfDataRequest<DataType[]>(DataType.class, 0, NB_EVENTS, BLOCK_SIZE) {
39 * public void handleData() {
40 * DataType[] data = request.getData();
41 * for (DataType e : data) {
45 * public void handleSuccess() {
49 * public void handleFailure() {
53 * public void handleCancel() {
58 *fProcessor.process(request, true);
61 * TODO: Consider decoupling from "time range", "rank", etc and for the more
62 * generic notion of "criteria". This would allow to extend for "time range", etc
63 * instead of providing specialized constructors. This also means removing the
64 * criteria info from the data structure (with the possible exception of fNbRequested).
65 * The nice thing about it is that it would prepare us well for the coming generation
68 * TODO: Implement request failures (codes, etc...)
70 public abstract class TmfDataRequest
<T
extends TmfData
> implements ITmfDataRequest
<T
> {
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
76 // The default maximum number of events per chunk
77 public static final int DEFAULT_BLOCK_SIZE
= 1000;
79 // The request count for all the events
80 public static final int ALL_DATA
= Integer
.MAX_VALUE
;
82 private static int fRequestNumber
= 0;
84 // ------------------------------------------------------------------------
86 // ------------------------------------------------------------------------
88 private final Class
<T
> fDataType
;
89 private final ExecutionType fExecType
;
90 private final int fRequestId
; // A unique request ID
91 private final int fIndex
; // The index (rank) of the requested event
92 private final int fNbRequested
; // The number of requested events (ALL_DATA for all)
93 private final int fBlockSize
; // The maximum number of events per chunk
94 private int fNbRead
; // The number of reads so far
96 private final Object lock
;
97 private boolean fRequestRunning
= false;
98 private boolean fRequestCompleted
= false;
99 private boolean fRequestFailed
= false;
100 private boolean fRequestCanceled
= false;
102 private T
[] fData
; // Data object
104 // ------------------------------------------------------------------------
106 // ------------------------------------------------------------------------
109 * Resets the request counter (used for testing)
111 public static void reset() {
116 * Default constructor
118 * @param dataType the requested data type
120 public TmfDataRequest(Class
<T
> dataType
) {
121 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.SHORT
);
124 public TmfDataRequest(Class
<T
> dataType
, ExecutionType execType
) {
125 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, execType
);
129 * @param dataType the requested data type
130 * @param nbRequested the number of data items requested
132 public TmfDataRequest(Class
<T
> dataType
, int index
) {
133 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.SHORT
);
136 public TmfDataRequest(Class
<T
> dataType
, int index
, ExecutionType execType
) {
137 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, execType
);
141 * @param dataType the requested data type
142 * @param index the index (rank) of the first event requested
143 * @param blockSize the number of data items per block
145 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
) {
146 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, ExecutionType
.SHORT
);
149 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, ExecutionType execType
) {
150 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, execType
);
154 * @param dataType the requested data type
155 * @param index the index (rank) of the first event requested
156 * @param nbRequested the number of data items requested
157 * @param blockSize the number of data items per block
159 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, int blockSize
) {
160 this(dataType
, index
, nbRequested
, blockSize
, ExecutionType
.SHORT
);
163 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, int blockSize
, ExecutionType execType
) {
164 fRequestId
= fRequestNumber
++;
165 fDataType
= dataType
;
167 fNbRequested
= nbRequested
;
168 fBlockSize
= blockSize
;
169 fExecType
= execType
;
172 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "created");
178 @SuppressWarnings("unused")
179 private TmfDataRequest(TmfDataRequest
<T
> other
) {
180 this(null, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
);
183 // ------------------------------------------------------------------------
185 // ------------------------------------------------------------------------
188 * @return the request ID
190 public int getRequestId() {
195 * @return the index of the first event requested
197 public int getIndex() {
202 * @return the index of the first event requested
204 public ExecutionType
getExecType() {
209 * @return the number of requested events (ALL_DATA = all)
211 public int getNbRequested() {
216 * @return the block size
218 public int getBlockize() {
223 * @return the number of events read so far
225 public synchronized int getNbRead() {
230 * @return indicates if the request is completed
232 public boolean isRunning() {
233 return fRequestRunning
;
237 * @return indicates if the request is completed
239 public boolean isCompleted() {
240 return fRequestCompleted
;
244 * @return indicates if the request is canceled
246 public boolean isFailed() {
247 return fRequestFailed
;
251 * @return indicates if the request is canceled
253 public boolean isCancelled() {
254 return fRequestCanceled
;
258 * @return the requested data type
260 public Class
<T
> getDataType() {
264 // ------------------------------------------------------------------------
266 // ------------------------------------------------------------------------
269 * Sets the data object to specified value. To be called by the
270 * asynchronous method implementor.
271 * @param data Data value to set.
273 public synchronized void setData(T
[] data
) {
274 fNbRead
+= data
.length
;
279 * Returns the data value, null if not set.
281 public synchronized T
[] getData() {
286 * Handle a block of incoming data. This method is called every time
287 * a block of data becomes available.
289 * - Data items are received in the order they appear in the stream.
290 * - Called by the request processor, in its execution thread, every time a
291 * block of data becomes available.
292 * - Request processor performs a synchronous call to handlePartialResult()
293 * i.e. its execution threads holds until handlePartialData() returns.
294 * - Original data items are disposed of on return i.e. keep a reference
295 * (or a copy) if some persistence is needed between invocations.
296 * - When there is no more data, done() is called.
298 * @param events - an array of events
300 public abstract void handleData();
302 public void handleStarted() {
306 * Handle the completion of the request. It is called when there is no more
307 * data available either because:
308 * - the request completed normally
309 * - the request failed
310 * - the request was canceled
312 * As a convenience, handleXXXX methods are provided. They are meant to be
313 * overridden by the application if it needs to handle these conditions.
315 public void handleCompleted() {
316 if (fRequestFailed
) {
317 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "failed");
320 else if (fRequestCanceled
) {
321 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "cancelled");
325 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "succeeded");
330 public void handleSuccess() {
333 public void handleFailure() {
336 public void handleCancel() {
340 * To suspend the client thread until the request completes (or is
343 * @throws InterruptedException
345 public void waitForCompletion() throws InterruptedException
{
346 synchronized (lock
) {
347 while (!fRequestCompleted
)
353 * Called by the request processor upon starting to service the request.
355 public void start() {
356 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "starting");
358 fRequestRunning
= true;
362 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "started");
366 * Called by the request processor upon completion.
369 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(this, "completing");
371 fRequestRunning
= false;
372 fRequestCompleted
= true;
379 * Called by the request processor upon failure.
383 fRequestFailed
= true;
389 * Called by the request processor upon cancellation.
391 public void cancel() {
393 fRequestCanceled
= true;
398 // ------------------------------------------------------------------------
400 // ------------------------------------------------------------------------
403 // All requests have a unique id
404 public int hashCode() {
405 return getRequestId();
409 public boolean equals(Object other
) {
410 if (other
instanceof TmfDataRequest
<?
>) {
411 TmfDataRequest
<?
> request
= (TmfDataRequest
<?
>) other
;
412 return (request
.fDataType
== fDataType
) &&
413 (request
.fIndex
== fIndex
) &&
414 (request
.fNbRequested
== fNbRequested
);
420 public String
toString() {
421 return "[TmfDataRequest(" + fRequestId
+ "," + fDataType
.getSimpleName()
422 + "," + fIndex
+ "," + fNbRequested
+ "," + fBlockSize
+ ")]";
This page took 0.040863 seconds and 5 git commands to generate.