tmf: Remove the serialization logic from TmfStateValue
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 26 Apr 2013 19:50:14 +0000 (15:50 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 26 Apr 2013 20:39:39 +0000 (16:39 -0400)
Serializing the state values is the history backend's job (as in,
each backend could do it differently). Move the existing logic
to HTInterval.

This also fixes a very bad bug where the stringsEntrySize of intervals
containing a Long state value would be incorrectly reported as 0.
This would mean that nodes could try inserting a value that wouldn't
fit, which would end up corrupting the interval data.

Change-Id: I6c54ef905b45b06b69ed21ce59c70d2eaa16b7f8
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/12254
Tested-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
IP-Clean: Patrick Tasse <patrick.tasse@gmail.com>

org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/internal/tmf/core/statesystem/backends/historytree/HTInterval.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/IntegerStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/LongStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/NullStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/StringStateValue.java
org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statevalue/TmfStateValue.java

index ce733e8279d5e3b5a5d952dbec7b986791fe41e7..49100da9b3b1aa6d4610e4450d065aeeadf56fc7 100644 (file)
@@ -177,9 +177,6 @@ final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
      * @return The size of the Strings Entry that was written, if any.
      */
     int writeInterval(ByteBuffer buffer, int endPosOfStringEntry) {
-        int sizeOfStringEntry;
-        byte[] byteArrayToWrite;
-
         buffer.putLong(start);
         buffer.putLong(end);
         buffer.putInt(attribute);
@@ -200,41 +197,37 @@ final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
                  */
                 e.printStackTrace();
             }
-            return 0; /* we didn't use a Strings section entry */
+            break;
 
         case TYPE_STRING:
-            byteArrayToWrite = sv.toByteArray();
-            /*
-             * Size to write (+2 = +1 for size at the start, +1 for the 0 at the
-             * end)
-             */
-            sizeOfStringEntry = byteArrayToWrite.length + 2;
+            byte[] byteArrayToWrite;
+            try {
+                byteArrayToWrite = sv.unboxStr().getBytes();
+            } catch (StateValueTypeException e1) {
+                /* Should not happen, we're in a switch/case for string type */
+                throw new RuntimeException();
+            }
 
             /* we use the valueOffset as an offset. */
-            buffer.putInt(endPosOfStringEntry - sizeOfStringEntry);
+            buffer.putInt(endPosOfStringEntry - stringsEntrySize);
             buffer.mark();
-            buffer.position(endPosOfStringEntry - sizeOfStringEntry);
+            buffer.position(endPosOfStringEntry - stringsEntrySize);
 
             /*
              * write the Strings entry (1st byte = size, then the bytes, then the 0)
              */
-            buffer.put((byte) sizeOfStringEntry);
+            buffer.put((byte) stringsEntrySize);
             buffer.put(byteArrayToWrite);
             buffer.put((byte) 0);
             assert (buffer.position() == endPosOfStringEntry);
             buffer.reset();
-            return sizeOfStringEntry;
+            break;
 
         case TYPE_LONG:
-            /*
-             * Size to write is the number of bytes in a Long
-             */
-            sizeOfStringEntry = 8;
-
             /* we use the valueOffset as an offset. */
-            buffer.putInt(endPosOfStringEntry - sizeOfStringEntry);
+            buffer.putInt(endPosOfStringEntry - stringsEntrySize);
             buffer.mark();
-            buffer.position(endPosOfStringEntry - sizeOfStringEntry);
+            buffer.position(endPosOfStringEntry - stringsEntrySize);
 
             /*
              * write the Long in the Strings section
@@ -250,11 +243,12 @@ final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
             }
             assert (buffer.position() == endPosOfStringEntry);
             buffer.reset();
-            return sizeOfStringEntry;
+            break;
 
         default:
-            return 0;
+            break;
         }
+        return stringsEntrySize;
     }
 
     @Override
@@ -306,11 +300,27 @@ final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
     }
 
     private int computeStringsEntrySize() {
-        if (sv.toByteArray() == null) {
+        switch(sv.getType()) {
+        case NULL:
+        case INTEGER:
+            /* Those don't use the strings section at all */
             return 0;
+        case LONG:
+            /* The value's bytes are written directly into the strings section */
+            return 8;
+        case STRING:
+            try {
+                /* String's length + 2 (1 byte for size, 1 byte for \0 at the end */
+                return sv.unboxStr().getBytes().length + 2;
+            } catch (StateValueTypeException e) {
+                /* We're inside a switch/case for the string type, can't happen */
+                throw new RuntimeException();
+            }
+        default:
+            /* It's very important that we know how to write the state value in
+             * the file!! */
+            throw new RuntimeException();
         }
-        return sv.toByteArray().length + 2;
-        /* (+1 for the first byte indicating the size, +1 for the 0'ed byte) */
     }
 
     /**
index d3db069ff1d631de7a17fde0e0f6744deeaa8d72..6cb107bccee767625ee8572e0b1922d873303c30 100644 (file)
@@ -41,11 +41,6 @@ final class IntegerStateValue extends TmfStateValue {
         return valueInt;
     }
 
-    @Override
-    public byte[] toByteArray() {
-        return null;
-    }
-
     @Override
     public String toString() {
         return String.format("%3d", valueInt); //$NON-NLS-1$
index 3a353b3f89be14e5d02fb9016c6ead30b172265e..0a879656aea11ebdd3774b4cb794f9ef29060991 100644 (file)
@@ -41,11 +41,6 @@ final class LongStateValue extends TmfStateValue {
         return valueLong;
     }
 
-    @Override
-    public byte[] toByteArray() {
-        return null;
-    }
-
     @Override
     public String toString() {
         return String.format("%3d", valueLong); //$NON-NLS-1$
index 52de5e5fccc2146ff15683a9253b069ef1384db5..c6bffdaca40a92dcbc2fa9ccb4b8bde653616dd1 100644 (file)
@@ -38,11 +38,6 @@ final class NullStateValue extends TmfStateValue {
         return null;
     }
 
-    @Override
-    public byte[] toByteArray() {
-        return null;
-    }
-
     @Override
     public String toString() {
         return "nullValue"; //$NON-NLS-1$
index bf478cb9ba0557d03a38ecdd4e3ece3fa1d8705e..64822a9122ebf9018849b83c4f8b69bfbddebba5 100644 (file)
@@ -42,11 +42,6 @@ final class StringStateValue extends TmfStateValue {
         return valueStr;
     }
 
-    @Override
-    public byte[] toByteArray() {
-        return valueStr.getBytes();
-    }
-
     @Override
     public String toString() {
         return valueStr;
index b2405ed54d0300a93807402f89b60616573ebcb3..c4398575e6466485f023ee8c0af333907204b0b8 100644 (file)
@@ -43,16 +43,6 @@ public abstract class TmfStateValue implements ITmfStateValue {
      */
     protected abstract Object getValue();
 
-    /**
-     * Specify how to "serialize" this value when writing it to a file.
-     * Alternatively you can return "null" here if you do not need a byte-array
-     * indirection (the getValue will get written as-in instead of the offset in
-     * the file block)
-     *
-     * @return The state value in byte array form
-     */
-    public abstract byte[] toByteArray();
-
     @Override
     public boolean equals(Object other) {
         if (this == other) {
@@ -87,16 +77,6 @@ public abstract class TmfStateValue implements ITmfStateValue {
         return this.getValue().hashCode();
     }
 
-    /**
-     * Return the max size that a variable-length state value can have when
-     * serialized.
-     *
-     * @return The maximum size in bytes
-     */
-    public static int getStateValueMaxSize() {
-        return Byte.MAX_VALUE;
-    }
-
     /*
      * Since all "null state values" are the same, we only need one copy in
      * memory.
This page took 0.030088 seconds and 5 git commands to generate.