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
;
16 import java
.util
.List
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.core
.runtime
.jobs
.Job
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.component
.ITmfDataProvider
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.filter
.ITmfFilter
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
30 * The generic TMF Events table events cache
32 * This can help avoid re-reading the trace when the user scrolls a window,
36 * @author Patrick Tasse
38 public class TmfEventsCache
{
41 * The generic TMF Events table cached event
44 * @author Patrick Tasse
46 public static class CachedEvent
{
51 * Constructor for new cached events.
54 * The original trace event
56 * The rank of this event in the trace
58 public CachedEvent (ITmfEvent iTmfEvent
, long rank
) {
59 this.event
= iTmfEvent
;
64 private final CachedEvent
[] fCache
;
65 private int fCacheStartIndex
= 0;
66 private int fCacheEndIndex
= 0;
68 private ITmfTrace fTrace
;
69 private final TmfEventsTable fTable
;
70 private ITmfFilter fFilter
;
71 private final List
<Integer
> fFilterIndex
= new ArrayList
<Integer
>(); // contains the event rank at each 'cache size' filtered events
74 * Constructor for the event cache
77 * The size of the cache, in number of events
79 * The Events table this cache will cover
81 public TmfEventsCache(int cacheSize
, TmfEventsTable table
) {
82 fCache
= new CachedEvent
[cacheSize
];
87 * Assign a new trace to this events cache. This clears the current
91 * The trace to assign.
93 public void setTrace(ITmfTrace trace
) {
99 * Clear the current contents of this cache.
101 public synchronized void clear() {
102 fCacheStartIndex
= 0;
104 fFilterIndex
.clear();
108 * Apply a filter on this event cache. This clears the current cache
112 * The ITmfFilter to apply.
114 public void applyFilter(ITmfFilter filter
) {
120 * Clear the current filter on this cache. This also clears the current
123 public void clearFilter() {
129 * Get an event from the cache. This will remove the event from the cache.
131 * FIXME this does not currently remove the event!
134 * The index of this event in the cache
135 * @return The cached event, or 'null' if there is no event at that index
137 public synchronized CachedEvent
getEvent(int index
) {
138 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
139 int i
= index
- fCacheStartIndex
;
142 populateCache(index
);
147 * Read an event, but without removing it from the cache.
150 * Index of the event to peek
151 * @return A reference to the event, or 'null' if there is no event at this
154 public synchronized CachedEvent
peekEvent(int index
) {
155 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
156 int i
= index
- fCacheStartIndex
;
163 * Add a trace event to the cache.
166 * The original trace event to be cached
168 * The rank of this event in the trace
170 * The index this event will occupy in the cache
172 public synchronized void storeEvent(ITmfEvent event
, long rank
, int index
) {
173 if (index
== fCacheEndIndex
) {
174 int i
= index
- fCacheStartIndex
;
175 if (i
< fCache
.length
) {
176 fCache
[i
] = new CachedEvent(event
.clone(), rank
);
180 if ((fFilter
!= null) && ((index
% fCache
.length
) == 0)) {
181 int i
= index
/ fCache
.length
;
182 fFilterIndex
.add(i
, Integer
.valueOf((int) rank
));
187 * Get the cache index of an event from his rank in the trace. This will
188 * take in consideration any filter that might be applied.
191 * The rank of the event in the trace
192 * @return The position (index) this event should use once cached
194 public int getFilteredEventIndex(final long rank
) {
197 TmfDataRequest request
;
198 final ITmfFilter filter
= fFilter
;
199 synchronized (this) {
201 int end
= fFilterIndex
.size();
203 if ((fCacheEndIndex
- fCacheStartIndex
) > 1) {
204 if (rank
< fCache
[0].rank
) {
205 end
= (fCacheStartIndex
/ fCache
.length
) + 1;
206 } else if (rank
> fCache
[fCacheEndIndex
- fCacheStartIndex
- 1].rank
) {
207 start
= fCacheEndIndex
/ fCache
.length
;
209 for (int i
= 0; i
< (fCacheEndIndex
- fCacheStartIndex
); i
++) {
210 if (fCache
[i
].rank
>= rank
) {
211 return fCacheStartIndex
+ i
;
214 return fCacheEndIndex
;
218 current
= (start
+ end
) / 2;
219 while (current
!= start
) {
220 if (rank
< fFilterIndex
.get(current
)) {
222 current
= (start
+ end
) / 2;
225 current
= (start
+ end
) / 2;
228 startRank
= fFilterIndex
.size() > 0 ? fFilterIndex
.get(current
) : 0;
231 final int index
= current
* fCache
.length
;
233 class DataRequest
extends TmfDataRequest
{
238 DataRequest(Class
<?
extends ITmfEvent
> dataType
, ITmfFilter filter
, int start
, int nbRequested
) {
239 super(dataType
, start
, nbRequested
);
246 public void handleData(ITmfEvent event
) {
247 super.handleData(event
);
256 if (fFilter
.matches(event
)) {
261 public int getFilteredIndex() {
266 request
= new DataRequest(ITmfEvent
.class, filter
, startRank
, TmfDataRequest
.ALL_DATA
);
267 ((ITmfDataProvider
) fTrace
).sendRequest(request
);
269 request
.waitForCompletion();
270 return ((DataRequest
) request
).getFilteredIndex();
271 } catch (InterruptedException e
) {
272 Activator
.getDefault().logError("Filter request interrupted!", e
); //$NON-NLS-1$
277 // ------------------------------------------------------------------------
278 // Event cache population
279 // ------------------------------------------------------------------------
281 // The event fetching job
283 private synchronized void populateCache(final int index
) {
285 /* Check if the current job will fetch the requested event:
286 * 1. The job must exist
287 * 2. It must be running (i.e. not completed)
288 * 3. The requested index must be within the cache range
290 * If the job meets these conditions, we simply exit.
291 * Otherwise, we create a new job but we might have to cancel
292 * an existing job for an obsolete range.
295 if (job
.getState() != Job
.NONE
) {
296 if ((index
>= fCacheStartIndex
) && (index
< (fCacheStartIndex
+ fCache
.length
))) {
299 // The new index is out of the requested range
300 // Kill the job and start a new one
305 fCacheStartIndex
= index
;
306 fCacheEndIndex
= index
;
308 job
= new Job("Fetching Events") { //$NON-NLS-1$
309 private int startIndex
= index
;
310 private int skipCount
= 0;
312 protected IStatus
run(final IProgressMonitor monitor
) {
315 if (fFilter
== null) {
316 nbRequested
= fCache
.length
;
318 nbRequested
= TmfDataRequest
.ALL_DATA
;
319 int i
= index
/ fCache
.length
;
320 if (i
< fFilterIndex
.size()) {
321 startIndex
= fFilterIndex
.get(i
);
322 skipCount
= index
- (i
* fCache
.length
);
326 TmfDataRequest request
= new TmfDataRequest(ITmfEvent
.class, startIndex
, nbRequested
) {
327 private int count
= 0;
328 private long rank
= startIndex
;
330 public void handleData(ITmfEvent event
) {
331 // If the job is canceled, cancel the request so waitForCompletion() will unlock
332 if (monitor
.isCanceled()) {
336 super.handleData(event
);
338 if (((fFilter
== null) || fFilter
.matches(event
)) && (skipCount
-- <= 0)) {
339 synchronized (TmfEventsCache
.this) {
340 fCache
[count
] = new CachedEvent(event
.clone(), rank
);
344 if (fFilter
!= null) {
345 fTable
.cacheUpdated(false);
349 if (count
>= fCache
.length
) {
351 } else if ((fFilter
!= null) && (count
>= (fTable
.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
358 ((ITmfDataProvider
) fTrace
).sendRequest(request
);
360 request
.waitForCompletion();
361 } catch (InterruptedException e
) {
362 Activator
.getDefault().logError("Wait for completion interrupted for populateCache ", e
); //$NON-NLS-1$
365 fTable
.cacheUpdated(true);
367 // Flag the UI thread that the cache is ready
368 if (monitor
.isCanceled()) {
369 return Status
.CANCEL_STATUS
;
371 return Status
.OK_STATUS
;
374 //job.setSystem(true);
375 job
.setPriority(Job
.SHORT
);
This page took 0.046931 seconds and 6 git commands to generate.