1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
;
15 import java
.io
.IOException
;
17 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.IStateHistoryBackend
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfComponent
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
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
.timestamp
.TmfTimeRange
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
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()
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.
44 public class HistoryBuilder
extends TmfComponent
{
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 */
52 * Instantiate a new HistoryBuilder helper. The input -> ss -> backend
53 * relationships should have been set up already.
55 * @param stateChangeInput
56 * The input plugin to use
58 * The state system object that will receive the state changes
61 * The back-end storage to use, which will receive the intervals
63 * @param buildManually
64 * Should we build this history in-band or not. True means we
65 * will start the building ourselves and block the caller until
66 * construction is done. False (out-of-band) means we will start
67 * listening for the signal and return immediately.
69 public HistoryBuilder(IStateChangeInput stateChangeInput
, StateSystem ss
,
70 IStateHistoryBackend backend
, boolean buildManually
) {
71 if (stateChangeInput
== null || backend
== null || ss
== null) {
72 throw new IllegalArgumentException();
74 if (stateChangeInput
.getAssignedStateSystem() != ss
) {
75 /* Logic check to make sure the input is setup properly */
76 throw new RuntimeException();
79 sci
= stateChangeInput
;
84 TmfSignalManager
.deregister(this);
88 /* We'll now wait for the signal to start building */
93 * Factory-style method to open an existing history, you only have to
94 * provide the already-instantiated IStateHistoryBackend object.
97 * The history-backend object
98 * @return A IStateSystemBuilder reference to the new state system. If you
99 * will only run queries on this history, you should *definitely*
100 * cast it to IStateSystemQuerier.
101 * @throws IOException
102 * If there was something wrong.
104 public static ITmfStateSystemBuilder
openExistingHistory(
105 IStateHistoryBackend hb
) throws IOException
{
106 return new StateSystem(hb
, false);
110 * Return a read/write reference to the state system object that was
113 * @return Reference to the state system, with access to everything.
115 public ITmfStateSystemBuilder
getStateSystemBuilder() {
120 * Return a read-only reference to the state system object that was created.
122 * @return Reference to the state system, but only with the query methods
125 public ITmfStateSystem
getStateSystemQuerier() {
130 * Build the state history without waiting for signals or anything
132 private void buildManually() {
133 StateSystemBuildRequest request
= new StateSystemBuildRequest(this);
135 /* Send the request to the trace here, since there is probably no
137 sci
.getTrace().sendRequest(request
);
139 request
.waitForCompletion();
140 } catch (InterruptedException e
) {
146 // ------------------------------------------------------------------------
148 // ------------------------------------------------------------------------
151 * Listen to the "trace range updated" signal to start the state history
155 * The "trace range updated" signal. Listening to this
156 * signal will coalesce this request with the one from the
157 * indexer and histogram.
160 public void traceRangeUpdated(final TmfTraceRangeUpdatedSignal signal
) {
162 * Check if this signal is for this trace, or for an experiment
163 * containing this trace.
165 ITmfTrace sender
= signal
.getTrace();
166 ITmfTrace target
= null;
167 for (ITmfTrace trace
: sender
.getTraces()) {
168 if (trace
== sci
.getTrace()) {
174 if (target
== null) {
180 StateSystemBuildRequest request
= new StateSystemBuildRequest(this);
181 sender
.sendRequest(request
);
186 * Listen to the "trace closed" signal to clean up if necessary.
189 * The "trace closed" signal.
192 public void traceClosed(TmfTraceClosedSignal signal
) {
194 * Check if this signal is for this trace, or for an experiment
195 * containing this trace.
197 boolean found
= false;
198 for (ITmfTrace trace
: signal
.getTrace().getTraces()) {
199 if (trace
== sci
.getTrace()) {
205 if (found
&& !started
) {
210 // ------------------------------------------------------------------------
211 // Methods reserved for the request object below
212 // ------------------------------------------------------------------------
214 /** Get the input plugin object */
215 IStateChangeInput
getInputPlugin() {
219 void close(boolean deleteFiles
) {
228 class StateSystemBuildRequest
extends TmfEventRequest
{
230 /** The amount of events queried at a time through the requests */
231 private final static int chunkSize
= 50000;
233 private final HistoryBuilder builder
;
234 private final IStateChangeInput sci
;
235 private final ITmfTrace trace
;
237 StateSystemBuildRequest(HistoryBuilder builder
) {
238 super(builder
.getInputPlugin().getExpectedEventType(),
239 TmfTimeRange
.ETERNITY
,
240 TmfDataRequest
.ALL_DATA
,
242 ITmfDataRequest
.ExecutionType
.BACKGROUND
);
243 this.builder
= builder
;
244 this.sci
= builder
.getInputPlugin();
245 this.trace
= sci
.getTrace();
249 public void handleData(final ITmfEvent event
) {
250 super.handleData(event
);
252 if (event
.getTrace() == trace
) {
253 sci
.processEvent(event
);
259 public void handleSuccess() {
260 super.handleSuccess();
261 builder
.close(false);
265 public void handleCancel() {
266 super.handleCancel();
271 public void handleFailure() {
272 super.handleFailure();