1 /*******************************************************************************
2 * Copyright (c) 2011 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 * Patrick Tasse - Initial API and implementation
11 ******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.events
;
15 import java
.util
.ArrayList
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.core
.runtime
.jobs
.Job
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.component
.ITmfDataProvider
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.ITmfFilter
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
27 public class TmfEventsCache
{
29 public class CachedEvent
{
33 public CachedEvent (TmfEvent event
, long rank
) {
39 private CachedEvent
[] fCache
;
40 private int fCacheStartIndex
= 0;
41 private int fCacheEndIndex
= 0;
43 private ITmfTrace
<?
> fTrace
;
44 private TmfEventsTable fTable
;
45 private ITmfFilter fFilter
;
46 private ArrayList
<Integer
> fFilterIndex
= new ArrayList
<Integer
>(); // contains the event rank at each 'cache size' filtered events
48 public TmfEventsCache(int cacheSize
, TmfEventsTable table
) {
49 fCache
= new CachedEvent
[cacheSize
];
53 public void setTrace(ITmfTrace
<?
> trace
) {
64 public void applyFilter(ITmfFilter filter
) {
69 public void clearFilter() {
74 public CachedEvent
getEvent(int index
) {
75 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
76 int i
= index
- fCacheStartIndex
;
83 public CachedEvent
peekEvent(int index
) {
84 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
85 int i
= index
- fCacheStartIndex
;
91 public synchronized void storeEvent(TmfEvent event
, long rank
, int index
) {
92 if (fCacheStartIndex
== fCacheEndIndex
) {
93 fCacheStartIndex
= index
;
94 fCacheEndIndex
= index
;
96 if (index
== fCacheEndIndex
) {
97 int i
= index
- fCacheStartIndex
;
98 if (i
< fCache
.length
) {
99 fCache
[i
] = new CachedEvent(event
.clone(), rank
);
103 if (fFilter
!= null && index
% fCache
.length
== 0) {
104 int i
= index
/ fCache
.length
;
105 fFilterIndex
.add(i
, new Integer((int) rank
));
109 @SuppressWarnings("unchecked")
110 public int getFilteredEventIndex(final long rank
) {
113 TmfDataRequest
<TmfEvent
> request
;
114 synchronized (this) {
116 int end
= fFilterIndex
.size();
118 if (fCacheEndIndex
- fCacheStartIndex
> 1) {
119 if (rank
< fCache
[0].rank
) {
120 end
= fCacheStartIndex
/ fCache
.length
+ 1;
121 } else if (rank
> fCache
[fCacheEndIndex
- fCacheStartIndex
- 1].rank
) {
122 start
= fCacheEndIndex
/ fCache
.length
;
124 for (int i
= 0; i
< fCacheEndIndex
- fCacheStartIndex
; i
++) {
125 if (fCache
[i
].rank
>= rank
) {
126 return fCacheStartIndex
+ i
;
129 return fCacheEndIndex
;
133 current
= (start
+ end
) / 2;
134 while (current
!= start
) {
135 if (rank
< fFilterIndex
.get(current
)) {
137 current
= (start
+ end
) / 2;
140 current
= (start
+ end
) / 2;
143 startRank
= fFilterIndex
.get(current
);
146 final int index
= current
* fCache
.length
;
148 class DataRequest
<T
extends TmfEvent
> extends TmfDataRequest
<T
> {
152 DataRequest(Class
<T
> dataType
, int start
, int nbRequested
) {
153 super(dataType
, start
, nbRequested
);
159 public void handleData(T event
) {
160 super.handleData(event
);
161 if (isCancelled()) return;
167 if (fFilter
.matches(event
)) {
172 public int getFilteredIndex() {
177 request
= new DataRequest
<TmfEvent
>(TmfEvent
.class, startRank
, TmfDataRequest
.ALL_DATA
);
178 ((ITmfDataProvider
<TmfEvent
>) fTrace
).sendRequest(request
);
180 request
.waitForCompletion();
181 return ((DataRequest
<TmfEvent
>) request
).getFilteredIndex();
182 } catch (InterruptedException e
) {
187 // ------------------------------------------------------------------------
188 // Event cache population
189 // ------------------------------------------------------------------------
191 // The event fetching job
193 private synchronized void populateCache(final int index
) {
195 /* Check if the current job will fetch the requested event:
196 * 1. The job must exist
197 * 2. It must be running (i.e. not completed)
198 * 3. The requested index must be within the cache range
200 * If the job meets these conditions, we simply exit.
201 * Otherwise, we create a new job but we might have to cancel
202 * an existing job for an obsolete range.
205 if (job
.getState() != Job
.NONE
) {
206 if (index
>= fCacheStartIndex
&& index
< (fCacheStartIndex
+ fCache
.length
)) {
209 // The new index is out of the requested range
210 // Kill the job and start a new one
215 fCacheStartIndex
= index
;
216 fCacheEndIndex
= index
;
218 job
= new Job("Fetching Events") { //$NON-NLS-1$
219 private int startIndex
= index
;
220 private int skipCount
= 0;
222 @SuppressWarnings("unchecked")
223 protected IStatus
run(final IProgressMonitor monitor
) {
226 if (fFilter
== null) {
227 nbRequested
= fCache
.length
;
229 nbRequested
= TmfDataRequest
.ALL_DATA
;
230 int i
= index
/ fCache
.length
;
231 if (i
< fFilterIndex
.size()) {
232 startIndex
= fFilterIndex
.get(i
);
233 skipCount
= index
- (i
* fCache
.length
);
237 TmfDataRequest
<TmfEvent
> request
= new TmfDataRequest
<TmfEvent
>(TmfEvent
.class, startIndex
, nbRequested
) {
238 private int count
= 0;
239 private long rank
= startIndex
;
241 public void handleData(TmfEvent event
) {
242 // If the job is canceled, cancel the request so waitForCompletion() will unlock
243 if (monitor
.isCanceled()) {
247 super.handleData(event
);
249 if ((fFilter
== null || fFilter
.matches(event
)) && skipCount
-- <= 0) {
250 synchronized (TmfEventsCache
.this) {
251 fCache
[count
] = new CachedEvent(event
.clone(), rank
);
255 if (fFilter
!= null) {
256 fTable
.cacheUpdated(false);
260 if (count
>= fCache
.length
) {
262 } else if (fFilter
!= null && count
>= fTable
.getTable().getItemCount() - 3) { // -1 for header row, -2 for top and bottom filter status rows
269 ((ITmfDataProvider
<TmfEvent
>) fTrace
).sendRequest(request
);
271 request
.waitForCompletion();
272 } catch (InterruptedException e
) {
276 fTable
.cacheUpdated(true);
278 // Flag the UI thread that the cache is ready
279 if (monitor
.isCanceled()) {
280 return Status
.CANCEL_STATUS
;
282 return Status
.OK_STATUS
;
286 //job.setSystem(true);
287 job
.setPriority(Job
.SHORT
);
This page took 0.038854 seconds and 6 git commands to generate.