1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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
;
20 import java
.io
.BufferedInputStream
;
22 import java
.io
.FileInputStream
;
23 import java
.io
.FileOutputStream
;
24 import java
.io
.IOException
;
25 import java
.io
.ObjectInputStream
;
26 import java
.io
.ObjectOutputStream
;
27 import java
.io
.PrintWriter
;
28 import java
.nio
.channels
.FileChannel
;
29 import java
.util
.ArrayList
;
30 import java
.util
.Arrays
;
31 import java
.util
.List
;
33 import org
.eclipse
.jdt
.annotation
.NonNull
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
37 * The Attribute Tree is the /proc-like filesystem used to organize attributes.
38 * Each node of this tree is both like a file and a directory in the
44 public final class AttributeTree
{
46 /* "Magic number" for attribute tree files or file sections */
47 private static final int ATTRIB_TREE_MAGIC_NUMBER
= 0x06EC3671;
49 private final StateSystem ss
;
50 private final List
<Attribute
> attributeList
;
51 private final Attribute attributeTreeRoot
;
54 * Standard constructor, create a new empty Attribute Tree
57 * The StateSystem to which this AT is attached
59 public AttributeTree(StateSystem ss
) {
61 this.attributeList
= new ArrayList
<>();
62 this.attributeTreeRoot
= new Attribute(null, "root", -1); //$NON-NLS-1$
66 * "Existing file" constructor. Builds an attribute tree from a
67 * "mapping file" or mapping section previously saved somewhere.
70 * StateSystem to which this AT is attached
72 * File stream where to read the AT information. Make sure it's
73 * sought at the right place!
75 * If there is a problem reading from the file stream
77 public AttributeTree(StateSystem ss
, FileInputStream fis
) throws IOException
{
79 ObjectInputStream ois
= new ObjectInputStream(new BufferedInputStream(fis
));
81 /* Read the header of the Attribute Tree file (or file section) */
82 int res
= ois
.readInt(); /* Magic number */
83 if (res
!= ATTRIB_TREE_MAGIC_NUMBER
) {
84 throw new IOException("The attribute tree file section is either invalid or corrupted."); //$NON-NLS-1$
88 ArrayList
<String
[]> attribList
;
90 @SuppressWarnings("unchecked")
91 ArrayList
<String
[]> list
= (ArrayList
<String
[]>) ois
.readObject();
93 } catch (ClassNotFoundException e
) {
94 throw new IOException("Unrecognizable attribute list"); //$NON-NLS-1$
98 * Now we have 'list', the ArrayList of String arrays representing all
99 * the attributes. Simply create attributes the normal way from them.
101 for (String
[] attrib
: attribList
) {
102 this.getQuarkAndAdd(-1, attrib
);
107 * Tell the Attribute Tree to write itself somewhere in a file.
110 * The file to write to
112 * The position (in bytes) in the file where to write
114 public synchronized void writeSelf(File file
, long pos
) {
115 try (FileOutputStream fos
= new FileOutputStream(file
, true);
116 FileChannel fc
= fos
.getChannel();) {
118 try (ObjectOutputStream oos
= new ObjectOutputStream(fos
)) {
120 /* Write the almost-magic number */
121 oos
.writeInt(ATTRIB_TREE_MAGIC_NUMBER
);
123 /* Compute the serialized list of attributes and write it */
124 List
<String
[]> list
= new ArrayList
<>(attributeList
.size());
125 for (Attribute entry
: this.attributeList
) {
126 list
.add(entry
.getFullAttribute());
128 oos
.writeObject(list
);
130 } catch (IOException e
) {
137 * Return the number of attributes this system as seen so far. Note that
138 * this also equals the integer value (quark) the next added attribute will
141 * @return The current number of attributes in the tree
143 public synchronized int getNbAttributes() {
144 return attributeList
.size();
148 * Get the quark for a given attribute path. No new attribute will be
149 * created : if the specified path does not exist, throw an error.
151 * @param startingNodeQuark
152 * The quark of the attribute from which relative queries will
153 * start. Use '-1' to start at the root node.
155 * The path to the attribute, relative to the starting node.
156 * @return The quark of the specified attribute
157 * @throws AttributeNotFoundException
158 * If the specified path was not found
160 public synchronized int getQuarkDontAdd(int startingNodeQuark
, String
... subPath
)
161 throws AttributeNotFoundException
{
162 assert (startingNodeQuark
>= -1);
166 /* If subPath is empty, simply return the starting quark */
167 if (subPath
== null || subPath
.length
== 0) {
168 return startingNodeQuark
;
171 /* Get the "starting node" */
172 if (startingNodeQuark
== -1) {
173 prevNode
= attributeTreeRoot
;
175 prevNode
= attributeList
.get(startingNodeQuark
);
178 int knownQuark
= prevNode
.getSubAttributeQuark(subPath
);
179 if (knownQuark
== -1) {
181 * The attribute doesn't exist, but we have been specified to NOT
182 * add any new attributes.
184 throw new AttributeNotFoundException(ss
.getSSID() + " Quark:" + startingNodeQuark
+ ", SubPath:" + Arrays
.toString(subPath
)); //$NON-NLS-1$ //$NON-NLS-2$
187 * The attribute was already existing, return the quark of that
194 * Get the quark of a given attribute path. If that specified path does not
195 * exist, it will be created (and the quark that was just created will be
198 * @param startingNodeQuark
199 * The quark of the attribute from which relative queries will
200 * start. Use '-1' to start at the root node.
202 * The path to the attribute, relative to the starting node.
203 * @return The quark of the attribute represented by the path
205 public synchronized int getQuarkAndAdd(int startingNodeQuark
, String
... subPath
) {
206 // FIXME synchronized here is probably quite costly... maybe only locking
207 // the "for" would be enough?
208 assert (subPath
!= null && subPath
.length
> 0);
209 assert (startingNodeQuark
>= -1);
211 Attribute nextNode
= null;
214 /* Get the "starting node" */
215 if (startingNodeQuark
== -1) {
216 prevNode
= attributeTreeRoot
;
218 prevNode
= attributeList
.get(startingNodeQuark
);
221 int knownQuark
= prevNode
.getSubAttributeQuark(subPath
);
222 if (knownQuark
== -1) {
224 * The attribute was not in the table previously, and we want to add
227 for (String curDirectory
: subPath
) {
228 nextNode
= prevNode
.getSubAttributeNode(curDirectory
);
229 if (nextNode
== null) {
230 /* This is where we need to start adding */
231 nextNode
= new Attribute(prevNode
, checkNotNull(curDirectory
), attributeList
.size());
232 prevNode
.addSubAttribute(nextNode
);
233 attributeList
.add(nextNode
);
234 ss
.addEmptyAttribute();
238 return attributeList
.size() - 1;
241 * The attribute was already existing, return the quark of that
248 * Returns the sub-attributes of the quark passed in parameter
250 * @param attributeQuark
251 * The quark of the attribute to print the sub-attributes of.
253 * Should the query be recursive or not? If false, only children
254 * one level deep will be returned. If true, all descendants will
255 * be returned (depth-first search)
256 * @return The list of quarks representing the children attributes
257 * @throws AttributeNotFoundException
258 * If 'attributeQuark' is invalid, or if there is no attrbiute
261 public synchronized @NonNull List
<Integer
> getSubAttributes(int attributeQuark
, boolean recursive
)
262 throws AttributeNotFoundException
{
263 List
<Integer
> listOfChildren
= new ArrayList
<>();
264 Attribute startingAttribute
;
266 /* Check if the quark is valid */
267 if (attributeQuark
< -1 || attributeQuark
>= attributeList
.size()) {
268 throw new AttributeNotFoundException(ss
.getSSID() + " Quark:" + attributeQuark
); //$NON-NLS-1$
271 /* Set up the node from which we'll start the search */
272 if (attributeQuark
== -1) {
273 startingAttribute
= attributeTreeRoot
;
275 startingAttribute
= attributeList
.get(attributeQuark
);
278 /* Iterate through the sub-attributes and add them to the list */
279 addSubAttributes(listOfChildren
, startingAttribute
, recursive
);
281 return listOfChildren
;
285 * Returns the parent quark of the attribute. The root attribute has no
286 * parent and will return <code>-1</code>
289 * The quark of the attribute
290 * @return Quark of the parent attribute or <code>-1</code> for the root
293 public synchronized int getParentAttributeQuark(int quark
) {
297 return attributeList
.get(quark
).getParentAttributeQuark();
300 private void addSubAttributes(List
<Integer
> list
, Attribute curAttribute
,
302 for (Attribute childNode
: curAttribute
.getSubAttributes()) {
303 list
.add(childNode
.getQuark());
305 addSubAttributes(list
, childNode
, true);
311 * Get then base name of an attribute specified by a quark.
314 * The quark of the attribute
315 * @return The (base) name of the attribute
317 public synchronized @NonNull String
getAttributeName(int quark
) {
318 return attributeList
.get(quark
).getName();
322 * Get the full path name of an attribute specified by a quark.
325 * The quark of the attribute
326 * @return The full path name of the attribute
328 public synchronized @NonNull String
getFullAttributeName(int quark
) {
329 return attributeList
.get(quark
).getFullAttributeName();
333 * Get the full path name (as an array of path elements) of an attribute
334 * specified by a quark.
337 * The quark of the attribute
338 * @return The path elements of the full path
340 public synchronized String
@NonNull [] getFullAttributePathArray(int quark
) {
341 return attributeList
.get(quark
).getFullAttribute();
345 * Debug-print all the attributes in the tree.
348 * The writer where to print the output
350 public synchronized void debugPrint(PrintWriter writer
) {
351 attributeTreeRoot
.debugPrint(writer
);