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