1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Arrays
;
18 import java
.util
.Collections
;
19 import java
.util
.List
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.TmfStateValue
;
27 * The Attribute Tree is the /proc-like filesystem used to organize attributes.
28 * Each node of this tree is both like a file and a directory in the
34 public final class AttributeTree
{
36 /* "Magic number" for attribute tree files or file sections */
37 private static final int ATTRIB_TREE_MAGIC_NUMBER
= 0x06EC3671;
39 private final StateSystem ss
;
40 private final List
<Attribute
> attributeList
;
41 private final Attribute attributeTreeRoot
;
44 * Standard constructor, create a new empty Attribute Tree
47 * The StateSystem to which this AT is attached
49 AttributeTree(StateSystem ss
) {
51 this.attributeList
= Collections
.synchronizedList(new ArrayList
<Attribute
>());
52 this.attributeTreeRoot
= new AlphaNumAttribute(null, "root", -1); //$NON-NLS-1$
56 * "Existing file" constructor. Builds an attribute tree from a
57 * "mapping file" or mapping section previously saved somewhere.
60 * StateSystem to which this AT is attached
62 * File stream where to read the AT information. Make sure it's
63 * sought at the right place!
66 AttributeTree(StateSystem ss
, FileInputStream fis
) throws IOException
{
68 DataInputStream in
= new DataInputStream(new BufferedInputStream(fis
));
70 /* Message for exceptions, shouldn't be externalized */
71 final String errorMessage
= "The attribute tree file section is either invalid or corrupted."; //$NON-NLS-1$
73 ArrayList
<String
[]> list
= new ArrayList
<String
[]>();
76 String
[] curStringArray
;
77 int res
, remain
, size
;
81 /* Read the header of the Attribute Tree file (or file section) */
82 res
= in
.readInt(); /* Magic number */
83 if (res
!= ATTRIB_TREE_MAGIC_NUMBER
) {
84 throw new IOException(errorMessage
);
87 /* Expected size of the section */
88 expectedSize
= in
.readInt();
89 if (expectedSize
<= 12) {
90 throw new IOException(errorMessage
);
93 /* How many entries we have to read */
94 remain
= in
.readInt();
98 for (; remain
> 0; remain
--) {
99 /* Read the first byte = the size of the entry */
100 size
= in
.readByte();
101 curByteArray
= new byte[size
];
102 res
= in
.read(curByteArray
);
104 throw new IOException(errorMessage
);
108 * Go buffer -> byteArray -> String -> String[] -> insert in list.
111 curFullString
= new String(curByteArray
);
112 curStringArray
= curFullString
.split("/"); //$NON-NLS-1$
113 list
.add(curStringArray
);
115 /* Read the 0'ed confirmation byte */
118 throw new IOException(errorMessage
);
120 total
+= curByteArray
.length
+ 2;
123 if (total
!= expectedSize
) {
124 throw new IOException(errorMessage
);
128 * Now we have 'list', the ArrayList of String arrays representing all
129 * the attributes. Simply create attributes the normal way from them.
131 for (String
[] attrib
: list
) {
132 this.getQuarkAndAdd(-1, attrib
);
137 * Tell the Attribute Tree to write itself somewhere. The passed
138 * FileOutputStream defines where (which file/position).
141 * Where to write. Make sure it's sought at the right position
143 * @return The total number of bytes written.
145 int writeSelf(File file
, long pos
) {
146 RandomAccessFile raf
= null;
151 raf
= new RandomAccessFile(file
, "rw"); //$NON-NLS-1$
154 /* Write the almost-magic number */
155 raf
.writeInt(ATTRIB_TREE_MAGIC_NUMBER
);
157 /* Placeholder for the total size of the section... */
160 /* Write the number of entries */
161 raf
.writeInt(this.attributeList
.size());
164 /* Write the attributes themselves */
165 for (Attribute entry
: this.attributeList
) {
166 curByteArray
= entry
.getFullAttributeName().getBytes();
167 if (curByteArray
.length
> Byte
.MAX_VALUE
) {
168 throw new IOException("Attribute with name \"" //$NON-NLS-1$
169 + Arrays
.toString(curByteArray
) + "\" is too long."); //$NON-NLS-1$
171 /* Write the first byte = size of the array */
172 raf
.writeByte((byte) curByteArray
.length
);
174 /* Write the array itself */
175 raf
.write(curByteArray
);
177 /* Write the 0'ed byte */
178 raf
.writeByte((byte) 0);
180 total
+= curByteArray
.length
+ 2;
183 /* Now go back and write the actual size of this section */
187 } catch (IOException e
) {
193 } catch (IOException e
) {
202 * Return the number of attributes this system as seen so far. Note that
203 * this also equals the integer value (quark) the next added attribute will
208 int getNbAttributes() {
209 return attributeList
.size();
213 * This is the version to specifically add missing attributes.
215 * If 'numericalNode' is true, all the new attributes created will be of
216 * type 'NumericalNode' instead of 'AlphaNumNode'. Be careful with this, if
217 * you do not want ALL added attributes to be numerical, call this function
218 * first with 'false' to create the parent nodes, then call it again to make
219 * sure only the final node is numerical.
221 * @throws AttributeNotFoundException
223 int getQuarkDontAdd(int startingNodeQuark
, String
... subPath
)
224 throws AttributeNotFoundException
{
225 assert (startingNodeQuark
>= -1);
229 /* If subPath is empty, simply return the starting quark */
230 if (subPath
== null || subPath
.length
== 0) {
231 return startingNodeQuark
;
234 /* Get the "starting node" */
235 if (startingNodeQuark
== -1) {
236 prevNode
= attributeTreeRoot
;
238 prevNode
= attributeList
.get(startingNodeQuark
);
241 int knownQuark
= prevNode
.getSubAttributeQuark(subPath
);
242 if (knownQuark
== -1) {
244 * The attribute doesn't exist, but we have been specified to NOT
245 * add any new attributes.
247 throw new AttributeNotFoundException();
250 * The attribute was already existing, return the quark of that
256 // FIXME synchronized here is probably quite costly... maybe only locking
257 // the "for" would be enough?
258 synchronized int getQuarkAndAdd(int startingNodeQuark
, String
... subPath
) {
259 assert (subPath
!= null && subPath
.length
> 0);
260 assert (startingNodeQuark
>= -1);
262 Attribute nextNode
= null;
265 /* Get the "starting node" */
266 if (startingNodeQuark
== -1) {
267 prevNode
= attributeTreeRoot
;
269 prevNode
= attributeList
.get(startingNodeQuark
);
272 int knownQuark
= prevNode
.getSubAttributeQuark(subPath
);
273 if (knownQuark
== -1) {
275 * The attribute was not in the table previously, and we want to add
278 for (String curDirectory
: subPath
) {
279 nextNode
= prevNode
.getSubAttributeNode(curDirectory
);
280 if (nextNode
== null) {
281 /* This is where we need to start adding */
282 nextNode
= new AlphaNumAttribute(prevNode
, curDirectory
,
283 attributeList
.size());
284 prevNode
.addSubAttribute(nextNode
);
285 attributeList
.add(nextNode
);
286 ss
.addEmptyAttribute();
291 * Insert an initial null value for this attribute in the state
292 * system (in case the state provider doesn't set one).
294 final int newAttrib
= attributeList
.size() - 1;
296 ss
.modifyAttribute(ss
.getStartTime(), TmfStateValue
.nullValue(), newAttrib
);
297 } catch (TimeRangeException e
) {
298 /* Should not happen, we're inserting at ss's start time */
299 throw new IllegalStateException(e
);
300 } catch (AttributeNotFoundException e
) {
301 /* Should not happen, we just created this attribute! */
302 throw new IllegalStateException(e
);
303 } catch (StateValueTypeException e
) {
304 /* Should not happen, there is no existing state value, and the
305 * one we insert is a null value anyway. */
306 throw new IllegalStateException(e
);
312 * The attribute was already existing, return the quark of that
318 int getSubAttributesCount(int quark
) {
319 return attributeList
.get(quark
).getSubAttributes().size();
323 * Returns the sub-attributes of the quark passed in parameter
325 * @param attributeQuark
328 * @throws AttributeNotFoundException
330 List
<Integer
> getSubAttributes(int attributeQuark
, boolean recursive
)
331 throws AttributeNotFoundException
{
332 List
<Integer
> listOfChildren
= new ArrayList
<Integer
>();
333 Attribute startingAttribute
;
335 /* Check if the quark is valid */
336 if (attributeQuark
< -1 || attributeQuark
>= attributeList
.size()) {
337 throw new AttributeNotFoundException();
340 /* Set up the node from which we'll start the search */
341 if (attributeQuark
== -1) {
342 startingAttribute
= attributeTreeRoot
;
344 startingAttribute
= attributeList
.get(attributeQuark
);
347 /* Iterate through the sub-attributes and add them to the list */
348 addSubAttributes(listOfChildren
, startingAttribute
, recursive
);
350 return listOfChildren
;
353 private void addSubAttributes(List
<Integer
> list
, Attribute curAttribute
,
355 for (Attribute childNode
: curAttribute
.getSubAttributes()) {
356 list
.add(childNode
.getQuark());
358 addSubAttributes(list
, childNode
, true);
363 String
getAttributeName(int quark
) {
364 return attributeList
.get(quark
).getName();
367 String
getFullAttributeName(int quark
) {
368 if (quark
>= attributeList
.size() || quark
< 0) {
371 return attributeList
.get(quark
).getFullAttributeName();
374 void debugPrint(PrintWriter writer
) {
375 attributeTreeRoot
.debugPrint(writer
);