void FileSystem::deleteFromFS(std::string remoteFile) { // hashing function to find the machine/s where to store the file; int fileKey = hashString(remoteFile); int position = findPositionByKey(fileKey); // cout<<"delete " << fileKey << " from "<<position<<" "<<virtualRing[position].ip_str<<" "<<virtualRing[position].key<<endl; bool attempt = deleteFromNode( position, remoteFile ); if( !attempt) { std::cout << "Problem removing at position " << position << std::endl; } attempt = deleteFromNode( position+1, remoteFile ); //if(attempt) //return; attempt = deleteFromNode( position+2, remoteFile ); //if(attempt) //return; //std::string destAddress = "localhost"; //put(destAddress, localFile, remoteFile); }
void BPlusTree::deleteEntry(int key) { BufferManager *bm = BufferManager::getInstance(); //lookup till the required node TreeNode* c = root; while( c->isLeaf() ) { int sKey = -1; int finalKey = -1; for(int i=0; i<(c->getTotalPairs()); i++){ 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))); } else c = new TreeNode(bm->pinPage(dbName, c->getRid(finalKey))); } deleteFromNode(c, key); }
void BPlusTree::deleteFromNode(TreeNode* N, int key) { BufferManager *bm = BufferManager::getInstance(); //finding the location of pair to be deleted int i; for(i=1;i<=N->getTotalPairs();i++) { if(N->getKey(i)==key) break; } //deleting the pair moveBytes( N->getBuffPage(), 8*i, 8+8*i, (N->getTotalPairs()-i+1)*8+4 ); N->updateSlot(1, N->getTotalPairs()-1); //if N is root AND has only one child if(N->getSlot(BLOCK_SIZE/4)==-1 && N->getSlot(1)==1) { //delete that node and make its only child as root int newRootId = N->getSlot(3); root = new TreeNode(bm->pinPage(dbName, N->getSlot(3))); int blockToFree = N->getSlot(BLOCK_SIZE/4-1); bm->unpinPage(dbName, blockToFree, true); StorageManager::freeBlock(dbName, blockToFree); root->updateSlot(BLOCK_SIZE/4, -1); } else { int parentPtr = readInt(N->getBuffPage(), BLOCK_SIZE-4); TreeNode* parent = new TreeNode(bm->pinPage(dbName, parentPtr)); int loc=parent->findPtr(N->getNodeId()); bool correct; int NDashId,keyDash; //finding next or previous node and the value between them if(loc>1) { correct=true; NDashId = loc-1; keyDash = parent->getKey(loc-1); } else { correct=false; NDashId = loc+1; keyDash = parent->getKey(loc); } TreeNode* NDash = new TreeNode(bm->pinPage(dbName, NDashId)); //if N and NDash can coalesce in one node if(N->getTotalPairs()+NDash->getTotalPairs()-1 == BT_N) { //swapping if successive nodes r in reverse order if(correct==false) { TreeNode* temp; temp = N; N = NDash; NDash = temp; } //coalescing nodes if(!N->isLeaf()) { int num = NDash->getTotalPairs(); NDash->updateSlot(2*NDash->getTotalPairs()+2, keyDash); for(int i=0;i<(N->getTotalPairs()*2);i++) { NDash->updateSlot(2*NDash->getTotalPairs()+2+i, N->getSlot(i+2)); NDash->updateSlot(1,NDash->getTotalPairs()+num); } } else { int num = NDash->getTotalPairs(); NDash->updateSlot(2*NDash->getTotalPairs()+2, keyDash); for(int i=0;i<(N->getTotalPairs()*2);i++) NDash->updateSlot(2*NDash->getTotalPairs()+2+i, N->getSlot(i+2)); NDash->updateSlot(1,NDash->getTotalPairs()+num); NDash->updateSlot(2+2*BT_N+1,N->getSlot(2+2*BT_N+1)); } deleteFromNode(parent, keyDash); } //if NDash is predecessor if(correct==true){ if(!N->isLeaf()) { //removing last key and pointer to next leaf int lastKey = NDash->getSlot(2+NDash->getTotalPairs()*2); int lastPtr = NDash->getSlot(2+NDash->getTotalPairs()*2+1); NDash->updateSlot(1,NDash->getTotalPairs()+1); //adding one pair to N moveBytes(N->getBuffPage(), 16, 8, N->getTotalPairs()*8+4); N->updateSlot(1, N->getTotalPairs()+1); N->updateSlot(3, lastPtr); N->updateSlot(4, keyDash); //replacing keyDash in parent by the removed key int location = parent->findKey(keyDash); parent->updateSlot(2+2*location, lastKey); } else { //removing last key and pointer to next leaf int lastKey = NDash->getSlot(2+NDash->getTotalPairs()*2); int lastPtr = NDash->getSlot(2+NDash->getTotalPairs()*2-1); NDash->updateSlot(1,NDash->getTotalPairs()+1); //adding one pair to N moveBytes(N->getBuffPage(), 16, 8, N->getTotalPairs()*8); N->updateSlot(1, N->getTotalPairs()+1); N->updateSlot(3, lastPtr); N->updateSlot(4, keyDash); //replacing keyDash in parent by the removed key int location = parent->findKey(keyDash); parent->updateSlot(2+2*location, lastKey); } } else { if(!NDash->isLeaf()) { //removing last key and pointer to next leaf int lastKey = N->getSlot(2+N->getTotalPairs()*2); int lastPtr = N->getSlot(2+N->getTotalPairs()*2+1); N->updateSlot(1,N->getTotalPairs()+1); //adding one pair to NDash moveBytes(NDash->getBuffPage(), 16, 8, NDash->getTotalPairs()*8+4); NDash->updateSlot(1, NDash->getTotalPairs()+1); NDash->updateSlot(3, lastPtr); NDash->updateSlot(4, keyDash); //replacing keyDash in parent by the removed key int location = parent->findKey(keyDash); parent->updateSlot(2+2*location, lastKey); } else { //removing last key and pointer to next leaf int lastKey = N->getSlot(2+N->getTotalPairs()*2); int lastPtr = N->getSlot(2+N->getTotalPairs()*2-1); N->updateSlot(1,N->getTotalPairs()+1); //adding one pair to NDash moveBytes(NDash->getBuffPage(), 16, 8, NDash->getTotalPairs()*8); NDash->updateSlot(1, NDash->getTotalPairs()+1); NDash->updateSlot(3, lastPtr); NDash->updateSlot(4, keyDash); //replacing keyDash in parent by the removed key int location = parent->findKey(keyDash); parent->updateSlot(2+2*location, lastKey); } } } }