tmf: Define the StateChangeInput's event type at the constructor
[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.TmfExperimentRangeUpdatedSignal;
24 import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
25 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
26 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
27 import org.eclipse.linuxtools.tmf.core.signal.TmfStateSystemBuildCompleted;
28 import org.eclipse.linuxtools.tmf.core.statesystem.IStateChangeInput;
29 import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemBuilder;
30 import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
31 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
32 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
33
34 /**
35 * This is the high-level wrapper around the State History and its input and
36 * storage plugins. Just create the object using the constructor then .run()
37 *
38 * You can use one HistoryBuilder and it will instantiate everything underneath.
39 * If you need more fine-grained control you can still ignore this and
40 * instantiate everything manually.
41 *
42 * @author alexmont
43 *
44 */
45 public class HistoryBuilder extends TmfComponent {
46
47 private final IStateChangeInput sci;
48 private final StateSystem ss;
49 private final IStateHistoryBackend hb;
50 private boolean started = true; /* Don't handle signals until we're ready */
51
52 /**
53 * Instantiate a new HistoryBuilder helper.
54 *
55 * @param stateChangeInput
56 * The input plugin to use. This is required.
57 * @param backend
58 * The backend storage to use.
59 * @param buildManually
60 * Should we build this history in-band or not. True means we
61 * will start the building ourselves and block the caller until
62 * construction is done. False (out-of-band) means we will
63 * start listening for the signal and return immediately. Another
64 * signal will be sent when finished.
65 * @throws IOException
66 * Is thrown if anything went wrong (usually with the storage
67 * backend)
68 */
69 public HistoryBuilder(IStateChangeInput stateChangeInput,
70 IStateHistoryBackend backend, boolean buildManually)
71 throws IOException {
72 if (stateChangeInput == null || backend == null) {
73 throw new IllegalArgumentException();
74 }
75 sci = stateChangeInput;
76 hb = backend;
77 ss = new StateSystem(hb, true);
78
79 sci.assignTargetStateSystem(ss);
80
81 if (buildManually) {
82 TmfSignalManager.deregister(this);
83 this.buildManually();
84 } else {
85 started = false;
86 /* We'll now wait for the signal to start building */
87 }
88 }
89
90 /**
91 * Factory-style method to open an existing history, you only have to
92 * provide the already-instantiated IStateHistoryBackend object.
93 *
94 * @param hb
95 * The history-backend object
96 * @return A IStateSystemBuilder reference to the new state system. If you
97 * will only run queries on this history, you should *definitely*
98 * cast it to IStateSystemQuerier.
99 * @throws IOException
100 * If there was something wrong.
101 */
102 public static IStateSystemBuilder openExistingHistory(
103 IStateHistoryBackend hb) throws IOException {
104 return new StateSystem(hb, false);
105 }
106
107 /**
108 * Return a read/write reference to the state system object that was
109 * created.
110 *
111 * @return Reference to the state system, with access to everything.
112 */
113 public IStateSystemBuilder getStateSystemBuilder() {
114 return ss;
115 }
116
117 /**
118 * Return a read-only reference to the state system object that was created.
119 *
120 * @return Reference to the state system, but only with the query methods
121 * available.
122 */
123 public IStateSystemQuerier getStateSystemQuerier() {
124 return ss;
125 }
126
127 /**
128 * Build the state history without waiting for signals or anything
129 */
130 private void buildManually() {
131 StateSystemBuildRequest request = new StateSystemBuildRequest(this);
132
133 /* Send the request to the trace here, since there is probably no
134 * experiment. */
135 sci.getTrace().sendRequest(request);
136 try {
137 request.waitForCompletion();
138 } catch (InterruptedException e) {
139 e.printStackTrace();
140 }
141 }
142
143
144 // ------------------------------------------------------------------------
145 // Signal handlers
146 // ------------------------------------------------------------------------
147
148 /**
149 * Listen to the "experiment selected" signal to start the state history
150 * construction.
151 *
152 * @param signal
153 * The "experiment range updated" signal. Listening to this
154 * signal will coalesce this request with the one from the
155 * indexer and histogram.
156 */
157 @TmfSignalHandler
158 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal) {
159 StateSystemBuildRequest request;
160 TmfExperiment exp;
161
162 if (!started) {
163 started = true;
164 request = new StateSystemBuildRequest(this);
165 exp = TmfExperiment.getCurrentExperiment();
166 if (exp == null) {
167 return;
168 }
169 exp.sendRequest(request);
170 }
171 }
172
173
174 // ------------------------------------------------------------------------
175 // Methods reserved for the request object below
176 // ------------------------------------------------------------------------
177
178 /** Get the input plugin object */
179 IStateChangeInput getInputPlugin() {
180 return sci;
181 }
182
183 void close(boolean deleteFiles) {
184 TmfSignal doneSig;
185
186 sci.dispose();
187 if (deleteFiles) {
188 hb.removeFiles();
189 /* We won't broadcast the signal if the request was cancelled */
190 } else {
191 /* Broadcast the signal saying the history is done building */
192 doneSig = new TmfStateSystemBuildCompleted(this, sci.getTrace());
193 TmfSignalManager.dispatchSignal(doneSig);
194 }
195
196 TmfSignalManager.deregister(this);
197 }
198 }
199
200 class StateSystemBuildRequest extends TmfEventRequest {
201
202 /** The amount of events queried at a time through the requests */
203 private final static int chunkSize = 50000;
204
205 private final HistoryBuilder builder;
206 private final IStateChangeInput sci;
207 private final ITmfTrace trace;
208
209 StateSystemBuildRequest(HistoryBuilder builder) {
210 super(builder.getInputPlugin().getExpectedEventType(),
211 TmfTimeRange.ETERNITY,
212 TmfDataRequest.ALL_DATA,
213 chunkSize,
214 ITmfDataRequest.ExecutionType.BACKGROUND);
215 this.builder = builder;
216 this.sci = builder.getInputPlugin();
217 this.trace = sci.getTrace();
218 }
219
220 @Override
221 public void handleData(final ITmfEvent event) {
222 super.handleData(event);
223 if (event != null) {
224 if (event.getTrace() == trace) {
225 sci.processEvent(event);
226 }
227 }
228 }
229
230 @Override
231 public void handleSuccess() {
232 super.handleSuccess();
233 builder.close(false);
234 }
235
236 @Override
237 public void handleCancel() {
238 super.handleCancel();
239 builder.close(true);
240 }
241
242 @Override
243 public void handleFailure() {
244 super.handleFailure();
245 builder.close(true);
246 }
247 }
This page took 0.035665 seconds and 5 git commands to generate.