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
.core
.component
;
15 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Tracer
;
16 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.request
.TmfCoalescedEventRequest
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
.ExecutionType
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
25 * An extension of TmfDataProvider timestamped events providers.
28 * @author Francois Chouinard
30 public abstract class TmfEventProvider
<T
extends ITmfEvent
> extends TmfDataProvider
<T
> {
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
36 public TmfEventProvider() {
41 public void init(String name
, Class
<T
> type
) {
42 super.init(name
, type
);
45 public TmfEventProvider(String name
, Class
<T
> type
) {
49 public TmfEventProvider(String name
, Class
<T
> type
, int queueSize
) {
50 super(name
, type
, queueSize
);
53 public TmfEventProvider(TmfEventProvider
<T
> other
) {
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 public boolean isCompleted(ITmfDataRequest
<T
> request
, T data
, int nbRead
) {
63 boolean requestCompleted
= super.isCompleted(request
, data
, nbRead
);
64 if (!requestCompleted
&& request
instanceof ITmfEventRequest
<?
>) {
65 ITmfTimestamp endTime
= ((ITmfEventRequest
<?
>) request
).getRange().getEndTime();
66 return data
.getTimestamp().compareTo(endTime
, false) > 0;
68 return requestCompleted
;
72 protected synchronized void newCoalescedDataRequest(ITmfDataRequest
<T
> request
) {
73 if (request
instanceof ITmfEventRequest
<?
>) {
74 ITmfEventRequest
<T
> eventRequest
= (ITmfEventRequest
<T
>) request
;
75 TmfCoalescedEventRequest
<T
> coalescedRequest
= new TmfCoalescedEventRequest
<T
>(eventRequest
.getDataType(), eventRequest
.getRange(),
76 eventRequest
.getIndex(), eventRequest
.getNbRequested(), eventRequest
.getBlockSize(), eventRequest
.getExecType());
77 coalescedRequest
.addRequest(eventRequest
);
78 if (Tracer
.isRequestTraced()) {
79 Tracer
.traceRequest(request
, "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
80 Tracer
.traceRequest(coalescedRequest
, "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
82 fPendingCoalescedRequests
.add(coalescedRequest
);
84 super.newCoalescedDataRequest(request
);
89 protected void queueBackgroundRequest(final ITmfDataRequest
<T
> request
, final int blockSize
, final boolean indexing
) {
91 if (! (request
instanceof ITmfEventRequest
)) {
92 super.queueBackgroundRequest(request
, blockSize
, indexing
);
96 final TmfDataProvider
<T
> provider
= this;
98 Thread thread
= new Thread() {
102 if (Tracer
.isRequestTraced()) {
103 Tracer
.traceRequest(request
, "is being serviced by " + provider
.getName()); //$NON-NLS-1$
108 final Integer
[] CHUNK_SIZE
= new Integer
[1];
109 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested(), blockSize
+ ((indexing
) ?
1 : 0));
111 final Integer
[] nbRead
= new Integer
[1];
114 final Boolean
[] isFinished
= new Boolean
[1];
115 isFinished
[0] = Boolean
.FALSE
;
117 long startIndex
= request
.getIndex();
119 while (!isFinished
[0]) {
121 TmfEventRequest
<T
> subRequest
= new TmfEventRequest
<T
>(request
.getDataType(), ((ITmfEventRequest
<?
>) request
).getRange(), startIndex
+ nbRead
[0], CHUNK_SIZE
[0], blockSize
, ExecutionType
.BACKGROUND
)
124 public void handleData(T data
) {
125 super.handleData(data
);
126 if (request
.getDataType().isInstance(data
)) {
127 request
.handleData(data
);
129 if (this.getNbRead() > CHUNK_SIZE
[0]) {
130 System
.out
.println("ERROR - Read too many events"); //$NON-NLS-1$
135 public void handleCompleted() {
136 nbRead
[0] += this.getNbRead();
137 if (nbRead
[0] >= request
.getNbRequested() || (this.getNbRead() < CHUNK_SIZE
[0])) {
138 if (this.isCancelled()) {
140 } else if (this.isFailed()) {
145 isFinished
[0] = Boolean
.TRUE
;
147 super.handleCompleted();
151 if (!isFinished
[0]) {
152 queueRequest(subRequest
);
155 subRequest
.waitForCompletion();
156 } catch (InterruptedException e
) {
160 if (startIndex
== 0 && nbRead
[0].equals(CHUNK_SIZE
[0])) { // do this only once if the event request index is unknown
161 startIndex
= subRequest
.getIndex(); // update the start index with the index of the first subrequest's
162 } // start time event which was set during the arm request
163 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested() - nbRead
[0], blockSize
);