import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
-import java.util.Map;
import java.util.Set;
-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.StructDeclaration;
import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
*/
@Test
public void testGetEvents() {
- Map<Long, IEventDeclaration> result = fixture.getEvents();
- assertNotNull(result);
+ assertNotNull(fixture.getEventDeclarations());
}
/**
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
-import java.util.Map;
+import java.util.ArrayList;
+import java.util.List;
import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
trace = new CTFTrace(tempTraceDir);
assertNotNull(trace);
- Map<Long, IEventDeclaration> events = trace.getEvents(0L);
- final EventDeclaration eventDeclaration = (EventDeclaration) events.get(2L);
+ final List<IEventDeclaration> eventDeclarations = new ArrayList<>(trace.getEventDeclarations(0L));
+ final EventDeclaration eventDeclaration = (EventDeclaration) eventDeclarations.get(2);
assertEquals("http://example.com/path_to_model?q=ust_tests_demo:done",
eventDeclaration.getCustomAttribute("model.emf.uri"));
}
import static org.junit.Assume.assumeTrue;
import java.nio.ByteOrder;
+import java.util.ArrayList;
+import java.util.List;
import java.util.UUID;
+import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
import org.eclipse.linuxtools.ctf.core.tests.shared.CtfTestTrace;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
+import org.eclipse.linuxtools.ctf.core.trace.CTFStream;
import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
import org.eclipse.linuxtools.ctf.core.trace.Metadata;
-import org.eclipse.linuxtools.ctf.core.trace.CTFStream;
import org.junit.Before;
import org.junit.Test;
assertNotNull(s);
}
assertEquals(1, count);
- assertEquals(1, trace.getEvents(0L).size());
+ assertEquals(1, trace.getEventDeclarations(0L).size());
return trace;
}
public void testStreamTextMD() throws CTFReaderException {
try (CTFTrace trace = testSingleFragment();) {
fixture.parseTextFragment(mdSecond);
- assertEquals(2, trace.getEvents(0L).size());
- assertEquals("bozo_the_clown", trace.getEvents(0L).get(1L).getName());
+ final List<IEventDeclaration> eventDeclarations = new ArrayList<>(trace.getEventDeclarations(0L));
+ assertEquals(2, eventDeclarations.size());
+ assertEquals("bozo_the_clown", eventDeclarations.get(1).getName());
}
}
*/
@Test
public void testGetId() {
- Long result = fixture.getId();
- assertNotNull(result);
+ assertEquals(1,fixture.id());
}
/**
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;
/**
* 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
*
* @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);
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
/**
- * 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
* 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);
}
}
*/
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;
timestampDef = variantCurrentField.lookupInteger("timestamp"); //$NON-NLS-1$
}
if (simpleIdDef != null) {
- eventID = simpleIdDef.getIntegerValue();
+ eventID = simpleIdDef.getIntegerValue().intValue();
}
if (timestampDef != null) {
timestamp = calculateTimestamp(timestampDef);
}
}
/* 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$
}
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.
* @return Unmodifiable set with the event definitions
*/
public Iterable<IEventDeclaration> getEventDeclarations() {
- return ImmutableSet.copyOf(fStreamInput.getStream().getEvents().values());
+ return ImmutableList.copyOf(fStreamInput.getStream().getEventDeclarations());
}
/**
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;
* 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
*
* 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);
}
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
*/
private StructDeclaration fFields = null;
- /**
- * Event id (can be null if only event in the stream).
- */
- private Long fId = UNSET_EVENT_ID;
-
/**
* Stream to which belongs this event.
*/
/** Map of this event type's custom CTF attributes */
private final Map<String, String> fCustomAttributes = new HashMap<>();
+ private int fId = (int) UNSET_EVENT_ID;
+
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
String[] fieldNames = new String[] { CTFStrings.LOST_EVENTS_FIELD, CTFStrings.LOST_EVENTS_DURATION };
Declaration[] fieldDeclarations = new Declaration[] { IntegerDeclaration.UINT_32B_DECL, IntegerDeclaration.UINT_64B_DECL };
lostEvent.fFields = new StructDeclaration(fieldNames, fieldDeclarations);
- lostEvent.fId = LOST_EVENT_ID;
+ lostEvent.fId = (int) LOST_EVENT_ID;
lostEvent.fName = CTFStrings.LOST_EVENT_NAME;
return lostEvent;
}
* the id
*/
public void setId(long id) {
- fId = id;
+ if (id < 0 || id > Integer.MAX_VALUE) {
+ throw new IllegalArgumentException("id out of range"); //$NON-NLS-1$
+ }
+ fId = (int) id;
}
@Override
public Long getId() {
+ return Long.valueOf(fId);
+ }
+
+ /**
+ * Faster get id assuming you have less than a billion event types
+ *
+ * @return the event id
+ */
+ public int id() {
return fId;
}
* @return is the id set?
*/
public boolean idIsSet() {
- return (fId != null && fId != UNSET_EVENT_ID);
+ return (fId != UNSET_EVENT_ID);
}
/**
} else if (!fFields.equals(other.fFields)) {
return false;
}
- if (fId == null) {
- if (other.fId != null) {
- return false;
- }
- } else if (!fId.equals(other.fId)) {
+ if (fId != (other.fId)) {
return false;
}
if (fName == null) {
result = (prime * result)
+ ((fContext == null) ? 0 : fContext.hashCode());
result = (prime * result) + ((fFields == null) ? 0 : fFields.hashCode());
- result = (prime * result) + ((fId == null) ? 0 : fId.hashCode());
+ result = (prime * result) + fId;
result = (prime * result) + ((fName == null) ? 0 : fName.hashCode());
result = (prime * result) + ((fStream == null) ? 0 : fStream.hashCode());
result = (prime * result) + fCustomAttributes.hashCode();
fEventDeclaration = declaration;
fSourceCPU = cpu;
- fTypeId = declaration.getId();
+ fTypeId = declaration.getId().longValue();
fEventName = declaration.getName();
fEvent = eventDefinition;
if (structFields != null) {
if (structFields.getFieldNames() != null) {
for (String curFieldName : structFields.getFieldNames()) {
- fields.add(CtfTmfEventField.parseField((IDefinition)structFields.getDefinition(curFieldName), curFieldName));
+ fields.add(CtfTmfEventField.parseField((IDefinition) structFields.getDefinition(curFieldName), curFieldName));
}
}
}
for (String contextName : structContext.getFieldNames()) {
/* Prefix field name */
String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + contextName;
- fields.add(CtfTmfEventField.parseField((IDefinition)structContext.getDefinition(contextName), curContextName));
+ fields.add(CtfTmfEventField.parseField((IDefinition) structContext.getDefinition(contextName), curContextName));
}
}