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
.ITmfEvent
;
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 static class CachedEvent
{
33 public CachedEvent (ITmfEvent iTmfEvent
, long rank
) {
34 this.event
= iTmfEvent
;
39 private final CachedEvent
[] fCache
;
40 private int fCacheStartIndex
= 0;
41 private int fCacheEndIndex
= 0;
43 private ITmfTrace
<?
> fTrace
;
44 private final TmfEventsTable fTable
;
45 private ITmfFilter fFilter
;
46 private final 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
) {
58 public synchronized void clear() {
64 public void applyFilter(ITmfFilter filter
) {
69 public void clearFilter() {
74 public synchronized CachedEvent
getEvent(int index
) {
75 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
76 int i
= index
- fCacheStartIndex
;
83 public synchronized CachedEvent
peekEvent(int index
) {
84 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
85 int i
= index
- fCacheStartIndex
;
91 public synchronized void storeEvent(ITmfEvent 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
, Integer
.valueOf((int) rank
));
109 @SuppressWarnings("unchecked")
110 public synchronized int getFilteredEventIndex(final long rank
) {
113 TmfDataRequest
<ITmfEvent
> 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 ITmfEvent
> 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
);
169 if (fFilter
.matches(event
)) {
174 public int getFilteredIndex() {
179 request
= new DataRequest
<ITmfEvent
>(ITmfEvent
.class, startRank
, TmfDataRequest
.ALL_DATA
);
180 ((ITmfDataProvider
<ITmfEvent
>) fTrace
).sendRequest(request
);
182 request
.waitForCompletion();
183 return ((DataRequest
<ITmfEvent
>) request
).getFilteredIndex();
184 } catch (InterruptedException e
) {
189 // ------------------------------------------------------------------------
190 // Event cache population
191 // ------------------------------------------------------------------------
193 // The event fetching job
195 private synchronized void populateCache(final int index
) {
197 /* Check if the current job will fetch the requested event:
198 * 1. The job must exist
199 * 2. It must be running (i.e. not completed)
200 * 3. The requested index must be within the cache range
202 * If the job meets these conditions, we simply exit.
203 * Otherwise, we create a new job but we might have to cancel
204 * an existing job for an obsolete range.
207 if (job
.getState() != Job
.NONE
) {
208 if ((index
>= fCacheStartIndex
) && (index
< (fCacheStartIndex
+ fCache
.length
))) {
211 // The new index is out of the requested range
212 // Kill the job and start a new one
217 fCacheStartIndex
= index
;
218 fCacheEndIndex
= index
;
220 job
= new Job("Fetching Events") { //$NON-NLS-1$
221 private int startIndex
= index
;
222 private int skipCount
= 0;
224 @SuppressWarnings("unchecked")
225 protected IStatus
run(final IProgressMonitor monitor
) {
228 if (fFilter
== null) {
229 nbRequested
= fCache
.length
;
231 nbRequested
= TmfDataRequest
.ALL_DATA
;
232 int i
= index
/ fCache
.length
;
233 if (i
< fFilterIndex
.size()) {
234 startIndex
= fFilterIndex
.get(i
);
235 skipCount
= index
- (i
* fCache
.length
);
239 TmfDataRequest
<ITmfEvent
> request
= new TmfDataRequest
<ITmfEvent
>(ITmfEvent
.class, startIndex
, nbRequested
) {
240 private int count
= 0;
241 private long rank
= startIndex
;
243 public void handleData(ITmfEvent event
) {
244 // If the job is canceled, cancel the request so waitForCompletion() will unlock
245 if (monitor
.isCanceled()) {
249 super.handleData(event
);
251 if (((fFilter
== null) || fFilter
.matches(event
)) && (skipCount
-- <= 0)) {
252 synchronized (TmfEventsCache
.this) {
253 fCache
[count
] = new CachedEvent(event
.clone(), rank
);
257 if (fFilter
!= null) {
258 fTable
.cacheUpdated(false);
262 if (count
>= fCache
.length
) {
264 } else if ((fFilter
!= null) && (count
>= (fTable
.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
271 ((ITmfDataProvider
<ITmfEvent
>) fTrace
).sendRequest(request
);
273 request
.waitForCompletion();
274 } catch (InterruptedException e
) {
278 fTable
.cacheUpdated(true);
280 // Flag the UI thread that the cache is ready
281 if (monitor
.isCanceled()) {
282 return Status
.CANCEL_STATUS
;
284 return Status
.OK_STATUS
;
288 //job.setSystem(true);
289 job
.setPriority(Job
.SHORT
);
This page took 0.036388 seconds and 5 git commands to generate.