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