B+ Tree Implementation Details for Minibase

Download Report

Transcript B+ Tree Implementation Details for Minibase

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
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
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  Scans over 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; };
220 bytes
union Keytype {
int intkey;
char charkey[MAX_KEY_SIZE1]; };
union Datatype {
PageId pageNo; // in index entries
RID rid; // for leaf page entries };
BTLeafPage
struct RID{ // in the tests these are fake
PageID pageID,
int slotID}
typedef enum { INDEX, LEAF } nodetype;
From include/minirel.h
enum AttrType { attrString, attrInteger,
attrReal, attrSymbol, attrNull };
int keyCompare(const void* key1,
const void* key2, AttrType t);
4
BTIndexPage and BTLeafPage internally
They 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
2) BM::newPage(&headerPageId, &Page)
4
4) BM::Pin(headerPageId, &Page)
3) DB::add_file_entry(name, headerPageId)
3
( other methods)
BufferManager Pin, Unpin,
Buf.C newpage,freepage, …
Read_page, write_page, alloc/dealloc_page
Storage Manager
Db.h
Main Memory
Secondary Storage
BTREEDRIVER (the database)
1)
2)
BTreeIndex, OtherIndices
DataFiles (nothing 6for this project)
btlog
O.S files
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
BM::Pin(root, &Page)
7
BM::unPin(root, &Page, dirty)
BufferManager
Buf.C
6) This step depends on the functionality you are implementing
6
7
e.g. for searching you are using BT.h::keycompare(void
*key1, void *key2, AttrType)
along with the BTIndexORLeafPage::getNext() iterator.
Searching a key in a B+ Tree
IndexFileScan *btfile::new_scan(const void *lo_key = NULL, const void *hi_key = NULL);
pageId
Index
8
Leaf
Inserting Keys in a B+ Tree
The usage of newchildentry
Before inserting 5
4
nodeptr
4 7 8 9
After inserting 5
4 7
nodeptr
4 5
9
newchildentry
7 8 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?
1. Create/Open an Existing B+ tree
2. Insert Keys (char *or int) into B+ tree
3. In order to implement Insert, certain functions in
BTfile/BTIndexPage/BTLeafPage must be
implemented.
4. Initially you may ignore the BTLeafPage and just
create the Index Level structure of the tree.
5. After the insertion you will have a B+ tree on which
you can perform various operations
6. 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