import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
-import java.io.PrintWriter;
import java.nio.channels.ClosedChannelException;
+import java.util.Deque;
+import java.util.LinkedList;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
/**
* The history tree that sits underneath.
*/
- private final @NonNull HistoryTree fSht;
+ private final @NonNull IHistoryTree fSht;
/** Indicates if the history tree construction is done */
private volatile boolean fFinishedBuilding = false;
* If there was a problem during creation
*/
@VisibleForTesting
- protected @NonNull HistoryTree initializeSHT(@NonNull HTConfig conf) throws IOException {
+ protected @NonNull IHistoryTree initializeSHT(@NonNull HTConfig conf) throws IOException {
return HistoryTreeFactory.createHistoryTree(conf);
}
* If there was a problem during creation
*/
@VisibleForTesting
- protected @NonNull HistoryTree initializeSHT(@NonNull File existingStateFile, int providerVersion) throws IOException {
+ protected @NonNull IHistoryTree initializeSHT(@NonNull File existingStateFile, int providerVersion) throws IOException {
return HistoryTreeFactory.createFromFile(existingStateFile.toPath(), providerVersion);
}
*
* @return The history tree
*/
- protected final @NonNull HistoryTree getSHT() {
+ protected final @NonNull IHistoryTree getSHT() {
return fSht;
}
throws TimeRangeException, StateSystemDisposedException {
checkValidTime(t);
+ /* Queue is a stack of nodes containing nodes intersecting t */
+ Deque<HTNode> 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());
- /* 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()) {
+ HTNode currentNode = queue.pop();
+ if (currentNode.getNodeType() == HTNode.NodeType.CORE) {
+ /*Here we add the relevant children nodes for BFS*/
+ queue.addAll(getSHT().selectNextChildren((ParentNode) currentNode, t));
+ }
currentNode.writeInfoFromNode(stateInfo, t);
}
} catch (ClosedChannelException e) {
@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) {
/**
* 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<HTNode> queue = new LinkedList<>();
+ queue.add(getSHT().getRootNode());
+ HTInterval interval = null;
+ while (interval == null && !queue.isEmpty()) {
+ HTNode currentNode = queue.pop();
+ if (currentNode.getNodeType() == HTNode.NodeType.CORE) {
+ queue.addAll(getSHT().selectNextChildren((ParentNode) currentNode, t));
}
- } catch (ClosedChannelException e) {
- throw new StateSystemDisposedException(e);
+ interval = currentNode.getRelevantInterval(key, t);
}
return interval;
}
return (int) ret;
}
- @Override
- public void debugPrint(PrintWriter writer) {
- /* By default don't print out all the intervals */
- debugPrint(writer, false, -1);
- }
-
- /**
- * The basic debugPrint method will print the tree structure, but not their
- * contents.
- *
- * This method here print the contents (the intervals) as well.
- *
- * @param writer
- * The PrintWriter to which the debug info will be written
- * @param printIntervals
- * Should we also print every contained interval individually?
- * @param ts
- * The timestamp that nodes have to intersect for intervals to be
- * printed. A negative value will print intervals for all nodes.
- * The timestamp only applies if printIntervals is true.
- */
- public void debugPrint(PrintWriter writer, boolean printIntervals, long ts) {
- /* Only used for debugging, shouldn't be externalized */
- writer.println("------------------------------"); //$NON-NLS-1$
- writer.println("State History Tree:\n"); //$NON-NLS-1$
- writer.println(getSHT().toString());
- writer.println("Average node utilization: " //$NON-NLS-1$
- + getAverageNodeUsage());
- writer.println(""); //$NON-NLS-1$
-
- getSHT().debugPrintFullTree(writer, printIntervals, ts);
- }
}