480c0d925f2f1c8f8eaaf8af72470804adfce445
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / backend / historytree / CoreNode.java
1 /*******************************************************************************
2 * Copyright (c) 2010, 2016 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.tracecompass.internal.statesystem.core.backend.historytree;
15
16 import java.nio.ByteBuffer;
17 import java.util.Arrays;
18 import java.util.concurrent.locks.ReentrantReadWriteLock;
19
20 /**
21 * A Core node is a first-level node of a History Tree which is not a leaf node.
22 *
23 * It extends HTNode by adding support for child nodes, and also extensions.
24 *
25 * @author Alexandre Montplaisir
26 */
27 public final class CoreNode extends HTNode {
28
29 /** Nb. of children this node has */
30 private int nbChildren;
31
32 /** Seq. numbers of the children nodes (size = MAX_NB_CHILDREN) */
33 private int[] children;
34
35 /** Start times of each of the children (size = MAX_NB_CHILDREN) */
36 private long[] childStart;
37
38 /** Seq number of this node's extension. -1 if none */
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);
46
47 /**
48 * Initial constructor. Use this to initialize a new EMPTY node.
49 *
50 * @param config
51 * Configuration of the History Tree
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 */
59 public CoreNode(HTConfig config, int seqNumber, int parentSeqNumber,
60 long start) {
61 super(config, seqNumber, parentSeqNumber, start);
62 this.nbChildren = 0;
63 int size = config.getMaxChildren();
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 */
71 this.children = new int[size];
72 this.childStart = new long[size];
73 }
74
75 @Override
76 protected void readSpecificHeader(ByteBuffer buffer) {
77 int size = getConfig().getMaxChildren();
78
79 extension = buffer.getInt();
80 nbChildren = buffer.getInt();
81
82 children = new int[size];
83 for (int i = 0; i < nbChildren; i++) {
84 children[i] = buffer.getInt();
85 }
86 for (int i = nbChildren; i < size; i++) {
87 buffer.getInt();
88 }
89
90 this.childStart = new long[size];
91 for (int i = 0; i < nbChildren; i++) {
92 childStart[i] = buffer.getLong();
93 }
94 for (int i = nbChildren; i < size; i++) {
95 buffer.getLong();
96 }
97 }
98
99 @Override
100 protected void writeSpecificHeader(ByteBuffer buffer) {
101 int size = getConfig().getMaxChildren();
102
103 buffer.putInt(extension);
104 buffer.putInt(nbChildren);
105
106 /* Write the "children's seq number" array */
107 for (int i = 0; i < nbChildren; i++) {
108 buffer.putInt(children[i]);
109 }
110 for (int i = nbChildren; i < size; i++) {
111 buffer.putInt(0);
112 }
113
114 /* Write the "children's start times" array */
115 for (int i = 0; i < nbChildren; i++) {
116 buffer.putLong(childStart[i]);
117 }
118 for (int i = nbChildren; i < size; i++) {
119 buffer.putLong(0);
120 }
121 }
122
123 /**
124 * Return the number of child nodes this node has.
125 *
126 * @return The number of child nodes
127 */
128 public int getNbChildren() {
129 rwl.readLock().lock();
130 int ret = nbChildren;
131 rwl.readLock().unlock();
132 return ret;
133 }
134
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) {
142 rwl.readLock().lock();
143 try {
144 return children[index];
145 } finally {
146 rwl.readLock().unlock();
147 }
148 }
149
150 /**
151 * Get the latest (right-most) child node of this node.
152 *
153 * @return The latest child node
154 */
155 public int getLatestChild() {
156 rwl.readLock().lock();
157 try {
158 return children[nbChildren - 1];
159 } finally {
160 rwl.readLock().unlock();
161 }
162 }
163
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) {
172 rwl.readLock().lock();
173 try {
174 return childStart[index];
175 } finally {
176 rwl.readLock().unlock();
177 }
178 }
179
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() {
186 rwl.readLock().lock();
187 try {
188 return childStart[nbChildren - 1];
189 } finally {
190 rwl.readLock().unlock();
191 }
192 }
193
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() {
201 return extension;
202 }
203
204 /**
205 * Tell this node that it has a new child (Congrats!)
206 *
207 * @param childNode
208 * The SHTNode object of the new child
209 */
210 public void linkNewChild(HTNode childNode) {
211 rwl.writeLock().lock();
212 try {
213 assert (nbChildren < getConfig().getMaxChildren());
214
215 children[nbChildren] = childNode.getSequenceNumber();
216 childStart[nbChildren] = childNode.getNodeStart();
217 nbChildren++;
218
219 } finally {
220 rwl.writeLock().unlock();
221 }
222 }
223
224 @Override
225 public NodeType getNodeType() {
226 return NodeType.CORE;
227 }
228
229 @Override
230 protected int getSpecificHeaderSize() {
231 int maxChildren = getConfig().getMaxChildren();
232 int specificSize =
233 Integer.BYTES /* 1x int (extension node) */
234 + Integer.BYTES /* 1x int (nbChildren) */
235
236 /* MAX_NB * int ('children' table) */
237 + Integer.BYTES * maxChildren
238
239 /* MAX_NB * Timevalue ('childStart' table) */
240 + Long.BYTES * maxChildren;
241
242 return specificSize;
243 }
244
245 @Override
246 public String toStringSpecific() {
247 /* Only used for debugging, shouldn't be externalized */
248 return String.format("Core Node, %d children %s", //$NON-NLS-1$
249 nbChildren, Arrays.toString(Arrays.copyOf(children, nbChildren)));
250 }
251
252 }
This page took 0.037161 seconds and 4 git commands to generate.