75c8576ad5982d7ae7514063e9a97ad31c811b80
1 /*******************************************************************************
2 * Copyright (c) 2009 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
.event
.TmfTimeRange
;
18 * <b><u>TmfDataRequest</u></b>
20 * TmfDataRequests are used to obtain blocks of contiguous data from a data
21 * provider, either all the data within a given time window or n elements
22 * starting at a specific timestamp. Open ranges can be used, especially for
23 * continuous streaming.
25 * The request is processed asynchronously by an ITmfRequestProcessor and,
26 * as blocks of data become available, the callback handlePartialData() is
27 * invoked, synchronously, for each block. When returning from the callback,
28 * the data instances go out of scope and become eligible for gc. It is
29 * is thus the responsibility of the requester to either copy or keep a
30 * reference to the data it wishes to track specifically.
32 * This data block approach is necessary to avoid busting the heap for very
33 * large trace files. The block size is configurable.
35 * The ITmfRequestProcessor indicates that the request is completed by
36 * calling done(). The request can be canceled at any time with cancel().
39 *<pre><code><i>TmfTimeWindow range = new TmfTimewindow(...);
40 *TmfDataRequest<DataType[]> request = new TmfDataRequest<DataType[]>(range, 0, NB_EVENTS, BLOCK_SIZE) {
42 * public void handlePartialResult() {
43 * DataType[] data = request.getData();
44 * for (DataType e : data) {
49 *fProcessor.process(request, true);
52 * TODO: Consider extending DataRequestMonitor from DSF concurrency plugin.
53 * The main issue is the slicing of the result in blocks and continuous
54 * streams. This would require using a thread executor and to carefully
55 * look at setData() and getData().
57 public class TmfDataRequest
<V
> {
59 // ========================================================================
61 // ========================================================================
63 // The default maximum number of events per chunk
64 public static final int DEFAULT_BLOCK_SIZE
= 1000;
66 // The request count for all the events
67 public static final int ALL_EVENTS
= -1;
69 // ========================================================================
71 // ========================================================================
73 private final TmfTimeRange fRange
; // The requested events timestamp range
74 private final int fIndex
; // The event index to get
75 private final long fOffset
; // The synchronization offset to apply
76 private final int fNbRequestedItems
; // The number of items to read (-1 == the whole range)
77 private final int fBlockSize
; // The maximum number of events per chunk
79 private Object lock
= new Object();
80 private boolean fRequestCompleted
= false;
81 private boolean fRequestCanceled
= false;
83 private V
[] fData
; // Data object
85 // ========================================================================
87 // ========================================================================
93 public TmfDataRequest(int index
, long offset
, int nbEvents
) {
94 this(null, index
, offset
, nbEvents
, DEFAULT_BLOCK_SIZE
);
102 public TmfDataRequest(TmfTimeRange range
, long offset
, int nbEvents
) {
103 this(range
, 0, offset
, nbEvents
, DEFAULT_BLOCK_SIZE
);
110 * @param maxBlockSize Size of the largest blocks expected
112 public TmfDataRequest(TmfTimeRange range
, long offset
, int nbEvents
, int maxBlockSize
) {
113 this(range
, 0, offset
, nbEvents
, maxBlockSize
);
121 * @param maxBlockSize Size of the largest blocks expected
123 public TmfDataRequest(TmfTimeRange range
, int index
, long offset
, int nbEvents
, int maxBlockSize
) {
127 fNbRequestedItems
= nbEvents
;
128 fBlockSize
= maxBlockSize
;
131 // ========================================================================
133 // ========================================================================
136 * @return the requested time range
138 public TmfTimeRange
getRange() {
145 public int getIndex() {
152 public long getOffset() {
157 * @return the number of requested events (-1 = all)
159 public int getNbRequestedItems() {
160 return fNbRequestedItems
;
164 * @return the block size
166 public int getBlockize() {
171 * @return indicates if the request is completed
173 public boolean isCompleted() {
174 return fRequestCompleted
;
178 * @return indicates if the request is canceled
180 public boolean isCancelled() {
181 return fRequestCanceled
;
184 // ========================================================================
186 // ========================================================================
189 * Sets the data object to specified value. To be called by the
190 * asynchronous method implementor.
191 * @param data Data value to set.
193 public synchronized void setData(V
[] data
) {
198 * Returns the data value, null if not set.
200 public synchronized V
[] getData() {
205 * handlePartialResult()
207 * - Data items are received in the order they appear in the stream.
208 * - Called by the request processor, in its execution thread, every time a
209 * block of data becomes available.
210 * - Request processor performs a synchronous call to handlePartialResult()
211 * i.e. its execution threads holds until handlePartialData() returns.
212 * - Original data items are disposed of on return i.e. keep a reference
213 * (or a copy) if some persistence is needed between invocations.
214 * - When there is no more data, done() is called.
216 * @param events - an array of events
218 public void handlePartialResult() {
221 public void handleCompleted() {
225 * To suspend the client thread until the request completes (or is
228 * @throws InterruptedException
230 public void waitForCompletion() {
231 synchronized (lock
) {
232 while (!fRequestCompleted
)
235 } catch (InterruptedException e
) {
242 * Complete the request. Called by the request processor upon completion.
246 fRequestCompleted
= true;
253 * Cancel the request.
255 public void cancel() {
257 fRequestCanceled
= true;
258 fRequestCompleted
= true;
This page took 0.035958 seconds and 4 git commands to generate.