tmf: Fix regression in event requests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / statesystem / HistoryBuilder.java
1 /*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
5 *
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
10 *
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.internal.tmf.core.statesystem;
14
15 import java.io.IOException;
16
17 import org.eclipse.linuxtools.tmf.core.component.TmfComponent;
18 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
19 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
20 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
21 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
22 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
23 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
24 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
25 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceClosedSignal;
26 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal;
27 import org.eclipse.linuxtools.tmf.core.statesystem.IStateChangeInput;
28 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
29 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystemBuilder;
30 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
31 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
32
33 /**
34 * This is the high-level wrapper around the State History and its input and
35 * storage plugins. Just create the object using the constructor then .run()
36 *
37 * You can use one HistoryBuilder and it will instantiate everything underneath.
38 * If you need more fine-grained control you can still ignore this and
39 * instantiate everything manually.
40 *
41 * @author alexmont
42 *
43 */
44 public class HistoryBuilder extends TmfComponent {
45
46 private final IStateChangeInput sci;
47 private final StateSystem ss;
48 private final IStateHistoryBackend hb;
49 private boolean started = true; /* Don't handle signals until we're ready */
50
51 /**
52 * Instantiate a new HistoryBuilder helper.
53 *
54 * @param stateChangeInput
55 * The input plugin to use. This is required.
56 * @param backend
57 * The backend storage to use.
58 * @param buildManually
59 * Should we build this history in-band or not. True means we
60 * will start the building ourselves and block the caller until
61 * construction is done. False (out-of-band) means we will start
62 * listening for the signal and return immediately. Another
63 * signal will be sent when finished.
64 * @throws IOException
65 * Is thrown if anything went wrong (usually with the storage
66 * backend)
67 */
68 public HistoryBuilder(IStateChangeInput stateChangeInput,
69 IStateHistoryBackend backend, boolean buildManually)
70 throws IOException {
71 if (stateChangeInput == null || backend == null) {
72 throw new IllegalArgumentException();
73 }
74 sci = stateChangeInput;
75 hb = backend;
76 ss = new StateSystem(hb, true);
77
78 sci.assignTargetStateSystem(ss);
79
80 if (buildManually) {
81 TmfSignalManager.deregister(this);
82 this.buildManually();
83 } else {
84 started = false;
85 /* We'll now wait for the signal to start building */
86 }
87 }
88
89 /**
90 * Factory-style method to open an existing history, you only have to
91 * provide the already-instantiated IStateHistoryBackend object.
92 *
93 * @param hb
94 * The history-backend object
95 * @return A IStateSystemBuilder reference to the new state system. If you
96 * will only run queries on this history, you should *definitely*
97 * cast it to IStateSystemQuerier.
98 * @throws IOException
99 * If there was something wrong.
100 */
101 public static ITmfStateSystemBuilder openExistingHistory(
102 IStateHistoryBackend hb) throws IOException {
103 return new StateSystem(hb, false);
104 }
105
106 /**
107 * Return a read/write reference to the state system object that was
108 * created.
109 *
110 * @return Reference to the state system, with access to everything.
111 */
112 public ITmfStateSystemBuilder getStateSystemBuilder() {
113 return ss;
114 }
115
116 /**
117 * Return a read-only reference to the state system object that was created.
118 *
119 * @return Reference to the state system, but only with the query methods
120 * available.
121 */
122 public ITmfStateSystem getStateSystemQuerier() {
123 return ss;
124 }
125
126 /**
127 * Build the state history without waiting for signals or anything
128 */
129 private void buildManually() {
130 StateSystemBuildRequest request = new StateSystemBuildRequest(this);
131
132 /* Send the request to the trace here, since there is probably no
133 * experiment. */
134 sci.getTrace().sendRequest(request);
135 try {
136 request.waitForCompletion();
137 } catch (InterruptedException e) {
138 e.printStackTrace();
139 }
140 }
141
142
143 // ------------------------------------------------------------------------
144 // Signal handlers
145 // ------------------------------------------------------------------------
146
147 /**
148 * Listen to the "trace range updated" signal to start the state history
149 * construction.
150 *
151 * @param signal
152 * The "trace range updated" signal. Listening to this
153 * signal will coalesce this request with the one from the
154 * indexer and histogram.
155 */
156 @TmfSignalHandler
157 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal) {
158 ITmfTrace trace = signal.getTrace();
159 if (signal.getTrace() instanceof TmfExperiment) {
160 TmfExperiment experiment = (TmfExperiment) signal.getTrace();
161 for (ITmfTrace expTrace : experiment.getTraces()) {
162 if (expTrace == sci.getTrace()) {
163 trace = expTrace;
164 break;
165 }
166 }
167 }
168 if (trace != sci.getTrace()) {
169 return;
170 }
171 /* the signal is for this trace or for an experiment containing this trace */
172
173 if (!started) {
174 started = true;
175 StateSystemBuildRequest request = new StateSystemBuildRequest(this);
176 trace = signal.getTrace();
177 trace.sendRequest(request);
178 }
179 }
180
181 /**
182 * Listen to the "trace closed" signal to clean up if necessary.
183 *
184 * @param signal
185 * The "trace closed" signal.
186 */
187 @TmfSignalHandler
188 public void traceClosed(TmfTraceClosedSignal signal) {
189 ITmfTrace trace = signal.getTrace();
190 if (signal.getTrace() instanceof TmfExperiment) {
191 TmfExperiment experiment = (TmfExperiment) signal.getTrace();
192 for (ITmfTrace expTrace : experiment.getTraces()) {
193 if (expTrace == sci.getTrace()) {
194 trace = expTrace;
195 break;
196 }
197 }
198 }
199 if (trace != sci.getTrace()) {
200 return;
201 }
202 /* the signal is for this trace or for an experiment containing this trace */
203
204 if (!started) {
205 close(true);
206 }
207 }
208
209 // ------------------------------------------------------------------------
210 // Methods reserved for the request object below
211 // ------------------------------------------------------------------------
212
213 /** Get the input plugin object */
214 IStateChangeInput getInputPlugin() {
215 return sci;
216 }
217
218 void close(boolean deleteFiles) {
219 sci.dispose();
220 if (deleteFiles) {
221 hb.removeFiles();
222 }
223 dispose();
224 }
225 }
226
227 class StateSystemBuildRequest extends TmfEventRequest {
228
229 /** The amount of events queried at a time through the requests */
230 private final static int chunkSize = 50000;
231
232 private final HistoryBuilder builder;
233 private final IStateChangeInput sci;
234 private final ITmfTrace trace;
235
236 StateSystemBuildRequest(HistoryBuilder builder) {
237 super(builder.getInputPlugin().getExpectedEventType(),
238 TmfTimeRange.ETERNITY,
239 TmfDataRequest.ALL_DATA,
240 chunkSize,
241 ITmfDataRequest.ExecutionType.BACKGROUND);
242 this.builder = builder;
243 this.sci = builder.getInputPlugin();
244 this.trace = sci.getTrace();
245 }
246
247 @Override
248 public void handleData(final ITmfEvent event) {
249 super.handleData(event);
250 if (event != null) {
251 if (event.getTrace() == trace) {
252 sci.processEvent(event);
253 }
254 }
255 }
256
257 @Override
258 public void handleSuccess() {
259 super.handleSuccess();
260 builder.close(false);
261 }
262
263 @Override
264 public void handleCancel() {
265 super.handleCancel();
266 builder.close(true);
267 }
268
269 @Override
270 public void handleFailure() {
271 super.handleFailure();
272 builder.close(true);
273 }
274 }
This page took 0.035905 seconds and 5 git commands to generate.