| 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.io.File; |
| 16 | import java.io.FileInputStream; |
| 17 | import java.io.IOException; |
| 18 | import java.io.PrintWriter; |
| 19 | import java.nio.ByteBuffer; |
| 20 | import java.nio.ByteOrder; |
| 21 | import java.nio.channels.ClosedChannelException; |
| 22 | import java.nio.channels.FileChannel; |
| 23 | import java.util.Vector; |
| 24 | |
| 25 | import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException; |
| 26 | |
| 27 | /** |
| 28 | * Meta-container for the History Tree. This structure contains all the |
| 29 | * high-level data relevant to the tree. |
| 30 | * |
| 31 | * @author alexmont |
| 32 | * |
| 33 | */ |
| 34 | class HistoryTree { |
| 35 | |
| 36 | private static final int HISTORY_FILE_MAGIC_NUMBER = 0x05FFA900; |
| 37 | |
| 38 | /** |
| 39 | * File format version. Increment minor on backwards-compatible changes. |
| 40 | * Increment major + set minor back to 0 when breaking compatibility. |
| 41 | */ |
| 42 | private static final int MAJOR_VERSION = 3; |
| 43 | private static final byte MINOR_VERSION = 0; |
| 44 | |
| 45 | /** |
| 46 | * Tree-specific configuration |
| 47 | */ |
| 48 | /* Container for all the configuration constants */ |
| 49 | protected final HTConfig config; |
| 50 | |
| 51 | /* Reader/writer object */ |
| 52 | private final HT_IO treeIO; |
| 53 | |
| 54 | /** |
| 55 | * Variable Fields (will change throughout the existance of the SHT) |
| 56 | */ |
| 57 | /* Latest timestamp found in the tree (at any given moment) */ |
| 58 | private long treeEnd; |
| 59 | |
| 60 | /* How many nodes exist in this tree, total */ |
| 61 | private int nodeCount; |
| 62 | |
| 63 | /* "Cache" to keep the active nodes in memory */ |
| 64 | protected Vector<CoreNode> latestBranch; |
| 65 | |
| 66 | /** |
| 67 | * Create a new State History from scratch, using a SHTConfig object for |
| 68 | * configuration |
| 69 | * |
| 70 | * @param conf |
| 71 | * @throws IOException |
| 72 | */ |
| 73 | private HistoryTree(HTConfig conf) throws IOException { |
| 74 | /* |
| 75 | * Simple assertion to make sure we have enough place in the 0th block |
| 76 | * for the tree configuration |
| 77 | */ |
| 78 | assert (conf.blockSize >= getTreeHeaderSize()); |
| 79 | |
| 80 | config = conf; |
| 81 | treeEnd = conf.treeStart; |
| 82 | nodeCount = 0; |
| 83 | latestBranch = new Vector<CoreNode>(); |
| 84 | |
| 85 | /* Prepare the IO object */ |
| 86 | treeIO = new HT_IO(this, true); |
| 87 | |
| 88 | /* Add the first node to the tree */ |
| 89 | CoreNode firstNode = initNewCoreNode(-1, conf.treeStart); |
| 90 | latestBranch.add(firstNode); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * "New State History" constructor, which doesn't use SHTConfig but the |
| 95 | * individual values separately. Kept for now for backwards compatibility, |
| 96 | * but you should definitely consider using SHTConfig instead (since its |
| 97 | * contents can then change without directly affecting SHT's API). |
| 98 | */ |
| 99 | HistoryTree(File newStateFile, int blockSize, int maxChildren, |
| 100 | long startTime) throws IOException { |
| 101 | this(new HTConfig(newStateFile, blockSize, maxChildren, startTime)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * "Reader" constructor : instantiate a SHTree from an existing tree file on |
| 106 | * disk |
| 107 | * |
| 108 | * @param existingFileName |
| 109 | * Path/filename of the history-file we are to open |
| 110 | * @throws IOException |
| 111 | */ |
| 112 | HistoryTree(File existingStateFile) throws IOException { |
| 113 | /* |
| 114 | * Open the file ourselves, get the tree header information we need, |
| 115 | * then pass on the descriptor to the TreeIO object. |
| 116 | */ |
| 117 | int rootNodeSeqNb, res; |
| 118 | int bs, maxc; |
| 119 | long startTime; |
| 120 | |
| 121 | /* Java I/O mumbo jumbo... */ |
| 122 | if (!existingStateFile.exists()) { |
| 123 | throw new IOException("Selected state file does not exist"); //$NON-NLS-1$ |
| 124 | } |
| 125 | if (existingStateFile.length() <= 0) { |
| 126 | throw new IOException("Invalid state file selected, " + //$NON-NLS-1$ |
| 127 | "target file is empty"); //$NON-NLS-1$ |
| 128 | } |
| 129 | |
| 130 | FileInputStream fis = new FileInputStream(existingStateFile); |
| 131 | ByteBuffer buffer = ByteBuffer.allocate(getTreeHeaderSize()); |
| 132 | FileChannel fc = fis.getChannel(); |
| 133 | buffer.order(ByteOrder.LITTLE_ENDIAN); |
| 134 | buffer.clear(); |
| 135 | fc.read(buffer); |
| 136 | buffer.flip(); |
| 137 | |
| 138 | /* |
| 139 | * Check the magic number,to make sure we're opening the right type of |
| 140 | * file |
| 141 | */ |
| 142 | res = buffer.getInt(); |
| 143 | if (res != HISTORY_FILE_MAGIC_NUMBER) { |
| 144 | fc.close(); |
| 145 | fis.close(); |
| 146 | throw new IOException("Selected file does not" + //$NON-NLS-1$ |
| 147 | "look like a History Tree file"); //$NON-NLS-1$ |
| 148 | } |
| 149 | |
| 150 | res = buffer.getInt(); /* Major version number */ |
| 151 | if (res != MAJOR_VERSION) { |
| 152 | fc.close(); |
| 153 | fis.close(); |
| 154 | throw new IOException("Select History Tree file is of an older " //$NON-NLS-1$ |
| 155 | + "format. Please use a previous version of " //$NON-NLS-1$ |
| 156 | + "the parser to open it."); //$NON-NLS-1$ |
| 157 | } |
| 158 | |
| 159 | res = buffer.getInt(); /* Minor version number */ |
| 160 | |
| 161 | bs = buffer.getInt(); /* Block Size */ |
| 162 | maxc = buffer.getInt(); /* Max nb of children per node */ |
| 163 | |
| 164 | this.nodeCount = buffer.getInt(); |
| 165 | rootNodeSeqNb = buffer.getInt(); |
| 166 | startTime = buffer.getLong(); |
| 167 | |
| 168 | this.config = new HTConfig(existingStateFile, bs, maxc, startTime); |
| 169 | fc.close(); |
| 170 | fis.close(); |
| 171 | /* |
| 172 | * FIXME We close fis here and the TreeIO will then reopen the same |
| 173 | * file, not extremely elegant. But how to pass the information here to |
| 174 | * the SHT otherwise? |
| 175 | */ |
| 176 | this.treeIO = new HT_IO(this, false); |
| 177 | |
| 178 | rebuildLatestBranch(rootNodeSeqNb); |
| 179 | this.treeEnd = latestBranch.firstElement().getNodeEnd(); |
| 180 | |
| 181 | /* |
| 182 | * Make sure the history start time we read previously is consistent |
| 183 | * with was is actually in the root node. |
| 184 | */ |
| 185 | if (startTime != latestBranch.firstElement().getNodeStart()) { |
| 186 | fc.close(); |
| 187 | fis.close(); |
| 188 | throw new IOException("Inconsistent start times in the" + //$NON-NLS-1$ |
| 189 | "history file, it might be corrupted."); //$NON-NLS-1$ |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * "Save" the tree to disk. This method will cause the treeIO object to |
| 195 | * commit all nodes to disk and then return the RandomAccessFile descriptor |
| 196 | * so the Tree object can save its configuration into the header of the |
| 197 | * file. |
| 198 | * |
| 199 | * @param requestedEndTime |
| 200 | */ |
| 201 | void closeTree(long requestedEndTime) { |
| 202 | FileChannel fc; |
| 203 | ByteBuffer buffer; |
| 204 | int i, res; |
| 205 | |
| 206 | /* |
| 207 | * Work-around the "empty branches" that get created when the root node |
| 208 | * becomes full. Overwrite the tree's end time with the original wanted |
| 209 | * end-time, to ensure no queries are sent into those empty nodes. |
| 210 | * |
| 211 | * This won't be needed once extended nodes are implemented. |
| 212 | */ |
| 213 | this.treeEnd = requestedEndTime; |
| 214 | |
| 215 | /* Close off the latest branch of the tree */ |
| 216 | for (i = 0; i < latestBranch.size(); i++) { |
| 217 | latestBranch.get(i).closeThisNode(treeEnd); |
| 218 | treeIO.writeNode(latestBranch.get(i)); |
| 219 | } |
| 220 | |
| 221 | /* Only use this for debugging purposes, it's VERY slow! */ |
| 222 | // this.checkIntegrity(); |
| 223 | |
| 224 | fc = treeIO.getFcOut(); |
| 225 | buffer = ByteBuffer.allocate(getTreeHeaderSize()); |
| 226 | buffer.order(ByteOrder.LITTLE_ENDIAN); |
| 227 | buffer.clear(); |
| 228 | |
| 229 | /* Save the config of the tree to the header of the file */ |
| 230 | try { |
| 231 | fc.position(0); |
| 232 | |
| 233 | buffer.putInt(HISTORY_FILE_MAGIC_NUMBER); |
| 234 | |
| 235 | buffer.putInt(MAJOR_VERSION); |
| 236 | buffer.putInt(MINOR_VERSION); |
| 237 | |
| 238 | buffer.putInt(config.blockSize); |
| 239 | buffer.putInt(config.maxChildren); |
| 240 | |
| 241 | buffer.putInt(nodeCount); |
| 242 | |
| 243 | /* root node seq. nb */ |
| 244 | buffer.putInt(latestBranch.firstElement().getSequenceNumber()); |
| 245 | |
| 246 | /* start time of this history */ |
| 247 | buffer.putLong(latestBranch.firstElement().getNodeStart()); |
| 248 | |
| 249 | buffer.flip(); |
| 250 | res = fc.write(buffer); |
| 251 | assert (res <= getTreeHeaderSize()); |
| 252 | /* done writing the file header */ |
| 253 | |
| 254 | } catch (IOException e) { |
| 255 | /* We should not have any problems at this point... */ |
| 256 | e.printStackTrace(); |
| 257 | } finally { |
| 258 | try { |
| 259 | fc.close(); |
| 260 | } catch (IOException e) { |
| 261 | e.printStackTrace(); |
| 262 | } |
| 263 | } |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @name Accessors |
| 269 | */ |
| 270 | |
| 271 | long getTreeStart() { |
| 272 | return config.treeStart; |
| 273 | } |
| 274 | |
| 275 | long getTreeEnd() { |
| 276 | return treeEnd; |
| 277 | } |
| 278 | |
| 279 | int getNodeCount() { |
| 280 | return nodeCount; |
| 281 | } |
| 282 | |
| 283 | HT_IO getTreeIO() { |
| 284 | return treeIO; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Rebuild the latestBranch "cache" object by reading the nodes from disk |
| 289 | * (When we are opening an existing file on disk and want to append to it, |
| 290 | * for example). |
| 291 | * |
| 292 | * @param rootNodeSeqNb |
| 293 | * The sequence number of the root node, so we know where to |
| 294 | * start |
| 295 | * @throws ClosedChannelException |
| 296 | */ |
| 297 | private void rebuildLatestBranch(int rootNodeSeqNb) throws ClosedChannelException { |
| 298 | HTNode nextChildNode; |
| 299 | |
| 300 | this.latestBranch = new Vector<CoreNode>(); |
| 301 | |
| 302 | nextChildNode = treeIO.readNodeFromDisk(rootNodeSeqNb); |
| 303 | latestBranch.add((CoreNode) nextChildNode); |
| 304 | while (latestBranch.lastElement().getNbChildren() > 0) { |
| 305 | nextChildNode = treeIO.readNodeFromDisk(latestBranch.lastElement().getLatestChild()); |
| 306 | latestBranch.add((CoreNode) nextChildNode); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Insert an interval in the tree |
| 312 | * |
| 313 | * @param interval |
| 314 | */ |
| 315 | void insertInterval(HTInterval interval) throws TimeRangeException { |
| 316 | if (interval.getStartTime() < config.treeStart) { |
| 317 | throw new TimeRangeException(); |
| 318 | } |
| 319 | tryInsertAtNode(interval, latestBranch.size() - 1); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Inner method to find in which node we should add the interval. |
| 324 | * |
| 325 | * @param interval |
| 326 | * The interval to add to the tree |
| 327 | * @param indexOfNode |
| 328 | * The index *in the latestBranch* where we are trying the |
| 329 | * insertion |
| 330 | */ |
| 331 | private void tryInsertAtNode(HTInterval interval, int indexOfNode) { |
| 332 | HTNode targetNode = latestBranch.get(indexOfNode); |
| 333 | |
| 334 | /* Verify if there is enough room in this node to store this interval */ |
| 335 | if (interval.getIntervalSize() > targetNode.getNodeFreeSpace()) { |
| 336 | /* Nope, not enough room. Insert in a new sibling instead. */ |
| 337 | addSiblingNode(indexOfNode); |
| 338 | tryInsertAtNode(interval, latestBranch.size() - 1); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | /* Make sure the interval time range fits this node */ |
| 343 | if (interval.getStartTime() < targetNode.getNodeStart()) { |
| 344 | /* |
| 345 | * No, this interval starts before the startTime of this node. We |
| 346 | * need to check recursively in parents if it can fit. |
| 347 | */ |
| 348 | assert (indexOfNode >= 1); |
| 349 | tryInsertAtNode(interval, indexOfNode - 1); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Ok, there is room, and the interval fits in this time slot. Let's add |
| 355 | * it. |
| 356 | */ |
| 357 | targetNode.addInterval(interval); |
| 358 | |
| 359 | /* Update treeEnd if needed */ |
| 360 | if (interval.getEndTime() > this.treeEnd) { |
| 361 | this.treeEnd = interval.getEndTime(); |
| 362 | } |
| 363 | return; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Method to add a sibling to any node in the latest branch. This will add |
| 368 | * children back down to the leaf level, if needed. |
| 369 | * |
| 370 | * @param indexOfNode |
| 371 | * The index in latestBranch where we start adding |
| 372 | */ |
| 373 | private void addSiblingNode(int indexOfNode) { |
| 374 | int i; |
| 375 | CoreNode newNode, prevNode; |
| 376 | long splitTime = treeEnd; |
| 377 | |
| 378 | assert (indexOfNode < latestBranch.size()); |
| 379 | |
| 380 | /* Check if we need to add a new root node */ |
| 381 | if (indexOfNode == 0) { |
| 382 | addNewRootNode(); |
| 383 | return; |
| 384 | } |
| 385 | |
| 386 | /* Check if we can indeed add a child to the target parent */ |
| 387 | if (latestBranch.get(indexOfNode - 1).getNbChildren() == config.maxChildren) { |
| 388 | /* If not, add a branch starting one level higher instead */ |
| 389 | addSiblingNode(indexOfNode - 1); |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | /* Split off the new branch from the old one */ |
| 394 | for (i = indexOfNode; i < latestBranch.size(); i++) { |
| 395 | latestBranch.get(i).closeThisNode(splitTime); |
| 396 | treeIO.writeNode(latestBranch.get(i)); |
| 397 | |
| 398 | prevNode = latestBranch.get(i - 1); |
| 399 | newNode = initNewCoreNode(prevNode.getSequenceNumber(), |
| 400 | splitTime + 1); |
| 401 | prevNode.linkNewChild(newNode); |
| 402 | |
| 403 | latestBranch.set(i, newNode); |
| 404 | } |
| 405 | return; |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Similar to the previous method, except here we rebuild a completely new |
| 410 | * latestBranch |
| 411 | */ |
| 412 | private void addNewRootNode() { |
| 413 | int i, depth; |
| 414 | CoreNode oldRootNode, newRootNode, newNode, prevNode; |
| 415 | long splitTime = this.treeEnd; |
| 416 | |
| 417 | oldRootNode = latestBranch.firstElement(); |
| 418 | newRootNode = initNewCoreNode(-1, config.treeStart); |
| 419 | |
| 420 | /* Tell the old root node that it isn't root anymore */ |
| 421 | oldRootNode.setParentSequenceNumber(newRootNode.getSequenceNumber()); |
| 422 | |
| 423 | /* Close off the whole current latestBranch */ |
| 424 | for (i = 0; i < latestBranch.size(); i++) { |
| 425 | latestBranch.get(i).closeThisNode(splitTime); |
| 426 | treeIO.writeNode(latestBranch.get(i)); |
| 427 | } |
| 428 | |
| 429 | /* Link the new root to its first child (the previous root node) */ |
| 430 | newRootNode.linkNewChild(oldRootNode); |
| 431 | |
| 432 | /* Rebuild a new latestBranch */ |
| 433 | depth = latestBranch.size(); |
| 434 | latestBranch = new Vector<CoreNode>(); |
| 435 | latestBranch.add(newRootNode); |
| 436 | for (i = 1; i < depth + 1; i++) { |
| 437 | prevNode = latestBranch.get(i - 1); |
| 438 | newNode = initNewCoreNode(prevNode.getParentSequenceNumber(), |
| 439 | splitTime + 1); |
| 440 | prevNode.linkNewChild(newNode); |
| 441 | latestBranch.add(newNode); |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Add a new empty node to the tree. |
| 447 | * |
| 448 | * @param parentSeqNumber |
| 449 | * Sequence number of this node's parent |
| 450 | * @param startTime |
| 451 | * Start time of the new node |
| 452 | * @return The newly created node |
| 453 | */ |
| 454 | private CoreNode initNewCoreNode(int parentSeqNumber, long startTime) { |
| 455 | CoreNode newNode = new CoreNode(this, this.nodeCount, parentSeqNumber, |
| 456 | startTime); |
| 457 | this.nodeCount++; |
| 458 | |
| 459 | /* Update the treeEnd if needed */ |
| 460 | if (startTime >= this.treeEnd) { |
| 461 | this.treeEnd = startTime + 1; |
| 462 | } |
| 463 | return newNode; |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Inner method to select the next child of the current node intersecting |
| 468 | * the given timestamp. Useful for moving down the tree following one |
| 469 | * branch. |
| 470 | * |
| 471 | * @param currentNode |
| 472 | * @param t |
| 473 | * @return The child node intersecting t |
| 474 | * @throws ClosedChannelException |
| 475 | * If the file channel was closed while we were reading the tree |
| 476 | */ |
| 477 | HTNode selectNextChild(CoreNode currentNode, long t) throws ClosedChannelException { |
| 478 | assert (currentNode.getNbChildren() > 0); |
| 479 | int potentialNextSeqNb = currentNode.getSequenceNumber(); |
| 480 | |
| 481 | for (int i = 0; i < currentNode.getNbChildren(); i++) { |
| 482 | if (t >= currentNode.getChildStart(i)) { |
| 483 | potentialNextSeqNb = currentNode.getChild(i); |
| 484 | } else { |
| 485 | break; |
| 486 | } |
| 487 | } |
| 488 | /* |
| 489 | * Once we exit this loop, we should have found a children to follow. If |
| 490 | * we didn't, there's a problem. |
| 491 | */ |
| 492 | assert (potentialNextSeqNb != currentNode.getSequenceNumber()); |
| 493 | |
| 494 | /* |
| 495 | * Since this code path is quite performance-critical, avoid iterating |
| 496 | * through the whole latestBranch array if we know for sure the next |
| 497 | * node has to be on disk |
| 498 | */ |
| 499 | if (currentNode.isDone()) { |
| 500 | return treeIO.readNodeFromDisk(potentialNextSeqNb); |
| 501 | } |
| 502 | return treeIO.readNode(potentialNextSeqNb); |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * Helper function to get the size of the "tree header" in the tree-file The |
| 507 | * nodes will use this offset to know where they should be in the file. This |
| 508 | * should always be a multiple of 4K. |
| 509 | */ |
| 510 | static int getTreeHeaderSize() { |
| 511 | return 4096; |
| 512 | } |
| 513 | |
| 514 | long getFileSize() { |
| 515 | return config.stateFile.length(); |
| 516 | } |
| 517 | |
| 518 | // ------------------------------------------------------------------------ |
| 519 | // Test/debugging methods |
| 520 | // ------------------------------------------------------------------------ |
| 521 | |
| 522 | /* Only used for debugging, shouldn't be externalized */ |
| 523 | @SuppressWarnings("nls") |
| 524 | boolean checkNodeIntegrity(HTNode zenode) { |
| 525 | |
| 526 | HTNode otherNode; |
| 527 | CoreNode node; |
| 528 | StringBuffer buf = new StringBuffer(); |
| 529 | boolean ret = true; |
| 530 | |
| 531 | // FIXME /* Only testing Core Nodes for now */ |
| 532 | if (!(zenode instanceof CoreNode)) { |
| 533 | return true; |
| 534 | } |
| 535 | |
| 536 | node = (CoreNode) zenode; |
| 537 | |
| 538 | try { |
| 539 | /* |
| 540 | * Test that this node's start and end times match the start of the |
| 541 | * first child and the end of the last child, respectively |
| 542 | */ |
| 543 | if (node.getNbChildren() > 0) { |
| 544 | otherNode = treeIO.readNode(node.getChild(0)); |
| 545 | if (node.getNodeStart() != otherNode.getNodeStart()) { |
| 546 | buf.append("Start time of node (" + node.getNodeStart() + ") " |
| 547 | + "does not match start time of first child " + "(" |
| 548 | + otherNode.getNodeStart() + "), " + "node #" |
| 549 | + otherNode.getSequenceNumber() + ")\n"); |
| 550 | ret = false; |
| 551 | } |
| 552 | if (node.isDone()) { |
| 553 | otherNode = treeIO.readNode(node.getLatestChild()); |
| 554 | if (node.getNodeEnd() != otherNode.getNodeEnd()) { |
| 555 | buf.append("End time of node (" + node.getNodeEnd() |
| 556 | + ") does not match end time of last child (" |
| 557 | + otherNode.getNodeEnd() + ", node #" |
| 558 | + otherNode.getSequenceNumber() + ")\n"); |
| 559 | ret = false; |
| 560 | } |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Test that the childStartTimes[] array matches the real nodes' start |
| 566 | * times |
| 567 | */ |
| 568 | for (int i = 0; i < node.getNbChildren(); i++) { |
| 569 | otherNode = treeIO.readNode(node.getChild(i)); |
| 570 | if (otherNode.getNodeStart() != node.getChildStart(i)) { |
| 571 | buf.append(" Expected start time of child node #" |
| 572 | + node.getChild(i) + ": " + node.getChildStart(i) |
| 573 | + "\n" + " Actual start time of node #" |
| 574 | + otherNode.getSequenceNumber() + ": " |
| 575 | + otherNode.getNodeStart() + "\n"); |
| 576 | ret = false; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | } catch (ClosedChannelException e) { |
| 581 | e.printStackTrace(); |
| 582 | } |
| 583 | |
| 584 | if (!ret) { |
| 585 | System.out.println(""); |
| 586 | System.out.println("SHT: Integrity check failed for node #" |
| 587 | + node.getSequenceNumber() + ":"); |
| 588 | System.out.println(buf.toString()); |
| 589 | } |
| 590 | return ret; |
| 591 | } |
| 592 | |
| 593 | void checkIntegrity() { |
| 594 | try { |
| 595 | for (int i = 0; i < nodeCount; i++) { |
| 596 | checkNodeIntegrity(treeIO.readNode(i)); |
| 597 | } |
| 598 | } catch (ClosedChannelException e) { |
| 599 | e.printStackTrace(); |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /* Only used for debugging, shouldn't be externalized */ |
| 604 | @SuppressWarnings("nls") |
| 605 | @Override |
| 606 | public String toString() { |
| 607 | return "Information on the current tree:\n\n" + "Blocksize: " |
| 608 | + config.blockSize + "\n" + "Max nb. of children per node: " |
| 609 | + config.maxChildren + "\n" + "Number of nodes: " + nodeCount |
| 610 | + "\n" + "Depth of the tree: " + latestBranch.size() + "\n" |
| 611 | + "Size of the treefile: " + this.getFileSize() + "\n" |
| 612 | + "Root node has sequence number: " |
| 613 | + latestBranch.firstElement().getSequenceNumber() + "\n" |
| 614 | + "'Latest leaf' has sequence number: " |
| 615 | + latestBranch.lastElement().getSequenceNumber(); |
| 616 | } |
| 617 | |
| 618 | private int curDepth; |
| 619 | |
| 620 | /** |
| 621 | * Start at currentNode and print the contents of all its children, in |
| 622 | * pre-order. Give the root node in parameter to visit the whole tree, and |
| 623 | * have a nice overview. |
| 624 | */ |
| 625 | @SuppressWarnings("nls") |
| 626 | private void preOrderPrint(PrintWriter writer, boolean printIntervals, |
| 627 | CoreNode currentNode) { |
| 628 | /* Only used for debugging, shouldn't be externalized */ |
| 629 | int i, j; |
| 630 | HTNode nextNode; |
| 631 | |
| 632 | writer.println(currentNode.toString()); |
| 633 | if (printIntervals) { |
| 634 | currentNode.debugPrintIntervals(writer); |
| 635 | } |
| 636 | curDepth++; |
| 637 | |
| 638 | try { |
| 639 | for (i = 0; i < currentNode.getNbChildren(); i++) { |
| 640 | nextNode = treeIO.readNode(currentNode.getChild(i)); |
| 641 | assert (nextNode instanceof CoreNode); // TODO temporary |
| 642 | for (j = 0; j < curDepth - 1; j++) { |
| 643 | writer.print(" "); |
| 644 | } |
| 645 | writer.print("+-"); |
| 646 | preOrderPrint(writer, printIntervals, (CoreNode) nextNode); |
| 647 | } |
| 648 | } catch (ClosedChannelException e) { |
| 649 | e.printStackTrace(); |
| 650 | } |
| 651 | curDepth--; |
| 652 | return; |
| 653 | } |
| 654 | |
| 655 | /** |
| 656 | * Print out the full tree for debugging purposes |
| 657 | * |
| 658 | * @param writer |
| 659 | * PrintWriter in which to write the output |
| 660 | * @param printIntervals |
| 661 | * Says if you want to output the full interval information |
| 662 | */ |
| 663 | void debugPrintFullTree(PrintWriter writer, boolean printIntervals) { |
| 664 | /* Only used for debugging, shouldn't be externalized */ |
| 665 | curDepth = 0; |
| 666 | this.preOrderPrint(writer, false, latestBranch.firstElement()); |
| 667 | |
| 668 | if (printIntervals) { |
| 669 | writer.println("\nDetails of intervals:"); //$NON-NLS-1$ |
| 670 | curDepth = 0; |
| 671 | this.preOrderPrint(writer, true, latestBranch.firstElement()); |
| 672 | } |
| 673 | writer.println('\n'); |
| 674 | } |
| 675 | |
| 676 | } |