diff options
author | Matthew Khouzam | 2014-07-09 19:29:04 +0000 |
---|---|---|
committer | Matthew Khouzam | 2014-08-21 14:08:54 +0000 |
commit | a72d4f4412c0e4fa1580ff534d3c2267107cf553 (patch) | |
tree | 11ef8369e58d652be0bf8d671abbfa6b1ac5b0cc /lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf | |
parent | bb0f2e477fa0d12de1de211bbc6a47772993fead (diff) | |
download | org.eclipse.linuxtools-a72d4f4412c0e4fa1580ff534d3c2267107cf553.tar.gz org.eclipse.linuxtools-a72d4f4412c0e4fa1580ff534d3c2267107cf553.tar.xz org.eclipse.linuxtools-a72d4f4412c0e4fa1580ff534d3c2267107cf553.zip |
ctf: replace HashMaps with ArrayLists for EventDeclaration storage
Parser no longer supports events with IDs larger than Integer.MAX_VALUE
Change-Id: I088943c5a041f16638cb89b832ab79f8ef9c1d76
Signed-off-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/25924
Tested-by: Hudson CI
Reviewed-by: Patrick Tasse <patrick.tasse@gmail.com>
Diffstat (limited to 'lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf')
4 files changed, 142 insertions, 35 deletions
diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStream.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStream.java index 15545c5e28..57f2445361 100644 --- a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStream.java +++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStream.java @@ -12,11 +12,16 @@ package org.eclipse.linuxtools.ctf.core.trace; -import java.util.HashMap; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; +import org.eclipse.jdt.annotation.NonNull; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.IDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.IEventHeaderDeclaration; @@ -57,7 +62,9 @@ public class CTFStream { /** * Maps event ids to events */ - private Map<Long, IEventDeclaration> fEvents = new HashMap<>(); + private final ArrayList<IEventDeclaration> fEvents = new ArrayList<>(); + + private boolean fEventUnsetId = false; /** * The inputs associated to this stream @@ -229,9 +236,48 @@ public class CTFStream { * * @return all the event declarations for this stream, using the id as a key * for the hashmap. + * @deprecated use {@link CTFStream#getEventDeclarations()} */ + @Deprecated public Map<Long, IEventDeclaration> getEvents() { - return fEvents; + throw new UnsupportedOperationException(); + } + + /** + * Get all the event declarations in this stream. + * + * @return The event declarations for this stream + * @since 3.1 + */ + public @NonNull Collection<IEventDeclaration> getEventDeclarations() { + List<IEventDeclaration> retVal = new ArrayList<>(fEvents); + retVal.removeAll(Collections.<IEventDeclaration> singletonList(null)); + return retVal; + } + + /** + * Get the event declaration for a given ID. + * + * @param eventId + * The ID, can be {@link EventDeclaration#UNSET_EVENT_ID}, or any + * positive value + * @return The event declaration with the given ID for this stream, or + * 'null' if there are no declaration with this ID + * @throws IllegalArgumentException + * If the passed ID is invalid + * @since 3.1 + */ + public @Nullable IEventDeclaration getEventDeclaration(int eventId) { + int eventIndex = (eventId == EventDeclaration.UNSET_EVENT_ID) ? 0 : eventId; + if (eventIndex < 0) { + /* Any negative value other than UNSET_EVENT_ID is invalid */ + throw new IllegalArgumentException("Event ID cannot be negative."); //$NON-NLS-1$ + } + if (eventIndex >= fEvents.size()) { + /* This ID could be valid, but there are no declarations with it */ + return null; + } + return fEvents.get(eventIndex); } // ------------------------------------------------------------------------ @@ -239,7 +285,7 @@ public class CTFStream { // ------------------------------------------------------------------------ /** - * Adds an event to the event map. + * Adds an event to the event list. * * An event in a stream can omit its id if it is the only event in this * stream. An event for which no id has been specified has a null id. It is @@ -254,31 +300,67 @@ public class CTFStream { * stream */ public void addEvent(IEventDeclaration event) throws ParseException { - /* - * If there is an event without id (the null key), it must be the only - * one - */ - if (fEvents.get(null) != null) { + if (fEventUnsetId) { throw new ParseException("Event without id with multiple events in a stream"); //$NON-NLS-1$ } + int id = ((EventDeclaration) event).id(); /* * If there is an event without id (the null key), it must be the only * one */ - if ((event.getId() == null) && (fEvents.size() != 0)) { - throw new ParseException("Event without id with multiple events in a stream"); //$NON-NLS-1$ + if (id == EventDeclaration.UNSET_EVENT_ID) { + if (!fEvents.isEmpty()) { + throw new ParseException("Event without id with multiple events in a stream"); //$NON-NLS-1$ + } + fEventUnsetId = true; + fEvents.add(event); + } else { + /* Check if an event with the same ID already exists */ + if (fEvents.size() > id && fEvents.get(id) != null) { + throw new ParseException("Event id already exists"); //$NON-NLS-1$ + } + ensureSize(fEvents, id); + /* Put the event in the list */ + fEvents.set(id, event); } + } - /* Check if an event with the same ID already exists */ - if (fEvents.get(event.getId()) != null) { - throw new ParseException("Event id already exists"); //$NON-NLS-1$ + /** + * Add a list of event declarations to this stream. There must be no overlap + * between the two lists of event declarations. This will merge the two + * lists and preserve the indexes of both lists. + * + * @param events + * list of the events to add + * @throws CTFReaderException + * if the list already contains data + * @since 3.1 + */ + public void addEvents(Collection<IEventDeclaration> events) throws CTFReaderException { + if (fEventUnsetId) { + throw new CTFReaderException("Cannot add to a stream with an unidentified event"); //$NON-NLS-1$ } - if (event.getId() == null) { - fEvents.put(EventDeclaration.UNSET_EVENT_ID, event); - } else { - /* Put the event in the map */ - fEvents.put(event.getId(), event); + if (fEvents.isEmpty()) { + fEvents.addAll(events); + return; + } + for (IEventDeclaration event : events) { + if (event != null) { + int index = event.getId().intValue(); + ensureSize(fEvents, index); + if (fEvents.get(index) != null) { + throw new CTFReaderException("Both lists have an event defined at position " + index); //$NON-NLS-1$ + } + fEvents.set(index, event); + } + } + } + + private static void ensureSize(ArrayList<? extends Object> list, int index) { + list.ensureCapacity(index); + while (list.size() <= index) { + list.add(null); } } diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStreamInputPacketReader.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStreamInputPacketReader.java index 07fa8d804d..3004025ffd 100644 --- a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStreamInputPacketReader.java +++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStreamInputPacketReader.java @@ -318,7 +318,8 @@ public class CTFStreamInputPacketReader implements IDefinitionScope, AutoCloseab */ public EventDefinition readNextEvent() throws CTFReaderException { /* Default values for those fields */ - long eventID = EventDeclaration.UNSET_EVENT_ID; + // compromise since we cannot have 64 bit addressing of arrays yet. + int eventID = (int) EventDeclaration.UNSET_EVENT_ID; long timestamp = 0; if (fHasLost) { fHasLost = false; @@ -409,7 +410,7 @@ public class CTFStreamInputPacketReader implements IDefinitionScope, AutoCloseab timestampDef = variantCurrentField.lookupInteger("timestamp"); //$NON-NLS-1$ } if (simpleIdDef != null) { - eventID = simpleIdDef.getIntegerValue(); + eventID = simpleIdDef.getIntegerValue().intValue(); } if (timestampDef != null) { timestamp = calculateTimestamp(timestampDef); @@ -417,7 +418,7 @@ public class CTFStreamInputPacketReader implements IDefinitionScope, AutoCloseab } } /* Get the right event definition using the event id. */ - IEventDeclaration eventDeclaration = fStreamInputReader.getStreamInput().getStream().getEvents().get(eventID); + IEventDeclaration eventDeclaration = fStreamInputReader.getStreamInput().getStream().getEventDeclaration(eventID); if (eventDeclaration == null) { throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$ } diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStreamInputReader.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStreamInputReader.java index 1a91b1bcb9..ebbe1d7803 100644 --- a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStreamInputReader.java +++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFStreamInputReader.java @@ -20,7 +20,7 @@ import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration; import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry; -import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableList; /** * A CTF trace event reader. Reads the events of a trace file. @@ -175,7 +175,7 @@ public class CTFStreamInputReader implements AutoCloseable { * @return Unmodifiable set with the event definitions */ public Iterable<IEventDeclaration> getEventDeclarations() { - return ImmutableSet.copyOf(fStreamInput.getStream().getEvents().values()); + return ImmutableList.copyOf(fStreamInput.getStream().getEventDeclarations()); } /** diff --git a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java index 4bb35847b7..c21fd709e8 100644 --- a/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java +++ b/lttng/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/CTFTrace.java @@ -24,12 +24,11 @@ import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import java.util.TreeSet; import java.util.UUID; @@ -236,12 +235,26 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable { * The ID of the stream from which to read * @return The Hash map with the event declarations * @since 2.0 + * @deprecated use {@link CTFTrace#getEventDeclarations(Long)} */ + @Deprecated public Map<Long, IEventDeclaration> getEvents(Long streamId) { return fStreams.get(streamId).getEvents(); } /** + * Gets an event declaration list for a given streamID + * + * @param streamId + * The ID of the stream from which to read + * @return The list of event declarations + * @since 3.1 + */ + public Collection<IEventDeclaration> getEventDeclarations(Long streamId) { + return fStreams.get(streamId).getEventDeclarations(); + } + + /** * Get an event by it's ID * * @param streamId @@ -250,8 +263,25 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable { * the ID of the event * @return the event declaration * @since 2.0 + * @deprecated use {@link CTFTrace#getEventType(long, int)} instead */ + @Deprecated public IEventDeclaration getEventType(long streamId, long id) { + return getStream(streamId).getEventDeclaration((int) id); + } + + + /** + * Get an event by it's ID + * + * @param streamId + * The ID of the stream from which to read + * @param id + * the ID of the event + * @return the event declaration + * @since 3.1 + */ + public IEventDeclaration getEventType(long streamId, int id) { return getEvents(streamId).get(id); } @@ -455,16 +485,10 @@ public class CTFTrace implements IDefinitionScope, AutoCloseable { private void addStream(CTFStreamInput s) { /* - * Copy the events + * add the stream */ - Iterator<Entry<Long, IEventDeclaration>> it = s.getStream() - .getEvents().entrySet().iterator(); - while (it.hasNext()) { - Entry<Long, IEventDeclaration> pairs = it.next(); - Long eventNum = pairs.getKey(); - IEventDeclaration eventDec = pairs.getValue(); - getEvents(s.getStream().getId()).put(eventNum, eventDec); - } + CTFStream stream = s.getStream(); + fStreams.put(stream.getId(), stream); /* * index the trace |