1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
;
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
;
20 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
23 * Class that contains factory methods to build different types of history trees
25 * @author Geneviève Bastien
28 public final class HistoryTreeFactory
{
30 private HistoryTreeFactory() {
34 * Create a new State History from scratch, using a {@link HTConfig} object
38 * The config to use for this History Tree.
39 * @return the new state history tree
41 * If an error happens trying to open/write to the file
42 * specified in the config
44 public static IHistoryTree
createHistoryTree(HTConfig conf
) throws IOException
{
45 return new HistoryTreeClassic(conf
);
49 * "Reader" factory : instantiate a SHTree from an existing tree file on
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
58 * If an error happens reading the file
60 public static IHistoryTree
createFromFile(Path existingStateFile
, int expectedProviderVersion
) throws IOException
{
62 * Check the file exists and has a positive length. These verifications
63 * will also be done in the HT's constructor.
65 if (!Files
.isReadable(existingStateFile
)) {
66 throw new IOException("Selected state file does not exist or is not readable."); //$NON-NLS-1$
68 if (Files
.size(existingStateFile
) <= 0) {
69 throw new IOException("Empty target file"); //$NON-NLS-1$
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
);
82 * Check the magic number to see which history tree class to create
84 int magicNumber
= buffer
.getInt();
85 switch (magicNumber
) {
86 case HistoryTreeClassic
.HISTORY_FILE_MAGIC_NUMBER
:
87 return new HistoryTreeClassic(existingStateFile
.toFile(), expectedProviderVersion
);
89 throw new IOException("Not a known history tree file"); //$NON-NLS-1$