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