Improve javadoc for ctfAdapter in Tmf.Core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputReader.java
index 7c4bf08676dcc0ff9d676d68bb40c3c0213f424a..d04a8089cd6222e20fc4c091c7bdb37e357765bd 100644 (file)
@@ -13,7 +13,6 @@
 package org.eclipse.linuxtools.ctf.core.trace;
 
 import java.nio.ByteOrder;
-import java.util.ListIterator;
 
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
@@ -44,7 +43,7 @@ public class StreamInputReader {
     /**
      * Iterator on the packet index
      */
-    private ListIterator<StreamInputPacketIndexEntry> packetIndexIt;
+    private int packetIndex;
 
     /**
      * Reference to the current event of this trace file (iow, the last on that
@@ -54,6 +53,10 @@ public class StreamInputReader {
 
     private int name;
 
+    private CTFTraceReader parent;
+
+    private final long prevIndex;
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -67,12 +70,12 @@ public class StreamInputReader {
     public StreamInputReader(StreamInput streamInput) {
         this.streamInput = streamInput;
         this.packetReader = new StreamInputPacketReader(this);
-
         /*
          * Get the iterator on the packet index.
          */
-        this.packetIndexIt = streamInput.getIndex().listIterator();
+        this.packetIndex = 0;
 
+        this.prevIndex = 0;
         /*
          * Make first packet the current one.
          */
@@ -83,30 +86,67 @@ public class StreamInputReader {
     // Getters/Setters/Predicates
     // ------------------------------------------------------------------------
 
+    /**
+     * Gets the current event in this stream
+     *
+     * @return the current event in the stream, null if the stream is
+     *         finished/empty/malformed
+     */
     public EventDefinition getCurrentEvent() {
         return this.currentEvent;
     }
 
+    /**
+     * gets the current packet context
+     *
+     * @return the current packet context (size, lost events and such)
+     */
     public StructDefinition getCurrentPacketContext() {
         return this.packetReader.getStreamPacketContextDef();
     }
 
+    /**
+     * Gets the byte order for a trace
+     *
+     * @return the trace byte order
+     */
     public ByteOrder getByteOrder() {
         return streamInput.getStream().getTrace().getByteOrder();
     }
 
+    /**
+     * Gets the name of the stream (it's an id and a number)
+     *
+     * @return gets the stream name (it's a number)
+     */
     public int getName() {
         return this.name;
     }
 
+    /**
+     * Sets the name of the stream
+     *
+     * @param name
+     *            the name of the stream, (it's a number)
+     */
     public void setName(int name) {
         this.name = name;
     }
 
+    /**
+     * Gets the CPU of a stream. It's the same as the one in /proc or running
+     * the asm CPUID instruction
+     *
+     * @return The CPU id (a number)
+     */
     public int getCPU() {
         return this.packetReader.getCPU();
     }
 
+    /**
+     * Gets the filename of the stream being read
+     * @return The filename of the stream being read
+     */
     public String getFilename() {
         return streamInput.getFilename();
     }
@@ -127,21 +167,31 @@ public class StreamInputReader {
      * @return If an event has been successfully read.
      */
     public boolean readNextEvent() {
+
         /*
          * Change packet if needed
          */
         if (!this.packetReader.hasMoreEvents()) {
-            goToNextPacket();
+            final StreamInputPacketIndexEntry prevPacket = this.packetReader
+                    .getCurrentPacket();
+            if (prevPacket != null) {
+                goToNextPacket();
+                final StreamInputPacketIndexEntry currentPacket = this.packetReader
+                        .getCurrentPacket();
+            }
         }
 
-        /*
+        /*autogenerate javadoc getter setter
          * If an event is available, read it.
          */
         if (this.packetReader.hasMoreEvents()) {
             try {
                 this.setCurrentEvent(this.packetReader.readNextEvent());
             } catch (CTFReaderException e) {
-                /* Some problem happened, we'll assume there is no more events */
+                /*
+                 * Some problem happened, we'll assume that there are no more
+                 * events
+                 */
                 return false;
             }
             return true;
@@ -154,14 +204,32 @@ public class StreamInputReader {
      * Change the current packet of the packet reader to the next one.
      */
     private void goToNextPacket() {
-        if (getPacketIndexIt().hasNext()) {
-            StreamInputPacketIndexEntry nextPacket = getPacketIndexIt().next();
-            this.packetReader.setCurrentPacket(nextPacket);
+        packetIndex++;
+        if (getPacketSize() >= (packetIndex + 1)) {
+            this.packetReader.setCurrentPacket(getPacket());
         } else {
-            this.packetReader.setCurrentPacket(null);
+            try {
+                if (this.streamInput.addPacketHeaderIndex()) {
+                    packetIndex = getPacketSize() - 1;
+                    this.packetReader.setCurrentPacket(getPacket());
+
+                } else {
+                    this.packetReader.setCurrentPacket(null);
+                }
+
+            } catch (CTFReaderException e) {
+                this.packetReader.setCurrentPacket(null);
+            }
         }
     }
 
+    /**
+     * @return
+     */
+    private int getPacketSize() {
+        return streamInput.getIndex().getEntries().size();
+    }
+
     /**
      * Changes the location of the trace file reader so that the current event
      * is the first event with a timestamp greater than the given timestamp.
@@ -171,15 +239,24 @@ public class StreamInputReader {
      */
     public long seek(long timestamp) {
         long offset = 0;
-        /*
-         * Search in the index for the packet to search in.
-         */
-        this.packetIndexIt = this.streamInput.getIndex().search(timestamp);
+
+        gotoPacket(timestamp);
 
         /*
-         * Switch to this packet.
+         * index up to the desired timestamp.
          */
-        goToNextPacket();
+        while ((this.packetReader.getCurrentPacket() != null)
+                && (this.packetReader.getCurrentPacket().getTimestampEnd() < timestamp)) {
+            try {
+                this.streamInput.addPacketHeaderIndex();
+                goToNextPacket();
+            } catch (CTFReaderException e) {
+                // do nothing here
+            }
+        }
+        if (this.packetReader.getCurrentPacket() == null) {
+            gotoPacket(timestamp);
+        }
 
         /*
          * Advance until A. we reached the end of the trace file (which means
@@ -188,7 +265,7 @@ public class StreamInputReader {
          */
         readNextEvent();
         boolean done = (this.getCurrentEvent() == null);
-        while (!done && (this.getCurrentEvent().timestamp < timestamp)) {
+        while (!done && (this.getCurrentEvent().getTimestamp() < timestamp)) {
             readNextEvent();
             done = (this.getCurrentEvent() == null);
             offset++;
@@ -196,60 +273,85 @@ public class StreamInputReader {
         return offset;
     }
 
-    public long seekIndex(long index) throws CTFReaderException {
+    /**
+     * @param timestamp
+     */
+    private void gotoPacket(long timestamp) {
+        this.packetIndex = this.streamInput.getIndex().search(timestamp)
+                .previousIndex();
+        /*
+         * Switch to this packet.
+         */
+        goToNextPacket();
+    }
+
+    /**
+     * Seeks the last event of a stream and returns it.
+     */
+    public void goToLastEvent() {
         /*
          * Search in the index for the packet to search in.
          */
-        this.packetIndexIt = this.streamInput.getIndex().searchIndex(index);
+        final int len = this.streamInput.getIndex().getEntries().size();
+
+        StreamInputPacketIndexEntry entry = null;
         /*
-         * Switch to this packet.
+         * Go to beginning of trace.
          */
-        goToNextPacket();
+        seek(0);
         /*
-         * Read the first packet
+         * if the trace is empty.
          */
-        readNextEvent();
+        if ((len == 0) || (this.packetReader.hasMoreEvents() == false)) {
+            /*
+             * This means the trace is empty. abort.
+             */
+            return;
+        }
         /*
-         * get the current index
+         * Go to the last packet that contains events.
          */
-        if (this.packetReader.getCurrentPacket() == null) {
-            throw new CTFReaderException(
-                    "Current packet null in index seek, did you index your trace yet?");
+        for (int pos = len - 1; pos > 0; pos--) {
+            packetIndex = pos;
+            this.packetReader.setCurrentPacket(getPacket());
+            if (this.packetReader.hasMoreEvents()) {
+                break;
+            }
         }
-        return this.packetReader.getCurrentPacket().getIndexBegin();
-
-    }
 
-    public void goToLastEvent() throws CTFReaderException {
         /*
-         * Search in the index for the packet to search in.
+         * Go until the end of that packet
          */
-        int len = this.streamInput.getIndex().getEntries().size();
-        int back = 0;
-        long desired_timestamp = -1;
-        do {
-            back++;
-            StreamInputPacketIndexEntry entry = this.streamInput.getIndex()
-                    .getEntries().get(len - back);
-            desired_timestamp = entry.getTimestampBegin() + 1;
-            seek(desired_timestamp);
-
-        } while (!this.packetReader.hasMoreEvents());
+        EventDefinition prevEvent = null;
+        while (this.currentEvent != null) {
+            prevEvent = this.currentEvent;
+            this.readNextEvent();
+        }
         /*
-         * Go until the end of that packet
+         * Go back to the previous event
          */
+        this.setCurrentEvent(prevEvent);
+    }
 
-        int packet_size = 0;
-        while (this.packetReader.hasMoreEvents()) {
-            this.packetReader.readNextEvent();
-            packet_size++;
-        }
-        seek(desired_timestamp);
-        for (int i = 0; i < (packet_size - 1); i++) {
-            this.packetReader.readNextEvent();
-        }
+    /**
+     * @return the parent
+     */
+    public CTFTraceReader getParent() {
+        return parent;
     }
 
+    /**
+     * @param parent
+     *            the parent to set
+     */
+    public void setParent(CTFTraceReader parent) {
+        this.parent = parent;
+    }
+
+    /**
+     * Sets the current event in a stream input reader
+     * @param currentEvent the event to set
+     */
     public void setCurrentEvent(EventDefinition currentEvent) {
         this.currentEvent = currentEvent;
     }
@@ -257,8 +359,12 @@ public class StreamInputReader {
     /**
      * @return the packetIndexIt
      */
-    private ListIterator<StreamInputPacketIndexEntry> getPacketIndexIt() {
-        return packetIndexIt;
+    private int getPacketIndex() {
+        return packetIndex;
+    }
+
+    private StreamInputPacketIndexEntry getPacket() {
+        return streamInput.getIndex().getEntries().get(getPacketIndex());
     }
 
     /**
@@ -268,4 +374,49 @@ public class StreamInputReader {
         return packetReader;
     }
 
+    /*
+     * (non-Javadoc)
+     *
+     * @see java.lang.Object#hashCode()
+     */
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = (prime * result) + name;
+        result = (prime * result)
+                + ((streamInput == null) ? 0 : streamInput.hashCode());
+        return result;
+    }
+
+    /*
+     * (non-Javadoc)
+     *
+     * @see java.lang.Object#equals(java.lang.Object)
+     */
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (!(obj instanceof StreamInputReader)) {
+            return false;
+        }
+        StreamInputReader other = (StreamInputReader) obj;
+        if (name != other.name) {
+            return false;
+        }
+        if (streamInput == null) {
+            if (other.streamInput != null) {
+                return false;
+            }
+        } else if (!streamInput.equals(other.streamInput)) {
+            return false;
+        }
+        return true;
+    }
+
 }
This page took 0.030088 seconds and 5 git commands to generate.