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