tmf: Add an ID to each state system that gets built
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statesystem / StateSystemManager.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.File;
16 import java.io.IOException;
17
18 import org.eclipse.linuxtools.internal.tmf.core.statesystem.HistoryBuilder;
19 import org.eclipse.linuxtools.internal.tmf.core.statesystem.IStateHistoryBackend;
20 import org.eclipse.linuxtools.internal.tmf.core.statesystem.historytree.HistoryTreeBackend;
21 import org.eclipse.linuxtools.internal.tmf.core.statesystem.historytree.ThreadedHistoryTreeBackend;
22 import org.eclipse.linuxtools.tmf.core.component.TmfComponent;
23 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
24
25 /**
26 * This abstract manager class handles loading or creating state history files
27 * for use in TMF's generic state system.
28 *
29 * @version 1.0
30 * @author Alexandre Montplaisir
31 */
32 public abstract class StateSystemManager extends TmfComponent {
33
34 /** Size of the blocking queue to use when building a state history */
35 private final static int QUEUE_SIZE = 10000;
36
37 /**
38 * Load the history file matching the target trace. If the file already
39 * exists, it will be opened directly. If not, it will be created from
40 * scratch. In the case the history has to be built, it's possible to block
41 * the calling thread until construction is complete.
42 *
43 * @param htFile
44 * The target name of the history file we want to use. If it
45 * exists it will be opened. If it doesn't, a new file will be
46 * created with this name/path.
47 * @param htInput
48 * The IStateChangeInput to use for building the history file. It
49 * may be required even if we are opening an already-existing
50 * history (ie, for partial histories).
51 * @param id
52 * The ID, or name, to give to the state system we will build.
53 * The signal that when be sent when the construction is finished
54 * will carry this ID. It has no effect if the file already
55 * exists.
56 * @param buildManually
57 * If false, the construction will wait for a signal before
58 * starting. If true, it will build everything right now and
59 * block the caller. It has no effect if the file already exists.
60 * @return A IStateSystemQuerier handler to the state system, with which you
61 * can then run queries on the history.
62 * @throws TmfTraceException
63 * If there was a problem reading or writing one of the files.
64 * See the contents of this exception for more info.
65 * @since 2.0
66 */
67 public static ITmfStateSystem loadStateHistory(File htFile,
68 IStateChangeInput htInput, String id, boolean buildManually)
69 throws TmfTraceException {
70 ITmfStateSystem ss;
71 IStateHistoryBackend htBackend;
72
73 /* If the target file already exists, do not rebuild it uselessly */
74 // TODO for now we assume it's complete. Might be a good idea to check
75 // at least if its range matches the trace's range.
76 if (htFile.exists()) {
77 /* Load an existing history */
78 try {
79 htBackend = new HistoryTreeBackend(htFile);
80 ss = HistoryBuilder.openExistingHistory(htBackend);
81 return ss;
82 } catch (IOException e) {
83 /*
84 * There was an error opening the existing file. Perhaps it was
85 * corrupted, perhaps it's an old version? We'll just
86 * fall-through and try to build a new one from scratch instead.
87 */
88 }
89 }
90
91 /* Create a new state history from scratch */
92 HistoryBuilder builder;
93
94 if (htInput == null) {
95 return null;
96 }
97 try {
98 htBackend = new ThreadedHistoryTreeBackend(htFile,
99 htInput.getStartTime(), QUEUE_SIZE);
100 builder = new HistoryBuilder(htInput, htBackend, id, buildManually);
101 } catch (IOException e) {
102 /*
103 * If it fails here however, it means there was a problem writing to
104 * the disk, so throw a real exception this time.
105 */
106 throw new TmfTraceException(e.toString(), e);
107 }
108 return builder.getStateSystemQuerier();
109 }
110 }
This page took 0.032947 seconds and 5 git commands to generate.