ctf: Expose map of attributes in Packet Descriptor
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Wed, 2 Dec 2015 21:07:40 +0000 (16:07 -0500)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Wed, 16 Dec 2015 21:45:57 +0000 (16:45 -0500)
The motivation is to allow accessing custom fields in CTF
packet contexts. It decouples the CTF parser from LTTng a bit.

This breaks an unused api.

Change-Id: I2bead35a7e080d4e3ab325395629c44eea455cfc
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-on: https://git.eclipse.org/r/61815
Reviewed-by: Hudson CI
Reviewed-by: Francis Giraldeau <francis.giraldeau@gmail.com>
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Tested-by: Patrick Tasse <patrick.tasse@gmail.com>
ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/ctf/core/trace/ICTFPacketDescriptor.java
ctf/org.eclipse.tracecompass.ctf.core/src/org/eclipse/tracecompass/internal/ctf/core/trace/StreamInputPacketIndexEntry.java

index 649c5f5600d4637b1a1dd0cfd1af6ee36d4f83d5..95b244b421da83af0c20c98c7fde6b898eb63a60 100644 (file)
  *******************************************************************************/
 package org.eclipse.tracecompass.ctf.core.trace;
 
+import java.util.Map;
+
+import org.eclipse.jdt.annotation.NonNull;
+
 /**
  * CTF Packet descriptor, can come from a packet header or an index file, this
  * will show certain information about the packet such as the size and
  * timerange.
  *
- * @since 1.0
+ * @since 2.0
  */
 public interface ICTFPacketDescriptor {
 
@@ -38,30 +42,33 @@ public interface ICTFPacketDescriptor {
     long getOffsetBits();
 
     /**
-     * Gets the size of the packet in bits. If you have a 1mb packet that is 499kb
-     * used and the header is 1kb, this will return 1mb
+     * Gets the size of the packet in bits. If you have a 1mb packet that is
+     * 499kb used and the header is 1kb, this will return 1mb
      *
      * @return the packetSizeBits
      */
     long getPacketSizeBits();
 
     /**
-     * Get the content size of the packet in bits. If you have a 1mb packet that is 499kb
-     * used and the header is 1kb, this will return 500kb (used data + header
+     * Get the content size of the packet in bits. If you have a 1mb packet that
+     * is 499kb used and the header is 1kb, this will return 500kb (used data +
+     * header
      *
      * @return the contentSizeBits
      */
     long getContentSizeBits();
 
     /**
-     * Gets the beginning timestamp of the packet, all events within the packet will have timestamps after or at this time
+     * Gets the beginning timestamp of the packet, all events within the packet
+     * will have timestamps after or at this time
      *
      * @return the timestampBegin
      */
     long getTimestampBegin();
 
     /**
-     * Gets the ending timestamp of the packet, all events within the packet will have timestamps before or at this time
+     * Gets the ending timestamp of the packet, all events within the packet
+     * will have timestamps before or at this time
      *
      * @return the timestampEnd
      */
@@ -75,13 +82,13 @@ public interface ICTFPacketDescriptor {
     long getLostEvents();
 
     /**
-     * Retrieve the value of an existing attribute
+     * Retrieve the map of the packet attributes
      *
-     * @param field
-     *            The name of the attribute
-     * @return The value that was stored, or null if it wasn't found
+     * @return The map of attributes stored. Example keys are "device" or
+     *         "timestamp_begin"
+     * @since 2.0
      */
-    Object lookupAttribute(String field);
+    @NonNull Map<String, Object> getAttributes();
 
     /**
      * Get the target of the packet (what device generated this packet)
index 64bb804615bfe43f0944b577f4602ab12f89b438..c85db80e8f87f8a7b20afbfb48a0affef5743739 100644 (file)
 package org.eclipse.tracecompass.internal.ctf.core.trace;
 
 import java.util.AbstractMap;
-import java.util.HashMap;
+import java.util.Collections;
 import java.util.Map;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.tracecompass.common.core.NonNullUtils;
 import org.eclipse.tracecompass.ctf.core.CTFStrings;
 import org.eclipse.tracecompass.ctf.core.event.types.EnumDefinition;
@@ -30,6 +31,9 @@ import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
 import org.eclipse.tracecompass.ctf.core.trace.ICTFPacketDescriptor;
 import org.eclipse.tracecompass.ctf.core.trace.IPacketReader;
 
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableMap.Builder;
+
 /**
  * <b><u>StreamInputPacketIndexEntry</u></b>
  * <p>
@@ -87,7 +91,7 @@ public class StreamInputPacketIndexEntry implements ICTFPacketDescriptor {
     /**
      * Attributes of this index entry
      */
-    private final Map<String, Object> fAttributes = new HashMap<>();
+    private final @NonNull Map<String, Object> fAttributes;
 
     private final long fEndPacketHeaderBits;
 
@@ -107,6 +111,7 @@ public class StreamInputPacketIndexEntry implements ICTFPacketDescriptor {
      */
 
     public StreamInputPacketIndexEntry(long dataOffsetBits, long fileSizeBytes) {
+        fAttributes = Collections.EMPTY_MAP;
         fContentSizeBits = (fileSizeBytes * Byte.SIZE);
         fPacketSizeBits = (fileSizeBytes * Byte.SIZE);
         fOffsetBits = dataOffsetBits;
@@ -153,22 +158,7 @@ public class StreamInputPacketIndexEntry implements ICTFPacketDescriptor {
      */
     public StreamInputPacketIndexEntry(long dataOffsetBits, StructDefinition streamPacketContextDef, long fileSizeBytes, long lostSoFar, long endPacketHeaderBits) {
         fEndPacketHeaderBits = endPacketHeaderBits;
-        for (String field : streamPacketContextDef.getDeclaration().getFieldsList()) {
-            IDefinition id = streamPacketContextDef.lookupDefinition(field);
-            if (id instanceof IntegerDefinition) {
-                fAttributes.put(field, ((IntegerDefinition) id).getValue());
-            } else if (id instanceof FloatDefinition) {
-                fAttributes.put(field, ((FloatDefinition) id).getValue());
-            } else if (id instanceof EnumDefinition) {
-                final EnumDefinition enumDec = (EnumDefinition) id;
-                fAttributes.put(field, new AbstractMap.SimpleImmutableEntry<>(
-                        NonNullUtils.checkNotNull(enumDec.getStringValue()),
-                        NonNullUtils.checkNotNull(enumDec.getIntegerValue())));
-            } else if (id instanceof StringDefinition) {
-                fAttributes.put(field, ((StringDefinition) id).getValue());
-            }
-        }
-
+        fAttributes = computeAttributeMap(streamPacketContextDef);
         fContentSizeBits = computeContentSize(fileSizeBytes);
         fPacketSizeBits = computePacketSize(fileSizeBytes);
         fTimestampBegin = computeTsBegin();
@@ -183,6 +173,26 @@ public class StreamInputPacketIndexEntry implements ICTFPacketDescriptor {
         fLostEvents = computeLostEvents(lostSoFar);
     }
 
+    private @NonNull static ImmutableMap<String, Object> computeAttributeMap(StructDefinition streamPacketContextDef) {
+        Builder<String, Object> attributeBuilder = ImmutableMap.<String, Object> builder();
+        for (String field : streamPacketContextDef.getDeclaration().getFieldsList()) {
+            IDefinition id = streamPacketContextDef.lookupDefinition(field);
+            if (id instanceof IntegerDefinition) {
+                attributeBuilder.put(field, ((IntegerDefinition) id).getValue());
+            } else if (id instanceof FloatDefinition) {
+                attributeBuilder.put(field, ((FloatDefinition) id).getValue());
+            } else if (id instanceof EnumDefinition) {
+                final EnumDefinition enumDec = (EnumDefinition) id;
+                attributeBuilder.put(field, new AbstractMap.SimpleImmutableEntry<>(
+                        NonNullUtils.checkNotNull(enumDec.getStringValue()),
+                        NonNullUtils.checkNotNull(enumDec.getIntegerValue())));
+            } else if (id instanceof StringDefinition) {
+                attributeBuilder.put(field, ((StringDefinition) id).getValue());
+            }
+        }
+        return attributeBuilder.build();
+    }
+
     private Long getPacketSize() {
         return (Long) fAttributes.get(CTFStrings.PACKET_SIZE);
     }
@@ -331,21 +341,9 @@ public class StreamInputPacketIndexEntry implements ICTFPacketDescriptor {
         return fLostEvents;
     }
 
-    /**
-     * Add an attribute to this index entry
-     *
-     * @param field
-     *            The name of the attribute
-     * @param value
-     *            The value to insert
-     */
-    public void addAttribute(String field, Object value) {
-        fAttributes.put(field, value);
-    }
-
     @Override
-    public Object lookupAttribute(String field) {
-        return fAttributes.get(field);
+    public Map<String, Object> getAttributes() {
+        return fAttributes;
     }
 
     @Override
This page took 0.027799 seconds and 5 git commands to generate.