From 9a4c098dc7e06b6e1ceaeb15b0cfaeeeb5f74bec Mon Sep 17 00:00:00 2001 From: =?utf8?q?Genevi=C3=A8ve=20Bastien?= Date: Sun, 10 Jul 2016 21:31:07 -0400 Subject: [PATCH] ss: Use a factory to create SHTs MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This is a first step to allow different history tree types to coexist in this plugin. Change-Id: I411d2a1a6de06258d98090ce418eeb24e80303a1 Signed-off-by: Geneviève Bastien Signed-off-by: Alexandre Montplaisir Reviewed-on: https://git.eclipse.org/r/77006 Reviewed-by: Hudson CI --- .../core/backend/historytree/HistoryTree.java | 6 +- .../historytree/HistoryTreeBackend.java | 4 +- .../historytree/HistoryTreeFactory.java | 93 +++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) create mode 100644 statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeFactory.java diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTree.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTree.java index 38ef0c1fc1..d675866e07 100644 --- a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTree.java +++ b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTree.java @@ -49,7 +49,11 @@ public class HistoryTree { */ public static final int TREE_HEADER_SIZE = 4096; - private static final int HISTORY_FILE_MAGIC_NUMBER = 0x05FFA900; + /** + * The magic number for this file format. Package-private for the factory + * class. + */ + static final int HISTORY_FILE_MAGIC_NUMBER = 0x05FFA900; /** File format version. Increment when breaking compatibility. */ private static final int FILE_VERSION = 7; diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeBackend.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeBackend.java index c67c44c784..75cea1a518 100644 --- a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeBackend.java +++ b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeBackend.java @@ -163,7 +163,7 @@ public class HistoryTreeBackend implements IStateHistoryBackend { */ @VisibleForTesting protected @NonNull HistoryTree initializeSHT(@NonNull HTConfig conf) throws IOException { - return new HistoryTree(conf); + return HistoryTreeFactory.createHistoryTree(conf); } /** @@ -180,7 +180,7 @@ public class HistoryTreeBackend implements IStateHistoryBackend { */ @VisibleForTesting protected @NonNull HistoryTree initializeSHT(@NonNull File existingStateFile, int providerVersion) throws IOException { - return new HistoryTree(existingStateFile, providerVersion); + return HistoryTreeFactory.createFromFile(existingStateFile.toPath(), providerVersion); } /** diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeFactory.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeFactory.java new file mode 100644 index 0000000000..bdf9bde3c4 --- /dev/null +++ b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeFactory.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (c) 2016 École Polytechnique de Montréal + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v1.0 which + * accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + *******************************************************************************/ + +package org.eclipse.tracecompass.internal.statesystem.core.backend.historytree; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.channels.ReadableByteChannel; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; + +import org.eclipse.jdt.annotation.NonNullByDefault; + +/** + * Class that contains factory methods to build different types of history trees + * + * @author Geneviève Bastien + */ +@NonNullByDefault +public final class HistoryTreeFactory { + + private HistoryTreeFactory() { + } + + /** + * Create a new State History from scratch, using a {@link HTConfig} object + * for configuration. + * + * @param conf + * The config to use for this History Tree. + * @return the new state history tree + * @throws IOException + * If an error happens trying to open/write to the file + * specified in the config + */ + public static HistoryTree createHistoryTree(HTConfig conf) throws IOException { + return new HistoryTree(conf); + } + + /** + * "Reader" factory : instantiate a SHTree from an existing tree file on + * disk + * + * @param existingStateFile + * Path/filename of the history-file we are to open + * @param expectedProviderVersion + * The expected version of the state provider + * @return The history tree + * @throws IOException + * If an error happens reading the file + */ + public static HistoryTree createFromFile(Path existingStateFile, int expectedProviderVersion) throws IOException { + + /* + * Check the file exists and has a positive length. These verifications + * will also be done in the HT's constructor. + */ + if (!Files.isReadable(existingStateFile)) { + throw new IOException("Selected state file does not exist or is not readable."); //$NON-NLS-1$ + } + if (Files.size(existingStateFile) <= 0) { + throw new IOException("Empty target file"); //$NON-NLS-1$ + } + + /* Read the magic number from the history tree file */ + ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); + try (ReadableByteChannel channel = Files.newByteChannel(existingStateFile, StandardOpenOption.READ);) { + buffer.order(ByteOrder.LITTLE_ENDIAN); + buffer.clear(); + channel.read(buffer); + buffer.flip(); + } + + /* + * Check the magic number to see which history tree class to create + */ + int magicNumber = buffer.getInt(); + switch (magicNumber) { + case HistoryTree.HISTORY_FILE_MAGIC_NUMBER: + return new HistoryTree(existingStateFile.toFile(), expectedProviderVersion); + default: + throw new IOException("Not a known history tree file"); //$NON-NLS-1$ + } + } +} -- 2.34.1