Add unit tests for the new getQuarks() method
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statesystem / AttributeTree.java
CommitLineData
a52fde77
AM
1/*******************************************************************************
2 * Copyright (c) 2012 Ericsson
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
5 *
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
10 *
11 *******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.statesystem;
14
15import java.io.*;
16import java.util.ArrayList;
17import java.util.Arrays;
18import java.util.Collections;
19import java.util.List;
20
21/**
22 * The Attribute Tree is the /proc-like filesystem used to organize attributes.
23 * Each node of this tree is both like a file and a directory in the
24 * "file system".
25 *
26 * @author alexmont
27 *
28 */
29final class AttributeTree {
30
31 /* "Magic number" for attribute tree files or file sections */
32 private final static int ATTRIB_TREE_MAGIC_NUMBER = 0x06EC3671;
33
34 private final StateSystem ss;
35 private final List<Attribute> attributeList;
36 private final Attribute attributeTreeRoot;
37
38 /**
39 * Standard constructor, create a new empty Attribute Tree
40 *
41 * @param ss
42 * The StateSystem to which this AT is attached
43 */
44 AttributeTree(StateSystem ss) {
45 this.ss = ss;
46 this.attributeList = Collections.synchronizedList(new ArrayList<Attribute>());
47 this.attributeTreeRoot = new AlphaNumAttribute(null, "root", -1); //$NON-NLS-1$
48 }
49
50 /**
51 * "Existing file" constructor Builds a attribute tree from a "mapping file"
52 * or mapping section previously saved somewhere.
53 *
54 * @param ss
55 * StateSystem to which this AT is attached
56 * @param fis
57 * File stream where to read the AT information. Make sure it's
58 * seeked at the right place!
59 * @throws IOException
60 */
61 AttributeTree(StateSystem ss, FileInputStream fis) throws IOException {
62 this(ss);
63 DataInputStream in = new DataInputStream(new BufferedInputStream(fis));
64
65 /* Message for exceptions, shouldn't be externalized */
66 final String errorMessage = "The attribute tree file section is either invalid or corrupted."; //$NON-NLS-1$
0a9de3d2 67
a52fde77
AM
68 ArrayList<String[]> list = new ArrayList<String[]>();
69 byte[] curByteArray;
70 String curFullString;
71 String[] curStringArray;
72 int res, remain, size;
73 int expectedSize = 0;
74 int total = 0;
75
76 /* Read the header of the Attribute Tree file (or file section) */
77 res = in.readInt(); /* Magic number */
78 if (res != ATTRIB_TREE_MAGIC_NUMBER) {
79 throw new IOException(errorMessage);
80 }
81
82 /* Expected size of the section */
83 expectedSize = in.readInt();
84 if (expectedSize <= 12) {
85 throw new IOException(errorMessage);
86 }
87
88 /* How many entries we have to read */
89 remain = in.readInt();
90 total += 12;
91
92 /* Read each entry */
93 for (; remain > 0; remain--) {
94 /* Read the first byte = the size of the entry */
95 size = in.readByte();
96 curByteArray = new byte[size];
97 in.read(curByteArray);
98
99 /*
100 * Go buffer -> byteArray -> String -> String[] -> insert in list.
101 * bleh
102 */
103 curFullString = new String(curByteArray);
104 curStringArray = curFullString.split("/"); //$NON-NLS-1$
105 list.add(curStringArray);
106
107 /* Read the 0'ed confirmation byte */
108 res = in.readByte();
109 if (res != 0) {
110 throw new IOException(errorMessage);
111 }
112 total += curByteArray.length + 2;
113 }
114
115 if (total != expectedSize) {
116 throw new IOException(errorMessage);
117 }
118
119 /*
120 * Now we have 'list', the ArrayList of String arrays representing all
121 * the attributes. Simply create attributes the normal way from them.
122 */
123 for (String[] attrib : list) {
124 this.getQuarkAndAdd(-1, attrib);
125 }
126 }
127
128 /**
129 * Tell the Attribute Tree to write itself somewhere. The passed
130 * FileOutputStream defines where (which file/position).
131 *
132 * @param fos
133 * Where to write. Make sure it's seeked at the right position
134 * you want.
135 * @return The total number of bytes written.
136 */
137 int writeSelf(File file, long pos) {
138 RandomAccessFile raf;
139 int total = 0;
140 byte[] curByteArray;
141
142 try {
143 raf = new RandomAccessFile(file, "rw"); //$NON-NLS-1$
144 raf.seek(pos);
145
146 /* Write the almost-magic number */
147 raf.writeInt(ATTRIB_TREE_MAGIC_NUMBER);
148
149 /* Placeholder for the total size of the section... */
150 raf.writeInt(-8000);
151
152 /* Write the number of entries */
153 raf.writeInt(this.attributeList.size());
154 total += 12;
155
156 /* Write the attributes themselves */
157 for (Attribute entry : this.attributeList) {
158 curByteArray = entry.getFullAttributeName().getBytes();
159 if (curByteArray.length > Byte.MAX_VALUE) {
160 throw new IOException("Attribute with name \"" //$NON-NLS-1$
161 + Arrays.toString(curByteArray) + "\" is too long."); //$NON-NLS-1$
162 }
163 /* Write the first byte = size of the array */
164 raf.writeByte((byte) curByteArray.length);
165
166 /* Write the array itself */
167 raf.write(curByteArray);
168
169 /* Write the 0'ed byte */
170 raf.writeByte((byte) 0);
171
172 total += curByteArray.length + 2;
173 }
174
175 /* Now go back and write the actual size of this section */
176 raf.seek(pos + 4);
177 raf.writeInt(total);
178
179 raf.close();
180 } catch (IOException e) {
181 e.printStackTrace();
182 }
183 return total;
184 }
185
186 /**
187 * Return the number of attributes this system as seen so far. Note that
188 * this also equals the integer value (quark) the next added attribute will
189 * have.
190 *
191 * @return
192 */
193 int getNbAttributes() {
194 return attributeList.size();
195 }
196
197 /**
198 * This is the version to specifically add missing attributes.
199 *
200 * If 'numericalNode' is true, all the new attributes created will be of
201 * type 'NumericalNode' instead of 'AlphaNumNode'. Be careful with this, if
202 * you do not want ALL added attributes to be numerical, call this function
203 * first with 'false' to create the parent nodes, then call it again to make
204 * sure only the final node is numerical.
205 *
206 * @throws AttributeNotFoundException
207 */
208 int getQuarkDontAdd(int startingNodeQuark, String... subPath)
209 throws AttributeNotFoundException {
a52fde77
AM
210 assert (startingNodeQuark >= -1);
211
212 Attribute prevNode;
213
6abc2d88
AM
214 /* If subPath is empty, simply return the starting quark */
215 if (subPath == null || subPath.length == 0) {
216 return startingNodeQuark;
217 }
218
a52fde77
AM
219 /* Get the "starting node" */
220 if (startingNodeQuark == -1) {
221 prevNode = attributeTreeRoot;
222 } else {
223 prevNode = attributeList.get(startingNodeQuark);
224 }
225
226 int knownQuark = prevNode.getSubAttributeQuark(subPath);
227 if (knownQuark == -1) {
228 /*
229 * The attribute doesn't exist, but we have been specified to NOT
230 * add any new attributes.
231 */
232 throw new AttributeNotFoundException();
233 }
234 /*
235 * The attribute was already existing, return the quark of that
236 * attribute
237 */
238 return knownQuark;
239 }
240
241 // FIXME synchronized here is probably quite costly... maybe only locking
242 // the "for" would be enough?
243 synchronized int getQuarkAndAdd(int startingNodeQuark, String... subPath) {
244 assert (subPath != null && subPath.length > 0);
245 assert (startingNodeQuark >= -1);
246
247 Attribute nextNode = null;
248 Attribute prevNode;
249
250 /* Get the "starting node" */
251 if (startingNodeQuark == -1) {
252 prevNode = attributeTreeRoot;
253 } else {
254 prevNode = attributeList.get(startingNodeQuark);
255 }
256
257 int knownQuark = prevNode.getSubAttributeQuark(subPath);
258 if (knownQuark == -1) {
259 /*
260 * The attribute was not in the table previously, and we want to add
261 * it
262 */
263 for (String curDirectory : subPath) {
264 nextNode = prevNode.getSubAttributeNode(curDirectory);
265 if (nextNode == null) {
266 /* This is where we need to start adding */
267 nextNode = new AlphaNumAttribute(prevNode, curDirectory,
268 attributeList.size());
269 prevNode.addSubAttribute(nextNode);
270 attributeList.add(nextNode);
271 ss.transState.addEmptyEntry();
272 }
273 prevNode = nextNode;
274 }
275 return attributeList.size() - 1;
276 }
277 /*
278 * The attribute was already existing, return the quark of that
279 * attribute
280 */
281 return knownQuark;
282 }
283
284 int getSubAttributesCount(int quark) {
285 return attributeList.get(quark).getSubAttributesList().size();
286 }
287
0a9de3d2
AM
288 /**
289 * Returns the sub-attributes of the quark passed in parameter
290 *
291 * @param attributeQuark
c66426fd 292 * @param recursive
0a9de3d2
AM
293 * @return
294 * @throws AttributeNotFoundException
295 */
c66426fd 296 List<Integer> getSubAttributes(int attributeQuark, boolean recursive)
0a9de3d2
AM
297 throws AttributeNotFoundException {
298 List<Integer> listOfChildren = new ArrayList<Integer>();
299 Attribute startingAttribute;
c66426fd 300
0a9de3d2 301 /* Check if the quark is valid */
f94a0bac 302 if (attributeQuark < -1 || attributeQuark >= attributeList.size()) {
0a9de3d2
AM
303 throw new AttributeNotFoundException();
304 }
c66426fd 305
0a9de3d2 306 /* Set up the node from which we'll start the search */
c66426fd 307 if (attributeQuark == -1) {
0a9de3d2
AM
308 startingAttribute = attributeTreeRoot;
309 } else {
310 startingAttribute = attributeList.get(attributeQuark);
311 }
c66426fd 312
0a9de3d2 313 /* Iterate through the sub-attributes and add them to the list */
c66426fd 314 addSubAttributes(listOfChildren, startingAttribute, recursive);
0a9de3d2 315
a52fde77
AM
316 return listOfChildren;
317 }
318
c66426fd
AM
319 private void addSubAttributes(List<Integer> list, Attribute curAttribute,
320 boolean recursive) {
321 for (Attribute childNode : curAttribute.getSubAttributesList()) {
322 list.add(childNode.getQuark());
323 if (recursive) {
324 addSubAttributes(list, childNode, true);
325 }
326 }
327 }
328
a52fde77
AM
329 String getFullAttributeName(int quark) {
330 if (quark >= attributeList.size() || quark < 0) {
331 return null;
332 }
333 return attributeList.get(quark).getFullAttributeName();
334 }
335
336 void debugPrint(PrintWriter writer) {
337 attributeTreeRoot.debugPrint(writer);
338 }
339
340}
This page took 0.037028 seconds and 5 git commands to generate.