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>
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
.tmf
.core
.statesystem
;
15 import java
.io
.IOException
;
16 import java
.util
.concurrent
.Semaphore
;
18 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.StateHistorySystem
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfComponent
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentRangeUpdatedSignal
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
33 * This is the high-level wrapper around the State History and its input and
34 * storage plugins. Just create the object using the constructor then .run()
36 * You can use one HistoryBuilder and it will instantiate everything underneath.
37 * If you need more fine-grained control you can still ignore this and
38 * instantiate everything manually.
43 public class HistoryBuilder
extends TmfComponent
{
45 private static final Semaphore hbSem
= new Semaphore(1);
47 private ITmfEventRequest
<CtfTmfEvent
> currentRequest
= null;
48 private boolean isRunning
= false;
50 private final IStateChangeInput sci
;
51 private final StateHistorySystem shs
;
52 private final IStateHistoryBackend hb
;
55 * Instantiate a new HistoryBuilder helper.
57 * @param stateChangeInput
58 * The input plugin to use. This is required.
60 * The backend storage to use. Use "null" here if you want a
61 * state system with no history.
63 * Is thrown if anything went wrong (usually with the storage
66 public HistoryBuilder(IStateChangeInput stateChangeInput
,
67 IStateHistoryBackend backend
) throws IOException
{
68 if (stateChangeInput
== null || backend
== null) {
69 throw new IllegalArgumentException();
71 sci
= stateChangeInput
;
73 shs
= new StateHistorySystem(hb
, true);
75 sci
.assignTargetStateSystem(shs
);
79 * Factory-style method to open an existing history, you only have to
80 * provide the already-instantiated IStateHistoryBackend object.
83 * The history-backend object
84 * @return A IStateSystemBuilder reference to the new state system. If you
85 * will only run queries on this history, you should *definitely*
86 * cast it to IStateSystemQuerier.
88 * If there was something wrong.
90 public static IStateSystemBuilder
openExistingHistory(
91 IStateHistoryBackend hb
) throws IOException
{
92 return new StateHistorySystem(hb
, false);
96 * Return a read/write reference to the state system object that was
99 * @return Reference to the state system, with access to everything.
101 public IStateSystemBuilder
getStateSystemBuilder() {
106 * Return a read-only reference to the state system object that was created.
108 * @return Reference to the state system, but only with the query methods
111 public IStateSystemQuerier
getStateSystemQuerier() {
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
120 * Signal handler to start building the configured state history.
122 * This could also be called by anyone to trigger the building of the
123 * history without using any TMF signals. Simply pass 'null' as a parameter
126 * @param signal The signal that triggered the build
129 public void startBuilding(final TmfExperimentRangeUpdatedSignal signal
) {
130 /* Start the construction of the history if it's not started yet */
131 if (!this.isRunning
) {
132 hbSem
.acquireUninterruptibly();
133 currentRequest
= new StateSystemBuildRequest(this);
135 sci
.getTrace().sendRequest(currentRequest
);
140 * Signal handler to cancel any currently running requests. It will delete
141 * any incomplete file that might have been created.
143 * This could also be called by anyone to cancel the current request without
144 * using any TMF signals. Simply pass 'null' as a parameter then.
149 public void cancelCurrentBuild(final TmfExperimentSelectedSignal
<?
extends ITmfEvent
> signal
) {
151 * We've switched experiments (or re-opened the current one), so stop
152 * whatever request is currently running.
154 if (this.isRunning
&& currentRequest
!= null) {
155 currentRequest
.cancel();
160 // ------------------------------------------------------------------------
161 // Methods reserved for the request object below
162 // ------------------------------------------------------------------------
164 /** Get the input plugin object */
165 IStateChangeInput
getInputPlugin() {
169 /** Shutdown this builder object when the request is over */
170 void finish(boolean deleteFile
) {
175 currentRequest
= null;
177 TmfSignalManager
.deregister(this);
183 class StateSystemBuildRequest
extends TmfEventRequest
<CtfTmfEvent
> {
185 /** The amount of events queried at a time through the requests */
186 private final static int chunkSize
= 50000;
188 private final HistoryBuilder builder
;
189 private final IStateChangeInput sci
;
191 StateSystemBuildRequest(HistoryBuilder builder
) {
192 super((Class
<CtfTmfEvent
>) builder
.getInputPlugin().getExpectedEventType().getClass(),
193 TmfTimeRange
.ETERNITY
, TmfDataRequest
.ALL_DATA
, chunkSize
,
194 ITmfDataRequest
.ExecutionType
.BACKGROUND
);
195 this.builder
= builder
;
196 this.sci
= builder
.getInputPlugin();
200 public void handleData(final CtfTmfEvent event
) {
201 super.handleData(event
);
203 sci
.processEvent(event
);
208 public void handleSuccess() {
209 super.handleSuccess();
210 builder
.finish(false);
214 public void handleCancel() {
215 super.handleCancel();
216 builder
.finish(true);
220 public void handleFailure() {
221 super.handleFailure();
222 builder
.finish(true);