Commit | Line | Data |
---|---|---|
e73a4ba5 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2013, 2014 École Polytechnique de Montréal |
e73a4ba5 GB |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Geneviève Bastien - Initial implementation and API | |
11 | *******************************************************************************/ | |
12 | ||
2bdf0193 | 13 | package org.eclipse.tracecompass.tmf.core.synchronization; |
e73a4ba5 GB |
14 | |
15 | import java.io.File; | |
16 | import java.io.IOException; | |
27213c57 | 17 | import java.util.Collection; |
e73a4ba5 | 18 | |
aa353506 | 19 | import org.eclipse.jdt.annotation.NonNull; |
2bdf0193 AM |
20 | import org.eclipse.tracecompass.internal.tmf.core.Activator; |
21 | import org.eclipse.tracecompass.tmf.core.component.TmfComponent; | |
22 | import org.eclipse.tracecompass.tmf.core.event.matching.ITmfEventMatching; | |
92bf3802 | 23 | import org.eclipse.tracecompass.tmf.core.event.matching.TmfEventMatching; |
2bdf0193 | 24 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
e73a4ba5 GB |
25 | |
26 | /** | |
27 | * This abstract manager class handles loading trace synchronization data or | |
28 | * otherwise their calculation. | |
29 | * | |
30 | * @author Geneviève Bastien | |
e73a4ba5 GB |
31 | */ |
32 | public abstract class SynchronizationManager extends TmfComponent { | |
33 | ||
34 | /** | |
35 | * Function called to synchronize traces using the fully incremental | |
36 | * synchronization algorithm | |
37 | * | |
38 | * @param syncFile | |
39 | * The target name of the synchronization file. If it exists, it | |
40 | * will be opened, otherwise it will be created and data from | |
41 | * this synchro run will be saved there | |
42 | * @param traces | |
43 | * The list of traces to synchronize | |
44 | * @param doSync | |
45 | * Whether to actually synchronize or just try opening a sync | |
46 | * file | |
47 | * @return The synchronization object | |
48 | */ | |
aa353506 AM |
49 | public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, |
50 | final Collection<@NonNull ITmfTrace> traces, boolean doSync) { | |
e73a4ba5 GB |
51 | |
52 | SynchronizationAlgorithm syncAlgo; | |
53 | if (doSync) { | |
51c08015 | 54 | syncAlgo = synchronize(syncFile, traces, SynchronizationAlgorithmFactory.getDefaultAlgorithm()); |
e73a4ba5 GB |
55 | } else { |
56 | syncAlgo = openExisting(syncFile); | |
57 | if (syncAlgo == null) { | |
51c08015 | 58 | syncAlgo = SynchronizationAlgorithmFactory.getDefaultAlgorithm(); |
e73a4ba5 GB |
59 | } |
60 | } | |
61 | return syncAlgo; | |
62 | } | |
63 | ||
64 | /** | |
65 | * Function called to synchronize traces with a specific synchronization | |
66 | * algorithm. If a synchronization already exists, but is not the requested | |
67 | * algorithm, the synchronization is done again using the new algorithm | |
68 | * | |
69 | * @param syncFile | |
70 | * The target name of the synchronization file. If it exists, it | |
71 | * will be opened, otherwise it will be created and data from | |
72 | * this synchro run will be saved there | |
73 | * @param traces | |
74 | * The list of traces to synchronize | |
75 | * @param algo | |
76 | * A synchronization algorithm object to determine the algorithm | |
77 | * used to synchronization. | |
78 | * @param doSync | |
79 | * Whether to actually synchronize or just try opening a sync | |
80 | * file | |
81 | * @return The synchronization object | |
82 | */ | |
aa353506 AM |
83 | public static SynchronizationAlgorithm synchronizeTraces(final File syncFile, |
84 | final Collection<@NonNull ITmfTrace> traces, SynchronizationAlgorithm algo, boolean doSync) { | |
e73a4ba5 GB |
85 | |
86 | SynchronizationAlgorithm syncAlgo; | |
87 | if (doSync) { | |
88 | syncAlgo = synchronize(syncFile, traces, algo); | |
89 | } else { | |
90 | syncAlgo = openExisting(syncFile); | |
91 | if (syncAlgo == null || (syncAlgo.getClass() != algo.getClass())) { | |
92 | if (algo != null) { | |
93 | syncAlgo = algo; | |
94 | } else { | |
51c08015 | 95 | syncAlgo = SynchronizationAlgorithmFactory.getDefaultAlgorithm(); |
e73a4ba5 GB |
96 | } |
97 | } | |
98 | } | |
99 | ||
100 | return syncAlgo; | |
101 | } | |
102 | ||
103 | private static SynchronizationAlgorithm openExisting(final File syncFile) { | |
104 | if ((syncFile != null) && syncFile.exists()) { | |
105 | /* Load an existing history */ | |
106 | try { | |
107 | SynchronizationBackend syncBackend = new SynchronizationBackend(syncFile); | |
108 | SynchronizationAlgorithm algo = syncBackend.openExistingSync(); | |
109 | return algo; | |
110 | } catch (IOException e) { | |
111 | /* | |
112 | * There was an error opening the existing file. Perhaps it was | |
113 | * corrupted, perhaps it's an old version? We'll just | |
114 | * fall-through and try to build a new one from scratch instead. | |
115 | */ | |
116 | Activator.logInfo("Problem opening existing trace synchronization file", e); //$NON-NLS-1$ | |
117 | } | |
118 | } | |
119 | return null; | |
120 | } | |
121 | ||
aa353506 AM |
122 | private static SynchronizationAlgorithm synchronize(final File syncFile, |
123 | final Collection<@NonNull ITmfTrace> traces, SynchronizationAlgorithm syncAlgo) { | |
92bf3802 | 124 | ITmfEventMatching matching = new TmfEventMatching(traces, syncAlgo); |
e73a4ba5 GB |
125 | matching.matchEvents(); |
126 | ||
127 | SynchronizationBackend syncBackend; | |
128 | try { | |
129 | syncBackend = new SynchronizationBackend(syncFile, false); | |
130 | syncBackend.saveSync(syncAlgo); | |
131 | } catch (IOException e) { | |
132 | Activator.logError("Error while saving trace synchronization file", e); //$NON-NLS-1$ | |
133 | } | |
134 | return syncAlgo; | |
135 | } | |
136 | ||
137 | } |