Title: B Tree Implementation Details for Minibase
1B Tree Implementation Details for Minibase
Department of Computer Science University of
California Riverside cs179G Database Project
- by
- Demetris Zeinalipour
- http//www.cs.ucr.edu/cs179g-t/
2The provided files
- Makefile ? Modify this file to include .C
files as you proceed - btfile.h ? Definition of the BTree
- 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
3What 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
4Revision 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
charkeyMAX_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
5BTIndexPage and BTLeafPage internallyThey are
like HFPage but the slot directory is sorted
the record is either ltkey,PageIdgt(Index) or
ltkey,RIDltpageId, slotIdgtgt (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.
6The 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) DBget_file_entry(name, headerPageId)
1
2) BMnewPage(headerPageId, Page)
2
4) BMPin(headerPageId, Page)
4
3) DBadd_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
7The 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
BMunPin(root, Page, dirty)
BMPin(root, Page)
BufferManager Buf.C
6) This step depends on the functionality you are
implementing e.g. for searching you are using
BT.hkeycompare(void key1, void key2,
AttrType) along with the BTIndexORLeafPagegetNex
t() iterator.
6
8Searching a key in a B Tree
IndexFileScan btfilenew_scan(const void
lo_key NULL, const void hi_key NULL)
pageId
Index
Leaf
9Inserting Keys in a B Tree
The usage of newchildentry
Before inserting 5
nodeptr
8
7
9
4
After inserting 5
7
4
nodeptr
newchildentry
5
4
9
8
7
10Deleting Keys from a B Tree
No Merges or Redistributions. We simply locate
the key and delete it in a recursive fashion.
11Where 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.
12C Clarifications
- Assert.h
- Used to diagnosing logic errors in the program
- const int MAGIC0 0xfeeb1e
- assert(headerPage-gtmagic0
(unsigned)MAGIC0) - if the test fails then the program will abort
- BTreeFileBTreeFile(Status , const char )
Assertion headerPage-gtmagic0 (unsigned)MAGIC0
' failed. - Aborted