1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
3 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
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
11 * Alexandre Montplaisir - Initial API and implementation
12 * Florian Wininger - Allow to change the size of a interval
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
.backends
.historytree
;
17 import java
.io
.IOException
;
18 import java
.nio
.ByteBuffer
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.TmfStateValue
;
27 * The interval component, which will be contained in a node of the History
30 * @author Alexandre Montplaisir
32 public final class HTInterval
implements ITmfStateInterval
, Comparable
<HTInterval
> {
34 private static final String errMsg
= "Invalid interval data. Maybe your file is corrupt?"; //$NON-NLS-1$
37 * Size of an entry in the data section.
40 * 16 2 x Timevalue/long (interval start + end)
43 * + 4 int (valueOffset)
46 private static final int DATA_ENTRY_SIZE
= 25;
48 /* 'Byte' equivalent for state values types */
49 private static final byte TYPE_NULL
= -1;
50 private static final byte TYPE_INTEGER
= 0;
51 private static final byte TYPE_STRING
= 1;
52 private static final byte TYPE_LONG
= 2;
53 private static final byte TYPE_DOUBLE
= 3;
55 /* String entry sizes of different state values */
56 private static final int NO_ENTRY_SIZE
= 0;
57 private static final int LONG_ENTRY_SIZE
= 8;
58 private static final int DOUBLE_ENTRY_SIZE
= 8;
59 // sizes of string values depend on the string itself
61 private final long start
;
62 private final long end
;
63 private final int attribute
;
64 private final TmfStateValue sv
;
67 * Size of the strings section entry used by this interval (= 0 if not used)
69 private final int stringsEntrySize
;
72 * Standard constructor
74 * @param intervalStart
75 * Start time of the interval
77 * End time of the interval
79 * Attribute (quark) to which the state represented by this
82 * State value represented by this interval
83 * @throws TimeRangeException
84 * If the start time or end time are invalid
86 public HTInterval(long intervalStart
, long intervalEnd
, int attribute
,
87 TmfStateValue value
) throws TimeRangeException
{
88 if (intervalStart
> intervalEnd
) {
89 throw new TimeRangeException();
92 this.start
= intervalStart
;
93 this.end
= intervalEnd
;
94 this.attribute
= attribute
;
96 this.stringsEntrySize
= computeStringsEntrySize();
100 * "Faster" constructor for inner use only. When we build an interval when
101 * reading it from disk (with {@link #readFrom}), we already know the size
102 * of the strings entry, so there is no need to call
103 * {@link #computeStringsEntrySize()} and do an extra copy.
105 private HTInterval(long intervalStart
, long intervalEnd
, int attribute
,
106 TmfStateValue value
, int size
) throws TimeRangeException
{
107 if (intervalStart
> intervalEnd
) {
108 throw new TimeRangeException();
111 this.start
= intervalStart
;
112 this.end
= intervalEnd
;
113 this.attribute
= attribute
;
115 this.stringsEntrySize
= size
;
119 * Reader factory method. Builds the interval using an already-allocated
120 * ByteBuffer, which normally comes from a NIO FileChannel.
123 * The ByteBuffer from which to read the information
124 * @return The interval object
125 * @throws IOException
126 * If there was an error reading from the buffer
128 public static final HTInterval
readFrom(ByteBuffer buffer
) throws IOException
{
130 long intervalStart
, intervalEnd
;
133 int valueOrOffset
, valueSize
, res
;
137 /* Read the Data Section entry */
138 intervalStart
= buffer
.getLong();
139 intervalEnd
= buffer
.getLong();
140 attribute
= buffer
.getInt();
142 /* Read the 'type' of the value, then react accordingly */
143 valueType
= buffer
.get();
144 valueOrOffset
= buffer
.getInt();
148 value
= TmfStateValue
.nullValue();
149 valueSize
= NO_ENTRY_SIZE
;
153 /* "ValueOrOffset" is the straight value */
154 value
= TmfStateValue
.newValueInt(valueOrOffset
);
155 valueSize
= NO_ENTRY_SIZE
;
159 /* Go read the matching entry in the Strings section of the block */
161 buffer
.position(valueOrOffset
);
163 /* the first byte = the size to read */
164 valueSize
= buffer
.get();
167 * Careful though, 'valueSize' is the total size of the entry,
168 * including the 'size' byte at the start and end (0'ed) byte at the
169 * end. Here we want 'array' to only contain the real payload of the
172 array
= new byte[valueSize
- 2];
174 value
= TmfStateValue
.newValueString(new String(array
));
176 /* Confirm the 0'ed byte at the end */
179 throw new IOException(errMsg
);
183 * Restore the file pointer's position (so we can read the next
190 /* Go read the matching entry in the Strings section of the block */
192 buffer
.position(valueOrOffset
);
193 value
= TmfStateValue
.newValueLong(buffer
.getLong());
194 valueSize
= LONG_ENTRY_SIZE
;
197 * Restore the file pointer's position (so we can read the next
204 /* Go read the matching entry in the Strings section of the block */
206 buffer
.position(valueOrOffset
);
207 value
= TmfStateValue
.newValueDouble(buffer
.getDouble());
208 valueSize
= DOUBLE_ENTRY_SIZE
;
211 * Restore the file pointer's position (so we can read the next
218 /* Unknown data, better to not make anything up... */
219 throw new IOException(errMsg
);
223 interval
= new HTInterval(intervalStart
, intervalEnd
, attribute
, value
, valueSize
);
224 } catch (TimeRangeException e
) {
225 throw new IOException(errMsg
);
231 * Antagonist of the previous constructor, write the Data entry
232 * corresponding to this interval in a ByteBuffer (mapped to a block in the
233 * history-file, hopefully)
236 * The already-allocated ByteBuffer corresponding to a SHT Node
237 * @param endPosOfStringEntry
238 * The initial (before calling this function for this interval)
239 * position of the Strings Entry for this node. This will change
240 * from one call to the other if we're writing String
242 * @return The size of the Strings Entry that was written, if any.
244 public int writeInterval(ByteBuffer buffer
, int endPosOfStringEntry
) {
245 buffer
.putLong(start
);
247 buffer
.putInt(attribute
);
248 buffer
.put(getByteFromType(sv
.getType()));
250 switch (getByteFromType(sv
.getType())) {
254 /* We write the 'valueOffset' field as a straight value. */
256 buffer
.putInt(sv
.unboxInt());
257 } catch (StateValueTypeException e
) {
259 * This should not happen, since the value told us it was of
260 * type Null or Integer (corrupted value?)
267 byte[] byteArrayToWrite
;
269 byteArrayToWrite
= sv
.unboxStr().getBytes();
270 } catch (StateValueTypeException e1
) {
271 /* Should not happen, we're in a switch/case for string type */
272 throw new RuntimeException();
275 /* we use the valueOffset as an offset. */
276 buffer
.putInt(endPosOfStringEntry
- stringsEntrySize
);
278 buffer
.position(endPosOfStringEntry
- stringsEntrySize
);
281 * write the Strings entry (1st byte = size, then the bytes, then the 0)
283 buffer
.put((byte) stringsEntrySize
);
284 buffer
.put(byteArrayToWrite
);
285 buffer
.put((byte) 0);
286 assert (buffer
.position() == endPosOfStringEntry
);
291 /* we use the valueOffset as an offset. */
292 buffer
.putInt(endPosOfStringEntry
- stringsEntrySize
);
294 buffer
.position(endPosOfStringEntry
- stringsEntrySize
);
297 * write the Long in the Strings section
300 buffer
.putLong(sv
.unboxLong());
301 } catch (StateValueTypeException e
) {
303 * This should not happen, since the value told us it was of
304 * type Long (corrupted value?)
308 assert (buffer
.position() == endPosOfStringEntry
);
313 /* we use the valueOffset as an offset. */
314 buffer
.putInt(endPosOfStringEntry
- stringsEntrySize
);
316 buffer
.position(endPosOfStringEntry
- stringsEntrySize
);
318 /* Write the Double in the Strings section */
320 buffer
.putDouble(sv
.unboxDouble());
321 } catch (StateValueTypeException e
) {
323 * This should not happen, since the value told us it was of
324 * type Double (corrupted value?)
328 if (buffer
.position() != endPosOfStringEntry
) {
329 throw new IllegalStateException();
337 return stringsEntrySize
;
341 public long getStartTime() {
346 public long getEndTime() {
351 public long getViewerEndTime() {
356 public int getAttribute() {
361 public ITmfStateValue
getStateValue() {
366 public boolean intersects(long timestamp
) {
367 if (start
<= timestamp
) {
368 if (end
>= timestamp
) {
375 int getStringsEntrySize() {
376 return stringsEntrySize
;
380 * Total serialized size of this interval
382 * @return The interval size
384 public int getIntervalSize() {
385 return stringsEntrySize
+ DATA_ENTRY_SIZE
;
388 private int computeStringsEntrySize() {
389 switch(sv
.getType()) {
392 /* Those don't use the strings section at all */
393 return NO_ENTRY_SIZE
;
395 /* The value's bytes are written directly into the strings section */
396 return LONG_ENTRY_SIZE
;
398 /* The value is also written directly into the strings section */
399 return DOUBLE_ENTRY_SIZE
;
402 /* String's length + 2 (1 byte for size, 1 byte for \0 at the end */
403 return sv
.unboxStr().getBytes().length
+ 2;
404 } catch (StateValueTypeException e
) {
405 /* We're inside a switch/case for the string type, can't happen */
406 throw new IllegalStateException(e
);
409 /* It's very important that we know how to write the state value in
411 throw new IllegalStateException();
416 * Compare the END TIMES of different intervals. This is used to sort the
417 * intervals when we close down a node.
420 public int compareTo(HTInterval other
) {
421 if (this.end
< other
.end
) {
423 } else if (this.end
> other
.end
) {
431 public boolean equals(Object other
) {
432 if (other
instanceof HTInterval
&&
433 this.compareTo((HTInterval
) other
) == 0) {
440 public int hashCode() {
441 return super.hashCode();
445 public String
toString() {
446 /* Only for debug, should not be externalized */
447 StringBuilder sb
= new StringBuilder();
450 sb
.append(", "); //$NON-NLS-1$
454 sb
.append(", attribute = "); //$NON-NLS-1$
455 sb
.append(attribute
);
457 sb
.append(", value = "); //$NON-NLS-1$
458 sb
.append(sv
.toString());
460 return sb
.toString();
464 * Here we determine how state values "types" are written in the 8-bit
465 * field that indicates the value type in the file.
467 private static byte getByteFromType(ITmfStateValue
.Type type
) {
480 /* Should not happen if the switch is fully covered */
481 throw new IllegalStateException();