ctf: Add support for scoped event contexts
authorMatthew Khouzam <matthew.khouzam@ericsson.com>
Tue, 23 Oct 2012 19:16:58 +0000 (15:16 -0400)
committerMatthew Khouzam <matthew.khouzam@ericsson.com>
Thu, 25 Oct 2012 15:08:42 +0000 (11:08 -0400)
Change-Id: I19f0ed63b494d6f27b434c58691ed428dc991911
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/8110
Tested-by: Hudson CI
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/event/EventDefinition.java
org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInputPacketReader.java

index 8671aaa32283da374064a8188c3e0123095decf9..9dc227fca278b592dfdb553bd5bc2c3ddee73776 100644 (file)
@@ -14,9 +14,11 @@ package org.eclipse.linuxtools.ctf.core.event;
 
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map.Entry;
 
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
 import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
+import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader;
 
@@ -100,14 +102,66 @@ public class EventDefinition implements IDefinitionScope {
     }
 
     /**
-     * Gets the context of this event
+     * Gets the context of this event without the context of the stream
      *
      * @return the context in struct form
+     * @since 1.2
      */
-    public StructDefinition getContext() {
+    public StructDefinition getEventContext() {
         return context;
     }
 
+    /**
+     * Gets the context of this event within a stream
+     *
+     * @return the context in struct form
+     */
+    public StructDefinition getContext() {
+        final StructDefinition streamContext =
+                streamInputReader.getPacketReader().getStreamEventContextDef();
+
+        /* Most common case so far */
+        if (streamContext == null) {
+            return context;
+        }
+
+        /* streamContext is not null, but the context of the event is null */
+        if (context == null) {
+            return streamContext;
+        }
+
+        /* The stream context and event context are assigned. */
+        StructDeclaration mergedDeclaration = new StructDeclaration(1);
+
+        /* Add fields from the stream */
+        HashMap<String, Definition> defs = streamContext.getDefinitions();
+        for (Entry<String, Definition> entry : defs.entrySet()) {
+            mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration());
+        }
+
+        /* Add fields from the event context, overwrite the stream ones if needed. */
+        for (Entry<String, Definition> entry : context.getDefinitions().entrySet()) {
+            mergedDeclaration.addField(entry.getKey(), entry.getValue().getDeclaration());
+        }
+
+        StructDefinition mergedContext = mergedDeclaration.createDefinition(null, "context"); //$NON-NLS-1$
+        for (String key : mergedContext.getDefinitions().keySet()) {
+            final Definition lookupDefinition = context.lookupDefinition(key);
+            /*
+             * If the key is in the event context, add it from there, if it is
+             * not, then it's in the stream. There is a priority with scoping so
+             * if there is a field like "context" in both stream and context,
+             * you display the context.
+             */
+            if (lookupDefinition != null) {
+                mergedContext.getDefinitions().put(key, lookupDefinition);
+            } else {
+                mergedContext.getDefinitions().put(key, streamContext.lookupDefinition(key));
+            }
+        }
+        return mergedContext;
+    }
+
     /**
      * Gets the stream input reader that this event was made by
      *
index 333390b419aba0404993b00b87844760ccec27c4..428effa92c104cd2b3a0a24fa5915fa9033c636f 100644 (file)
@@ -411,6 +411,11 @@ public class StreamInputPacketReader implements IDefinitionScope {
             timestamp = calculateTimestamp(timestampDef);
         }
 
+        EventDefinition eventDef = events.get(eventID);
+        if (eventDef == null) {
+            throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$
+        }
+
         /*
          * Read the stream event context.
          */
@@ -421,16 +426,13 @@ public class StreamInputPacketReader implements IDefinitionScope {
         /*
          * Get the right event definition using the event id.
          */
-        EventDefinition eventDef = events.get(eventID);
-        if (eventDef == null) {
-            throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$
-        }
+
 
         /*
          * Read the event context.
          */
-        if (eventDef.getContext() != null) {
-            eventDef.getContext().read(currentBitBuffer);
+        if (eventDef.getEventContext() != null) {
+            eventDef.getEventContext().read(currentBitBuffer);
         }
 
         /*
This page took 0.035186 seconds and 5 git commands to generate.