1 /*******************************************************************************
2 * Copyright (c) 2012, 2016 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
12 * Alexandre Montplaisir - Initial API and implementation
13 * Patrick Tasse - Add message to exceptions
14 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.internal
.statesystem
.core
;
18 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
19 import static org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
.INVALID_ATTRIBUTE
;
20 import static org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
.ROOT_ATTRIBUTE
;
22 import java
.io
.BufferedInputStream
;
24 import java
.io
.FileInputStream
;
25 import java
.io
.FileOutputStream
;
26 import java
.io
.IOException
;
27 import java
.io
.ObjectInputStream
;
28 import java
.io
.ObjectOutputStream
;
29 import java
.io
.PrintWriter
;
30 import java
.nio
.channels
.FileChannel
;
31 import java
.util
.ArrayList
;
32 import java
.util
.List
;
34 import org
.eclipse
.jdt
.annotation
.NonNull
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
38 * The Attribute Tree is the /proc-like filesystem used to organize attributes.
39 * Each node of this tree is both like a file and a directory in the
45 public final class AttributeTree
{
47 /* "Magic number" for attribute tree files or file sections */
48 private static final int ATTRIB_TREE_MAGIC_NUMBER
= 0x06EC3671;
50 private final StateSystem ss
;
51 private final List
<Attribute
> attributeList
;
52 private final Attribute attributeTreeRoot
;
55 * Standard constructor, create a new empty Attribute Tree
58 * The StateSystem to which this AT is attached
60 public AttributeTree(StateSystem ss
) {
62 this.attributeList
= new ArrayList
<>();
63 this.attributeTreeRoot
= new Attribute(null, "root", ROOT_ATTRIBUTE
); //$NON-NLS-1$
67 * "Existing file" constructor. Builds an attribute tree from a
68 * "mapping file" or mapping section previously saved somewhere.
71 * StateSystem to which this AT is attached
73 * File stream where to read the AT information. Make sure it's
74 * sought at the right place!
76 * If there is a problem reading from the file stream
78 public AttributeTree(StateSystem ss
, FileInputStream fis
) throws IOException
{
80 ObjectInputStream ois
= new ObjectInputStream(new BufferedInputStream(fis
));
82 /* Read the header of the Attribute Tree file (or file section) */
83 int res
= ois
.readInt(); /* Magic number */
84 if (res
!= ATTRIB_TREE_MAGIC_NUMBER
) {
85 throw new IOException("The attribute tree file section is either invalid or corrupted."); //$NON-NLS-1$
89 ArrayList
<String
[]> attribList
;
91 @SuppressWarnings("unchecked")
92 ArrayList
<String
[]> list
= (ArrayList
<String
[]>) ois
.readObject();
94 } catch (ClassNotFoundException e
) {
95 throw new IOException("Unrecognizable attribute list"); //$NON-NLS-1$
99 * Now we have 'list', the ArrayList of String arrays representing all
100 * the attributes. Simply create attributes the normal way from them.
102 for (String
[] attrib
: attribList
) {
103 this.getQuarkAndAdd(ROOT_ATTRIBUTE
, attrib
);
108 * Tell the Attribute Tree to write itself somewhere in a file.
111 * The file to write to
113 * The position (in bytes) in the file where to write
115 public synchronized void writeSelf(File file
, long pos
) {
116 try (FileOutputStream fos
= new FileOutputStream(file
, true);
117 FileChannel fc
= fos
.getChannel();) {
119 try (ObjectOutputStream oos
= new ObjectOutputStream(fos
)) {
121 /* Write the almost-magic number */
122 oos
.writeInt(ATTRIB_TREE_MAGIC_NUMBER
);
124 /* Compute the serialized list of attributes and write it */
125 List
<String
[]> list
= new ArrayList
<>(attributeList
.size());
126 for (Attribute entry
: this.attributeList
) {
127 list
.add(entry
.getFullAttribute());
129 oos
.writeObject(list
);
131 } catch (IOException e
) {
138 * Return the number of attributes this system as seen so far. Note that
139 * this also equals the integer value (quark) the next added attribute will
142 * @return The current number of attributes in the tree
144 public synchronized int getNbAttributes() {
145 return attributeList
.size();
149 * Get the quark for a given attribute path. No new attribute will be
150 * created : if the specified path does not exist, return
151 * {@link ITmfStateSystem#INVALID_ATTRIBUTE}.
153 * @param startingNodeQuark
154 * The quark of the attribute from which relative queries will
155 * start. Use {@link ITmfStateSystem#ROOT_ATTRIBUTE} to start at
158 * The path to the attribute, relative to the starting node.
159 * @return The quark of the specified attribute, or
160 * {@link ITmfStateSystem#INVALID_ATTRIBUTE} if that attribute does
162 * @throws IndexOutOfBoundsException
163 * If the starting node quark is out of range
165 public synchronized int getQuarkDontAdd(int startingNodeQuark
, String
... subPath
) {
168 /* If subPath is empty, simply return the starting quark */
169 if (subPath
== null || subPath
.length
== 0) {
170 return startingNodeQuark
;
173 /* Get the "starting node" */
174 if (startingNodeQuark
== ROOT_ATTRIBUTE
) {
175 prevNode
= attributeTreeRoot
;
177 prevNode
= attributeList
.get(startingNodeQuark
);
180 return prevNode
.getSubAttributeQuark(subPath
);
184 * Get the quark of a given attribute path. If that specified path does not
185 * exist, it will be created (and the quark that was just created will be
188 * @param startingNodeQuark
189 * The quark of the attribute from which relative queries will
190 * start. Use {@link ITmfStateSystem#ROOT_ATTRIBUTE} to start at
193 * The path to the attribute, relative to the starting node.
194 * @return The quark of the attribute represented by the path
195 * @throws IndexOutOfBoundsException
196 * If the starting node quark is out of range
198 public synchronized int getQuarkAndAdd(int startingNodeQuark
, String
... subPath
) {
199 // FIXME synchronized here is probably quite costly... maybe only locking
200 // the "for" would be enough?
202 Attribute nextNode
= null;
205 /* Get the "starting node" */
206 if (startingNodeQuark
== ROOT_ATTRIBUTE
) {
207 prevNode
= attributeTreeRoot
;
209 prevNode
= attributeList
.get(startingNodeQuark
);
212 int knownQuark
= prevNode
.getSubAttributeQuark(subPath
);
213 if (knownQuark
== INVALID_ATTRIBUTE
) {
215 * The attribute was not in the table previously, and we want to add
218 for (String curDirectory
: subPath
) {
219 nextNode
= prevNode
.getSubAttributeNode(curDirectory
);
220 if (nextNode
== null) {
221 /* This is where we need to start adding */
222 nextNode
= new Attribute(prevNode
, checkNotNull(curDirectory
), attributeList
.size());
223 prevNode
.addSubAttribute(nextNode
);
224 attributeList
.add(nextNode
);
225 ss
.addEmptyAttribute();
229 return attributeList
.size() - 1;
232 * The attribute was already existing, return the quark of that
239 * Returns the sub-attributes of the quark passed in parameter
241 * @param attributeQuark
242 * The quark of the attribute to print the sub-attributes of.
244 * Should the query be recursive or not? If false, only children
245 * one level deep will be returned. If true, all descendants will
246 * be returned (depth-first search)
247 * @return The list of quarks representing the children attributes
248 * @throws IndexOutOfBoundsException
249 * If the attribute quark is out of range
251 public synchronized @NonNull List
<@NonNull Integer
> getSubAttributes(int attributeQuark
, boolean recursive
) {
252 List
<@NonNull Integer
> listOfChildren
= new ArrayList
<>();
253 Attribute startingAttribute
;
255 /* Set up the node from which we'll start the search */
256 if (attributeQuark
== ROOT_ATTRIBUTE
) {
257 startingAttribute
= attributeTreeRoot
;
259 startingAttribute
= attributeList
.get(attributeQuark
);
262 /* Iterate through the sub-attributes and add them to the list */
263 addSubAttributes(listOfChildren
, startingAttribute
, recursive
);
265 return listOfChildren
;
269 * Returns the parent quark of the attribute. The root attribute has no
270 * parent and will return {@link ITmfStateSystem#ROOT_ATTRIBUTE}.
273 * The quark of the attribute
274 * @return Quark of the parent attribute or
275 * {@link ITmfStateSystem#ROOT_ATTRIBUTE} for the root attribute
276 * @throws IndexOutOfBoundsException
277 * If the quark is out of range
279 public synchronized int getParentAttributeQuark(int quark
) {
280 if (quark
== ROOT_ATTRIBUTE
) {
283 return attributeList
.get(quark
).getParentAttributeQuark();
286 private void addSubAttributes(List
<Integer
> list
, Attribute curAttribute
,
288 for (Attribute childNode
: curAttribute
.getSubAttributes()) {
289 list
.add(childNode
.getQuark());
291 addSubAttributes(list
, childNode
, true);
297 * Get then base name of an attribute specified by a quark.
300 * The quark of the attribute
301 * @return The (base) name of the attribute
302 * @throws IndexOutOfBoundsException
303 * If the quark is out of range
305 public synchronized @NonNull String
getAttributeName(int quark
) {
306 return attributeList
.get(quark
).getName();
310 * Get the full path name of an attribute specified by a quark.
313 * The quark of the attribute
314 * @return The full path name of the attribute
315 * @throws IndexOutOfBoundsException
316 * If the quark is out of range
318 public synchronized @NonNull String
getFullAttributeName(int quark
) {
319 return attributeList
.get(quark
).getFullAttributeName();
323 * Get the full path name (as an array of path elements) of an attribute
324 * specified by a quark.
327 * The quark of the attribute
328 * @return The path elements of the full path
329 * @throws IndexOutOfBoundsException
330 * If the quark is out of range
332 public synchronized String
@NonNull [] getFullAttributePathArray(int quark
) {
333 return attributeList
.get(quark
).getFullAttribute();
337 * Debug-print all the attributes in the tree.
340 * The writer where to print the output
342 public synchronized void debugPrint(PrintWriter writer
) {
343 attributeTreeRoot
.debugPrint(writer
);