bool insert(const KeyType& key, const filepoint& data){ // std::cout<<"-----ok in-----"<<std::endl; if (rootloc==0){ // std::cout<<"-----root NULL-----"<<std::endl; Tnode<KeyType> root; Tnode<KeyType> first; root.insert (key,data); root.setLeaf (1); first = root; firstloc = writeToFile (first); rootloc = firstloc; return true; } //空树 Tnode<KeyType> tempnode; filepoint temp; int keyindex; temp = findLeaf (key); tempnode = readFromFile (temp); keyindex = tempnode.getKeyIndex (key); if (keyindex>0 && key == tempnode.getKeyValue (keyindex-1)){ return false; } //已经在树中 if (tempnode.getKeyNum ()<MAXNUM_KEY){ //不分裂插入 // std::cout<<"notspl::"<<std::endl; insert_in_leaf(tempnode,key,data); updateFile (temp,tempnode); }else{ //裂开点 // std::cout<<"splite::"<<std::endl; Tnode<KeyType> newnode; insert_in_leaf(tempnode,key,data); newnode.setLeaf (1); for (int i=0;i<ORDER+1;++i){ newnode.setKeyValue (i,tempnode.getKeyValue (ORDER-1)); newnode.setChild (i,tempnode.getChild (ORDER-1)); tempnode.removeKey (ORDER-1,ORDER-1); } newnode.setChild (ORDER+1,tempnode.getChild (ORDER-1)); newnode.setKeyNum (ORDER+1); tempnode.setKeyNum (ORDER-1); filepoint newp = writeToFile (newnode); tempnode.setChild (ORDER-1,newp); updateFile (temp,tempnode); KeyType newkey = newnode.getKeyValue (0); insert_in_parent(temp,newkey,newp); } return true; }
bool insert_in_parent(filepoint x,const KeyType &key,filepoint y){ if (x == rootloc){ // std::cout<<"insert_in_parent"<<std::endl; Tnode<KeyType> newroot; newroot.setKeyNum (1); newroot.setChild (0,x); newroot.setChild (1,y); newroot.setKeyValue (0,key); rootloc = writeToFile (newroot); return true; }else{ filepoint parent = father[x]; Tnode<KeyType> parentnode=readFromFile (parent); if(parentnode.getKeyNum ()<MAXNUM_KEY){ int childindex = parentnode.getChildIndex (x); parentnode.insert (childindex,childindex+1,key,y); updateFile (parent,parentnode); }else{ int kid = parentnode.getKeyIndex (key); parentnode.insert (kid,kid+1,key,y); Tnode<KeyType> newnode; for (int i=0;i<ORDER;++i){ newnode.insert (i,i,parentnode.getKeyValue (ORDER),parentnode.getChild (ORDER)); parentnode.removeKey (ORDER,ORDER); } newnode.setChild (ORDER,parentnode.getChild (ORDER)); filepoint newp = writeToFile (newnode); KeyType newkey = parentnode.getKeyValue (ORDER-1); parentnode.removeKey (ORDER-1,ORDER); updateFile (parent,parentnode); insert_in_parent (parent,newkey,newp); } return true; } return false; }
void BPlusTree::insertEntry(int key, int rid) { cout<<"a\n"; BufferManager *bm = BufferManager::getInstance(); TreeNode* c = root; cout<<"b\n"; while( c->isLeaf() ) { cout<<"a1\n"; int sKey = -1; int finalKey = -1; for(int i=0; i<(c->getTotalPairs()); i++){ cout<<"a2\n"; sKey=c->getKey(i); if(sKey>key){ finalKey=i; break; } } if(finalKey==-1) { int m=c->getTotalPairs(); c = new TreeNode(bm->pinPage(dbName, c->getRid(m))); cout<<"a3\n"; } else cout<<"aelse\n"; c = new TreeNode(bm->pinPage(dbName, c->getRid(finalKey))); } cout<<"a\n"; /* int keyExists = -1; int position; for(int i=0; i<(c->getTotalPairs()); i++){ if(key == c->getKey(i)){ keyExists = key; position=i; } } */ if(c->getTotalPairs() < BT_N){ cout<<"inif 1\n"; insert_in_leaf(c, key, rid); cout<<"inserted in leaf\n"; } else { cout<<"inelse 1\n"; int temp; int* tempPtr = &temp; //creating new node int newNodeId = StorageManager::allocateBlock(dbName); int* newNodeIdPtr = &newNodeId; TreeNode * newNode = new TreeNode(bm->pinPage(dbName, newNodeId)); writeInt(newNode->getBuffPage(), BLOCK_SIZE-4, readInt(c->getBuffPage(), BLOCK_SIZE-4)); writeInt(newNode->getBuffPage(), BLOCK_SIZE-8, newNodeId); //creating temp node & copying data to temp node char *tempForTempNode = (char *)calloc(BLOCK_SIZE,sizeof(char)); memcpy(tempForTempNode, c->getBuffPage(), 8+8*BT_N); TreeNode * tempNode = new TreeNode(tempForTempNode); insert_in_leaf(newNode, key, rid); //add new node newNode in the linked list memcpy( (newNode->getBuffPage())+(8+BT_N*8), (c->getBuffPage())+(8+BT_N*8), 4); memcpy( (c->getBuffPage())+(8+BT_N*8), newNodeIdPtr, 4); //no erasing, but overwriting //write first half of tempNode into original one temp = ceil((float)(BT_N/2)); memcpy( (c->getBuffPage())+8, tempNode->getBuffPage()+8, 8*temp); memcpy( c->getBuffPage(), tempPtr, 4); //copy second half into newNode memcpy( (newNode->getBuffPage())+8, (tempNode->getBuffPage())+(8+8*temp), 8*(BT_N - temp)); temp = BT_N - temp; memcpy( newNode->getBuffPage(), tempPtr, 4); temp = 1; memcpy( (newNode->getBuffPage())+4, tempPtr, 4); //taking smallest key of new node to update(insert) in parent int smallestKey = newNode->getRid(1); insert_in_parent(c, smallestKey, newNode); } }
void BPlusTree::insert_in_parent( TreeNode* N, int key, TreeNode* NDash) { BufferManager *bm = BufferManager::getInstance(); if(N->isRoot()) { //creating new node int newNodeId = StorageManager::allocateBlock(dbName); int* newNodeIdPtr = &newNodeId; TreeNode * R = new TreeNode(bm->pinPage(dbName, newNodeId)); writeInt( R->getBuffPage(), 0, 1 ); writeInt( R->getBuffPage(), 4, 2 ); writeInt( R->getBuffPage(), 8, N->getNodeId() ); writeInt( R->getBuffPage(), 12, key ); writeInt( R->getBuffPage(), 16, NDash->getNodeId() ); writeInt( R->getBuffPage(), BLOCK_SIZE-4, -1 ); writeInt( R->getBuffPage(), BLOCK_SIZE-8, newNodeId ); writeInt( N->getBuffPage(), BLOCK_SIZE-4, R->getNodeId() ); } TreeNode* P = new TreeNode(bm->pinPage(dbName, N->getParent())); if(P->getTotalPairs() < BT_N) { //finding the location of pointer to N in the parent int i; for(i=1;i<=P->getTotalPairs();i++) { if(P->getKey(i)==N->getNodeId()) break; } //inserting the pair in parent p just after N writeInt( P->getBuffPage(), 0, P->getTotalPairs()+1 ); moveBytes(P->getBuffPage(), 8*P->getTotalPairs()-(8+8*i)+4, 8+8*i, 8);//**** writeInt( P->getBuffPage(), 8+8*i, NDash->getNodeId() );//**** writeInt( P->getBuffPage(), 12+8*i, key );//**** } else { //allocating temporary memory T char * T = (char *)calloc(BLOCK_SIZE+8,sizeof(char)); memcpy(T, P->getBuffPage(), 12+8*BT_N); //finding the location of pointer to N in the parent int i; for(i=1; i <= ((TreeNode*)T)->getTotalPairs(); i++) { if(P->getKey(i)==N->getNodeId()) break; } //inserting the pair in temp memory moveBytes(T, 8+8*i+8, 8+8*i,(P->getTotalPairs()-i)*8); writeInt(T, 8+8*i, NDash->getNodeId()); writeInt(T, 12+8*i, key); //temp int temp = ceil(BT_N/2); int* tempPtr = &temp; //keep only first half of T in P memcpy(P->getBuffPage(), T, 8+8*temp); writeInt(P->getBuffPage(), 4,temp); int newKey = P->getKey(temp); //new node int PDashId = StorageManager::allocateBlock(dbName); int* PDashIdPtr = &PDashId; TreeNode * PDash = new TreeNode(bm->pinPage(dbName, PDashId)); writeInt(PDash->getBuffPage(), BLOCK_SIZE-4, readInt(P->getBuffPage(), BLOCK_SIZE-4)); writeInt(PDash->getBuffPage(), BLOCK_SIZE-8, PDashId); //copying to new node memcpy(PDash->getBuffPage(), T+8+8*temp, 8*BT_N-8*temp+4); writeInt(PDash->getBuffPage(), 4, BT_N-temp); insert_in_parent(P,newKey,PDash); } }