30e6eb809ebf158438d22d67c0eec96abe2294e0
[deliverable/tracecompass.git] / statesystem / org.eclipse.tracecompass.statesystem.core / src / org / eclipse / tracecompass / internal / statesystem / core / backend / historytree / HTInterval.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal
3 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
4 *
5 * All rights reserved. This program and the accompanying materials are
6 * made available under the terms of the Eclipse Public License v1.0 which
7 * accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *
10 * Contributors:
11 * Alexandre Montplaisir - Initial API and implementation
12 * Florian Wininger - Allow to change the size of a interval
13 * Patrick Tasse - Add message to exceptions
14 *******************************************************************************/
15
16 package org.eclipse.tracecompass.internal.statesystem.core.backend.historytree;
17
18 import java.io.IOException;
19 import java.nio.ByteBuffer;
20
21 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
22 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
23 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
24 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
25 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
26
27 /**
28 * The interval component, which will be contained in a node of the History
29 * Tree.
30 *
31 * @author Alexandre Montplaisir
32 */
33 public final class HTInterval implements ITmfStateInterval, Comparable<HTInterval> {
34
35 private static final String errMsg = "Invalid interval data. Maybe your file is corrupt?"; //$NON-NLS-1$
36
37 /**
38 * Size of an entry in the data section.
39 *
40 * <pre>
41 * 16 2 x Timevalue/long (interval start + end)
42 * + 4 int (key)
43 * + 1 byte (type)
44 * + 4 int (valueOffset)
45 * </pre>
46 */
47 public static final int DATA_ENTRY_SIZE = 25;
48
49 /* 'Byte' equivalent for state values types */
50 private static final byte TYPE_NULL = -1;
51 private static final byte TYPE_INTEGER = 0;
52 private static final byte TYPE_STRING = 1;
53 private static final byte TYPE_LONG = 2;
54 private static final byte TYPE_DOUBLE = 3;
55
56 /* String entry sizes of different state values */
57 private static final int NO_ENTRY_SIZE = 0;
58 private static final int LONG_ENTRY_SIZE = 8;
59 private static final int DOUBLE_ENTRY_SIZE = 8;
60 // sizes of string values depend on the string itself
61
62 private final long start;
63 private final long end;
64 private final int attribute;
65 private final TmfStateValue sv;
66
67 /*
68 * Size of the strings section entry used by this interval (= 0 if not used)
69 */
70 private final int stringsEntrySize;
71
72 /**
73 * Standard constructor
74 *
75 * @param intervalStart
76 * Start time of the interval
77 * @param intervalEnd
78 * End time of the interval
79 * @param attribute
80 * Attribute (quark) to which the state represented by this
81 * interval belongs
82 * @param value
83 * State value represented by this interval
84 * @throws TimeRangeException
85 * If the start time or end time are invalid
86 */
87 public HTInterval(long intervalStart, long intervalEnd, int attribute,
88 TmfStateValue value) throws TimeRangeException {
89 if (intervalStart > intervalEnd) {
90 throw new TimeRangeException("Start:" + intervalStart + ", End:" + intervalEnd); //$NON-NLS-1$ //$NON-NLS-2$
91 }
92
93 this.start = intervalStart;
94 this.end = intervalEnd;
95 this.attribute = attribute;
96 this.sv = value;
97 this.stringsEntrySize = computeStringsEntrySize();
98 }
99
100 /**
101 * "Faster" constructor for inner use only. When we build an interval when
102 * reading it from disk (with {@link #readFrom}), we already know the size
103 * of the strings entry, so there is no need to call
104 * {@link #computeStringsEntrySize()} and do an extra copy.
105 */
106 private HTInterval(long intervalStart, long intervalEnd, int attribute,
107 TmfStateValue value, int size) throws TimeRangeException {
108 if (intervalStart > intervalEnd) {
109 throw new TimeRangeException("Start:" + intervalStart + ", End:" + intervalEnd); //$NON-NLS-1$ //$NON-NLS-2$
110 }
111
112 this.start = intervalStart;
113 this.end = intervalEnd;
114 this.attribute = attribute;
115 this.sv = value;
116 this.stringsEntrySize = size;
117 }
118
119 /**
120 * Reader factory method. Builds the interval using an already-allocated
121 * ByteBuffer, which normally comes from a NIO FileChannel.
122 *
123 * @param buffer
124 * The ByteBuffer from which to read the information
125 * @return The interval object
126 * @throws IOException
127 * If there was an error reading from the buffer
128 */
129 public static final HTInterval readFrom(ByteBuffer buffer) throws IOException {
130 HTInterval interval;
131 long intervalStart, intervalEnd;
132 int attribute;
133 TmfStateValue value;
134 int valueOrOffset, valueSize, res;
135 byte valueType;
136 byte array[];
137
138 /* Read the Data Section entry */
139 intervalStart = buffer.getLong();
140 intervalEnd = buffer.getLong();
141 attribute = buffer.getInt();
142
143 /* Read the 'type' of the value, then react accordingly */
144 valueType = buffer.get();
145 valueOrOffset = buffer.getInt();
146 switch (valueType) {
147
148 case TYPE_NULL:
149 value = TmfStateValue.nullValue();
150 valueSize = NO_ENTRY_SIZE;
151 break;
152
153 case TYPE_INTEGER:
154 /* "ValueOrOffset" is the straight value */
155 value = TmfStateValue.newValueInt(valueOrOffset);
156 valueSize = NO_ENTRY_SIZE;
157 break;
158
159 case TYPE_STRING:
160 /* Go read the matching entry in the Strings section of the block */
161 buffer.mark();
162 buffer.position(valueOrOffset);
163
164 /* the first byte = the size to read */
165 valueSize = buffer.get();
166
167 /*
168 * Careful though, 'valueSize' is the total size of the entry,
169 * including the 'size' byte at the start and end (0'ed) byte at the
170 * end. Here we want 'array' to only contain the real payload of the
171 * value.
172 */
173 array = new byte[valueSize - 2];
174 buffer.get(array);
175 value = TmfStateValue.newValueString(new String(array));
176
177 /* Confirm the 0'ed byte at the end */
178 res = buffer.get();
179 if (res != 0) {
180 throw new IOException(errMsg);
181 }
182
183 /*
184 * Restore the file pointer's position (so we can read the next
185 * interval)
186 */
187 buffer.reset();
188 break;
189
190 case TYPE_LONG:
191 /* Go read the matching entry in the Strings section of the block */
192 buffer.mark();
193 buffer.position(valueOrOffset);
194 value = TmfStateValue.newValueLong(buffer.getLong());
195 valueSize = LONG_ENTRY_SIZE;
196
197 /*
198 * Restore the file pointer's position (so we can read the next
199 * interval)
200 */
201 buffer.reset();
202 break;
203
204 case TYPE_DOUBLE:
205 /* Go read the matching entry in the Strings section of the block */
206 buffer.mark();
207 buffer.position(valueOrOffset);
208 value = TmfStateValue.newValueDouble(buffer.getDouble());
209 valueSize = DOUBLE_ENTRY_SIZE;
210
211 /*
212 * Restore the file pointer's position (so we can read the next
213 * interval)
214 */
215 buffer.reset();
216 break;
217
218 default:
219 /* Unknown data, better to not make anything up... */
220 throw new IOException(errMsg);
221 }
222
223 try {
224 interval = new HTInterval(intervalStart, intervalEnd, attribute, value, valueSize);
225 } catch (TimeRangeException e) {
226 throw new IOException(errMsg);
227 }
228 return interval;
229 }
230
231 /**
232 * Antagonist of the previous constructor, write the Data entry
233 * corresponding to this interval in a ByteBuffer (mapped to a block in the
234 * history-file, hopefully)
235 *
236 * @param buffer
237 * The already-allocated ByteBuffer corresponding to a SHT Node
238 * @param endPosOfStringEntry
239 * The initial (before calling this function for this interval)
240 * position of the Strings Entry for this node. This will change
241 * from one call to the other if we're writing String
242 * StateValues.
243 * @return The size of the Strings Entry that was written, if any.
244 */
245 public int writeInterval(ByteBuffer buffer, int endPosOfStringEntry) {
246 buffer.putLong(start);
247 buffer.putLong(end);
248 buffer.putInt(attribute);
249 buffer.put(getByteFromType(sv.getType()));
250
251 switch (getByteFromType(sv.getType())) {
252
253 case TYPE_NULL:
254 case TYPE_INTEGER:
255 /* We write the 'valueOffset' field as a straight value. */
256 try {
257 buffer.putInt(sv.unboxInt());
258 } catch (StateValueTypeException e) {
259 /*
260 * This should not happen, since the value told us it was of
261 * type Null or Integer (corrupted value?)
262 */
263 e.printStackTrace();
264 }
265 break;
266
267 case TYPE_STRING:
268 byte[] byteArrayToWrite;
269 try {
270 byteArrayToWrite = sv.unboxStr().getBytes();
271 } catch (StateValueTypeException e1) {
272 /* Should not happen, we're in a switch/case for string type */
273 throw new RuntimeException();
274 }
275
276 /* we use the valueOffset as an offset. */
277 buffer.putInt(endPosOfStringEntry - stringsEntrySize);
278 buffer.mark();
279 buffer.position(endPosOfStringEntry - stringsEntrySize);
280
281 /*
282 * write the Strings entry (1st byte = size, then the bytes, then the 0)
283 */
284 buffer.put((byte) stringsEntrySize);
285 buffer.put(byteArrayToWrite);
286 buffer.put((byte) 0);
287 assert (buffer.position() == endPosOfStringEntry);
288 buffer.reset();
289 break;
290
291 case TYPE_LONG:
292 /* we use the valueOffset as an offset. */
293 buffer.putInt(endPosOfStringEntry - stringsEntrySize);
294 buffer.mark();
295 buffer.position(endPosOfStringEntry - stringsEntrySize);
296
297 /*
298 * write the Long in the Strings section
299 */
300 try {
301 buffer.putLong(sv.unboxLong());
302 } catch (StateValueTypeException e) {
303 /*
304 * This should not happen, since the value told us it was of
305 * type Long (corrupted value?)
306 */
307 e.printStackTrace();
308 }
309 assert (buffer.position() == endPosOfStringEntry);
310 buffer.reset();
311 break;
312
313 case TYPE_DOUBLE:
314 /* we use the valueOffset as an offset. */
315 buffer.putInt(endPosOfStringEntry - stringsEntrySize);
316 buffer.mark();
317 buffer.position(endPosOfStringEntry - stringsEntrySize);
318
319 /* Write the Double in the Strings section */
320 try {
321 buffer.putDouble(sv.unboxDouble());
322 } catch (StateValueTypeException e) {
323 /*
324 * This should not happen, since the value told us it was of
325 * type Double (corrupted value?)
326 */
327 e.printStackTrace();
328 }
329 if (buffer.position() != endPosOfStringEntry) {
330 throw new IllegalStateException();
331 }
332 buffer.reset();
333 break;
334
335 default:
336 break;
337 }
338 return stringsEntrySize;
339 }
340
341 @Override
342 public long getStartTime() {
343 return start;
344 }
345
346 @Override
347 public long getEndTime() {
348 return end;
349 }
350
351 @Override
352 public int getAttribute() {
353 return attribute;
354 }
355
356 @Override
357 public ITmfStateValue getStateValue() {
358 return sv;
359 }
360
361 @Override
362 public boolean intersects(long timestamp) {
363 if (start <= timestamp) {
364 if (end >= timestamp) {
365 return true;
366 }
367 }
368 return false;
369 }
370
371 int getStringsEntrySize() {
372 return stringsEntrySize;
373 }
374
375 /**
376 * Total serialized size of this interval
377 *
378 * @return The interval size
379 */
380 public int getIntervalSize() {
381 return stringsEntrySize + DATA_ENTRY_SIZE;
382 }
383
384 private int computeStringsEntrySize() {
385 switch(sv.getType()) {
386 case NULL:
387 case INTEGER:
388 /* Those don't use the strings section at all */
389 return NO_ENTRY_SIZE;
390 case LONG:
391 /* The value's bytes are written directly into the strings section */
392 return LONG_ENTRY_SIZE;
393 case DOUBLE:
394 /* The value is also written directly into the strings section */
395 return DOUBLE_ENTRY_SIZE;
396 case STRING:
397 try {
398 /* String's length + 2 (1 byte for size, 1 byte for \0 at the end */
399 return sv.unboxStr().getBytes().length + 2;
400 } catch (StateValueTypeException e) {
401 /* We're inside a switch/case for the string type, can't happen */
402 throw new IllegalStateException(e);
403 }
404 default:
405 /* It's very important that we know how to write the state value in
406 * the file!! */
407 throw new IllegalStateException();
408 }
409 }
410
411 /**
412 * Compare the END TIMES of different intervals. This is used to sort the
413 * intervals when we close down a node.
414 */
415 @Override
416 public int compareTo(HTInterval other) {
417 if (this.end < other.end) {
418 return -1;
419 } else if (this.end > other.end) {
420 return 1;
421 } else {
422 return 0;
423 }
424 }
425
426 @Override
427 public boolean equals(Object other) {
428 if (other instanceof HTInterval &&
429 this.compareTo((HTInterval) other) == 0) {
430 return true;
431 }
432 return false;
433 }
434
435 @Override
436 public int hashCode() {
437 return super.hashCode();
438 }
439
440 @Override
441 public String toString() {
442 /* Only for debug, should not be externalized */
443 StringBuilder sb = new StringBuilder();
444 sb.append('[');
445 sb.append(start);
446 sb.append(", "); //$NON-NLS-1$
447 sb.append(end);
448 sb.append(']');
449
450 sb.append(", attribute = "); //$NON-NLS-1$
451 sb.append(attribute);
452
453 sb.append(", value = "); //$NON-NLS-1$
454 sb.append(sv.toString());
455
456 return sb.toString();
457 }
458
459 /**
460 * Here we determine how state values "types" are written in the 8-bit
461 * field that indicates the value type in the file.
462 */
463 private static byte getByteFromType(ITmfStateValue.Type type) {
464 switch(type) {
465 case NULL:
466 return TYPE_NULL;
467 case INTEGER:
468 return TYPE_INTEGER;
469 case STRING:
470 return TYPE_STRING;
471 case LONG:
472 return TYPE_LONG;
473 case DOUBLE:
474 return TYPE_DOUBLE;
475 default:
476 /* Should not happen if the switch is fully covered */
477 throw new IllegalStateException();
478 }
479 }
480 }
This page took 0.067639 seconds and 4 git commands to generate.