| author | Alexandre Montplaisir | 2012-03-20 16:25:37 (EDT) |
|---|---|---|
| committer | Francois Chouinard | 2012-03-27 13:52:22 (EDT) |
| commit | 3b64b4e3caac38ab55a5fd20a8daed92e3ca5c74 (patch) (side-by-side diff) | |
| tree | 73af72a5f01fd3f15c7ab0806acc1e6e06ba0bf0 | |
| parent | 7e5945e9f37805b4e29b8873a736564a3359e5ac (diff) | |
| download | org.eclipse.linuxtools-3b64b4e3caac38ab55a5fd20a8daed92e3ca5c74.zip org.eclipse.linuxtools-3b64b4e3caac38ab55a5fd20a8daed92e3ca5c74.tar.gz org.eclipse.linuxtools-3b64b4e3caac38ab55a5fd20a8daed92e3ca5c74.tar.bz2 | |
Add method to get attributes with a wildcard in the path
One can now use getQuarks("x", "*", "z"), and it will return a List
of all attribute quarks whose path matches the "x" and "z" parts.
Not all edge cases have been tested (it fails if the * is a the end),
but proper unit tests will be added shortly. It's also probably
very slow and not yet completely optimized.
Signed-off-by: Alexandre Montplaisir <alexandre.montplaisir@polymtl.ca>
2 files changed, 110 insertions, 1 deletions
diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/AttributeTree.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/AttributeTree.java index 9c5d8fc..6ed9428 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/AttributeTree.java +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/AttributeTree.java @@ -295,7 +295,7 @@ final class AttributeTree { Attribute startingAttribute; /* Check if the quark is valid */ - if (attributeQuark < 0 || attributeQuark >= attributeList.size()) { + if (attributeQuark < -1 || attributeQuark >= attributeList.size()) { throw new AttributeNotFoundException(); } diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/StateSystem.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/StateSystem.java index 30edbb3..90e6f5b 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/StateSystem.java +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/statesystem/StateSystem.java @@ -13,6 +13,7 @@ package org.eclipse.linuxtools.tmf.core.statesystem; import java.io.PrintWriter; +import java.util.LinkedList; import java.util.List; import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue; @@ -154,6 +155,114 @@ public class StateSystem { } /** + * Batch quark-retrieving method. This method allows you to specify a path + * pattern which includes a wildcard "*" somewhere. It will check all the + * existing attributes in the attribute tree and return those who match the + * pattern. + * + * For example, passing ("Threads", "*", "Exec_mode") will return the list + * of quarks for attributes "Threads/1000/Exec_mode", + * "Threads/1500/Exec_mode", and so on, depending on what exists at this + * time in the attribute tree. + * + * If no wildcard is specified, the behavior is the same as + * getQuarkAbsolute() (except it will return a List with one entry). This + * method will never create new attributes. + * + * Only one wildcard "*" is supported at this time. + * + * @param pattern + * The array of strings representing the pattern to look for. It + * should ideally contain one entry that is only a "*". + * @return A List of attribute quarks, representing attributes that matched + * the pattern. If no attribute matched, the list will be empty (but + * not null). + */ + public List<Integer> getQuarks(String... pattern) { + List<Integer> quarks = new LinkedList<Integer>(); + List<String> prefix = new LinkedList<String>(); + List<String> suffix = new LinkedList<String>(); + 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; + } + + if (split) { + suffix.add(entry); + } else { + prefix.add(entry); + } + } + prefixStr = prefix.toArray(new String[prefix.size()]); + suffixStr = suffix.toArray(new String[suffix.size()]); + + /* + * If there was no wildcard, we'll only return the one matching + * attribute, if there is one. + */ + if (split == false) { + 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; + } + + 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; + } else { + startingAttribute = getQuarkAbsolute(prefixStr); + } + directChildren = attributeTree.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; + } + quarks.add(matchingQuark); + } + + return quarks; + } + + /** * @name External methods related to insertions in the history - */ |

