B+ TREE HEADER FILE ------------------- #ifndef _BTFILE_H #define _BTFILE_H /* * This is the main definition of class BTreeFile, which derives from * abstract base class IndexFile. * * class BTreeFileScan is defined in btreefilescan.h; this class provides * a search/iterate interface to a BTreeFile. */ #define BT_TRACE /* When #defined, BT_TRACE causes a structured trace to be written to the ostream pointed to by the class variable BTreeFile::Trace. This output is used to drive a visualization tool that shows the inner workings of the b-tree during its operations. You must set BTreeFile::Trace to the ostream of your choice before creating your BTreeFile. */ #include "btindex_page.h" #include "btleaf_page.h" #include "index.h" #include "btreefilescan.h" #include "bt.h" #define NAIVE_DELETE 0 #define FULL_DELETE 1 class BTreeFile: public IndexFile { friend class BTreeFileScan; friend class BTreeTest; private: /* * Structure of a B+ tree index header page. There is quite a bit * of wasted space here... */ public: enum ErrorTypes { // our interface for the erroneous error protocol. _OK = 0, // 0 is always OK CANT_FIND_HEADER, // tried to open index but db said no header CANT_PIN_HEADER, // buffer manager failed to pin header page CANT_ALLOC_HEADER, // failed to allocate block for header page CANT_ADD_FILE_ENTRY, // couldn't register new index file w/ db CANT_UNPIN_HEADER, // can't unpin header page CANT_PIN_PAGE, // can't pin some index/leaf page CANT_UNPIN_PAGE, // can't unpin some index/leaf page INVALID_SCAN, // attempt to use bad Scan object DELETE_CURRENT_FAILED, // failed to delete current rid in scan CANT_DELETE_FILE_ENTRY, // db failed to delete file entry CANT_FREE_PAGE, // buffer manager failed to free a page CANT_DELETE_SUBTREE, // _destroyFile failed on a subtree KEY_TOO_LONG, // the key given in BTreeFile::insert is too long INSERT_FAILED, // BTreeFile::insert not successful COULD_NOT_CREATE_ROOT, // BtreeFile::insert could not create new root DELETE_DATAENTRY_FAILED,// could not delete a data entry DATA_ENTRY_NOT_FOUND, // could not find data entry to delete CANT_GET_PAGE_NO, // get_page_no on BTIndexPage failed CANT_ALLOCATE_NEW_PAGE, // bm::newPage failed CANT_SPLIT_LEAF_PAGE, // could not split leaf page CANT_SPLIT_INDEX_PAGE, // could not split index page NR_ERRORS // and this is the number of them }; static const char *Errors[NR_ERRORS]; // an index with given filename should already exist; this opens it. BTreeFile(Status& status, const char *filename); // if index exists, open it; else create it. BTreeFile(Status& status, const char *filename, const AttrType keytype, const int keysize, int delete_fashion = FULL_DELETE); // closes index ~BTreeFile(); // destroy entire index file Status destroyFile(); // insert recid with the key key Status insert(const void *key, const RID rid); // delete leaf entry recid given its key // (`rid' is IN the data entry; it is not the id of the data entry) Status Delete(const void *key, const RID rid); // create a scan with given keys // Cases: // (1) lo_key = NULL, hi_key = NULL // scan the whole index // (2) lo_key = NULL, hi_key!= NULL // range scan from min to the hi_key // (3) lo_key!= NULL, hi_key = NULL // range scan from the lo_key to max // (4) lo_key!= NULL, hi_key!= NULL, lo_key = hi_key // exact match ( might not unique) // (5) lo_key!= NULL, hi_key!= NULL, lo_key < hi_key // range scan from lo_key to hi_key IndexFileScan *new_scan(const void *lo_key = NULL, const void *hi_key = NULL); int keysize(); void PrintHeader(); // print the header info void PrintRoot(); // print the root page void PrintLeafPages(); // print all the leaf pages void PrintPage(PageId id); // print page with id #if defined(BT_TRACE) static class ostream *Trace; // Set this to your stream to get a trace. #endif private: }; #endif // _BTFILE_H