ctf: Don't include all test traces in jar
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / statesystem / backends / historytree / CoreNode.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2014 Ericsson, École Polytechnique de Montréal, and others
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 * Florian Wininger - Add Extension and Leaf Node
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree;
15
16 import java.nio.ByteBuffer;
17 import java.util.concurrent.locks.ReentrantReadWriteLock;
18
19 /**
20 * A Core node is a first-level node of a History Tree which is not a leaf node.
21 *
22 * It extends HTNode by adding support for child nodes, and also extensions.
23 *
24 * @author Alexandre Montplaisir
25 */
26 public final class CoreNode extends HTNode {
27
28 /** Number of bytes in a int */
29 private static final int SIZE_INT = 4;
30
31 /** Number of bytes in a long */
32 private static final int SIZE_LONG = 8;
33
34 /** Nb. of children this node has */
35 private int nbChildren;
36
37 /** Seq. numbers of the children nodes (size = MAX_NB_CHILDREN) */
38 private int[] children;
39
40 /** Start times of each of the children (size = MAX_NB_CHILDREN) */
41 private long[] childStart;
42
43 /** Seq number of this node's extension. -1 if none */
44 private volatile int extension = -1;
45
46 /**
47 * Lock used to gate the accesses to the children arrays. Meant to be a
48 * different lock from the one in {@link HTNode}.
49 */
50 private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(false);
51
52 /**
53 * Initial constructor. Use this to initialize a new EMPTY node.
54 *
55 * @param config
56 * Configuration of the History Tree
57 * @param seqNumber
58 * The (unique) sequence number assigned to this particular node
59 * @param parentSeqNumber
60 * The sequence number of this node's parent node
61 * @param start
62 * The earliest timestamp stored in this node
63 */
64 public CoreNode(HTConfig config, int seqNumber, int parentSeqNumber,
65 long start) {
66 super(config, seqNumber, parentSeqNumber, start);
67 this.nbChildren = 0;
68 int size = config.getMaxChildren();
69
70 /*
71 * We instantiate the two following arrays at full size right away,
72 * since we want to reserve that space in the node's header.
73 * "this.nbChildren" will tell us how many relevant entries there are in
74 * those tables.
75 */
76 this.children = new int[size];
77 this.childStart = new long[size];
78 }
79
80 @Override
81 protected void readSpecificHeader(ByteBuffer buffer) {
82 int size = getConfig().getMaxChildren();
83
84 extension = buffer.getInt();
85 nbChildren = buffer.getInt();
86
87 children = new int[size];
88 for (int i = 0; i < nbChildren; i++) {
89 children[i] = buffer.getInt();
90 }
91 for (int i = nbChildren; i < size; i++) {
92 buffer.getInt();
93 }
94
95 this.childStart = new long[size];
96 for (int i = 0; i < nbChildren; i++) {
97 childStart[i] = buffer.getLong();
98 }
99 for (int i = nbChildren; i < size; i++) {
100 buffer.getLong();
101 }
102 }
103
104 @Override
105 protected void writeSpecificHeader(ByteBuffer buffer) {
106 int size = getConfig().getMaxChildren();
107
108 buffer.putInt(extension);
109 buffer.putInt(nbChildren);
110
111 /* Write the "children's seq number" array */
112 for (int i = 0; i < nbChildren; i++) {
113 buffer.putInt(children[i]);
114 }
115 for (int i = nbChildren; i < size; i++) {
116 buffer.putInt(0);
117 }
118
119 /* Write the "children's start times" array */
120 for (int i = 0; i < nbChildren; i++) {
121 buffer.putLong(childStart[i]);
122 }
123 for (int i = nbChildren; i < size; i++) {
124 buffer.putLong(0);
125 }
126 }
127
128 /**
129 * Return the number of child nodes this node has.
130 *
131 * @return The number of child nodes
132 */
133 public int getNbChildren() {
134 rwl.readLock().lock();
135 int ret = nbChildren;
136 rwl.readLock().unlock();
137 return ret;
138 }
139
140 /**
141 * Get the child node corresponding to the specified index
142 *
143 * @param index The index of the child to lookup
144 * @return The child node
145 */
146 public int getChild(int index) {
147 rwl.readLock().lock();
148 try {
149 return children[index];
150 } finally {
151 rwl.readLock().unlock();
152 }
153 }
154
155 /**
156 * Get the latest (right-most) child node of this node.
157 *
158 * @return The latest child node
159 */
160 public int getLatestChild() {
161 rwl.readLock().lock();
162 try {
163 return children[nbChildren - 1];
164 } finally {
165 rwl.readLock().unlock();
166 }
167 }
168
169 /**
170 * Get the start time of the specified child node.
171 *
172 * @param index
173 * The index of the child node
174 * @return The start time of the that child node.
175 */
176 public long getChildStart(int index) {
177 rwl.readLock().lock();
178 try {
179 return childStart[index];
180 } finally {
181 rwl.readLock().unlock();
182 }
183 }
184
185 /**
186 * Get the start time of the latest (right-most) child node.
187 *
188 * @return The start time of the latest child
189 */
190 public long getLatestChildStart() {
191 rwl.readLock().lock();
192 try {
193 return childStart[nbChildren - 1];
194 } finally {
195 rwl.readLock().unlock();
196 }
197 }
198
199 /**
200 * Get the sequence number of the extension to this node (if there is one).
201 *
202 * @return The sequence number of the extended node. '-1' is returned if
203 * there is no extension node.
204 */
205 public int getExtensionSequenceNumber() {
206 return extension;
207 }
208
209 /**
210 * Tell this node that it has a new child (Congrats!)
211 *
212 * @param childNode
213 * The SHTNode object of the new child
214 */
215 public void linkNewChild(HTNode childNode) {
216 rwl.writeLock().lock();
217 try {
218 assert (nbChildren < getConfig().getMaxChildren());
219
220 children[nbChildren] = childNode.getSequenceNumber();
221 childStart[nbChildren] = childNode.getNodeStart();
222 nbChildren++;
223
224 } finally {
225 rwl.writeLock().unlock();
226 }
227 }
228
229 @Override
230 public NodeType getNodeType() {
231 return NodeType.CORE;
232 }
233
234 @Override
235 protected int getSpecificHeaderSize() {
236 int maxChildren = getConfig().getMaxChildren();
237 int specificSize =
238 SIZE_INT /* 1x int (extension node) */
239 + SIZE_INT /* 1x int (nbChildren) */
240
241 /* MAX_NB * int ('children' table) */
242 + SIZE_INT * maxChildren
243
244 /* MAX_NB * Timevalue ('childStart' table) */
245 + SIZE_LONG * maxChildren;
246
247 return specificSize;
248 }
249
250 @Override
251 public String toStringSpecific() {
252 /* Only used for debugging, shouldn't be externalized */
253 return "Core Node, " + nbChildren + " children, "; //$NON-NLS-1$ //$NON-NLS-2$
254 }
255
256 }
This page took 0.037631 seconds and 5 git commands to generate.