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
.component
;
15 import org
.eclipse
.linuxtools
.tmf
.Tracer
;
16 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
17 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimestamp
;
18 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
;
19 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
.ExecutionType
;
20 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfEventRequest
;
21 import org
.eclipse
.linuxtools
.tmf
.request
.TmfCoalescedEventRequest
;
22 import org
.eclipse
.linuxtools
.tmf
.request
.TmfEventRequest
;
25 * <b><u>TmfEventProvider</u></b>
27 * Implement me. Please.
29 public abstract class TmfEventProvider
<T
extends TmfEvent
> extends TmfDataProvider
<T
> {
31 public TmfEventProvider(String name
, Class
<T
> type
) {
35 public TmfEventProvider(String name
, Class
<T
> type
, int queueSize
) {
36 super(name
, type
, queueSize
);
39 public TmfEventProvider(TmfEventProvider
<T
> oldProvider
) {
44 public boolean isCompleted(ITmfDataRequest
<T
> request
, T data
, int nbRead
) {
45 boolean requestCompleted
= super.isCompleted(request
, data
, nbRead
);
46 if (!requestCompleted
&& request
instanceof ITmfEventRequest
<?
> && !data
.isNullRef()) {
47 TmfTimestamp endTime
= ((ITmfEventRequest
<?
>) request
).getRange().getEndTime();
48 return data
.getTimestamp().compareTo(endTime
, false) > 0;
50 return requestCompleted
;
54 protected synchronized void newCoalescedDataRequest(ITmfDataRequest
<T
> request
) {
55 if (request
instanceof ITmfEventRequest
<?
>) {
56 ITmfEventRequest
<T
> eventRequest
= (ITmfEventRequest
<T
>) request
;
57 TmfCoalescedEventRequest
<T
> coalescedRequest
=
58 new TmfCoalescedEventRequest
<T
>(
59 fType
, eventRequest
.getRange(), eventRequest
.getIndex(), eventRequest
.getNbRequested(), eventRequest
.getBlockSize(),
60 eventRequest
.getExecType());
61 coalescedRequest
.addRequest(eventRequest
);
62 if (Tracer
.isRequestTraced()) {
63 Tracer
.traceRequest(request
, "coalesced with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
65 fPendingCoalescedRequests
.add(coalescedRequest
);
68 super.newCoalescedDataRequest(request
);
73 protected void queueBackgroundRequest(final ITmfDataRequest
<T
> request
, final int blockSize
, final boolean indexing
) {
75 if (! (request
instanceof ITmfEventRequest
)) {
76 super.queueBackgroundRequest(request
, blockSize
, indexing
);
80 Thread thread
= new Thread() {
85 final Integer
[] CHUNK_SIZE
= new Integer
[1];
86 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested(), blockSize
+ ((indexing
) ?
1 : 0));
88 final Integer
[] nbRead
= new Integer
[1];
91 final Boolean
[] isFinished
= new Boolean
[1];
92 isFinished
[0] = Boolean
.FALSE
;
94 int startIndex
= request
.getIndex();
96 while (!isFinished
[0]) {
98 TmfEventRequest
<T
> subRequest
= new TmfEventRequest
<T
>(request
.getDataType(), ((ITmfEventRequest
<?
>) request
).getRange(), startIndex
+ nbRead
[0], CHUNK_SIZE
[0], blockSize
, ExecutionType
.BACKGROUND
)
101 public void handleData(T data
) {
102 super.handleData(data
);
103 request
.handleData(data
);
104 if (this.getNbRead() > CHUNK_SIZE
[0]) {
105 System
.out
.println("ERROR - Read too many events"); //$NON-NLS-1$
110 public void handleCompleted() {
111 nbRead
[0] += this.getNbRead();
112 if (nbRead
[0] >= request
.getNbRequested() || (this.getNbRead() < CHUNK_SIZE
[0])) {
113 if (this.isCancelled()) {
115 } else if (this.isFailed()) {
120 isFinished
[0] = Boolean
.TRUE
;
122 super.handleCompleted();
126 if (!isFinished
[0]) {
127 queueRequest(subRequest
);
130 subRequest
.waitForCompletion();
131 } catch (InterruptedException e
) {
135 if (startIndex
== 0 && nbRead
[0] == CHUNK_SIZE
[0]) { // do this only once if the event request index is unknown
136 startIndex
= subRequest
.getIndex(); // update the start index with the index of the first subrequest's
137 } // start time event which was set during the arm request
138 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested() - nbRead
[0], blockSize
);