common: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / AttributeTree.java
CommitLineData
a52fde77 1/*******************************************************************************
e13bd4cd 2 * Copyright (c) 2012, 2015 Ericsson
a52fde77
AM
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
8d1346f0 5 *
a52fde77
AM
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
8d1346f0 10 *
e13bd4cd
PT
11 * Contributors:
12 * Alexandre Montplaisir - Initial API and implementation
13 * Patrick Tasse - Add message to exceptions
a52fde77
AM
14 *******************************************************************************/
15
e894a508 16package org.eclipse.tracecompass.internal.statesystem.core;
a52fde77 17
04927a83
AM
18import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
19
085a78a8 20import java.io.BufferedInputStream;
085a78a8
AM
21import java.io.File;
22import java.io.FileInputStream;
da66cc75 23import java.io.FileOutputStream;
085a78a8 24import java.io.IOException;
da66cc75
AM
25import java.io.ObjectInputStream;
26import java.io.ObjectOutputStream;
085a78a8 27import java.io.PrintWriter;
da66cc75 28import java.nio.channels.FileChannel;
a52fde77
AM
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.Collections;
32import java.util.List;
33
50a47aa6 34import org.eclipse.jdt.annotation.NonNull;
e894a508 35import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
6d08acca 36
a52fde77
AM
37/**
38 * The Attribute Tree is the /proc-like filesystem used to organize attributes.
39 * Each node of this tree is both like a file and a directory in the
40 * "file system".
8d1346f0 41 *
a52fde77 42 * @author alexmont
8d1346f0 43 *
a52fde77 44 */
cb42195c 45public final class AttributeTree {
a52fde77
AM
46
47 /* "Magic number" for attribute tree files or file sections */
cb42195c 48 private static final int ATTRIB_TREE_MAGIC_NUMBER = 0x06EC3671;
a52fde77
AM
49
50 private final StateSystem ss;
51 private final List<Attribute> attributeList;
52 private final Attribute attributeTreeRoot;
53
54 /**
55 * Standard constructor, create a new empty Attribute Tree
8d1346f0 56 *
a52fde77
AM
57 * @param ss
58 * The StateSystem to which this AT is attached
59 */
a6917276 60 public AttributeTree(StateSystem ss) {
a52fde77
AM
61 this.ss = ss;
62 this.attributeList = Collections.synchronizedList(new ArrayList<Attribute>());
c3f21a07 63 this.attributeTreeRoot = new Attribute(null, "root", -1); //$NON-NLS-1$
a52fde77
AM
64 }
65
66 /**
6f04e06c
EB
67 * "Existing file" constructor. Builds an attribute tree from a
68 * "mapping file" or mapping section previously saved somewhere.
8d1346f0 69 *
a52fde77
AM
70 * @param ss
71 * StateSystem to which this AT is attached
72 * @param fis
73 * File stream where to read the AT information. Make sure it's
6f04e06c 74 * sought at the right place!
a52fde77 75 * @throws IOException
a6917276 76 * If there is a problem reading from the file stream
a52fde77 77 */
a6917276 78 public AttributeTree(StateSystem ss, FileInputStream fis) throws IOException {
a52fde77 79 this(ss);
da66cc75 80 ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis));
a52fde77
AM
81
82 /* Read the header of the Attribute Tree file (or file section) */
da66cc75 83 int res = ois.readInt(); /* Magic number */
a52fde77 84 if (res != ATTRIB_TREE_MAGIC_NUMBER) {
da66cc75 85 throw new IOException("The attribute tree file section is either invalid or corrupted."); //$NON-NLS-1$
a52fde77
AM
86 }
87
a52fde77 88
da66cc75
AM
89 ArrayList<String[]> attribList;
90 try {
91 @SuppressWarnings("unchecked")
92 ArrayList<String[]> list = (ArrayList<String[]>) ois.readObject();
93 attribList = list;
94 } catch (ClassNotFoundException e) {
95 throw new IOException("Unrecognizable attribute list"); //$NON-NLS-1$
a52fde77
AM
96 }
97
98 /*
99 * Now we have 'list', the ArrayList of String arrays representing all
100 * the attributes. Simply create attributes the normal way from them.
101 */
da66cc75 102 for (String[] attrib : attribList) {
a52fde77
AM
103 this.getQuarkAndAdd(-1, attrib);
104 }
105 }
106
107 /**
a6917276 108 * Tell the Attribute Tree to write itself somewhere in a file.
8d1346f0 109 *
a6917276
AM
110 * @param file
111 * The file to write to
112 * @param pos
113 * The position (in bytes) in the file where to write
a52fde77 114 */
da66cc75
AM
115 public void writeSelf(File file, long pos) {
116 try (FileOutputStream fos = new FileOutputStream(file, true);
117 FileChannel fc = fos.getChannel();) {
118 fc.position(pos);
119 try (ObjectOutputStream oos = new ObjectOutputStream(fos)) {
120
121 /* Write the almost-magic number */
122 oos.writeInt(ATTRIB_TREE_MAGIC_NUMBER);
123
124 /* Compute the serialized list of attributes and write it */
125 List<String[]> list = new ArrayList<>(attributeList.size());
126 for (Attribute entry : this.attributeList) {
127 list.add(entry.getFullAttribute());
a52fde77 128 }
da66cc75 129 oos.writeObject(list);
a52fde77 130 }
a52fde77
AM
131 } catch (IOException e) {
132 e.printStackTrace();
133 }
da66cc75 134
a52fde77
AM
135 }
136
137 /**
138 * Return the number of attributes this system as seen so far. Note that
139 * this also equals the integer value (quark) the next added attribute will
140 * have.
8d1346f0 141 *
a6917276 142 * @return The current number of attributes in the tree
a52fde77 143 */
a6917276 144 public int getNbAttributes() {
a52fde77
AM
145 return attributeList.size();
146 }
147
148 /**
a6917276
AM
149 * Get the quark for a given attribute path. No new attribute will be
150 * created : if the specified path does not exist, throw an error.
8d1346f0 151 *
a6917276
AM
152 * @param startingNodeQuark
153 * The quark of the attribute from which relative queries will
154 * start. Use '-1' to start at the root node.
155 * @param subPath
156 * The path to the attribute, relative to the starting node.
157 * @return The quark of the specified attribute
a52fde77 158 * @throws AttributeNotFoundException
a6917276 159 * If the specified path was not found
a52fde77 160 */
a6917276 161 public int getQuarkDontAdd(int startingNodeQuark, String... subPath)
a52fde77 162 throws AttributeNotFoundException {
a52fde77
AM
163 assert (startingNodeQuark >= -1);
164
165 Attribute prevNode;
166
6abc2d88
AM
167 /* If subPath is empty, simply return the starting quark */
168 if (subPath == null || subPath.length == 0) {
169 return startingNodeQuark;
170 }
171
a52fde77
AM
172 /* Get the "starting node" */
173 if (startingNodeQuark == -1) {
174 prevNode = attributeTreeRoot;
175 } else {
176 prevNode = attributeList.get(startingNodeQuark);
177 }
178
179 int knownQuark = prevNode.getSubAttributeQuark(subPath);
180 if (knownQuark == -1) {
181 /*
182 * The attribute doesn't exist, but we have been specified to NOT
183 * add any new attributes.
184 */
e13bd4cd 185 throw new AttributeNotFoundException(ss.getSSID() + " Quark:" + startingNodeQuark + ", SubPath:" + Arrays.toString(subPath)); //$NON-NLS-1$ //$NON-NLS-2$
a52fde77
AM
186 }
187 /*
188 * The attribute was already existing, return the quark of that
189 * attribute
190 */
191 return knownQuark;
192 }
193
a6917276
AM
194 /**
195 * Get the quark of a given attribute path. If that specified path does not
196 * exist, it will be created (and the quark that was just created will be
197 * returned).
198 *
199 * @param startingNodeQuark
200 * The quark of the attribute from which relative queries will
201 * start. Use '-1' to start at the root node.
202 * @param subPath
203 * The path to the attribute, relative to the starting node.
204 * @return The quark of the attribute represented by the path
205 */
206 public synchronized int getQuarkAndAdd(int startingNodeQuark, String... subPath) {
207 // FIXME synchronized here is probably quite costly... maybe only locking
208 // the "for" would be enough?
a52fde77
AM
209 assert (subPath != null && subPath.length > 0);
210 assert (startingNodeQuark >= -1);
211
212 Attribute nextNode = null;
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 was not in the table previously, and we want to add
226 * it
227 */
228 for (String curDirectory : subPath) {
229 nextNode = prevNode.getSubAttributeNode(curDirectory);
230 if (nextNode == null) {
231 /* This is where we need to start adding */
04927a83 232 nextNode = new Attribute(prevNode, checkNotNull(curDirectory), attributeList.size());
a52fde77
AM
233 prevNode.addSubAttribute(nextNode);
234 attributeList.add(nextNode);
8d1346f0 235 ss.addEmptyAttribute();
a52fde77
AM
236 }
237 prevNode = nextNode;
238 }
085a78a8 239 return attributeList.size() - 1;
a52fde77
AM
240 }
241 /*
242 * The attribute was already existing, return the quark of that
243 * attribute
244 */
245 return knownQuark;
246 }
247
0a9de3d2
AM
248 /**
249 * Returns the sub-attributes of the quark passed in parameter
8d1346f0 250 *
0a9de3d2 251 * @param attributeQuark
a6917276 252 * The quark of the attribute to print the sub-attributes of.
c66426fd 253 * @param recursive
a6917276
AM
254 * Should the query be recursive or not? If false, only children
255 * one level deep will be returned. If true, all descendants will
256 * be returned (depth-first search)
257 * @return The list of quarks representing the children attributes
0a9de3d2 258 * @throws AttributeNotFoundException
a6917276
AM
259 * If 'attributeQuark' is invalid, or if there is no attrbiute
260 * associated to it.
0a9de3d2 261 */
50a47aa6 262 public @NonNull List<Integer> getSubAttributes(int attributeQuark, boolean recursive)
0a9de3d2 263 throws AttributeNotFoundException {
a4524c1b 264 List<Integer> listOfChildren = new ArrayList<>();
0a9de3d2 265 Attribute startingAttribute;
c66426fd 266
0a9de3d2 267 /* Check if the quark is valid */
f94a0bac 268 if (attributeQuark < -1 || attributeQuark >= attributeList.size()) {
e13bd4cd 269 throw new AttributeNotFoundException(ss.getSSID() + " Quark:" + attributeQuark); //$NON-NLS-1$
0a9de3d2 270 }
c66426fd 271
0a9de3d2 272 /* Set up the node from which we'll start the search */
c66426fd 273 if (attributeQuark == -1) {
0a9de3d2
AM
274 startingAttribute = attributeTreeRoot;
275 } else {
276 startingAttribute = attributeList.get(attributeQuark);
277 }
c66426fd 278
0a9de3d2 279 /* Iterate through the sub-attributes and add them to the list */
c66426fd 280 addSubAttributes(listOfChildren, startingAttribute, recursive);
0a9de3d2 281
a52fde77
AM
282 return listOfChildren;
283 }
284
0fdd2c45
FG
285 /**
286 * Returns the parent quark of the attribute. The root attribute has no
287 * parent and will return <code>-1</code>
288 *
289 * @param quark
290 * The quark of the attribute
291 * @return Quark of the parent attribute or <code>-1</code> for the root
292 * attribute
293 */
294 public int getParentAttributeQuark(int quark) {
295 if (quark == -1) {
296 return quark;
297 }
298 return attributeList.get(quark).getParentAttributeQuark();
299 }
300
c66426fd
AM
301 private void addSubAttributes(List<Integer> list, Attribute curAttribute,
302 boolean recursive) {
cb42195c 303 for (Attribute childNode : curAttribute.getSubAttributes()) {
c66426fd
AM
304 list.add(childNode.getQuark());
305 if (recursive) {
306 addSubAttributes(list, childNode, true);
307 }
308 }
309 }
310
a6917276
AM
311 /**
312 * Get then base name of an attribute specified by a quark.
313 *
314 * @param quark
315 * The quark of the attribute
316 * @return The (base) name of the attribute
317 */
04927a83 318 public @NonNull String getAttributeName(int quark) {
50678114
AM
319 return attributeList.get(quark).getName();
320 }
321
a6917276
AM
322 /**
323 * Get the full path name of an attribute specified by a quark.
324 *
325 * @param quark
326 * The quark of the attribute
327 * @return The full path name of the attribute
328 */
04927a83 329 public @NonNull String getFullAttributeName(int quark) {
a52fde77
AM
330 return attributeList.get(quark).getFullAttributeName();
331 }
332
34638411
AM
333 /**
334 * Get the full path name (as an array of path elements) of an attribute
335 * specified by a quark.
336 *
337 * @param quark
338 * The quark of the attribute
339 * @return The path elements of the full path
340 */
341 public @NonNull String[] getFullAttributePathArray(int quark) {
342 return attributeList.get(quark).getFullAttribute();
343 }
344
a6917276
AM
345 /**
346 * Debug-print all the attributes in the tree.
347 *
348 * @param writer
349 * The writer where to print the output
350 */
351 public void debugPrint(PrintWriter writer) {
a52fde77
AM
352 attributeTreeRoot.debugPrint(writer);
353 }
354
a6917276 355}
This page took 0.083369 seconds and 5 git commands to generate.