tmf: Update the Javadoc for everything GSS-related
[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 StateHistorySystem shs;
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 shs = new StateHistorySystem(hb, true);
78
79 sci.assignTargetStateSystem(shs);
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 StateHistorySystem(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 shs;
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 shs;
125 }
126
127 /**
128 * Build the state history without waiting for signals or anything
129 */
130 @SuppressWarnings("unchecked")
131 private void buildManually() {
132 StateSystemBuildRequest request = new StateSystemBuildRequest(this);
133
134 /* Send the request to the trace here, since there is probably no
135 * experiment. */
136 sci.getTrace().sendRequest(request);
137 try {
138 request.waitForCompletion();
139 } catch (InterruptedException e) {
140 e.printStackTrace();
141 }
142 }
143
144
145 // ------------------------------------------------------------------------
146 // Signal handlers
147 // ------------------------------------------------------------------------
148
149 /**
150 * Listen to the "experiment selected" signal to start the state history
151 * construction.
152 *
153 * @param signal
154 * The "experiment range updated" signal. Listening to this
155 * signal will coalesce this request with the one from the
156 * indexer and histogram.
157 */
158 @SuppressWarnings("unchecked")
159 @TmfSignalHandler
160 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal) {
161 StateSystemBuildRequest request;
162 TmfExperiment<ITmfEvent> exp;
163
164 if (!started) {
165 started = true;
166 request = new StateSystemBuildRequest(this);
167 exp = (TmfExperiment<ITmfEvent>) TmfExperiment.getCurrentExperiment();
168 if (exp == null) {
169 return;
170 }
171 exp.sendRequest(request);
172 }
173 }
174
175
176 // ------------------------------------------------------------------------
177 // Methods reserved for the request object below
178 // ------------------------------------------------------------------------
179
180 /** Get the input plugin object */
181 IStateChangeInput getInputPlugin() {
182 return sci;
183 }
184
185 void close(boolean deleteFiles) {
186 sci.dispose();
187 if (deleteFiles) {
188 hb.removeFiles();
189 }
190
191 /* Broadcast the signal saying the history is done building */
192 TmfSignal doneSig = new TmfStateSystemBuildCompleted(this, sci.getTrace());
193 TmfSignalManager.dispatchSignal(doneSig);
194
195 TmfSignalManager.deregister(this);
196 }
197 }
198
199 class StateSystemBuildRequest extends TmfEventRequest<ITmfEvent> {
200
201 /** The amount of events queried at a time through the requests */
202 private final static int chunkSize = 50000;
203
204 private final HistoryBuilder builder;
205 private final IStateChangeInput sci;
206 private final ITmfTrace<ITmfEvent> trace;
207
208 @SuppressWarnings("unchecked")
209 StateSystemBuildRequest(HistoryBuilder builder) {
210 super((Class<ITmfEvent>) builder.getInputPlugin().getExpectedEventType().getClass(),
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.036114 seconds and 5 git commands to generate.