tmf: Remove deprecated HistoryTree constructor
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statesystem / StateSystemManager.java
CommitLineData
6e71ce46
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>
83134537 5 *
6e71ce46
AM
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
83134537 10 *
6e71ce46
AM
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.statesystem;
14
15import java.io.File;
16import java.io.IOException;
17
2ab9afbc 18import org.eclipse.linuxtools.internal.tmf.core.statesystem.HistoryBuilder;
7e634be6 19import org.eclipse.linuxtools.internal.tmf.core.statesystem.StateSystem;
f9a76cac
AM
20import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.IStateHistoryBackend;
21import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.InMemoryBackend;
22import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.NullBackend;
23import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree.HistoryTreeBackend;
24import org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree.ThreadedHistoryTreeBackend;
6e71ce46
AM
25import org.eclipse.linuxtools.tmf.core.component.TmfComponent;
26import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
6e71ce46
AM
27
28/**
29 * This abstract manager class handles loading or creating state history files
30 * for use in TMF's generic state system.
83134537 31 *
2cb26548
AM
32 * @version 1.0
33 * @author Alexandre Montplaisir
6e71ce46
AM
34 */
35public abstract class StateSystemManager extends TmfComponent {
36
37 /** Size of the blocking queue to use when building a state history */
38 private final static int QUEUE_SIZE = 10000;
39
40 /**
41 * Load the history file matching the target trace. If the file already
42 * exists, it will be opened directly. If not, it will be created from
43 * scratch. In the case the history has to be built, it's possible to block
44 * the calling thread until construction is complete.
83134537 45 *
6e71ce46
AM
46 * @param htFile
47 * The target name of the history file we want to use. If it
48 * exists it will be opened. If it doesn't, a new file will be
49 * created with this name/path.
50 * @param htInput
51 * The IStateChangeInput to use for building the history file. It
52 * may be required even if we are opening an already-existing
53 * history (ie, for partial histories).
e34f7caa
AM
54 * @param buildManually
55 * If false, the construction will wait for a signal before
56 * starting. If true, it will build everything right now and
57 * block the caller. It has no effect if the file already exists.
6e71ce46
AM
58 * @return A IStateSystemQuerier handler to the state system, with which you
59 * can then run queries on the history.
60 * @throws TmfTraceException
5df842b3
AM
61 * If there was a problem reading or writing one of the files.
62 * See the contents of this exception for more info.
f1f86dfb 63 * @since 2.0
6e71ce46 64 */
f1f86dfb 65 public static ITmfStateSystem loadStateHistory(File htFile,
e45de797 66 IStateChangeInput htInput, boolean buildManually)
51e216bd 67 throws TmfTraceException {
6e71ce46 68 IStateHistoryBackend htBackend;
51e216bd 69
6e71ce46
AM
70 /* If the target file already exists, do not rebuild it uselessly */
71 // TODO for now we assume it's complete. Might be a good idea to check
72 // at least if its range matches the trace's range.
73 if (htFile.exists()) {
74 /* Load an existing history */
75 try {
76 htBackend = new HistoryTreeBackend(htFile);
7e634be6 77 ITmfStateSystem ss = HistoryBuilder.openExistingHistory(htBackend);
6e71ce46
AM
78 return ss;
79 } catch (IOException e) {
80 /*
81 * There was an error opening the existing file. Perhaps it was
82 * corrupted, perhaps it's an old version? We'll just
83 * fall-through and try to build a new one from scratch instead.
84 */
85 }
86 }
87
88 /* Create a new state history from scratch */
89 HistoryBuilder builder;
51e216bd 90
6e71ce46
AM
91 if (htInput == null) {
92 return null;
93 }
94 try {
7e634be6
AM
95 htBackend = new ThreadedHistoryTreeBackend(htFile, htInput.getStartTime(), QUEUE_SIZE);
96 StateSystem ss = new StateSystem(htBackend);
97 htInput.assignTargetStateSystem(ss);
98 builder = new HistoryBuilder(htInput, ss, htBackend, buildManually);
6e71ce46 99 } catch (IOException e) {
51e216bd
AM
100 /*
101 * If it fails here however, it means there was a problem writing to
102 * the disk, so throw a real exception this time.
6e71ce46
AM
103 */
104 throw new TmfTraceException(e.toString(), e);
105 }
106 return builder.getStateSystemQuerier();
107 }
f9a76cac
AM
108
109 /**
110 * Create a new state system using a null history back-end. This means that
111 * no history intervals will be saved anywhere, and as such only
112 * {@link ITmfStateSystem#queryOngoingState} will be available.
113 *
114 * This has to be built "manually" (which means you should call
115 * input.processEvent() to update the ongoing state of the state system).
116 *
117 * @param input
118 * The input plugin to build the history
119 * @return Reference to the history-less state system that got built
120 * @since 2.0
121 */
122 public static ITmfStateSystem newNullHistory(IStateChangeInput input) {
123 IStateHistoryBackend backend = new NullBackend();
7e634be6
AM
124 StateSystem ss = new StateSystem(backend);
125 input.assignTargetStateSystem(ss);
126
127 HistoryBuilder builder = new HistoryBuilder(input, ss, backend, true);
128 return builder.getStateSystemQuerier();
f9a76cac
AM
129 }
130
131 /**
132 * Create a new state system using in-memory interval storage. This should
133 * only be done for very small state system, and will be naturally limited
134 * to 2^31 intervals.
135 *
136 * This will block the caller while the construction is ongoing.
137 *
138 * @param input
139 * The state change input to use
140 * @param buildManually
141 * Set to true to block the caller and build without using TMF
142 * signals (for test programs most of the time). Use false if you
143 * are using the TMF facilities (experiments, etc.)
144 * @return Reference to the state system that just got built
145 * @since 2.0
146 */
147 public static ITmfStateSystem newInMemHistory(IStateChangeInput input,
148 boolean buildManually) {
149 IStateHistoryBackend backend = new InMemoryBackend(input.getStartTime());
7e634be6
AM
150 StateSystem ss = new StateSystem(backend);
151 input.assignTargetStateSystem(ss);
152
153 HistoryBuilder builder = new HistoryBuilder(input, ss, backend, buildManually);
f9a76cac
AM
154 return builder.getStateSystemQuerier();
155 }
6e71ce46 156}
This page took 0.040716 seconds and 5 git commands to generate.