/* 
   J. Dean Brock, 1998

   Written for UNCA CSCI 343

   Include file for the B+-tree

*/


#ifndef _BTREE_H_
#define _BTREE_H_

#define RECINBLK 7

#define KEYSIZE 12

typedef struct simpRec {
   char     name[KEYSIZE] ;
   int      value ;	/* MUST be stored in little endian order */
} simpRecT ;

/* used for the first "record" in the block */
typedef struct headRec {
  int reserved1 ;
  int reserved2 ;
  unsigned char type ;
  unsigned char numUsed ;
  unsigned char reserved3 ;
  unsigned char reserved4 ;
  int value ;
} headRecT ;

/* Used to record the types of blocks */
#define BLKINTERIOR 0x01
#define BLKLEAF     0x02

/* invariant for the data structure 
 *
 *   There are X.head.numUsed elements of X.data that are in use
 *   These X.head.numUsed elements sorted by the X.data[*}.name field
 *   If the BLKINTERIOR bit of X.head.type is set,
 *      the block corresponds to an interior node
 *      X.data[i].value points to a subtree that contains all database
 *          names greater than X.data[i-1].name (if i>0) and
 *          less than or equal to X.data[i].name
 *      X.head.value points to a subtree that contains all database
 *          values greater than X.data[X.head.numUsed-1].name
 *   If the BLKINTERIOR bit is *not* set,
 *      the block is a leaf node
 *      X.data[i].value is the value of the database elements
 *          with key X.data[i].name
 *      X.data.value points to the next leaf node 
 */

typedef struct blockRec {
  headRecT head ;
  simpRecT data[RECINBLK] ;
} blockRecT ;

/* prototypes */

/* btupdate.c routines */
int btreeUpdate(FILE *, simpRecT *, simpRecT *) ;

/* btuperly.c routines */
int btreeUpdate_early(FILE *, blockRecT *, simpRecT *, simpRecT *) ;

/* btio.c routines */
int addBlock(FILE *, blockRecT *) ;

int updateBlock(FILE *, int, blockRecT *) ;

int readBlock(FILE *, int, blockRecT *) ;

/* btmisc.c routines */

void findLEpos(blockRecT *, simpRecT *,	int *, int *) ;

void findSubLoc(blockRecT *, simpRecT *, int *, int *) ;

void splitBlock(blockRecT *, simpRecT *, int, blockRecT *, blockRecT *) ;

int badBlock(blockRecT *) ;

#endif
