ss: History trees can define their own node types
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / backend / historytree / HistoryTreeFactory.java
CommitLineData
9a4c098d
GB
1/*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
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
10package org.eclipse.tracecompass.internal.statesystem.core.backend.historytree;
11
12import java.io.IOException;
13import java.nio.ByteBuffer;
14import java.nio.ByteOrder;
15import java.nio.channels.ReadableByteChannel;
16import java.nio.file.Files;
17import java.nio.file.Path;
18import java.nio.file.StandardOpenOption;
19
20import org.eclipse.jdt.annotation.NonNullByDefault;
f4baf640 21import org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.classic.HistoryTreeClassic;
9a4c098d
GB
22
23/**
24 * Class that contains factory methods to build different types of history trees
25 *
26 * @author Geneviève Bastien
27 */
28@NonNullByDefault
29public final class HistoryTreeFactory {
30
31 private HistoryTreeFactory() {
32 }
33
34 /**
35 * Create a new State History from scratch, using a {@link HTConfig} object
36 * for configuration.
37 *
38 * @param conf
39 * The config to use for this History Tree.
40 * @return the new state history tree
41 * @throws IOException
42 * If an error happens trying to open/write to the file
43 * specified in the config
44 */
3a081e85
GB
45 public static IHistoryTree createHistoryTree(HTConfig conf) throws IOException {
46 return new HistoryTreeClassic(conf);
9a4c098d
GB
47 }
48
49 /**
50 * "Reader" factory : instantiate a SHTree from an existing tree file on
51 * disk
52 *
53 * @param existingStateFile
54 * Path/filename of the history-file we are to open
55 * @param expectedProviderVersion
56 * The expected version of the state provider
57 * @return The history tree
58 * @throws IOException
59 * If an error happens reading the file
60 */
3a081e85 61 public static IHistoryTree createFromFile(Path existingStateFile, int expectedProviderVersion) throws IOException {
9a4c098d
GB
62 /*
63 * Check the file exists and has a positive length. These verifications
64 * will also be done in the HT's constructor.
65 */
66 if (!Files.isReadable(existingStateFile)) {
67 throw new IOException("Selected state file does not exist or is not readable."); //$NON-NLS-1$
68 }
69 if (Files.size(existingStateFile) <= 0) {
70 throw new IOException("Empty target file"); //$NON-NLS-1$
71 }
72
73 /* Read the magic number from the history tree file */
74 ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES);
75 try (ReadableByteChannel channel = Files.newByteChannel(existingStateFile, StandardOpenOption.READ);) {
76 buffer.order(ByteOrder.LITTLE_ENDIAN);
77 buffer.clear();
78 channel.read(buffer);
79 buffer.flip();
80 }
81
82 /*
83 * Check the magic number to see which history tree class to create
84 */
85 int magicNumber = buffer.getInt();
86 switch (magicNumber) {
3a081e85
GB
87 case HistoryTreeClassic.HISTORY_FILE_MAGIC_NUMBER:
88 return new HistoryTreeClassic(existingStateFile.toFile(), expectedProviderVersion);
9a4c098d
GB
89 default:
90 throw new IOException("Not a known history tree file"); //$NON-NLS-1$
91 }
92 }
93}
This page took 0.029326 seconds and 5 git commands to generate.