Commit | Line | Data |
---|---|---|
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 | ||
13 | package org.eclipse.linuxtools.tmf.core.statesystem; | |
14 | ||
15 | import java.io.File; | |
16 | import java.io.IOException; | |
17 | ||
2ab9afbc AM |
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; | |
6e71ce46 AM |
22 | import org.eclipse.linuxtools.tmf.core.component.TmfComponent; |
23 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; | |
6e71ce46 AM |
24 | |
25 | /** | |
26 | * This abstract manager class handles loading or creating state history files | |
27 | * for use in TMF's generic state system. | |
83134537 | 28 | * |
6e71ce46 | 29 | * @author alexmont |
83134537 | 30 | * |
6e71ce46 AM |
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. | |
83134537 | 42 | * |
6e71ce46 AM |
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 waitForCompletion | |
52 | * Should we block the calling thread until the construction is | |
53 | * complete? It has no effect if the file already exists. | |
54 | * @return A IStateSystemQuerier handler to the state system, with which you | |
55 | * can then run queries on the history. | |
56 | * @throws TmfTraceException | |
57 | */ | |
58 | public static IStateSystemQuerier loadStateHistory(File htFile, | |
51e216bd AM |
59 | IStateChangeInput htInput, boolean waitForCompletion) |
60 | throws TmfTraceException { | |
6e71ce46 AM |
61 | IStateSystemQuerier ss; |
62 | IStateHistoryBackend htBackend; | |
51e216bd | 63 | |
6e71ce46 AM |
64 | /* If the target file already exists, do not rebuild it uselessly */ |
65 | // TODO for now we assume it's complete. Might be a good idea to check | |
66 | // at least if its range matches the trace's range. | |
67 | if (htFile.exists()) { | |
68 | /* Load an existing history */ | |
69 | try { | |
70 | htBackend = new HistoryTreeBackend(htFile); | |
71 | ss = HistoryBuilder.openExistingHistory(htBackend); | |
72 | return ss; | |
73 | } catch (IOException e) { | |
74 | /* | |
75 | * There was an error opening the existing file. Perhaps it was | |
76 | * corrupted, perhaps it's an old version? We'll just | |
77 | * fall-through and try to build a new one from scratch instead. | |
78 | */ | |
79 | } | |
80 | } | |
81 | ||
82 | /* Create a new state history from scratch */ | |
83 | HistoryBuilder builder; | |
51e216bd | 84 | |
6e71ce46 AM |
85 | if (htInput == null) { |
86 | return null; | |
87 | } | |
88 | try { | |
89 | htBackend = new ThreadedHistoryTreeBackend(htFile, | |
90 | htInput.getStartTime(), QUEUE_SIZE); | |
91 | builder = new HistoryBuilder(htInput, htBackend); | |
6e71ce46 | 92 | } catch (IOException e) { |
51e216bd AM |
93 | /* |
94 | * If it fails here however, it means there was a problem writing to | |
95 | * the disk, so throw a real exception this time. | |
6e71ce46 AM |
96 | */ |
97 | throw new TmfTraceException(e.toString(), e); | |
98 | } | |
99 | return builder.getStateSystemQuerier(); | |
100 | } | |
101 | } |