ss: Move selectNextChildren to CoreNode and return sequenceNumber
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / backend / historytree / HistoryTreeBackend.java
index 188c9da8be29dafaefc7492d592b66dafef5781e..5e752deeb98b3d09ed35c390aa329a776c25517b 100644 (file)
@@ -19,9 +19,13 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.nio.channels.ClosedChannelException;
+import java.util.Deque;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.logging.Logger;
 
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.tracecompass.common.core.log.TraceCompassLog;
 import org.eclipse.tracecompass.internal.statesystem.core.Activator;
 import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
@@ -40,6 +44,8 @@ import com.google.common.annotations.VisibleForTesting;
  */
 public class HistoryTreeBackend implements IStateHistoryBackend {
 
+    private static final Logger LOGGER = TraceCompassLog.getLogger(HistoryTreeBackend.class);
+
     private final @NonNull String fSsid;
 
     /**
@@ -249,6 +255,7 @@ public class HistoryTreeBackend implements IStateHistoryBackend {
     @Override
     public void dispose() {
         if (fFinishedBuilding) {
+            LOGGER.info(() -> "[HistoryTreeBackend:ClosingFile] size=" + getSHT().getFileSize());  //$NON-NLS-1$
             getSHT().closeFile();
         } else {
             /*
@@ -265,14 +272,21 @@ public class HistoryTreeBackend implements IStateHistoryBackend {
             throws TimeRangeException, StateSystemDisposedException {
         checkValidTime(t);
 
+        /* Queue is a stack of nodes containing nodes intersecting t */
+        Deque<Integer> queue = new LinkedList<>();
+
         /* We start by reading the information in the root node */
-        HTNode currentNode = getSHT().getRootNode();
-        currentNode.writeInfoFromNode(stateInfo, t);
+        queue.add(getSHT().getRootNode().getSequenceNumber());
 
-        /* Then we follow the branch down in the relevant children */
+        /* Then we follow the down in the relevant children */
         try {
-            while (currentNode.getNodeType() == HTNode.NodeType.CORE) {
-                currentNode = getSHT().selectNextChild((CoreNode) currentNode, t);
+            while (!queue.isEmpty()) {
+                int sequenceNumber = queue.pop();
+                HTNode currentNode = getSHT().readNode(sequenceNumber);
+                if (currentNode.getNodeType() == HTNode.NodeType.CORE) {
+                    /* Here we add the relevant children nodes for BFS */
+                    queue.addAll(((ParentNode) currentNode).selectNextChildren(t));
+                }
                 currentNode.writeInfoFromNode(stateInfo, t);
             }
         } catch (ClosedChannelException e) {
@@ -288,7 +302,11 @@ public class HistoryTreeBackend implements IStateHistoryBackend {
     @Override
     public ITmfStateInterval doSingularQuery(long t, int attributeQuark)
             throws TimeRangeException, StateSystemDisposedException {
-        return getRelevantInterval(t, attributeQuark);
+        try {
+            return getRelevantInterval(t, attributeQuark);
+        } catch (ClosedChannelException e) {
+            throw new StateSystemDisposedException(e);
+        }
     }
 
     private void checkValidTime(long t) {
@@ -303,25 +321,22 @@ public class HistoryTreeBackend implements IStateHistoryBackend {
     /**
      * Inner method to find the interval in the tree containing the requested
      * key/timestamp pair, wherever in which node it is.
-     *
-     * @param t
-     * @param key
-     * @return The node containing the information we want
      */
     private HTInterval getRelevantInterval(long t, int key)
-            throws TimeRangeException, StateSystemDisposedException {
+            throws TimeRangeException, ClosedChannelException {
         checkValidTime(t);
 
-        HTNode currentNode = getSHT().getRootNode();
-        HTInterval interval = currentNode.getRelevantInterval(key, t);
-
-        try {
-            while (interval == null && currentNode.getNodeType() == HTNode.NodeType.CORE) {
-                currentNode = getSHT().selectNextChild((CoreNode) currentNode, t);
-                interval = currentNode.getRelevantInterval(key, t);
+        Deque<Integer> queue = new LinkedList<>();
+        queue.add(getSHT().getRootNode().getSequenceNumber());
+        HTInterval interval = null;
+        while (interval == null && !queue.isEmpty()) {
+            int sequenceNumber = queue.pop();
+            HTNode currentNode = getSHT().readNode(sequenceNumber);
+            if (currentNode.getNodeType() == HTNode.NodeType.CORE) {
+                /* Here we add the relevant children nodes for BFS */
+                queue.addAll(((ParentNode) currentNode).selectNextChildren(t));
             }
-        } catch (ClosedChannelException e) {
-            throw new StateSystemDisposedException(e);
+            interval = currentNode.getRelevantInterval(key, t);
         }
         return interval;
     }
This page took 0.025784 seconds and 5 git commands to generate.