ss: Use a factory to create SHTs
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Mon, 11 Jul 2016 01:31:07 +0000 (21:31 -0400)
committerAlexandre Montplaisir <alexmonthy@efficios.com>
Fri, 22 Jul 2016 23:19:15 +0000 (19:19 -0400)
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 <gbastien+lttng@versatic.net>
Signed-off-by: Alexandre Montplaisir <alexmonthy@efficios.com>
Reviewed-on: https://git.eclipse.org/r/77006
Reviewed-by: Hudson CI
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTree.java
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeBackend.java
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HistoryTreeFactory.java [new file with mode: 0644]

index 38ef0c1fc13785f951755dc94c5197d3cb0ba13f..d675866e0765fd547909271d2da64b7a6101a757 100644 (file)
@@ -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;
index c67c44c784f57fe3f8756b123862cc6094cd162e..75cea1a518c0d58b7bffe30d5a2808572603d73c 100644 (file)
@@ -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 (file)
index 0000000..bdf9bde
--- /dev/null
@@ -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$
+        }
+    }
+}
This page took 0.0282 seconds and 5 git commands to generate.