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
.tmf
.core
.event
.ITmfEvent
;
17 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
.ExecutionType
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfCoalescedEventRequest
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
25 * <b><u>TmfEventProvider</u></b>
28 public abstract class TmfEventProvider
<T
extends ITmfEvent
> extends TmfDataProvider
<T
> {
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
34 public TmfEventProvider() {
39 public void init(String name
, Class
<T
> type
) {
40 super.init(name
, type
);
43 public TmfEventProvider(String name
, Class
<T
> type
) {
47 public TmfEventProvider(String name
, Class
<T
> type
, int queueSize
) {
48 super(name
, type
, queueSize
);
51 public TmfEventProvider(TmfEventProvider
<T
> other
) {
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 public boolean isCompleted(ITmfDataRequest
<T
> request
, T data
, int nbRead
) {
61 boolean requestCompleted
= super.isCompleted(request
, data
, nbRead
);
62 if (!requestCompleted
&& request
instanceof ITmfEventRequest
<?
>) {
63 ITmfTimestamp endTime
= ((ITmfEventRequest
<?
>) request
).getRange().getEndTime();
64 return data
.getTimestamp().compareTo(endTime
, false) > 0;
66 return requestCompleted
;
70 protected synchronized void newCoalescedDataRequest(ITmfDataRequest
<T
> request
) {
71 if (request
instanceof ITmfEventRequest
<?
>) {
72 ITmfEventRequest
<T
> eventRequest
= (ITmfEventRequest
<T
>) request
;
73 TmfCoalescedEventRequest
<T
> coalescedRequest
= new TmfCoalescedEventRequest
<T
>(eventRequest
.getDataType(), eventRequest
.getRange(),
74 eventRequest
.getIndex(), eventRequest
.getNbRequested(), eventRequest
.getBlockSize(), eventRequest
.getExecType());
75 coalescedRequest
.addRequest(eventRequest
);
76 if (Tracer
.isRequestTraced()) {
77 Tracer
.traceRequest(request
, "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
78 Tracer
.traceRequest(coalescedRequest
, "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
80 fPendingCoalescedRequests
.add(coalescedRequest
);
82 super.newCoalescedDataRequest(request
);
87 protected void queueBackgroundRequest(final ITmfDataRequest
<T
> request
, final int blockSize
, final boolean indexing
) {
89 if (! (request
instanceof ITmfEventRequest
)) {
90 super.queueBackgroundRequest(request
, blockSize
, indexing
);
94 final TmfDataProvider
<T
> provider
= this;
96 Thread thread
= new Thread() {
100 if (Tracer
.isRequestTraced()) {
101 Tracer
.traceRequest(request
, "is being serviced by " + provider
.getName()); //$NON-NLS-1$
106 final Integer
[] CHUNK_SIZE
= new Integer
[1];
107 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested(), blockSize
+ ((indexing
) ?
1 : 0));
109 final Integer
[] nbRead
= new Integer
[1];
112 final Boolean
[] isFinished
= new Boolean
[1];
113 isFinished
[0] = Boolean
.FALSE
;
115 long startIndex
= request
.getIndex();
117 while (!isFinished
[0]) {
119 TmfEventRequest
<T
> subRequest
= new TmfEventRequest
<T
>(request
.getDataType(), ((ITmfEventRequest
<?
>) request
).getRange(), startIndex
+ nbRead
[0], CHUNK_SIZE
[0], blockSize
, ExecutionType
.BACKGROUND
)
122 public void handleData(T data
) {
123 super.handleData(data
);
124 if (request
.getDataType().isInstance(data
)) {
125 request
.handleData(data
);
127 if (this.getNbRead() > CHUNK_SIZE
[0]) {
128 System
.out
.println("ERROR - Read too many events"); //$NON-NLS-1$
133 public void handleCompleted() {
134 nbRead
[0] += this.getNbRead();
135 if (nbRead
[0] >= request
.getNbRequested() || (this.getNbRead() < CHUNK_SIZE
[0])) {
136 if (this.isCancelled()) {
138 } else if (this.isFailed()) {
143 isFinished
[0] = Boolean
.TRUE
;
145 super.handleCompleted();
149 if (!isFinished
[0]) {
150 queueRequest(subRequest
);
153 subRequest
.waitForCompletion();
154 } catch (InterruptedException e
) {
158 if (startIndex
== 0 && nbRead
[0].equals(CHUNK_SIZE
[0])) { // do this only once if the event request index is unknown
159 startIndex
= subRequest
.getIndex(); // update the start index with the index of the first subrequest's
160 } // start time event which was set during the arm request
161 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested() - nbRead
[0], blockSize
);