tmf: Add a new package for state history backends
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / internal / tmf / core / statesystem / backends / historytree / CoreNode.java
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
13 package org.eclipse.linuxtools.internal.tmf.core.statesystem.backends.historytree;
14
15 import java.nio.ByteBuffer;
16
17 /**
18 * A Core node is a first-level node of a History Tree which is not a leaf node.
19 *
20 * It extends HTNode by adding support for child nodes, and also extensions.
21 *
22 * @author alexmont
23 *
24 */
25 class CoreNode extends HTNode {
26
27 /* Nb. of children this node has */
28 private int nbChildren;
29
30 /* Seq. numbers of the children nodes (size = MAX_NB_CHILDREN) */
31 private int[] children;
32
33 /* Start times of each of the children (size = MAX_NB_CHILDREN) */
34 private long[] childStart;
35
36 /* Seq number of this node's extension. -1 if none */
37 private int extension;
38
39 /**
40 * Initial constructor. Use this to initialize a new EMPTY node.
41 *
42 * @param tree
43 * The HistoryTree to which this node belongs
44 * @param seqNumber
45 * The (unique) sequence number assigned to this particular node
46 * @param parentSeqNumber
47 * The sequence number of this node's parent node
48 * @param start
49 * The earliest timestamp stored in this node
50 */
51 CoreNode(HistoryTree tree, int seqNumber, int parentSeqNumber,
52 long start) {
53 super(tree, seqNumber, parentSeqNumber, start);
54 this.nbChildren = 0;
55
56 /*
57 * We instantiate the two following arrays at full size right away,
58 * since we want to reserve that space in the node's header.
59 * "this.nbChildren" will tell us how many relevant entries there are in
60 * those tables.
61 */
62 this.children = new int[ownerTree.config.maxChildren];
63 this.childStart = new long[ownerTree.config.maxChildren];
64 }
65
66 @Override
67 protected void readSpecificHeader(ByteBuffer buffer) {
68 int i;
69
70 extension = buffer.getInt();
71 nbChildren = buffer.getInt();
72
73 children = new int[ownerTree.config.maxChildren];
74 for (i = 0; i < nbChildren; i++) {
75 children[i] = buffer.getInt();
76 }
77 for (i = nbChildren; i < ownerTree.config.maxChildren; i++) {
78 buffer.getInt();
79 }
80
81 this.childStart = new long[ownerTree.config.maxChildren];
82 for (i = 0; i < nbChildren; i++) {
83 childStart[i] = buffer.getLong();
84 }
85 for (i = nbChildren; i < ownerTree.config.maxChildren; i++) {
86 buffer.getLong();
87 }
88 }
89
90 @Override
91 protected void writeSpecificHeader(ByteBuffer buffer) {
92 int i;
93
94 buffer.putInt(extension);
95 buffer.putInt(nbChildren);
96
97 /* Write the "children's seq number" array */
98 for (i = 0; i < nbChildren; i++) {
99 buffer.putInt(children[i]);
100 }
101 for (i = nbChildren; i < ownerTree.config.maxChildren; i++) {
102 buffer.putInt(0);
103 }
104
105 /* Write the "children's start times" array */
106 for (i = 0; i < nbChildren; i++) {
107 buffer.putLong(childStart[i]);
108 }
109 for (i = nbChildren; i < ownerTree.config.maxChildren; i++) {
110 buffer.putLong(0);
111 }
112 }
113
114 int getNbChildren() {
115 return nbChildren;
116 }
117
118 int getChild(int index) {
119 return children[index];
120 }
121
122 int getLatestChild() {
123 return children[nbChildren - 1];
124 }
125
126 long getChildStart(int index) {
127 return childStart[index];
128 }
129
130 long getLatestChildStart() {
131 return childStart[nbChildren - 1];
132 }
133
134 int getExtensionSequenceNumber() {
135 return extension;
136 }
137
138 /**
139 * Tell this node that it has a new child (Congrats!)
140 *
141 * @param childNode
142 * The SHTNode object of the new child
143 */
144 void linkNewChild(CoreNode childNode) {
145 assert (this.nbChildren < ownerTree.config.maxChildren);
146
147 this.children[nbChildren] = childNode.getSequenceNumber();
148 this.childStart[nbChildren] = childNode.getNodeStart();
149 this.nbChildren++;
150 }
151
152 @Override
153 protected byte getNodeType() {
154 return 1;
155 }
156
157 @Override
158 protected int getTotalHeaderSize() {
159 int specificSize;
160 specificSize = 4 /* 1x int (extension node) */
161 + 4 /* 1x int (nbChildren) */
162
163 /* MAX_NB * int ('children' table) */
164 + 4 * ownerTree.config.maxChildren
165
166 /* MAX_NB * Timevalue ('childStart' table) */
167 + 8 * ownerTree.config.maxChildren;
168
169 return getCommonHeaderSize() + specificSize;
170 }
171
172 @Override
173 protected String toStringSpecific() {
174 /* Only used for debugging, shouldn't be externalized */
175 return "Core Node, " + nbChildren + " children, "; //$NON-NLS-1$ //$NON-NLS-2$
176 }
177
178 }
This page took 0.034908 seconds and 6 git commands to generate.