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
<>();
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
) {
149 try (RandomAccessFile raf
= new RandomAccessFile(file
, "rw");) { //$NON-NLS-1$
152 /* Write the almost-magic number */
153 raf
.writeInt(ATTRIB_TREE_MAGIC_NUMBER
);
155 /* Placeholder for the total size of the section... */
158 /* Write the number of entries */
159 raf
.writeInt(this.attributeList
.size());
162 /* Write the attributes themselves */
163 for (Attribute entry
: this.attributeList
) {
164 curByteArray
= entry
.getFullAttributeName().getBytes();
165 if (curByteArray
.length
> Byte
.MAX_VALUE
) {
166 throw new IOException("Attribute with name \"" //$NON-NLS-1$
167 + Arrays
.toString(curByteArray
) + "\" is too long."); //$NON-NLS-1$
169 /* Write the first byte = size of the array */
170 raf
.writeByte((byte) curByteArray
.length
);
172 /* Write the array itself */
173 raf
.write(curByteArray
);
175 /* Write the 0'ed byte */
176 raf
.writeByte((byte) 0);
178 total
+= curByteArray
.length
+ 2;
181 /* Now go back and write the actual size of this section */
185 } catch (IOException e
) {
192 * Return the number of attributes this system as seen so far. Note that
193 * this also equals the integer value (quark) the next added attribute will
198 int getNbAttributes() {
199 return attributeList
.size();
203 * This is the version to specifically add missing attributes.
205 * If 'numericalNode' is true, all the new attributes created will be of
206 * type 'NumericalNode' instead of 'AlphaNumNode'. Be careful with this, if
207 * you do not want ALL added attributes to be numerical, call this function
208 * first with 'false' to create the parent nodes, then call it again to make
209 * sure only the final node is numerical.
211 * @throws AttributeNotFoundException
213 int getQuarkDontAdd(int startingNodeQuark
, String
... subPath
)
214 throws AttributeNotFoundException
{
215 assert (startingNodeQuark
>= -1);
219 /* If subPath is empty, simply return the starting quark */
220 if (subPath
== null || subPath
.length
== 0) {
221 return startingNodeQuark
;
224 /* Get the "starting node" */
225 if (startingNodeQuark
== -1) {
226 prevNode
= attributeTreeRoot
;
228 prevNode
= attributeList
.get(startingNodeQuark
);
231 int knownQuark
= prevNode
.getSubAttributeQuark(subPath
);
232 if (knownQuark
== -1) {
234 * The attribute doesn't exist, but we have been specified to NOT
235 * add any new attributes.
237 throw new AttributeNotFoundException();
240 * The attribute was already existing, return the quark of that
246 // FIXME synchronized here is probably quite costly... maybe only locking
247 // the "for" would be enough?
248 synchronized int getQuarkAndAdd(int startingNodeQuark
, String
... subPath
) {
249 assert (subPath
!= null && subPath
.length
> 0);
250 assert (startingNodeQuark
>= -1);
252 Attribute nextNode
= null;
255 /* Get the "starting node" */
256 if (startingNodeQuark
== -1) {
257 prevNode
= attributeTreeRoot
;
259 prevNode
= attributeList
.get(startingNodeQuark
);
262 int knownQuark
= prevNode
.getSubAttributeQuark(subPath
);
263 if (knownQuark
== -1) {
265 * The attribute was not in the table previously, and we want to add
268 for (String curDirectory
: subPath
) {
269 nextNode
= prevNode
.getSubAttributeNode(curDirectory
);
270 if (nextNode
== null) {
271 /* This is where we need to start adding */
272 nextNode
= new AlphaNumAttribute(prevNode
, curDirectory
,
273 attributeList
.size());
274 prevNode
.addSubAttribute(nextNode
);
275 attributeList
.add(nextNode
);
276 ss
.addEmptyAttribute();
281 * Insert an initial null value for this attribute in the state
282 * system (in case the state provider doesn't set one).
284 final int newAttrib
= attributeList
.size() - 1;
286 ss
.modifyAttribute(ss
.getStartTime(), TmfStateValue
.nullValue(), newAttrib
);
287 } catch (TimeRangeException e
) {
288 /* Should not happen, we're inserting at ss's start time */
289 throw new IllegalStateException(e
);
290 } catch (AttributeNotFoundException e
) {
291 /* Should not happen, we just created this attribute! */
292 throw new IllegalStateException(e
);
293 } catch (StateValueTypeException e
) {
294 /* Should not happen, there is no existing state value, and the
295 * one we insert is a null value anyway. */
296 throw new IllegalStateException(e
);
302 * The attribute was already existing, return the quark of that
308 int getSubAttributesCount(int quark
) {
309 return attributeList
.get(quark
).getSubAttributes().size();
313 * Returns the sub-attributes of the quark passed in parameter
315 * @param attributeQuark
318 * @throws AttributeNotFoundException
320 List
<Integer
> getSubAttributes(int attributeQuark
, boolean recursive
)
321 throws AttributeNotFoundException
{
322 List
<Integer
> listOfChildren
= new ArrayList
<>();
323 Attribute startingAttribute
;
325 /* Check if the quark is valid */
326 if (attributeQuark
< -1 || attributeQuark
>= attributeList
.size()) {
327 throw new AttributeNotFoundException();
330 /* Set up the node from which we'll start the search */
331 if (attributeQuark
== -1) {
332 startingAttribute
= attributeTreeRoot
;
334 startingAttribute
= attributeList
.get(attributeQuark
);
337 /* Iterate through the sub-attributes and add them to the list */
338 addSubAttributes(listOfChildren
, startingAttribute
, recursive
);
340 return listOfChildren
;
343 private void addSubAttributes(List
<Integer
> list
, Attribute curAttribute
,
345 for (Attribute childNode
: curAttribute
.getSubAttributes()) {
346 list
.add(childNode
.getQuark());
348 addSubAttributes(list
, childNode
, true);
353 String
getAttributeName(int quark
) {
354 return attributeList
.get(quark
).getName();
357 String
getFullAttributeName(int quark
) {
358 if (quark
>= attributeList
.size() || quark
< 0) {
361 return attributeList
.get(quark
).getFullAttributeName();
364 void debugPrint(PrintWriter writer
) {
365 attributeTreeRoot
.debugPrint(writer
);