Merge branch 'master' into lttng_2_0_control_dev
[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$
67
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 {
210 assert (subPath != null && subPath.length > 0);
211 assert (startingNodeQuark >= -1);
212
213 Attribute prevNode;
214
215 /* Get the "starting node" */
216 if (startingNodeQuark == -1) {
217 prevNode = attributeTreeRoot;
218 } else {
219 prevNode = attributeList.get(startingNodeQuark);
220 }
221
222 int knownQuark = prevNode.getSubAttributeQuark(subPath);
223 if (knownQuark == -1) {
224 /*
225 * The attribute doesn't exist, but we have been specified to NOT
226 * add any new attributes.
227 */
228 throw new AttributeNotFoundException();
229 }
230 /*
231 * The attribute was already existing, return the quark of that
232 * attribute
233 */
234 return knownQuark;
235 }
236
237 // FIXME synchronized here is probably quite costly... maybe only locking
238 // the "for" would be enough?
239 synchronized int getQuarkAndAdd(int startingNodeQuark, String... subPath) {
240 assert (subPath != null && subPath.length > 0);
241 assert (startingNodeQuark >= -1);
242
243 Attribute nextNode = null;
244 Attribute prevNode;
245
246 /* Get the "starting node" */
247 if (startingNodeQuark == -1) {
248 prevNode = attributeTreeRoot;
249 } else {
250 prevNode = attributeList.get(startingNodeQuark);
251 }
252
253 int knownQuark = prevNode.getSubAttributeQuark(subPath);
254 if (knownQuark == -1) {
255 /*
256 * The attribute was not in the table previously, and we want to add
257 * it
258 */
259 for (String curDirectory : subPath) {
260 nextNode = prevNode.getSubAttributeNode(curDirectory);
261 if (nextNode == null) {
262 /* This is where we need to start adding */
263 nextNode = new AlphaNumAttribute(prevNode, curDirectory,
264 attributeList.size());
265 prevNode.addSubAttribute(nextNode);
266 attributeList.add(nextNode);
267 ss.transState.addEmptyEntry();
268 }
269 prevNode = nextNode;
270 }
271 return attributeList.size() - 1;
272 }
273 /*
274 * The attribute was already existing, return the quark of that
275 * attribute
276 */
277 return knownQuark;
278 }
279
280 int getSubAttributesCount(int quark) {
281 return attributeList.get(quark).getSubAttributesList().size();
282 }
283
284 ArrayList<Integer> getSubAttributes(int attributeQuark) {
285 ArrayList<Integer> listOfChildren = new ArrayList<Integer>();
286
287 for (Attribute childNode : attributeList.get(attributeQuark).getSubAttributesList()) {
288 listOfChildren.add(childNode.getQuark());
289 }
290 return listOfChildren;
291 }
292
293 String getFullAttributeName(int quark) {
294 if (quark >= attributeList.size() || quark < 0) {
295 return null;
296 }
297 return attributeList.get(quark).getFullAttributeName();
298 }
299
300 void debugPrint(PrintWriter writer) {
301 attributeTreeRoot.debugPrint(writer);
302 }
303
304}
This page took 0.03648 seconds and 5 git commands to generate.