tmf: Cleanup generated files when a request is cancelled
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / 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.tmf.core.statesystem;
14
15 import java.io.IOException;
16
17 import org.eclipse.linuxtools.internal.tmf.core.statesystem.StateHistorySystem;
18 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
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.ITmfEventRequest;
22 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
23 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
24
25 /**
26 * This is the high-level wrapper around the State History and its input and
27 * storage plugins. Just create the object using the constructor then .run()
28 *
29 * You can use one HistoryBuilder and it will instantiate everything underneath.
30 * If you need more fine-grained control you can still ignore this and
31 * instantiate everything manually.
32 *
33 * @author alexmont
34 *
35 */
36 public class HistoryBuilder implements Runnable {
37
38 private final IStateChangeInput sci;
39 private final StateHistorySystem shs;
40 private final IStateHistoryBackend hb;
41
42 /**
43 * Instantiate a new HistoryBuilder helper.
44 *
45 * @param stateChangeInput
46 * The input plugin to use. This is required.
47 * @param backend
48 * The backend storage to use. Use "null" here if you want a
49 * state system with no history.
50 * @throws IOException
51 * Is thrown if anything went wrong (usually with the storage
52 * backend)
53 */
54 public HistoryBuilder(IStateChangeInput stateChangeInput,
55 IStateHistoryBackend backend) throws IOException {
56 if (stateChangeInput == null || backend == null) {
57 throw new IllegalArgumentException();
58 }
59
60 sci = stateChangeInput;
61 hb = backend;
62 shs = new StateHistorySystem(hb, true);
63
64 sci.assignTargetStateSystem(shs);
65 }
66
67 /**
68 * Factory-style method to open an existing history, you only have to
69 * provide the already-instantiated IStateHistoryBackend object.
70 *
71 * @param hb
72 * The history-backend object
73 * @return A IStateSystemBuilder reference to the new state system. If you
74 * will only run queries on this history, you should *definitely*
75 * cast it to IStateSystemQuerier.
76 * @throws IOException
77 * If there was something wrong.
78 */
79 public static IStateSystemBuilder openExistingHistory(
80 IStateHistoryBackend hb) throws IOException {
81 return new StateHistorySystem(hb, false);
82 }
83
84 @Override
85 public void run() {
86 /* Send a TMF request for all the events in the trace */
87 final ITmfEventRequest<CtfTmfEvent> request;
88 request = new StateSystemBuildRequest(this);
89
90 /* Submit the request and wait for completion */
91 sci.getTrace().sendRequest(request);
92 try {
93 request.waitForCompletion();
94 } catch (InterruptedException e) {
95 e.printStackTrace();
96 }
97 }
98
99 /**
100 * Return a read/write reference to the state system object that was
101 * created.
102 *
103 * @return Reference to the state system, with access to everything.
104 */
105 public IStateSystemBuilder getStateSystemBuilder() {
106 return shs;
107 }
108
109 /**
110 * Return a read-only reference to the state system object that was created.
111 *
112 * @return Reference to the state system, but only with the query methods
113 * available.
114 */
115 public IStateSystemQuerier getStateSystemQuerier() {
116 return shs;
117 }
118
119 /**
120 * @name Methods reserved for the request object below
121 */
122
123 /** Get the input plugin object */
124 IStateChangeInput getInputPlugin() {
125 return sci;
126 }
127
128 /** Tell the backend to cleanup if the request is cancelled */
129 void cleanup() {
130 hb.removeFiles();
131 }
132 }
133
134
135 class StateSystemBuildRequest extends TmfEventRequest<CtfTmfEvent> {
136
137 /** The amount of events queried at a time through the requests */
138 private final static int chunkSize = 50000;
139
140 private final HistoryBuilder builder;
141 private final IStateChangeInput sci;
142
143 StateSystemBuildRequest(HistoryBuilder builder) {
144 super((Class<CtfTmfEvent>) builder.getInputPlugin().getExpectedEventType().getClass(),
145 TmfTimeRange.ETERNITY, TmfDataRequest.ALL_DATA, chunkSize,
146 ITmfDataRequest.ExecutionType.BACKGROUND);
147 this.builder = builder;
148 this.sci = builder.getInputPlugin();
149 }
150
151 @Override
152 public void handleData(final CtfTmfEvent event) {
153 super.handleData(event);
154 if (event != null) {
155 sci.processEvent(event);
156 }
157 }
158
159 @Override
160 public void handleSuccess() {
161 //
162 }
163
164 @Override
165 public void handleCancel() {
166 sci.dispose();
167 builder.cleanup();
168 }
169
170 @Override
171 public void handleCompleted() {
172 super.handleCompleted();
173 sci.dispose();
174 }
175 }
This page took 0.037832 seconds and 5 git commands to generate.