/* 
   J. Dean Brock, 1998

   Written for UNCA CSCI 343

   "Interior" routines for updating the B+-tree

*/

#include <stdio.h>
#include <string.h>

#include "btree.h"

int btreeUpdate_int(
		    FILE      *dbFile,		/* B+-tree file */
		    int        prsBlkNum,	/* block number to read */
		    simpRecT  *oldRec,		/* buffer for present record */
		    simpRecT  *newRec,		/* record to be added */
		    char      *splitName,	/* if split,
                                                   value for first block */
		    int       *splitBlkNum	/* if split,
						   address for 2nd block */
		   )
			/* Returns	-1, on failure
			 * 		 0, if record not already in btree
			 *			inserted without a split
			 *		 1, if record already in btree
			 *		 2, if record not already btree
			 *			insertion requires a split
			 */

{
  blockRecT prsBlk, prsBlkNew, nxtBlk ;
  simpRecT newIntRec ;
  int childPos, childAddr, nxtBlkNum, mySplitBlkNum, found, rc ;
  char mySplitName[KEYSIZE] ;

  if (readBlock(dbFile, prsBlkNum, &prsBlk))
    return(-1) ;

  if (badBlock(&prsBlk))
    return(-1) ;

  if (prsBlk.head.type == BLKLEAF) {
    findLEpos(&prsBlk, newRec, &childPos, &found) ;
    if (found) {
      /* replacing an existing value */
      *oldRec = prsBlk.data[childPos] ;
      prsBlk.data[childPos] = *newRec ;
      if (updateBlock(dbFile, prsBlkNum, &prsBlk))
	return -1 ;
      return 1 ;
    } else if (prsBlk.head.numUsed < RECINBLK) {
      /* adding a new value to this LEAF */
      insertRec(&prsBlk, newRec, childPos) ;
      if (updateBlock(dbFile, prsBlkNum, &prsBlk))
	return -1 ;
      return 0 ;
    } else {
      /* splitting a leaf */
      splitBlock(&prsBlk, newRec, childPos, &prsBlkNew, &nxtBlk) ;
      nxtBlk.head.value = prsBlk.head.value ;
      if ((*splitBlkNum=addBlock(dbFile, &nxtBlk)) == -1)
	return -1 ;
      prsBlkNew.head.value = *splitBlkNum ;
      if (updateBlock(dbFile, prsBlkNum, &prsBlkNew))
	return -1 ;
      (void) strncpy(splitName,
		     prsBlkNew.data[prsBlkNew.head.numUsed-1].name,
		     KEYSIZE) ;
      return 2 ;
    }
  } else {
    /* this is an interior node */
    findSubLoc(&prsBlk, newRec, &childPos, &childAddr) ;
    rc = btreeUpdate_int(dbFile, childAddr,
			 oldRec, newRec,
			 &mySplitName[0], &mySplitBlkNum) ;
    if (rc < 2)
      return(rc) ;
    else if ( prsBlk.head.numUsed < RECINBLK) {
      /* The child has split -- but we got room */
      if (childPos < prsBlk.head.numUsed) {
	(void) strncpy(newIntRec.name,
		       prsBlk.data[childPos].name,
		       KEYSIZE) ;
	newIntRec.value = mySplitBlkNum ;
	(void) strncpy(prsBlk.data[childPos].name,
		       &mySplitName[0],
		       KEYSIZE) ;
	insertRec(&prsBlk, &newIntRec, childPos+1) ;
      } else {
	(void) strncpy(prsBlk.data[childPos].name,
		       &mySplitName[0],
		       KEYSIZE) ;
	prsBlk.data[childPos].value = prsBlk.head.value ;
	prsBlk.head.value = mySplitBlkNum ;
	++prsBlk.head.numUsed ;
      }
      if (updateBlock(dbFile, prsBlkNum, &prsBlk))
	return -1 ;
      return  0 ;
    } else {
      if (childPos < prsBlk.head.numUsed) {
	(void) strncpy(newIntRec.name,
		       prsBlk.data[childPos].name,
		       KEYSIZE) ;
	newIntRec.value = mySplitBlkNum ;
	(void) strncpy(prsBlk.data[childPos].name,
		       &mySplitName[0],
		       KEYSIZE) ;
	splitBlock(&prsBlk, &newIntRec, childPos+1, &prsBlkNew, &nxtBlk) ;
      } else {
	/* adding in at end of record -- messy */
	(void) strncpy(newIntRec.name,
		       &mySplitName[0],
		       KEYSIZE) ;
	newIntRec.value = prsBlk.head.value ;
	splitBlock(&prsBlk, &newIntRec, childPos, &prsBlkNew, &nxtBlk) ;
	nxtBlk.head.value = mySplitBlkNum ;
      }
      --prsBlkNew.head.numUsed ;
      prsBlkNew.head.value = prsBlkNew.data[prsBlkNew.head.numUsed].value ;
      (void) strncpy(splitName,
		     &prsBlkNew.data[prsBlkNew.head.numUsed].name[0],
		     KEYSIZE) ;
      if ((*splitBlkNum=addBlock(dbFile, &nxtBlk)) == -1)
	return -1 ;
      if (updateBlock(dbFile, prsBlkNum, &prsBlkNew))
	return -1 ;
      return 2 ;
    }
  }
}

int btreeUpdate(FILE *dbFile,	/* pointer to open btree file */
		simpRecT *o,	/* buffer for old record, if updated */
		simpRecT *n)	/* pointer to new record */
			/* Returns	-1, on failure
			 * 		 0, if record not already in btree
			 *		 1, if record already in btree
			 *			set 2nd parameter in this case
			 */

{
  blockRecT root ;
  int childPos, childAddr, rc, mySplitBlkNum ;
  int firstNodePos, posBlk1, posBlk2 ;
  char mySplitName[KEYSIZE] ;
  blockRecT firstNode, newRoot, newBlk1, newBlk2 ;
  simpRecT newIntRec ;

  if (readBlock(dbFile, 0, &root))
    return(-1) ;

  if (badBlock(&root))
    return(-1) ;

  if (root.head.type != BLKINTERIOR ) {
    /* being very careful */
    (void) fputs("BLKINTERIOR not set in root!\n", stderr) ;    
    return(-1) ;
  }

  if (root.head.value == 0) {
    /* Adding the first record !!! */
    firstNode.head.type = BLKLEAF ;
    firstNode.head.numUsed = 1 ;
    firstNode.head.value = 0 ;
    firstNode.data[0] = *n ;
    if ((firstNodePos = addBlock(dbFile, &firstNode)) == -1)
      return(-1) ;
    root.head.numUsed = 0 ;
    root.head.value = firstNodePos ;	/* better by 1 */
    if (updateBlock(dbFile, 0, &root))
      return(-1) ;
    else
      return(0) ;
  } else {
    /* there is a record! */

    if (root.head.numUsed)
      findSubLoc(&root, n, &childPos, &childAddr) ;
    else {
      /* a very, very special case */
      childPos  = 0 ;
      childAddr = root.head.value ;
    }
    rc = btreeUpdate_int(dbFile, childAddr, o, n,
			 &mySplitName[0], &mySplitBlkNum) ;
    if (rc < 2)
      return(rc) ;
    else if ( root.head.numUsed < RECINBLK) {
      /* The child has split -- but we got room */
      if (childPos < root.head.numUsed) {
	(void) strncpy(newIntRec.name,
		       root.data[childPos].name,
		       KEYSIZE) ;
	newIntRec.value = mySplitBlkNum ;
	(void) strncpy(root.data[childPos].name,
		       &mySplitName[0], 
		       KEYSIZE) ;
	insertRec(&root, &newIntRec, childPos+1) ;
      } else {
	(void) strncpy(root.data[childPos].name,
		       &mySplitName[0],
		       KEYSIZE) ;
	root.data[childPos].value = root.head.value ;
	root.head.value = mySplitBlkNum ;
	++root.head.numUsed ;
      }
	
      if (updateBlock(dbFile, 0, &root))
	return -1 ;
      return  0 ;
    } else {
      if (childPos < root.head.numUsed) {
	(void) strncpy(newIntRec.name,
		       root.data[childPos].name,
		       KEYSIZE) ;
	newIntRec.value = mySplitBlkNum ;
	(void) strncpy(root.data[childPos].name,
		       &mySplitName[0],
		       KEYSIZE) ;
	splitBlock(&root, &newIntRec, childPos+1, &newBlk1, &newBlk2) ;
      } else {
	/* adding in at end of record -- messy */
	(void) strncpy(newIntRec.name,
		       &mySplitName[0],
		       KEYSIZE) ;
	newIntRec.value = root.head.value ;
	splitBlock(&root, &newIntRec, childPos, &newBlk1, &newBlk2) ;
	newBlk2.head.value = mySplitBlkNum ;
      }
      --newBlk1.head.numUsed ;
      newBlk1.head.value = newBlk1.data[newBlk1.head.numUsed].value ;
      if ((posBlk1=addBlock(dbFile, &newBlk1)) == -1)
	return -1 ;
      if ((posBlk2=addBlock(dbFile, &newBlk2)) == -1)
	return -1 ;
      newRoot.head.type = BLKINTERIOR ;
      newRoot.head.numUsed = 1 ;
      (void) strncpy(&newRoot.data[0].name[0],
		     &newBlk1.data[newBlk1.head.numUsed].name[0],
		     KEYSIZE) ;
      newRoot.data[0].value = posBlk1 ;
      newRoot.head.value = posBlk2 ;
      if (updateBlock(dbFile, 0, &newRoot))
	return -1 ;
      return  0 ;
    }
  }

}
