Commit | Line | Data |
---|---|---|
a52fde77 AM |
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 | ||
18ab1d18 | 13 | package org.eclipse.linuxtools.tmf.core.statesystem; |
a52fde77 AM |
14 | |
15 | import java.io.IOException; | |
16 | ||
18ab1d18 | 17 | import org.eclipse.linuxtools.internal.tmf.core.statesystem.StateHistorySystem; |
2c2f900e AM |
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; | |
a52fde77 AM |
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; | |
d26f90fd | 39 | private final StateHistorySystem shs; |
a52fde77 AM |
40 | private final IStateHistoryBackend hb; |
41 | ||
a52fde77 AM |
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 | assert (stateChangeInput != null); | |
d26f90fd | 57 | assert (backend != null); |
a52fde77 | 58 | |
d26f90fd AM |
59 | sci = stateChangeInput; |
60 | hb = backend; | |
61 | shs = new StateHistorySystem(hb, true); | |
a52fde77 | 62 | |
d26f90fd | 63 | sci.assignTargetStateSystem(shs); |
a52fde77 AM |
64 | } |
65 | ||
d26f90fd AM |
66 | /** |
67 | * Factory-style method to open an existing history, you only have to | |
68 | * provide the already-instantiated IStateHistoryBackend object. | |
69 | * | |
70 | * @param hb | |
71 | * The history-backend object | |
72 | * @return A IStateSystemBuilder reference to the new state system. If you | |
73 | * will only run queries on this history, you should *definitely* | |
74 | * cast it to IStateSystemQuerier. | |
75 | * @throws IOException | |
76 | * If there was something wrong. | |
77 | */ | |
78 | public static IStateSystemBuilder openExistingHistory( | |
79 | IStateHistoryBackend hb) throws IOException { | |
80 | return new StateHistorySystem(hb, false); | |
81 | } | |
82 | ||
a52fde77 AM |
83 | @Override |
84 | public void run() { | |
2c2f900e AM |
85 | /* Send a TMF request for all the events in the trace */ |
86 | final ITmfEventRequest<CtfTmfEvent> request; | |
87 | request = new StateSystemBuildRequest(sci); | |
a52fde77 | 88 | |
2c2f900e AM |
89 | /* Submit the request and wait for completion */ |
90 | sci.getTrace().sendRequest(request); | |
a52fde77 | 91 | try { |
2c2f900e | 92 | request.waitForCompletion(); |
a52fde77 AM |
93 | } catch (InterruptedException e) { |
94 | e.printStackTrace(); | |
a52fde77 AM |
95 | } |
96 | } | |
97 | ||
98 | /** | |
d26f90fd AM |
99 | * Return a read/write reference to the state system object that was |
100 | * created. | |
101 | * | |
102 | * @return Reference to the state system, with access to everything. | |
103 | */ | |
104 | public IStateSystemBuilder getStateSystemBuilder() { | |
105 | return shs; | |
106 | } | |
107 | ||
108 | /** | |
109 | * Return a read-only reference to the state system object that was created. | |
a52fde77 | 110 | * |
d26f90fd AM |
111 | * @return Reference to the state system, but only with the query methods |
112 | * available. | |
a52fde77 | 113 | */ |
d26f90fd AM |
114 | public IStateSystemQuerier getStateSystemQuerier() { |
115 | return shs; | |
a52fde77 | 116 | } |
2c2f900e AM |
117 | } |
118 | ||
119 | class StateSystemBuildRequest extends TmfEventRequest<CtfTmfEvent> { | |
120 | ||
121 | /** The amount of events queried at a time through the requests */ | |
122 | private final static int chunkSize = 50000; | |
123 | ||
124 | private final IStateChangeInput sci; | |
125 | ||
126 | StateSystemBuildRequest(IStateChangeInput sci) { | |
127 | super((Class<CtfTmfEvent>) sci.getExpectedEventType().getClass(), | |
128 | TmfTimeRange.ETERNITY, TmfDataRequest.ALL_DATA, chunkSize, | |
129 | ITmfDataRequest.ExecutionType.BACKGROUND); | |
130 | this.sci = sci; | |
131 | } | |
a52fde77 | 132 | |
2c2f900e AM |
133 | @Override |
134 | public void handleData(final CtfTmfEvent event) { | |
135 | super.handleData(event); | |
136 | if (event != null) { | |
137 | sci.processEvent(event); | |
138 | } | |
139 | } | |
140 | ||
141 | @Override | |
142 | public void handleSuccess() { | |
143 | // | |
144 | } | |
145 | ||
146 | @Override | |
147 | public void handleCompleted() { | |
148 | super.handleCompleted(); | |
149 | sci.dispose(); | |
150 | } | |
a52fde77 | 151 | } |