ss: add a wrapper for the state system delete files
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / StateSystem.java
index d4c85ff77cc79f5bae48f31d62597d7d1e18faff..81b7feada9a820309077da1f1226ec4dc8395a9a 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2015 Ericsson
+ * Copyright (c) 2012, 2016 Ericsson
  * Copyright (c) 2010, 2011 École Polytechnique de Montréal
  * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
  *
@@ -17,15 +17,17 @@ package org.eclipse.tracecompass.internal.statesystem.core;
 
 import java.io.File;
 import java.io.IOException;
-import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
+import java.util.logging.Logger;
 
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.common.core.log.TraceCompassLog;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
 import org.eclipse.tracecompass.statesystem.core.backend.IStateHistoryBackend;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
@@ -37,6 +39,9 @@ import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue.Type;
 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
 
+import com.google.common.collect.ImmutableCollection.Builder;
+import com.google.common.collect.ImmutableSet;
+
 /**
  * This is the core class of the Generic State System. It contains all the
  * methods to build and query a state history. It's exposed externally through
@@ -52,6 +57,11 @@ import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
  */
 public class StateSystem implements ITmfStateSystemBuilder {
 
+    private static final String PARENT = ".."; //$NON-NLS-1$
+    private static final String WILDCARD = "*"; //$NON-NLS-1$
+
+    private static final Logger LOGGER = TraceCompassLog.getLogger(StateSystem.class);
+
     /* References to the inner structures */
     private final AttributeTree attributeTree;
     private final TransientState transState;
@@ -235,17 +245,35 @@ public class StateSystem implements ITmfStateSystemBuilder {
     @Override
     public int getQuarkAbsolute(String... attribute)
             throws AttributeNotFoundException {
-        return getAttributeTree().getQuarkDontAdd(-1, attribute);
+        int quark = getAttributeTree().getQuarkDontAdd(ROOT_ATTRIBUTE, attribute);
+        if (quark == INVALID_ATTRIBUTE) {
+            throw new AttributeNotFoundException(getSSID() + " Path:" + Arrays.toString(attribute)); //$NON-NLS-1$
+        }
+        return quark;
+    }
+
+    @Override
+    public int optQuarkAbsolute(String... attribute) {
+        return getAttributeTree().getQuarkDontAdd(ROOT_ATTRIBUTE, attribute);
     }
 
     @Override
     public int getQuarkAbsoluteAndAdd(String... attribute) {
-        return getAttributeTree().getQuarkAndAdd(-1, attribute);
+        return getAttributeTree().getQuarkAndAdd(ROOT_ATTRIBUTE, attribute);
     }
 
     @Override
     public int getQuarkRelative(int startingNodeQuark, String... subPath)
             throws AttributeNotFoundException {
+        int quark = getAttributeTree().getQuarkDontAdd(startingNodeQuark, subPath);
+        if (quark == INVALID_ATTRIBUTE) {
+            throw new AttributeNotFoundException(getSSID() + " Quark:" + startingNodeQuark + ", SubPath:" + Arrays.toString(subPath)); //$NON-NLS-1$ //$NON-NLS-2$
+        }
+        return quark;
+    }
+
+    @Override
+    public int optQuarkRelative(int startingNodeQuark, String... subPath) {
         return getAttributeTree().getQuarkDontAdd(startingNodeQuark, subPath);
     }
 
@@ -255,16 +283,14 @@ public class StateSystem implements ITmfStateSystemBuilder {
     }
 
     @Override
-    public List<Integer> getSubAttributes(int quark, boolean recursive)
-            throws AttributeNotFoundException {
+    public List<@NonNull Integer> getSubAttributes(int quark, boolean recursive) {
         return getAttributeTree().getSubAttributes(quark, recursive);
     }
 
     @Override
-    public List<Integer> getSubAttributes(int quark, boolean recursive, String pattern)
-            throws AttributeNotFoundException {
+    public List<@NonNull Integer> getSubAttributes(int quark, boolean recursive, String pattern) {
         List<Integer> all = getSubAttributes(quark, recursive);
-        List<Integer> ret = new LinkedList<>();
+        List<@NonNull Integer> ret = new LinkedList<>();
         for (Integer attQuark : all) {
             String name = getAttributeName(attQuark.intValue());
             if (name.matches(pattern)) {
@@ -281,86 +307,51 @@ public class StateSystem implements ITmfStateSystemBuilder {
 
     @Override
     public List<@NonNull Integer> getQuarks(String... pattern) {
-        List<@NonNull Integer> quarks = new LinkedList<>();
-        List<String> prefix = new LinkedList<>();
-        List<String> suffix = new LinkedList<>();
-        boolean split = false;
-        String[] prefixStr;
-        String[] suffixStr;
-        List<Integer> directChildren;
-        int startingAttribute;
-
-        /* Fill the "prefix" and "suffix" parts of the pattern around the '*' */
-        for (String entry : pattern) {
-            if (entry.equals("*")) { //$NON-NLS-1$
-                if (split) {
-                    /*
-                     * Split was already true? This means there was more than
-                     * one wildcard. This is not supported, return an empty
-                     * list.
-                     */
-                    return quarks;
-                }
-                split = true;
-                continue;
-            }
+        return getQuarks(ROOT_ATTRIBUTE, pattern);
+    }
 
-            if (split) {
-                suffix.add(entry);
-            } else {
-                prefix.add(entry);
-            }
+    @Override
+    public List<@NonNull Integer> getQuarks(int startingNodeQuark, String... pattern) {
+        Builder<@NonNull Integer> builder = ImmutableSet.builder();
+        if (pattern.length > 0) {
+            getQuarks(builder, startingNodeQuark, Arrays.asList(pattern));
+        } else {
+            builder.add(startingNodeQuark);
         }
-        prefixStr = prefix.toArray(new String[prefix.size()]);
-        suffixStr = suffix.toArray(new String[suffix.size()]);
+        return builder.build().asList();
+    }
 
-        /*
-         * If there was no wildcard, we'll only return the one matching
-         * attribute, if there is one.
-         */
-        if (!split) {
-            int quark;
-            try {
-                quark = getQuarkAbsolute(prefixStr);
-            } catch (AttributeNotFoundException e) {
-                /* It's fine, we'll just return the empty List */
-                return quarks;
-            }
-            quarks.add(quark);
-            return quarks;
+    private void getQuarks(Builder<@NonNull Integer> builder, int quark, List<String> pattern) {
+        String element = pattern.get(0);
+        if (element == null) {
+            return;
         }
-
-        try {
-            if (prefix.size() == 0) {
-                /*
-                 * If 'prefix' is empty, this means the wildcard was the first
-                 * element. Look for the root node's sub-attributes.
-                 */
-                startingAttribute = -1;
+        List<String> remainder = pattern.subList(1, pattern.size());
+        if (remainder.isEmpty()) {
+            if (element.equals(WILDCARD)) {
+                builder.addAll(getSubAttributes(quark, false));
+            } else if (element.equals(PARENT)){
+                builder.add(getParentAttributeQuark(quark));
             } else {
-                startingAttribute = getQuarkAbsolute(prefixStr);
+                int subQuark = optQuarkRelative(quark, element);
+                if (subQuark != INVALID_ATTRIBUTE) {
+                    builder.add(subQuark);
+                }
             }
-            directChildren = getSubAttributes(startingAttribute, false);
-        } catch (AttributeNotFoundException e) {
-            /* That attribute path did not exist, return the empty array */
-            return quarks;
-        }
-
-        /*
-         * Iterate of all the sub-attributes, and only keep those who match the
-         * 'suffix' part of the initial pattern.
-         */
-        for (int childQuark : directChildren) {
-            int matchingQuark;
-            try {
-                matchingQuark = getQuarkRelative(childQuark, suffixStr);
-            } catch (AttributeNotFoundException e) {
-                continue;
+        } else {
+            if (element.equals(WILDCARD)) {
+                for (@NonNull Integer subquark : getSubAttributes(quark, false)) {
+                    getQuarks(builder, subquark, remainder);
+                }
+            } else if (element.equals(PARENT)){
+                getQuarks(builder, getParentAttributeQuark(quark), remainder);
+            } else {
+                int subQuark = optQuarkRelative(quark, element);
+                if (subQuark != INVALID_ATTRIBUTE) {
+                    getQuarks(builder, subQuark, remainder);
+                }
             }
-            quarks.add(matchingQuark);
         }
-
-        return quarks;
     }
 
     //--------------------------------------------------------------------------
@@ -368,23 +359,15 @@ public class StateSystem implements ITmfStateSystemBuilder {
     //--------------------------------------------------------------------------
 
     @Override
-    public void modifyAttribute(long t, ITmfStateValue value, int attributeQuark)
-            throws TimeRangeException, AttributeNotFoundException,
-            StateValueTypeException {
-        if (value == null) {
-            /*
-             * TODO Replace with @NonNull parameter (will require fixing all the
-             * state providers!)
-             */
-            throw new IllegalArgumentException();
-        }
+    public void modifyAttribute(long t, @NonNull ITmfStateValue value, int attributeQuark)
+            throws TimeRangeException, StateValueTypeException {
         transState.processStateChange(t, value, attributeQuark);
     }
 
+    @Deprecated
     @Override
     public void incrementAttribute(long t, int attributeQuark)
-            throws StateValueTypeException, TimeRangeException,
-            AttributeNotFoundException {
+            throws StateValueTypeException, TimeRangeException {
         ITmfStateValue stateValue = queryOngoingState(attributeQuark);
         int prevValue = 0;
         /* if the attribute was previously null, start counting at 0 */
@@ -396,9 +379,8 @@ public class StateSystem implements ITmfStateSystemBuilder {
     }
 
     @Override
-    public void pushAttribute(long t, ITmfStateValue value, int attributeQuark)
-            throws TimeRangeException, AttributeNotFoundException,
-            StateValueTypeException {
+    public void pushAttribute(long t, @NonNull ITmfStateValue value, int attributeQuark)
+            throws TimeRangeException, StateValueTypeException {
         int stackDepth;
         int subAttributeQuark;
         ITmfStateValue previousSV = transState.getOngoingStateValue(attributeQuark);
@@ -423,7 +405,7 @@ public class StateSystem implements ITmfStateSystemBuilder {
              * out of control due to buggy insertions
              */
             String message = " Stack limit reached, not pushing"; //$NON-NLS-1$
-            throw new AttributeNotFoundException(getSSID() + " Quark:" + attributeQuark + message); //$NON-NLS-1$
+            throw new IllegalStateException(getSSID() + " Quark:" + attributeQuark + message); //$NON-NLS-1$
         }
 
         stackDepth++;
@@ -435,8 +417,7 @@ public class StateSystem implements ITmfStateSystemBuilder {
 
     @Override
     public ITmfStateValue popAttribute(long t, int attributeQuark)
-            throws AttributeNotFoundException, TimeRangeException,
-            StateValueTypeException {
+            throws TimeRangeException, StateValueTypeException {
         /* These are the state values of the stack-attribute itself */
         ITmfStateValue previousSV = transState.getOngoingStateValue(attributeQuark);
 
@@ -465,7 +446,13 @@ public class StateSystem implements ITmfStateSystemBuilder {
         }
 
         /* The attribute should already exist at this point */
-        int subAttributeQuark = getQuarkRelative(attributeQuark, String.valueOf(stackDepth));
+        int subAttributeQuark;
+        try {
+            subAttributeQuark = getQuarkRelative(attributeQuark, String.valueOf(stackDepth));
+        } catch (AttributeNotFoundException e) {
+            String message = " Stack attribute missing sub-attribute for depth:" + stackDepth; //$NON-NLS-1$
+            throw new IllegalStateException(getSSID() + " Quark:" + attributeQuark + message); //$NON-NLS-1$
+        }
         ITmfStateValue poppedValue = queryOngoingState(subAttributeQuark);
 
         /* Update the state value of the stack-attribute */
@@ -486,11 +473,7 @@ public class StateSystem implements ITmfStateSystemBuilder {
 
     @Override
     public void removeAttribute(long t, int attributeQuark)
-            throws TimeRangeException, AttributeNotFoundException {
-        if (attributeQuark < 0) {
-            throw new IllegalArgumentException();
-        }
-
+            throws TimeRangeException {
         /*
          * Nullify our children first, recursively. We pass 'false' because we
          * handle the recursion ourselves.
@@ -520,20 +503,17 @@ public class StateSystem implements ITmfStateSystemBuilder {
     //--------------------------------------------------------------------------
 
     @Override
-    public ITmfStateValue queryOngoingState(int attributeQuark)
-            throws AttributeNotFoundException {
+    public ITmfStateValue queryOngoingState(int attributeQuark) {
         return transState.getOngoingStateValue(attributeQuark);
     }
 
     @Override
-    public long getOngoingStartTime(int attribute)
-            throws AttributeNotFoundException {
+    public long getOngoingStartTime(int attribute) {
         return transState.getOngoingStartTime(attribute);
     }
 
     @Override
-    public void updateOngoingState(ITmfStateValue newValue, int attributeQuark)
-            throws AttributeNotFoundException {
+    public void updateOngoingState(ITmfStateValue newValue, int attributeQuark) {
         transState.changeOngoingStateValue(attributeQuark, newValue);
     }
 
@@ -560,6 +540,8 @@ public class StateSystem implements ITmfStateSystemBuilder {
             throw new StateSystemDisposedException();
         }
 
+        LOGGER.info(() -> "[StateSystem:FullQueryStart] ssid=" + this.getSSID() + ", ts=" + t);  //$NON-NLS-1$//$NON-NLS-2$
+
         final int nbAttr = getNbAttributes();
         List<@Nullable ITmfStateInterval> stateInfo = new ArrayList<>(nbAttr);
 
@@ -587,17 +569,19 @@ public class StateSystem implements ITmfStateSystemBuilder {
                 throw new IllegalStateException("Incoherent interval storage"); //$NON-NLS-1$
             }
         }
+        LOGGER.info(() -> "[StateSystem:FullQueryEnd]");  //$NON-NLS-1$
         return stateInfo;
     }
 
     @Override
     public ITmfStateInterval querySingleState(long t, int attributeQuark)
-            throws AttributeNotFoundException, TimeRangeException,
-            StateSystemDisposedException {
+            throws TimeRangeException, StateSystemDisposedException {
         if (isDisposed) {
             throw new StateSystemDisposedException();
         }
 
+        LOGGER.info(() -> "[StateSystem:SingleQueryStart] ssid=" + this.getSSID() + ", ts=" + t + ", attribute=" + attributeQuark);  //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
+
         ITmfStateInterval ret = transState.getIntervalAt(t, attributeQuark);
         if (ret == null) {
             /*
@@ -614,9 +598,15 @@ public class StateSystem implements ITmfStateSystemBuilder {
              */
             throw new IllegalStateException("Incoherent interval storage"); //$NON-NLS-1$
         }
+        LOGGER.info(() -> "[StateSystem:SingleQueryEnd]");  //$NON-NLS-1$
         return ret;
     }
 
+    @Override
+    public void removeFiles() {
+        backend.removeFiles();
+    }
+
     //--------------------------------------------------------------------------
     //        Debug methods
     //--------------------------------------------------------------------------
@@ -626,17 +616,4 @@ public class StateSystem implements ITmfStateSystemBuilder {
                 attribute + " at time " + timestamp + //$NON-NLS-1$
                 ", returning dummy interval"); //$NON-NLS-1$
     }
-
-    /**
-     * Print out the contents of the inner structures.
-     *
-     * @param writer
-     *            The PrintWriter in which to print the output
-     */
-    public void debugPrint(@NonNull PrintWriter writer) {
-        getAttributeTree().debugPrint(writer);
-        transState.debugPrint(writer);
-        backend.debugPrint(writer);
-    }
-
 }
This page took 0.039391 seconds and 5 git commands to generate.