int debugBTree()

in hfs/btree.c [606:674]


int debugBTree(BTree* tree, int displayTree) {
  unsigned char* map;
  uint32_t *heightTable;
  BTKey* retFirstKey;
  BTKey* retLastKey;
  
  uint32_t numMapNodes;
  uint32_t traverseCount;
  uint32_t linearCount;
  uint32_t errorCount;
  
  uint8_t i;
  
  errorCount = 0;
  
  printf("Mapping nodes...\n"); fflush(stdout);
  map = mapNodes(tree, &numMapNodes, &errorCount);
  
  printf("Initializing height table...\n"); fflush(stdout);
  heightTable = (uint32_t*) malloc(sizeof(uint32_t) * (tree->headerRec->treeDepth + 1));
  for(i = 0; i <= tree->headerRec->treeDepth; i++) {
    heightTable[i] = 0;
  }

  if(tree->headerRec->rootNode == 0) {
    if(tree->headerRec->firstLeafNode == 0 && tree->headerRec->lastLeafNode == 0) {
      traverseCount = 0;
      linearCount = 0;
    } else {
      printf("BTREE CONSISTENCY ERROR: First leaf node (%d) and last leaf node (%d) inconsistent with empty BTree\n",
                tree->headerRec->firstLeafNode, tree->headerRec->lastLeafNode);
      
      // Try to see if we can get a linear count
      if(tree->headerRec->firstLeafNode != 0)
        heightTable[1] = tree->headerRec->firstLeafNode;
      else
        heightTable[1] = tree->headerRec->lastLeafNode;
        
      linearCount = linearCheck(heightTable, map, tree, &errorCount);
    }
  } else {
    printf("Performing tree traversal...\n"); fflush(stdout);
    traverseCount = traverseNode(tree->headerRec->rootNode, tree, map, 0, &retFirstKey, &retLastKey, heightTable, &errorCount, displayTree);

    free(retFirstKey);
    free(retLastKey);

    printf("Performing linear traversal...\n"); fflush(stdout);
    linearCount = linearCheck(heightTable, map, tree, &errorCount);
  }
  
  printf("Total traverse nodes: %d\n", traverseCount); fflush(stdout);
  printf("Total linear nodes: %d\n", linearCount); fflush(stdout);
  printf("Error count: %d\n", errorCount); fflush(stdout);

  if(traverseCount != linearCount) {
    printf("BTREE CONSISTENCY ERROR: Linear count and traverse count are inconsistent\n");
  }
  
  if(traverseCount != (tree->headerRec->totalNodes - tree->headerRec->freeNodes - numMapNodes - 1)) {
    printf("BTREE CONSISTENCY ERROR: Free nodes and total nodes (%d) and traverse count are inconsistent\n",
      tree->headerRec->totalNodes - tree->headerRec->freeNodes);
  }

  free(heightTable);
  free(map); 
  
  return errorCount;
}