Merge master in TmfTraceModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / events / TmfEventsCache.java
1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 ******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.viewers.events;
14
15 import java.util.ArrayList;
16
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;
26
27 public class TmfEventsCache {
28
29 public static class CachedEvent {
30 ITmfEvent event;
31 long rank;
32
33 public CachedEvent (ITmfEvent iTmfEvent, long rank) {
34 this.event = iTmfEvent;
35 this.rank = rank;
36 }
37 }
38
39 private final CachedEvent[] fCache;
40 private int fCacheStartIndex = 0;
41 private int fCacheEndIndex = 0;
42
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
47
48 public TmfEventsCache(int cacheSize, TmfEventsTable table) {
49 fCache = new CachedEvent[cacheSize];
50 fTable = table;
51 }
52
53 public void setTrace(ITmfTrace<?> trace) {
54 fTrace = trace;
55 clear();
56 }
57
58 public synchronized void clear() {
59 fCacheStartIndex = 0;
60 fCacheEndIndex = 0;
61 fFilterIndex.clear();
62 }
63
64 public void applyFilter(ITmfFilter filter) {
65 fFilter = filter;
66 clear();
67 }
68
69 public void clearFilter() {
70 fFilter = null;
71 clear();
72 }
73
74 public synchronized CachedEvent getEvent(int index) {
75 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
76 int i = index - fCacheStartIndex;
77 return fCache[i];
78 }
79 populateCache(index);
80 return null;
81 }
82
83 public synchronized CachedEvent peekEvent(int index) {
84 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
85 int i = index - fCacheStartIndex;
86 return fCache[i];
87 }
88 return null;
89 }
90
91 public synchronized void storeEvent(ITmfEvent event, long rank, int index) {
92 if (fCacheStartIndex == fCacheEndIndex) {
93 fCacheStartIndex = index;
94 fCacheEndIndex = index;
95 }
96 if (index == fCacheEndIndex) {
97 int i = index - fCacheStartIndex;
98 if (i < fCache.length) {
99 fCache[i] = new CachedEvent(event.clone(), rank);
100 fCacheEndIndex++;
101 }
102 }
103 if ((fFilter != null) && ((index % fCache.length) == 0)) {
104 int i = index / fCache.length;
105 fFilterIndex.add(i, Integer.valueOf((int) rank));
106 }
107 }
108
109 @SuppressWarnings("unchecked")
110 public synchronized int getFilteredEventIndex(final long rank) {
111 int current;
112 int startRank;
113 TmfDataRequest<ITmfEvent> request;
114 synchronized (this) {
115 int start = 0;
116 int end = fFilterIndex.size();
117
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;
123 } else {
124 for (int i = 0; i < (fCacheEndIndex - fCacheStartIndex); i++) {
125 if (fCache[i].rank >= rank) {
126 return fCacheStartIndex + i;
127 }
128 }
129 return fCacheEndIndex;
130 }
131 }
132
133 current = (start + end) / 2;
134 while (current != start) {
135 if (rank < fFilterIndex.get(current)) {
136 end = current;
137 current = (start + end) / 2;
138 } else {
139 start = current;
140 current = (start + end) / 2;
141 }
142 }
143 startRank = fFilterIndex.get(current);
144 }
145
146 final int index = current * fCache.length;
147
148 class DataRequest<T extends ITmfEvent> extends TmfDataRequest<T> {
149 int fRank;
150 int fIndex;
151
152 DataRequest(Class<T> dataType, int start, int nbRequested) {
153 super(dataType, start, nbRequested);
154 fRank = start;
155 fIndex = index;
156 }
157
158 @Override
159 public void handleData(T event) {
160 super.handleData(event);
161 if (isCancelled()) {
162 return;
163 }
164 if (fRank >= rank) {
165 cancel();
166 return;
167 }
168 fRank++;
169 if (fFilter.matches(event)) {
170 fIndex++;
171 }
172 }
173
174 public int getFilteredIndex() {
175 return fIndex;
176 }
177 }
178
179 request = new DataRequest<ITmfEvent>(ITmfEvent.class, startRank, TmfDataRequest.ALL_DATA);
180 ((ITmfDataProvider<ITmfEvent>) fTrace).sendRequest(request);
181 try {
182 request.waitForCompletion();
183 return ((DataRequest<ITmfEvent>) request).getFilteredIndex();
184 } catch (InterruptedException e) {
185 }
186 return 0;
187 }
188
189 // ------------------------------------------------------------------------
190 // Event cache population
191 // ------------------------------------------------------------------------
192
193 // The event fetching job
194 private Job job;
195 private synchronized void populateCache(final int index) {
196
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
201 *
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.
205 */
206 if (job != null) {
207 if (job.getState() != Job.NONE) {
208 if ((index >= fCacheStartIndex) && (index < (fCacheStartIndex + fCache.length))) {
209 return;
210 }
211 // The new index is out of the requested range
212 // Kill the job and start a new one
213 job.cancel();
214 }
215 }
216
217 fCacheStartIndex = index;
218 fCacheEndIndex = index;
219
220 job = new Job("Fetching Events") { //$NON-NLS-1$
221 private int startIndex = index;
222 private int skipCount = 0;
223 @Override
224 @SuppressWarnings("unchecked")
225 protected IStatus run(final IProgressMonitor monitor) {
226
227 int nbRequested;
228 if (fFilter == null) {
229 nbRequested = fCache.length;
230 } else {
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);
236 }
237 }
238
239 TmfDataRequest<ITmfEvent> request = new TmfDataRequest<ITmfEvent>(ITmfEvent.class, startIndex, nbRequested) {
240 private int count = 0;
241 private long rank = startIndex;
242 @Override
243 public void handleData(ITmfEvent event) {
244 // If the job is canceled, cancel the request so waitForCompletion() will unlock
245 if (monitor.isCanceled()) {
246 cancel();
247 return;
248 }
249 super.handleData(event);
250 if (event != null) {
251 if (((fFilter == null) || fFilter.matches(event)) && (skipCount-- <= 0)) {
252 synchronized (TmfEventsCache.this) {
253 fCache[count] = new CachedEvent(event.clone(), rank);
254 count++;
255 fCacheEndIndex++;
256 }
257 if (fFilter != null) {
258 fTable.cacheUpdated(false);
259 }
260 }
261 }
262 if (count >= fCache.length) {
263 cancel();
264 } else if ((fFilter != null) && (count >= (fTable.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
265 cancel();
266 }
267 rank++;
268 }
269 };
270
271 ((ITmfDataProvider<ITmfEvent>) fTrace).sendRequest(request);
272 try {
273 request.waitForCompletion();
274 } catch (InterruptedException e) {
275 e.printStackTrace();
276 }
277
278 fTable.cacheUpdated(true);
279
280 // Flag the UI thread that the cache is ready
281 if (monitor.isCanceled()) {
282 return Status.CANCEL_STATUS;
283 } else {
284 return Status.OK_STATUS;
285 }
286 }
287 };
288 //job.setSystem(true);
289 job.setPriority(Job.SHORT);
290 job.schedule();
291 }
292
293 }
This page took 0.037071 seconds and 5 git commands to generate.