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 int fRequestId
; // A unique request ID
90 private final int fIndex
; // The index (rank) of the requested event
91 private final int fNbRequested
; // The number of requested events (ALL_DATA for all)
92 private final int fBlockSize
; // The maximum number of events per chunk
93 private int fNbRead
; // The number of reads so far
95 private final Object lock
;
96 private boolean fRequestCompleted
;
97 private boolean fRequestFailed
;
98 private boolean fRequestCanceled
;
100 private T
[] fData
; // Data object
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
107 * Resets the request counter (used for testing)
109 public static void reset() {
114 * Default constructor
116 * @param dataType the requested data type
118 public TmfDataRequest(Class
<T
> dataType
) {
119 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
);
123 * @param dataType the requested data type
124 * @param nbRequested the number of data items requested
126 public TmfDataRequest(Class
<T
> dataType
, int index
) {
127 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
);
131 * @param dataType the requested data type
132 * @param index the index (rank) of the first event requested
133 * @param blockSize the number of data items per block
135 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
) {
136 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
);
140 * @param dataType the requested data type
141 * @param index the index (rank) of the first event requested
142 * @param nbRequested the number of data items requested
143 * @param blockSize the number of data items per block
145 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, int blockSize
) {
146 fRequestId
= fRequestNumber
++;
147 fDataType
= dataType
;
149 fNbRequested
= nbRequested
;
150 fBlockSize
= blockSize
;
154 Tracer
.trace("Request #" + fRequestId
+ " (" + getClass().getName() + ", " + fDataType
.getName() + ") created");
160 @SuppressWarnings("unused")
161 private TmfDataRequest(TmfDataRequest
<T
> other
) {
171 // ------------------------------------------------------------------------
173 // ------------------------------------------------------------------------
176 * @return the request ID
178 public int getRequestId() {
183 * @return the index of the first event requested
185 public int getIndex() {
190 * @return the number of requested events (ALL_DATA = all)
192 public int getNbRequested() {
197 * @return the block size
199 public int getBlockize() {
204 * @return the number of events read so far
206 public synchronized int getNbRead() {
211 * @return indicates if the request is completed
213 public boolean isCompleted() {
214 return fRequestCompleted
;
218 * @return indicates if the request is canceled
220 public boolean isFailed() {
221 return fRequestFailed
;
225 * @return indicates if the request is canceled
227 public boolean isCancelled() {
228 return fRequestCanceled
;
232 * @return the requested data type
234 public Class
<T
> getDataType() {
238 // ------------------------------------------------------------------------
240 // ------------------------------------------------------------------------
243 * Sets the data object to specified value. To be called by the
244 * asynchronous method implementor.
245 * @param data Data value to set.
247 public synchronized void setData(T
[] data
) {
248 fNbRead
+= data
.length
;
253 * Returns the data value, null if not set.
255 public synchronized T
[] getData() {
260 * Handle a block of incoming data. This method is called every time
261 * a block of data becomes available.
263 * - Data items are received in the order they appear in the stream.
264 * - Called by the request processor, in its execution thread, every time a
265 * block of data becomes available.
266 * - Request processor performs a synchronous call to handlePartialResult()
267 * i.e. its execution threads holds until handlePartialData() returns.
268 * - Original data items are disposed of on return i.e. keep a reference
269 * (or a copy) if some persistence is needed between invocations.
270 * - When there is no more data, done() is called.
272 * @param events - an array of events
274 public abstract void handleData();
277 * Handle the completion of the request. It is called when there is no more
278 * data available either because:
279 * - the request completed normally
280 * - the request failed
281 * - the request was canceled
283 * As a convenience, handleXXXX methods are provided. They are meant to be
284 * overridden by the application if it needs to handle these conditions.
286 public void handleCompleted() {
287 if (fRequestFailed
) {
288 Tracer
.trace("Request #" + fRequestId
+ " failed, completed");
291 else if (fRequestCanceled
) {
292 Tracer
.trace("Request #" + fRequestId
+ " cancelled, completed");
296 Tracer
.trace("Request #" + fRequestId
+ " succeeded, completed");
301 public void handleSuccess() {
304 public void handleFailure() {
307 public void handleCancel() {
311 * To suspend the client thread until the request completes (or is
314 * @throws InterruptedException
316 public void waitForCompletion() throws InterruptedException
{
317 synchronized (lock
) {
318 while (!fRequestCompleted
)
324 * Called by the request processor upon completion.
328 fRequestCompleted
= true;
335 * Called by the request processor upon failure.
339 fRequestFailed
= true;
345 * Called by the request processor upon cancellation.
347 public void cancel() {
349 fRequestCanceled
= true;
354 // ------------------------------------------------------------------------
356 // ------------------------------------------------------------------------
359 // All requests have a unique id
360 public int hashCode() {
361 return getRequestId();
365 public boolean equals(Object other
) {
366 if (other
instanceof TmfDataRequest
<?
>) {
367 TmfDataRequest
<?
> request
= (TmfDataRequest
<?
>) other
;
368 return (request
.fDataType
== fDataType
) &&
369 (request
.fIndex
== fIndex
) &&
370 (request
.fNbRequested
== fNbRequested
);
376 public String
toString() {
377 return "[TmfDataRequest(" + fRequestId
+ "," + fDataType
.getSimpleName()
378 + "," + fIndex
+ "," + fNbRequested
+ "," + fBlockSize
+ ")]";
This page took 0.037729 seconds and 5 git commands to generate.