120 likes | 354 Views
Department of Computer Science University of California – Riverside cs179G – Database Project. B+ Tree Implementation Details for Minibase. by Demetris Zeinalipour http://www.cs.ucr.edu/~cs179g-t/. The provided files. Makefile Modify this file to include .C files as you proceed
E N D
Department of Computer Science University of California – Riverside cs179G – Database Project B+ Tree Implementation Details for Minibase by Demetris Zeinalipour http://www.cs.ucr.edu/~cs179g-t/ 1
The provided files • Makefile Modify this file to include .C files as you proceed • btfile.h Definition of the B+Tree • btindex_page.h Definition of an Index Page • btleaf_page.h Definition of a Leaf Page • btreefilescan.h Scansover the leaf pages using ranges • key.C Auxiliary Functions to deal with Keys • btree_driver.C Contains the tests (test1, …, test4) • main.C Launches the tests • results Sample Output Results • keys Contains Strings (keys) that will be inserted in tree * Bold shows the classes for which you need to provide the .C source 2
What needs to be implemented? • You are asked to provide functionality to: • Create/Open an Existing B+ tree • Insert Keys (char *or int) into B+ tree • Delete Keys from B+ tree • Do range queries (IndexScans) Most Functions are based on Recursion 3
Revision of BTIndexPage and BTLeafPage(Inherited from SortedPage HFPage) Necessary defs in include/bt.h BTIndexPage struct KeyDataEntry { Keytype key; Datatype data; }; union Keytype { int intkey; char charkey[MAX_KEY_SIZE1]; }; union Datatype { PageId pageNo; // in index entries RID rid; // for leaf page entries }; struct RID{ // in the tests these are fake PageID pageID, int slotID} typedef enum { INDEX, LEAF } nodetype; int keyCompare(const void* key1, const void* key2,AttrType t); 220 bytes BTLeafPage From include/minirel.h enum AttrType { attrString, attrInteger, attrReal, attrSymbol, attrNull }; 4
BTIndexPage and BTLeafPage internallyThey are like HFPage but the slot directory is sorted + the record is either <key,PageId>(Index) or <key,RID<pageId, slotId>> (leaf) In order to iterate the entries of these pages use (should make calls to the appropriate HFPage funcs) Status get_first(RID& curid, void *curkey, RID & dataRid); //gets 1st record with key = curkey & puts in dataRid Status get_next (RID& curid, void *curkey, RID & dataRid);until the status becomes NOMOREREC. 5
The Big Picture of the Project Application (Btree_driver.C) Test1() // insert randomly 2000 integers in a B+ Tree btf = new BTreeFile(status, "BTreeIndex", attrInteger, sizeof(int)); 1 1) DB::get_file_entry(name, &headerPageId) 2) BM::newPage(&headerPageId, &Page) 2 4 4) BM::Pin(headerPageId, &Page) 3) DB::add_file_entry(name, headerPageId) 3 ( other methods) Pin, Unpin, newpage,freepage, … BufferManager Buf.C Read_page, write_page, alloc/dealloc_page Storage Manager Db.h Main Memory Secondary Storage btlog • BTREEDRIVER (the database) • BTreeIndex, OtherIndices • DataFiles (nothing for this project) O.S files 6
The Header Page //define in btfile.h struct BTreeHeaderPage { unsigned long magic0; // magic number for sanity checking PageId root; // page containing root of tree AttrType key_type; // type of keys in tree int keysize; // max key length (specified at index creation) int delete_fashion; // naive delete algorithm or full delete algorithm }; BTreeFile Btfile.C 5 7 BM::unPin(root, &Page, dirty) BM::Pin(root, &Page) BufferManager Buf.C 6) This step depends on the functionality you are implementing e.g. for searching you are using BT.h::keycompare(void *key1, void *key2, AttrType) along with the BTIndexORLeafPage::getNext() iterator. 6 7
Searching a key in a B+ Tree IndexFileScan *btfile::new_scan(const void *lo_key = NULL, const void *hi_key = NULL); pageId Index Leaf 8
4 Inserting Keys in a B+ Tree The usage of newchildentry Before inserting 5 nodeptr 4 7 8 9 After inserting 5 4 7 nodeptr newchildentry 4 5 7 8 9 9
Deleting Keys from a B+ Tree No Merges or Redistributions. We simply locate the key and delete it in a recursive fashion. 10
Where to start from? • Create/Open an Existing B+ tree • Insert Keys (char *or int) into B+ tree • In order to implement Insert, certain functions in BTfile/BTIndexPage/BTLeafPage must be implemented. • Initially you may ignore the BTLeafPage and just create the Index Level structure of the tree. • After the insertion you will have a B+ tree on which you can perform various operations • Move on to Deletes of entries, searches (range searches) and testing/debugging. 11
C++ Clarifications • Assert.h Used to diagnosing logic errors in the program const int MAGIC0 = 0xfeeb1e; assert(headerPage->magic0 == (unsigned)MAGIC0); if the test fails then the program will abort BTreeFile::BTreeFile(Status &, const char *): Assertion `headerPage->magic0 == (unsigned)MAGIC0 ' failed. Aborted 12