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
;
21 import org
.eclipse
.tracecompass
.internal
.statesystem
.core
.backend
.historytree
.classic
.HistoryTreeClassic
;
24 * Class that contains factory methods to build different types of history trees
26 * @author Geneviève Bastien
29 public final class HistoryTreeFactory
{
31 private HistoryTreeFactory() {
35 * Create a new State History from scratch, using a {@link HTConfig} object
39 * The config to use for this History Tree.
40 * @return the new state history tree
42 * If an error happens trying to open/write to the file
43 * specified in the config
45 public static IHistoryTree
createHistoryTree(HTConfig conf
) throws IOException
{
46 return new HistoryTreeClassic(conf
);
50 * "Reader" factory : instantiate a SHTree from an existing tree file on
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
59 * If an error happens reading the file
61 public static IHistoryTree
createFromFile(Path existingStateFile
, int expectedProviderVersion
) throws IOException
{
63 * Check the file exists and has a positive length. These verifications
64 * will also be done in the HT's constructor.
66 if (!Files
.isReadable(existingStateFile
)) {
67 throw new IOException("Selected state file does not exist or is not readable."); //$NON-NLS-1$
69 if (Files
.size(existingStateFile
) <= 0) {
70 throw new IOException("Empty target file"); //$NON-NLS-1$
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
);
83 * Check the magic number to see which history tree class to create
85 int magicNumber
= buffer
.getInt();
86 switch (magicNumber
) {
87 case HistoryTreeClassic
.HISTORY_FILE_MAGIC_NUMBER
:
88 return new HistoryTreeClassic(existingStateFile
.toFile(), expectedProviderVersion
);
90 throw new IOException("Not a known history tree file"); //$NON-NLS-1$