ss: Move the safe byte buffer to datastore plugin
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Fri, 25 Nov 2016 20:07:54 +0000 (15:07 -0500)
committerAlexandre Montplaisir <alexmonthy@efficios.com>
Thu, 5 Jan 2017 22:06:28 +0000 (17:06 -0500)
The unit tests also moved

Change-Id: I9118cb56bad032ce61065a5a81513c03a47fcc84
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/85799
Reviewed-by: Hudson CI
Reviewed-by: Alexandre Montplaisir <alexmonthy@efficios.com>
23 files changed:
statesystem/org.eclipse.tracecompass.datastore.core.tests/META-INF/MANIFEST.MF
statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/datastore/core/serialization/SafeByteBufferWrapperTest.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.datastore.core/META-INF/MANIFEST.MF
statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/datastore/core/serialization/SafeByteBufferWrapper.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/datastore/core/serialization/package-info.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/ISafeByteBufferReader.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/ISafeByteBufferWriter.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/SafeByteBufferFactory.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/package-info.java [new file with mode: 0644]
statesystem/org.eclipse.tracecompass.statesystem.core.tests/META-INF/MANIFEST.MF
statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/CustomStateValueTest.java
statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/SafeByteBufferWrapperTest.java [deleted file]
statesystem/org.eclipse.tracecompass.statesystem.core.tests/stubs/org/eclipse/tracecompass/statesystem/core/tests/stubs/statevalues/CustomStateValueStub.java
statesystem/org.eclipse.tracecompass.statesystem.core/META-INF/MANIFEST.MF
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/CustomStateValue.java
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/ISafeByteBufferReader.java [deleted file]
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/ISafeByteBufferWriter.java [deleted file]
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/SafeByteBufferFactory.java [deleted file]
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/backend/historytree/HTInterval.java
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/statevalue/SafeByteBufferWrapper.java [deleted file]
statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/statevalue/package-info.java [deleted file]
statesystem/pom.xml
tmf/org.eclipse.tracecompass.tmf/feature.xml

index ae41c3d204c5bb96b13149a163ed8cc20574aed6..8fcea04745f51735f683f62015e94d87bbdefee2 100644 (file)
@@ -8,3 +8,4 @@ Bundle-SymbolicName: org.eclipse.tracecompass.datastore.core.tests;singleton:=tr
 Fragment-Host: org.eclipse.tracecompass.datastore.core
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Require-Bundle: org.junit;bundle-version="4.0.0"
+Export-Package: org.eclipse.tracecompass.internal.datastore.core.serialization
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/datastore/core/serialization/SafeByteBufferWrapperTest.java b/statesystem/org.eclipse.tracecompass.datastore.core.tests/src/org/eclipse/tracecompass/internal/datastore/core/serialization/SafeByteBufferWrapperTest.java
new file mode 100644 (file)
index 0000000..80df618
--- /dev/null
@@ -0,0 +1,357 @@
+/*******************************************************************************
+ * 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.datastore.core.serialization;
+
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferReader;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferWriter;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.SafeByteBufferFactory;
+import org.junit.Test;
+
+/**
+ * Test for the {@link SafeByteBufferWrapper} class
+ *
+ * @author Geneviève Bastien
+ */
+public class SafeByteBufferWrapperTest {
+
+    private final ByteBuffer fMainBuffer;
+
+    /**
+     * Constructor. Prepares the main buffer and safe buffer
+     */
+    public SafeByteBufferWrapperTest() {
+        fMainBuffer = ByteBuffer.allocate(1024);
+    }
+
+    /**
+     * Test the {@link SafeByteBufferWrapper#put(byte)}
+     * {@link SafeByteBufferWrapper#get()} methods
+     */
+    @Test
+    public void testReadWriteByte() {
+        byte val = Byte.MAX_VALUE;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.put(val);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        assertEquals(val, reader.get());
+    }
+
+    /**
+     * Test the {@link SafeByteBufferWrapper#put(byte[])}
+     * {@link SafeByteBufferWrapper#get(byte[])} methods
+     */
+    @Test
+    public void testReadWriteByteArray() {
+        byte[] val = { 0, 2, 1, 3 };
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.put(val);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        byte[] ret = new byte[4];
+        reader.get(ret);
+        assertArrayEquals(val, ret);
+    }
+
+    /**
+     * Test the {@link SafeByteBufferWrapper#putChar(char)}
+     * {@link SafeByteBufferWrapper#getChar()} methods
+     */
+    @Test
+    public void testReadWriteChar() {
+        char val = 24;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.putChar(val);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        assertEquals(val, reader.getChar());
+    }
+
+    /**
+     * Test the {@link SafeByteBufferWrapper#putDouble(double)}
+     * {@link SafeByteBufferWrapper#getDouble()} methods
+     */
+    @Test
+    public void testReadWriteDouble() {
+        double val = Double.MAX_VALUE;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.putDouble(val);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        assertEquals(val, reader.getDouble(), 10);
+    }
+
+    /**
+     * Test the {@link SafeByteBufferWrapper#putFloat(float)}
+     * {@link SafeByteBufferWrapper#getFloat()} methods
+     */
+    @Test
+    public void testReadWriteFloat() {
+        float val = Float.MIN_VALUE;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.putFloat(val);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        assertEquals(val, reader.getFloat(), 10);
+    }
+
+    /**
+     * Test the {@link SafeByteBufferWrapper#putInt(int)}
+     * {@link SafeByteBufferWrapper#getInt()} methods
+     */
+    @Test
+    public void testReadWriteInt() {
+        int val = Integer.MAX_VALUE;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.putInt(val);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        assertEquals(val, reader.getInt());
+    }
+
+    /**
+     * Test the {@link SafeByteBufferWrapper#putLong(long)}
+     * {@link SafeByteBufferWrapper#getLong()} methods
+     */
+    @Test
+    public void testReadWriteLong() {
+        long val = Long.MIN_VALUE;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.putLong(val);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        assertEquals(val, reader.getLong());
+    }
+
+    /**
+     * Test the {@link SafeByteBufferWrapper#putShort(short)}
+     * {@link SafeByteBufferWrapper#getShort()} methods
+     */
+    @Test
+    public void testReadWriteShort() {
+        short val = Short.MIN_VALUE;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.putShort(val);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        assertEquals(val, reader.getShort());
+    }
+
+    /**
+     * Test the {@link SafeByteBufferWrapper#putString(String)}
+     * {@link SafeByteBufferWrapper#getString()} methods
+     */
+    @Test
+    public void testReadWriteString() {
+        String val = "abcdefg";
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.putString(val);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        assertEquals(val, reader.getString());
+    }
+
+    /**
+     * Test adding multiple values to the buffer, inside the limits
+     */
+    @Test
+    public void testMultipleValues() {
+        int valInt = 98;
+        short valShort = 34;
+        String valStr = "myString";
+        long valLong = 254238908543254L;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
+        buffer.putInt(valInt);
+        buffer.putShort(valShort);
+        buffer.putString(valStr);
+        buffer.putLong(valLong);
+
+        // Reset the buffer and read it again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
+        assertEquals(valInt, reader.getInt());
+        assertEquals(valShort, reader.getShort());
+        assertEquals(valStr, reader.getString());
+        assertEquals(valLong, reader.getLong());
+    }
+
+    /**
+     * Test writing over the limit of the buffer
+     */
+    @Test(expected = BufferOverflowException.class)
+    public void testLimit() {
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 5);
+        buffer.putDouble(Double.MIN_VALUE);
+    }
+
+    /**
+     * Test writing to main buffer after writing to safe buffer
+     */
+    @Test
+    public void testMainBuffer() {
+        String valString = "defghi";
+        long valLong = 54262542352L;
+        int valInt = 2048;
+        int bufferSize = Integer.BYTES + valString.length() + Long.BYTES;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, bufferSize);
+        buffer.putString(valString);
+        buffer.putLong(valLong);
+        fMainBuffer.putInt(valInt);
+
+        // Flip the main buffer to read again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, bufferSize);
+        assertEquals(valString, reader.getString());
+        assertEquals(valLong, reader.getLong());
+        assertEquals(valInt, fMainBuffer.getInt());
+    }
+
+    /**
+     * Test writing to main buffer after writing to safe buffer but not
+     * completely
+     */
+    @Test
+    public void testMainBuffer2() {
+        String valString = "defghi";
+        long valLong = 54262542352L;
+        int valInt = 2048;
+        int bufferSize = Integer.BYTES + valString.length() + Long.BYTES + Long.BYTES;
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, bufferSize);
+        buffer.putString(valString);
+        buffer.putLong(valLong);
+
+        // Assert the main buffer's position is after the safe buffer, even
+        // though it is not completely written
+        assertEquals(bufferSize, fMainBuffer.position());
+        fMainBuffer.putInt(valInt);
+
+        // Write the extra long at the end of the safe buffer
+        buffer.putLong(valLong);
+
+        // Start reading again
+        fMainBuffer.flip();
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, bufferSize);
+        assertEquals(valString, reader.getString());
+        assertEquals(valLong, reader.getLong());
+        assertEquals(valLong, reader.getLong());
+        assertEquals(valInt, fMainBuffer.getInt());
+    }
+
+    /**
+     * Test writing to main buffer before writing to safe buffer
+     */
+    @Test
+    public void testMainBuffer3() {
+        String valString = "defghi";
+        long valLong = 54262542352L;
+        int valInt = 2048;
+        int bufferSize = Integer.BYTES + valString.length() + Long.BYTES;
+
+        fMainBuffer.putLong(valLong);
+        fMainBuffer.putInt(valInt);
+
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, bufferSize);
+        buffer.putString(valString);
+        buffer.putLong(valLong);
+
+        fMainBuffer.flip();
+        assertEquals(valLong, fMainBuffer.getLong());
+        assertEquals(valInt, fMainBuffer.getInt());
+
+        // Flip the main buffer
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, bufferSize);
+        assertEquals(valString, reader.getString());
+        assertEquals(valLong, reader.getLong());
+    }
+
+    /**
+     * Test writing in buffers with different endianness
+     */
+    @Test
+    public void testEndianness() {
+        long valLong = 54262542352L;
+        int valInt = 2048;
+        int bufferSize = Integer.BYTES + Long.BYTES;
+
+        // Change the order of the buffer
+        ByteOrder order = fMainBuffer.order();
+        ByteOrder newOrder = (order == ByteOrder.BIG_ENDIAN ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN);
+        fMainBuffer.order(newOrder);
+
+        ByteBuffer otherBuffer = ByteBuffer.allocate(fMainBuffer.capacity());
+        otherBuffer.order(newOrder);
+
+        // Wrap one of the buffer in a safe buffer
+        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, bufferSize);
+
+        // Write the same data in both buffer and make sure they are equal
+        buffer.putLong(valLong);
+        otherBuffer.putLong(valLong);
+        buffer.putInt(valInt);
+        otherBuffer.putInt(valInt);
+
+        fMainBuffer.flip();
+        otherBuffer.flip();
+
+        byte[] expected = new byte[bufferSize];
+        byte[] actual = new byte[bufferSize];
+
+        fMainBuffer.get(actual, 0, bufferSize);
+        otherBuffer.get(expected, 0, bufferSize);
+
+        assertArrayEquals(expected, actual);
+
+        fMainBuffer.flip();
+        otherBuffer.flip();
+
+        // Read the safe byte buffer and make sure the results are the same
+        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, bufferSize);
+        assertEquals(otherBuffer.getLong(), reader.getLong());
+        assertEquals(otherBuffer.getInt(), reader.getInt());
+    }
+
+}
index db1cec4989b8b1cafebc35088c176429640e267a..c328dc60a08f55d096d9fee4155882dcb21553ca 100644 (file)
@@ -10,4 +10,6 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Bundle-ActivationPolicy: lazy
 Require-Bundle: org.eclipse.core.runtime,
  org.eclipse.tracecompass.common.core
-Export-Package: org.eclipse.tracecompass.internal.datastore.core;x-internal:=true
+Export-Package: org.eclipse.tracecompass.internal.datastore.core;x-internal:=true,
+ org.eclipse.tracecompass.internal.datastore.core.serialization;x-internal:=true,
+ org.eclipse.tracecompass.internal.provisional.datastore.core.serialization;x-friends:="org.eclipse.tracecompass.statesystem.core,org.eclipse.tracecompass.statesystem.core.tests"
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/datastore/core/serialization/SafeByteBufferWrapper.java b/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/datastore/core/serialization/SafeByteBufferWrapper.java
new file mode 100644 (file)
index 0000000..1e21b19
--- /dev/null
@@ -0,0 +1,157 @@
+/*******************************************************************************
+ * 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.datastore.core.serialization;
+
+import java.nio.ByteBuffer;
+
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferReader;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferWriter;
+
+/**
+ * This class is a wrapper around a ByteBuffer. The size to read/write to the
+ * buffer must be known from the beginning. It will not overflow onto the main
+ * buffer.
+ *
+ * This class may be used to wrap a small part of a bigger ByteBuffer in such a
+ * way that it limits the number of bytes to read/write. It will not overflow
+ * over or below the allowed positions in the big ByteBuffer while not requiring
+ * extra copies of byte arrays.
+ *
+ * This allows sequential read and write operations but does not allow resizes or
+ * seeks.
+ *
+ * @author Geneviève Bastien
+ */
+public class SafeByteBufferWrapper implements ISafeByteBufferReader, ISafeByteBufferWriter {
+
+    private final ByteBuffer fBuffer;
+
+    /**
+     * Constructor.
+     *
+     * @param buffer
+     *            The big ByteBuffer to safely wrap
+     */
+    public SafeByteBufferWrapper(ByteBuffer buffer) {
+        fBuffer = buffer;
+    }
+
+    @Override
+    public byte get() {
+        return fBuffer.get();
+    }
+
+    @Override
+    public void get(byte[] dst) {
+        fBuffer.get(dst);
+    }
+
+    @Override
+    public char getChar() {
+        return fBuffer.getChar();
+    }
+
+    @Override
+    public double getDouble() {
+        return fBuffer.getDouble();
+    }
+
+    @Override
+    public float getFloat() {
+        return fBuffer.getFloat();
+    }
+
+    @Override
+    public int getInt() {
+        return fBuffer.getInt();
+    }
+
+    @Override
+    public long getLong() {
+        return fBuffer.getLong();
+    }
+
+    @Override
+    public short getShort() {
+        return fBuffer.getShort();
+    }
+
+    @Override
+    public String getString() {
+        int strSize = fBuffer.getShort();
+        byte[] array = new byte[strSize];
+        fBuffer.get(array);
+        return new String(array);
+    }
+
+    @Override
+    public void put(byte value) {
+        fBuffer.put(value);
+    }
+
+    @Override
+    public void put(byte[] src) {
+        fBuffer.put(src);
+    }
+
+    @Override
+    public void putChar(char value) {
+        fBuffer.putChar(value);
+    }
+
+    @Override
+    public void putDouble(double value) {
+        fBuffer.putDouble(value);
+    }
+
+    @Override
+    public void putFloat(float value) {
+        fBuffer.putFloat(value);
+    }
+
+    @Override
+    public void putInt(int value) {
+        fBuffer.putInt(value);
+    }
+
+    @Override
+    public void putLong(long value) {
+        fBuffer.putLong(value);
+    }
+
+    @Override
+    public void putShort(short value) {
+        fBuffer.putShort(value);
+    }
+
+    @Override
+    public void putString(String value) {
+        String toWrite = value;
+        if (value.length() > Short.MAX_VALUE) {
+            toWrite = toWrite.substring(0, Short.MAX_VALUE);
+        }
+        fBuffer.putShort((short) value.length());
+        fBuffer.put(toWrite.getBytes());
+    }
+
+    /**
+     * Return the serialized size of the string in this byte buffer. The maximum
+     * size is {@link Short#MAX_VALUE}. A string with larger size will be
+     * truncated.
+     *
+     * @param string
+     *            The string to serialize
+     * @return The size of the serialized string
+     */
+    public static int getStringSizeInBuffer(String string) {
+        return Short.BYTES + Math.min(Short.MAX_VALUE, string.length());
+    }
+
+}
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/datastore/core/serialization/package-info.java b/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/datastore/core/serialization/package-info.java
new file mode 100644 (file)
index 0000000..735d72a
--- /dev/null
@@ -0,0 +1,11 @@
+/*******************************************************************************
+ * 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
+ *******************************************************************************/
+
+@org.eclipse.jdt.annotation.NonNullByDefault
+package org.eclipse.tracecompass.internal.datastore.core.serialization;
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/ISafeByteBufferReader.java b/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/ISafeByteBufferReader.java
new file mode 100644 (file)
index 0000000..26649b1
--- /dev/null
@@ -0,0 +1,87 @@
+/*******************************************************************************
+ * 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.provisional.datastore.core.serialization;
+
+/**
+ * Interface for a safe ByteBuffer for reading purposes. This interface allows
+ * only to read data from a buffer, no other operation is allowed on it. The
+ * implementations must make sure that only the allowed data can be read.
+ *
+ * @author Geneviève Bastien
+ * @noimplement This interface is not intended to be implemented by clients.
+ */
+public interface ISafeByteBufferReader {
+
+    /**
+     * Reads a byte at the buffer's current position
+     *
+     * @return The byte read
+     */
+    byte get();
+
+    /**
+     * Transfers bytes from this buffer's current position into the destination
+     * array
+     *
+     * @param dst
+     *            The destination array
+     */
+    void get(byte[] dst);
+
+    /**
+     * Reads the char at the buffer's current position
+     *
+     * @return The char read
+     */
+    char getChar();
+
+    /**
+     * Reads the double at the buffer's current position
+     *
+     * @return The double read
+     */
+    double getDouble();
+
+    /**
+     * Reads the float at the buffer's current position
+     *
+     * @return The float read
+     */
+    float getFloat();
+
+    /**
+     * Reads the int at the buffer's current position
+     *
+     * @return The int read
+     */
+    int getInt();
+
+    /**
+     * Reads the long at the buffer's current position
+     *
+     * @return The long read
+     */
+    long getLong();
+
+    /**
+     * Reads the short at the buffer's current position
+     *
+     * @return The short read
+     */
+    short getShort();
+
+    /**
+     * Gets a string from the byte buffer.
+     *
+     * @return The string value read
+     */
+    String getString();
+
+}
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/ISafeByteBufferWriter.java b/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/ISafeByteBufferWriter.java
new file mode 100644 (file)
index 0000000..e49c77a
--- /dev/null
@@ -0,0 +1,98 @@
+/*******************************************************************************
+ * 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.provisional.datastore.core.serialization;
+
+/**
+ * Interface for a safe ByteBuffer for writing purposes. This interface allows
+ * only to write data from a buffer, no other operation is allowed on it. The
+ * implementation needs to make sure that the buffer does not write over the
+ * limits of the buffer.
+ *
+ * @author Geneviève Bastien
+ * @noimplement This interface is not intended to be implemented by clients.
+ */
+public interface ISafeByteBufferWriter {
+
+    /**
+     * Writes a byte at the buffer's current position
+     *
+     * @param value
+     *            The byte to write
+     */
+    void put(byte value);
+
+    /**
+     * Transfers the bytes from the src array in the buffer at the current
+     * position
+     *
+     * @param src
+     *            the byte array to write
+     */
+    void put(byte[] src);
+
+    /**
+     * Writes a char at the buffer's current position
+     *
+     * @param value
+     *            The char to write
+     */
+    void putChar(char value);
+
+    /**
+     * Writes a double at the buffer's current position
+     *
+     * @param value
+     *            The double to write
+     */
+    void putDouble(double value);
+
+    /**
+     * Writes a float at the buffer's current position
+     *
+     * @param value
+     *            The float to write
+     */
+    void putFloat(float value);
+
+    /**
+     * Writes an int at the buffer's current position
+     *
+     * @param value
+     *            The int to write
+     */
+    void putInt(int value);
+
+    /**
+     * Writes a long at the buffer's current position
+     *
+     * @param value
+     *            The long to write
+     */
+    void putLong(long value);
+
+    /**
+     * Writes a short at the buffer's current position
+     *
+     * @param value
+     *            The short to write
+     */
+    void putShort(short value);
+
+    /**
+     * Writes a string value in the byte buffer. The implementation can decide
+     * what format it will use. They can also have a maximum size, in which case
+     * string should be truncated if they are larger than that.
+     *
+     * @param value
+     *            The String value to write to the buffer
+     */
+    void putString(String value);
+
+}
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/SafeByteBufferFactory.java b/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/SafeByteBufferFactory.java
new file mode 100644 (file)
index 0000000..7086e68
--- /dev/null
@@ -0,0 +1,89 @@
+/*******************************************************************************
+ * 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.provisional.datastore.core.serialization;
+
+import java.nio.ByteBuffer;
+
+import org.eclipse.tracecompass.internal.datastore.core.serialization.SafeByteBufferWrapper;
+
+/**
+ * Class that creates instances of safe byte buffers wrappers from a part of a
+ * ByteBuffer instance
+ *
+ * @author Geneviève Bastien
+ */
+public final class SafeByteBufferFactory {
+
+    private SafeByteBufferFactory() {
+
+    }
+
+    /**
+     * Creates a new safe byte buffer reader from the ByteBuffer's current
+     * position with a size limited to 'size'.
+     *
+     * @param buffer
+     *            The big ByteBuffer to safely wrap for reading
+     * @param size
+     *            The size of the new sub-buffer
+     * @return The safe byte buffer reader instance
+     */
+    public static ISafeByteBufferReader wrapReader(ByteBuffer buffer, int size) {
+        int pos = buffer.position();
+        // Slice the main buffer, so that position 0 is the current position
+        // set it as read-only also
+        ByteBuffer readOnlyBuffer = buffer.slice().asReadOnlyBuffer();
+        readOnlyBuffer.order(buffer.order());
+        // Set its limit to the request limit
+        readOnlyBuffer.limit(size);
+        // Operations on fBuffer will not affect the main buffer's position, so
+        // we set its position to after the limit
+        buffer.position(pos + size);
+        return new SafeByteBufferWrapper(readOnlyBuffer);
+    }
+
+    /**
+     * Creates a new safe byte buffer writer from the ByteBuffer's current
+     * position with a size limited to 'size'.
+     *
+     * @param buffer
+     *            The big ByteBuffer to safely wrap for reading
+     * @param size
+     *            The size of the new sub-buffer
+     * @return The safe byte buffer writer instance
+     */
+    public static ISafeByteBufferWriter wrapWriter(ByteBuffer buffer, int size) {
+        int pos = buffer.position();
+        // Slice the main buffer, so that position 0 is the current position
+        ByteBuffer readWriteBuffer = buffer.slice();
+        readWriteBuffer.order(buffer.order());
+        // Set its limit to the request limit
+        readWriteBuffer.limit(size);
+        // Operations on fBuffer will not affect the main buffer's position, so
+        // we set its position to after the limit
+        buffer.position(pos + size);
+        return new SafeByteBufferWrapper(readWriteBuffer);
+    }
+
+    /**
+     * Get the serialized of a string object if it uses the
+     * {@link ISafeByteBufferWriter#putString(String)} method
+     *
+     * @param string
+     *            The string to write to the buffer
+     * @return The size of the string serialized by the
+     *         {@link ISafeByteBufferWriter#putString(String)} method, or -1 if
+     *         the string cannot be serialized
+     */
+    public static int getStringSizeInBuffer(String string) {
+        return SafeByteBufferWrapper.getStringSizeInBuffer(string);
+    }
+
+}
diff --git a/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/package-info.java b/statesystem/org.eclipse.tracecompass.datastore.core/src/org/eclipse/tracecompass/internal/provisional/datastore/core/serialization/package-info.java
new file mode 100644 (file)
index 0000000..bdff1c0
--- /dev/null
@@ -0,0 +1,11 @@
+/*******************************************************************************
+ * 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
+ *******************************************************************************/
+
+@org.eclipse.jdt.annotation.NonNullByDefault
+package org.eclipse.tracecompass.internal.provisional.datastore.core.serialization;
index a6a25a999e399544603f596f40aa49fee83bcb61..b4ad6120a8ecdc3ef36c30beef3f62fd55de35b9 100644 (file)
@@ -12,7 +12,8 @@ Require-Bundle: org.junit;bundle-version="4.0.0",
  org.eclipse.core.resources,
  org.eclipse.test.performance,
  org.eclipse.tracecompass.common.core,
- org.eclipse.tracecompass.statesystem.core
+ org.eclipse.tracecompass.statesystem.core,
+ org.eclipse.tracecompass.datastore.core
 Export-Package: org.eclipse.tracecompass.statesystem.core.tests,
  org.eclipse.tracecompass.statesystem.core.tests.backend,
  org.eclipse.tracecompass.statesystem.core.tests.backend.historytree,
index 51fc7a2d6b0f9a333bf85cb8f75fbe9ded8c48b4..4dec1cb9ee08aae0645588d842107d2839c9cf93 100644 (file)
@@ -14,10 +14,10 @@ import static org.junit.Assert.assertEquals;
 import java.nio.ByteBuffer;
 
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferReader;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferWriter;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.SafeByteBufferFactory;
 import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.CustomStateValue;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.ISafeByteBufferReader;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.ISafeByteBufferWriter;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.SafeByteBufferFactory;
 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue.Type;
 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/SafeByteBufferWrapperTest.java b/statesystem/org.eclipse.tracecompass.statesystem.core.tests/src/org/eclipse/tracecompass/statesystem/core/tests/statevalue/SafeByteBufferWrapperTest.java
deleted file mode 100644 (file)
index ec75013..0000000
+++ /dev/null
@@ -1,311 +0,0 @@
-/*******************************************************************************
- * 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.statesystem.core.tests.statevalue;
-
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertEquals;
-
-import java.nio.BufferOverflowException;
-import java.nio.ByteBuffer;
-
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.ISafeByteBufferReader;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.ISafeByteBufferWriter;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.SafeByteBufferFactory;
-import org.eclipse.tracecompass.internal.statesystem.core.statevalue.SafeByteBufferWrapper;
-import org.junit.Test;
-
-/**
- * Test for the {@link SafeByteBufferWrapper} class
- *
- * @author Geneviève Bastien
- */
-public class SafeByteBufferWrapperTest {
-
-    private final ByteBuffer fMainBuffer;
-
-    /**
-     * Constructor. Prepares the main buffer and safe buffer
-     */
-    public SafeByteBufferWrapperTest() {
-        fMainBuffer = ByteBuffer.allocate(1024);
-    }
-
-    /**
-     * Test the {@link SafeByteBufferWrapper#put(byte)}
-     * {@link SafeByteBufferWrapper#get()} methods
-     */
-    @Test
-    public void testReadWriteByte() {
-        byte val = Byte.MAX_VALUE;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.put(val);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        assertEquals(val, reader.get());
-    }
-
-    /**
-     * Test the {@link SafeByteBufferWrapper#put(byte[])}
-     * {@link SafeByteBufferWrapper#get(byte[])} methods
-     */
-    @Test
-    public void testReadWriteByteArray() {
-        byte[] val = { 0, 2, 1, 3 };
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.put(val);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        byte[] ret = new byte[4];
-        reader.get(ret);
-        assertArrayEquals(val, ret);
-    }
-
-    /**
-     * Test the {@link SafeByteBufferWrapper#putChar(char)}
-     * {@link SafeByteBufferWrapper#getChar()} methods
-     */
-    @Test
-    public void testReadWriteChar() {
-        char val = 24;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.putChar(val);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        assertEquals(val, reader.getChar());
-    }
-
-    /**
-     * Test the {@link SafeByteBufferWrapper#putDouble(double)}
-     * {@link SafeByteBufferWrapper#getDouble()} methods
-     */
-    @Test
-    public void testReadWriteDouble() {
-        double val = Double.MAX_VALUE;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.putDouble(val);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        assertEquals(val, reader.getDouble(), 10);
-    }
-
-    /**
-     * Test the {@link SafeByteBufferWrapper#putFloat(float)}
-     * {@link SafeByteBufferWrapper#getFloat()} methods
-     */
-    @Test
-    public void testReadWriteFloat() {
-        float val = Float.MIN_VALUE;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.putFloat(val);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        assertEquals(val, reader.getFloat(), 10);
-    }
-
-    /**
-     * Test the {@link SafeByteBufferWrapper#putInt(int)}
-     * {@link SafeByteBufferWrapper#getInt()} methods
-     */
-    @Test
-    public void testReadWriteInt() {
-        int val = Integer.MAX_VALUE;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.putInt(val);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        assertEquals(val, reader.getInt());
-    }
-
-    /**
-     * Test the {@link SafeByteBufferWrapper#putLong(long)}
-     * {@link SafeByteBufferWrapper#getLong()} methods
-     */
-    @Test
-    public void testReadWriteLong() {
-        long val = Long.MIN_VALUE;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.putLong(val);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        assertEquals(val, reader.getLong());
-    }
-
-    /**
-     * Test the {@link SafeByteBufferWrapper#putShort(short)}
-     * {@link SafeByteBufferWrapper#getShort()} methods
-     */
-    @Test
-    public void testReadWriteShort() {
-        short val = Short.MIN_VALUE;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.putShort(val);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        assertEquals(val, reader.getShort());
-    }
-
-    /**
-     * Test the {@link SafeByteBufferWrapper#putString(String)}
-     * {@link SafeByteBufferWrapper#getString()} methods
-     */
-    @Test
-    public void testReadWriteString() {
-        String val = "abcdefg";
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.putString(val);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        assertEquals(val, reader.getString());
-    }
-
-    /**
-     * Test adding multiple values to the buffer, inside the limits
-     */
-    @Test
-    public void testMultipleValues() {
-        int valInt = 98;
-        short valShort = 34;
-        String valStr = "myString";
-        long valLong = 254238908543254L;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 512);
-        buffer.putInt(valInt);
-        buffer.putShort(valShort);
-        buffer.putString(valStr);
-        buffer.putLong(valLong);
-
-        // Reset the buffer and read it again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, 512);
-        assertEquals(valInt, reader.getInt());
-        assertEquals(valShort, reader.getShort());
-        assertEquals(valStr, reader.getString());
-        assertEquals(valLong, reader.getLong());
-    }
-
-    /**
-     * Test writing over the limit of the buffer
-     */
-    @Test(expected = BufferOverflowException.class)
-    public void testLimit() {
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, 5);
-        buffer.putDouble(Double.MIN_VALUE);
-    }
-
-    /**
-     * Test writing to main buffer after writing to safe buffer
-     */
-    @Test
-    public void testMainBuffer() {
-        String valString = "defghi";
-        long valLong = 54262542352L;
-        int valInt = 2048;
-        int bufferSize = Integer.BYTES + valString.length() + Long.BYTES;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, bufferSize);
-        buffer.putString(valString);
-        buffer.putLong(valLong);
-        fMainBuffer.putInt(valInt);
-
-        // Flip the main buffer to read again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, bufferSize);
-        assertEquals(valString, reader.getString());
-        assertEquals(valLong, reader.getLong());
-        assertEquals(valInt, fMainBuffer.getInt());
-    }
-
-    /**
-     * Test writing to main buffer after writing to safe buffer but not
-     * completely
-     */
-    @Test
-    public void testMainBuffer2() {
-        String valString = "defghi";
-        long valLong = 54262542352L;
-        int valInt = 2048;
-        int bufferSize = Integer.BYTES + valString.length() + Long.BYTES + Long.BYTES;
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, bufferSize);
-        buffer.putString(valString);
-        buffer.putLong(valLong);
-
-        // Assert the main buffer's position is after the safe buffer, even
-        // though it is not completely written
-        assertEquals(bufferSize, fMainBuffer.position());
-        fMainBuffer.putInt(valInt);
-
-        // Write the extra long at the end of the safe buffer
-        buffer.putLong(valLong);
-
-        // Start reading again
-        fMainBuffer.flip();
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, bufferSize);
-        assertEquals(valString, reader.getString());
-        assertEquals(valLong, reader.getLong());
-        assertEquals(valLong, reader.getLong());
-        assertEquals(valInt, fMainBuffer.getInt());
-    }
-
-    /**
-     * Test writing to main buffer before writing to safe buffer
-     */
-    @Test
-    public void testMainBuffer3() {
-        String valString = "defghi";
-        long valLong = 54262542352L;
-        int valInt = 2048;
-        int bufferSize = Integer.BYTES + valString.length() + Long.BYTES;
-
-        fMainBuffer.putLong(valLong);
-        fMainBuffer.putInt(valInt);
-
-        ISafeByteBufferWriter buffer = SafeByteBufferFactory.wrapWriter(fMainBuffer, bufferSize);
-        buffer.putString(valString);
-        buffer.putLong(valLong);
-
-        fMainBuffer.flip();
-        assertEquals(valLong, fMainBuffer.getLong());
-        assertEquals(valInt, fMainBuffer.getInt());
-
-        // Flip the main buffer
-        ISafeByteBufferReader reader = SafeByteBufferFactory.wrapReader(fMainBuffer, bufferSize);
-        assertEquals(valString, reader.getString());
-        assertEquals(valLong, reader.getLong());
-    }
-
-}
index a92218ca28421273b335e1bd41f3afebca5c711d..52d8625429ece8360272832854c0a3c8a808eca9 100644 (file)
@@ -11,9 +11,9 @@ package org.eclipse.tracecompass.statesystem.core.tests.stubs.statevalues;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferWriter;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.SafeByteBufferFactory;
 import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.CustomStateValue;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.ISafeByteBufferWriter;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.SafeByteBufferFactory;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
 
index eae7c0758a5c0d993445953edb8e002d238c3155..81f3d3cb3f8c92793f25e4a179750e0902aeb6ae 100644 (file)
@@ -10,13 +10,13 @@ Bundle-ActivationPolicy: lazy
 Bundle-RequiredExecutionEnvironment: JavaSE-1.8
 Require-Bundle: org.eclipse.core.runtime,
  org.eclipse.core.resources,
- org.eclipse.tracecompass.common.core
+ org.eclipse.tracecompass.common.core,
+ org.eclipse.tracecompass.datastore.core
 Export-Package: org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue;x-friends:="org.eclipse.tracecompass.statesystem.core.tests",
  org.eclipse.tracecompass.internal.statesystem.core;x-friends:="org.eclipse.tracecompass.statesystem.core.tests",
  org.eclipse.tracecompass.internal.statesystem.core.backend;x-internal:=true,
  org.eclipse.tracecompass.internal.statesystem.core.backend.historytree;x-friends:="org.eclipse.tracecompass.statesystem.core.tests",
  org.eclipse.tracecompass.internal.statesystem.core.backend.historytree.classic;x-friends:="org.eclipse.tracecompass.statesystem.core.tests",
- org.eclipse.tracecompass.internal.statesystem.core.statevalue;x-friends:="org.eclipse.tracecompass.statesystem.core.tests",
  org.eclipse.tracecompass.statesystem.core,
  org.eclipse.tracecompass.statesystem.core.backend,
  org.eclipse.tracecompass.statesystem.core.exceptions,
index 8a0dc799c91fbac6f8ff554ef27f096e6752bf75..8c7cf51470981c3bfc88aabd0c2cebba71c7c792 100644 (file)
@@ -12,6 +12,8 @@ package org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalu
 import java.nio.BufferOverflowException;
 
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferReader;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferWriter;
 import org.eclipse.tracecompass.internal.statesystem.core.Activator;
 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
 
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/ISafeByteBufferReader.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/ISafeByteBufferReader.java
deleted file mode 100644 (file)
index 29d0f79..0000000
+++ /dev/null
@@ -1,86 +0,0 @@
-/*******************************************************************************
- * 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.provisional.statesystem.core.statevalue;
-
-/**
- * Interface for a safe ByteBuffer for reading purposes. This interface allows
- * only to read data from a buffer, no other operation is allowed on it. The
- * implementations must make sure that only the allowed data can be read.
- *
- * @author Geneviève Bastien
- */
-public interface ISafeByteBufferReader {
-
-    /**
-     * Reads a byte at the buffer's current position
-     *
-     * @return The byte read
-     */
-    byte get();
-
-    /**
-     * Transfers bytes from this buffer's current position into the destination
-     * array
-     *
-     * @param dst
-     *            The destination array
-     */
-    void get(byte[] dst);
-
-    /**
-     * Reads the char at the buffer's current position
-     *
-     * @return The char read
-     */
-    char getChar();
-
-    /**
-     * Reads the double at the buffer's current position
-     *
-     * @return The double read
-     */
-    double getDouble();
-
-    /**
-     * Reads the float at the buffer's current position
-     *
-     * @return The float read
-     */
-    float getFloat();
-
-    /**
-     * Reads the int at the buffer's current position
-     *
-     * @return The int read
-     */
-    int getInt();
-
-    /**
-     * Reads the long at the buffer's current position
-     *
-     * @return The long read
-     */
-    long getLong();
-
-    /**
-     * Reads the short at the buffer's current position
-     *
-     * @return The short read
-     */
-    short getShort();
-
-    /**
-     * Gets a string from the byte buffer.
-     *
-     * @return The string value read
-     */
-    String getString();
-
-}
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/ISafeByteBufferWriter.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/ISafeByteBufferWriter.java
deleted file mode 100644 (file)
index f50f288..0000000
+++ /dev/null
@@ -1,97 +0,0 @@
-/*******************************************************************************
- * 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.provisional.statesystem.core.statevalue;
-
-/**
- * Interface for a safe ByteBuffer for writing purposes. This interface allows
- * only to write data from a buffer, no other operation is allowed on it. The
- * implementation needs to make sure that the buffer does not write over the
- * limits of the buffer.
- *
- * @author Geneviève Bastien
- */
-public interface ISafeByteBufferWriter {
-
-    /**
-     * Writes a byte at the buffer's current position
-     *
-     * @param value
-     *            The byte to write
-     */
-    void put(byte value);
-
-    /**
-     * Transfers the bytes from the src array in the buffer at the current
-     * position
-     *
-     * @param src
-     *            the byte array to write
-     */
-    void put(byte[] src);
-
-    /**
-     * Writes a char at the buffer's current position
-     *
-     * @param value
-     *            The char to write
-     */
-    void putChar(char value);
-
-    /**
-     * Writes a double at the buffer's current position
-     *
-     * @param value
-     *            The double to write
-     */
-    void putDouble(double value);
-
-    /**
-     * Writes a float at the buffer's current position
-     *
-     * @param value
-     *            The float to write
-     */
-    void putFloat(float value);
-
-    /**
-     * Writes an int at the buffer's current position
-     *
-     * @param value
-     *            The int to write
-     */
-    void putInt(int value);
-
-    /**
-     * Writes a long at the buffer's current position
-     *
-     * @param value
-     *            The long to write
-     */
-    void putLong(long value);
-
-    /**
-     * Writes a short at the buffer's current position
-     *
-     * @param value
-     *            The short to write
-     */
-    void putShort(short value);
-
-    /**
-     * Writes a string value in the byte buffer. The implementation can decide
-     * what format it will use. They can also have a maximum size, in which case
-     * string should be truncated if they are larger than that.
-     *
-     * @param value
-     *            The String value to write to the buffer
-     */
-    void putString(String value);
-
-}
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/SafeByteBufferFactory.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/provisional/statesystem/core/statevalue/SafeByteBufferFactory.java
deleted file mode 100644 (file)
index 86122a5..0000000
+++ /dev/null
@@ -1,87 +0,0 @@
-/*******************************************************************************
- * 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.provisional.statesystem.core.statevalue;
-
-import java.nio.ByteBuffer;
-
-import org.eclipse.tracecompass.internal.statesystem.core.statevalue.SafeByteBufferWrapper;
-
-/**
- * Class that creates instances of safe byte buffers wrappers from a part of a
- * ByteBuffer instance
- *
- * @author Geneviève Bastien
- */
-public final class SafeByteBufferFactory {
-
-    private SafeByteBufferFactory() {
-
-    }
-
-    /**
-     * Creates a new safe byte buffer reader from the ByteBuffer's current
-     * position with a size limited to 'size'.
-     *
-     * @param buffer
-     *            The big ByteBuffer to safely wrap for reading
-     * @param size
-     *            The size of the new sub-buffer
-     * @return The safe byte buffer reader instance
-     */
-    public static ISafeByteBufferReader wrapReader(ByteBuffer buffer, int size) {
-        int pos = buffer.position();
-        // Slice the main buffer, so that position 0 is the current position
-        // set it as read-only also
-        ByteBuffer readOnlyBuffer = buffer.slice().asReadOnlyBuffer();
-        // Set its limit to the request limit
-        readOnlyBuffer.limit(size);
-        // Operations on fBuffer will not affect the main buffer's position, so
-        // we set its position to after the limit
-        buffer.position(pos + size);
-        return new SafeByteBufferWrapper(readOnlyBuffer);
-    }
-
-    /**
-     * Creates a new safe byte buffer writer from the ByteBuffer's current
-     * position with a size limited to 'size'.
-     *
-     * @param buffer
-     *            The big ByteBuffer to safely wrap for reading
-     * @param size
-     *            The size of the new sub-buffer
-     * @return The safe byte buffer writer instance
-     */
-    public static ISafeByteBufferWriter wrapWriter(ByteBuffer buffer, int size) {
-        int pos = buffer.position();
-        // Slice the main buffer, so that position 0 is the current position
-        ByteBuffer readWriteBuffer = buffer.slice();
-        // Set its limit to the request limit
-        readWriteBuffer.limit(size);
-        // Operations on fBuffer will not affect the main buffer's position, so
-        // we set its position to after the limit
-        buffer.position(pos + size);
-        return new SafeByteBufferWrapper(readWriteBuffer);
-    }
-
-    /**
-     * Get the serialized of a string object if it uses the
-     * {@link ISafeByteBufferWriter#putString(String)} method
-     *
-     * @param string
-     *            The string to write to the buffer
-     * @return The size of the string serialized by the
-     *         {@link ISafeByteBufferWriter#putString(String)} method, or -1 if
-     *         the string cannot be serialized
-     */
-    public static int getStringSizeInBuffer(String string) {
-        return SafeByteBufferWrapper.getStringSizeInBuffer(string);
-    }
-
-}
index 96d33a73756598e2bc2353a86afd8ba08119ba29..e0ab848711d4370b4d53376ef9bfc78d68324c8a 100644 (file)
@@ -21,10 +21,10 @@ import java.nio.charset.Charset;
 import java.util.Objects;
 
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferReader;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.ISafeByteBufferWriter;
+import org.eclipse.tracecompass.internal.provisional.datastore.core.serialization.SafeByteBufferFactory;
 import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.CustomStateValue;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.ISafeByteBufferReader;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.ISafeByteBufferWriter;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.SafeByteBufferFactory;
 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/statevalue/SafeByteBufferWrapper.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/statevalue/SafeByteBufferWrapper.java
deleted file mode 100644 (file)
index c642f40..0000000
+++ /dev/null
@@ -1,158 +0,0 @@
-/*******************************************************************************
- * 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.statevalue;
-
-import java.nio.ByteBuffer;
-
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.ISafeByteBufferReader;
-import org.eclipse.tracecompass.internal.provisional.statesystem.core.statevalue.ISafeByteBufferWriter;
-
-/**
- * This class is a wrapper around a ByteBuffer. The size to read/write to the
- * buffer must be known from the beginning. It will not overflow onto the main
- * buffer.
- *
- * This class may be used to wrap a small part of a bigger ByteBuffer in such a
- * way that it limits the number of bytes to read/write. It will not overflow
- * over or below the allowed positions in the big ByteBuffer while not requiring
- * extra copies of byte arrays.
- *
- * This allows sequential read and write operations but does not allow resizes or
- * seeks.
- *
- * @author Geneviève Bastien
- * @since 2.0
- */
-public class SafeByteBufferWrapper implements ISafeByteBufferReader, ISafeByteBufferWriter {
-
-    private final ByteBuffer fBuffer;
-
-    /**
-     * Constructor.
-     *
-     * @param buffer
-     *            The big ByteBuffer to safely wrap
-     */
-    public SafeByteBufferWrapper(ByteBuffer buffer) {
-        fBuffer = buffer;
-    }
-
-    @Override
-    public byte get() {
-        return fBuffer.get();
-    }
-
-    @Override
-    public void get(byte[] dst) {
-        fBuffer.get(dst);
-    }
-
-    @Override
-    public char getChar() {
-        return fBuffer.getChar();
-    }
-
-    @Override
-    public double getDouble() {
-        return fBuffer.getDouble();
-    }
-
-    @Override
-    public float getFloat() {
-        return fBuffer.getFloat();
-    }
-
-    @Override
-    public int getInt() {
-        return fBuffer.getInt();
-    }
-
-    @Override
-    public long getLong() {
-        return fBuffer.getLong();
-    }
-
-    @Override
-    public short getShort() {
-        return fBuffer.getShort();
-    }
-
-    @Override
-    public String getString() {
-        int strSize = fBuffer.getShort();
-        byte[] array = new byte[strSize];
-        fBuffer.get(array);
-        return new String(array);
-    }
-
-    @Override
-    public void put(byte value) {
-        fBuffer.put(value);
-    }
-
-    @Override
-    public void put(byte[] src) {
-        fBuffer.put(src);
-    }
-
-    @Override
-    public void putChar(char value) {
-        fBuffer.putChar(value);
-    }
-
-    @Override
-    public void putDouble(double value) {
-        fBuffer.putDouble(value);
-    }
-
-    @Override
-    public void putFloat(float value) {
-        fBuffer.putFloat(value);
-    }
-
-    @Override
-    public void putInt(int value) {
-        fBuffer.putInt(value);
-    }
-
-    @Override
-    public void putLong(long value) {
-        fBuffer.putLong(value);
-    }
-
-    @Override
-    public void putShort(short value) {
-        fBuffer.putShort(value);
-    }
-
-    @Override
-    public void putString(String value) {
-        String toWrite = value;
-        if (value.length() > Short.MAX_VALUE) {
-            toWrite = toWrite.substring(0, Short.MAX_VALUE);
-        }
-        fBuffer.putShort((short) value.length());
-        fBuffer.put(toWrite.getBytes());
-    }
-
-    /**
-     * Return the serialized size of the string in this byte buffer. The maximum
-     * size is {@link Short#MAX_VALUE}. A string with larger size will be
-     * truncated.
-     *
-     * @param string
-     *            The string to serialize
-     * @return The size of the serialized string
-     */
-    public static int getStringSizeInBuffer(String string) {
-        return Short.BYTES + Math.min(Short.MAX_VALUE, string.length());
-    }
-
-}
diff --git a/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/statevalue/package-info.java b/statesystem/org.eclipse.tracecompass.statesystem.core/src/org/eclipse/tracecompass/internal/statesystem/core/statevalue/package-info.java
deleted file mode 100644 (file)
index b7b1a62..0000000
+++ /dev/null
@@ -1,12 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2014 Ericsson
- *
- * 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
- *
- * Contributors:
- *    Ericsson - Initial API and implementation
- *******************************************************************************/
-@org.eclipse.jdt.annotation.NonNullByDefault package org.eclipse.tracecompass.internal.statesystem.core.statevalue;
index 97cd7628099e9df7e1ffda400141efed7d48ec8d..0db2c8aec7d413bdb8af64cc001bce74d7344c45 100644 (file)
@@ -26,7 +26,7 @@
 
   <modules>
     <module>org.eclipse.tracecompass.datastore.core</module>
-    <!-- <module>org.eclipse.tracecompass.datastore.core.tests</module> -->
+    <module>org.eclipse.tracecompass.datastore.core.tests</module>
     <module>org.eclipse.tracecompass.segmentstore.core</module>
     <module>org.eclipse.tracecompass.segmentstore.core.tests</module>
     <module>org.eclipse.tracecompass.statesystem.core</module>
index 62523105c6a8804a4a96510daeedff5eda83907f..4aaa8e660274d0aa3fec1d2baa5a35ca0f87cc20 100644 (file)
          version="0.0.0"
          unpack="false"/>
 
+   <plugin
+         id="org.eclipse.tracecompass.datastore.core"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
 </feature>
This page took 0.045974 seconds and 5 git commands to generate.