示例#1
0
void gerenciadorArvoreB::driver(int key,int offset){
	fileIO* IO = new fileIO();
	IO->openFile(string("./data/root"));
	stringstream rootRaw;
	if(PED->getNumRegs() == 0 ){
		BTPage* reg = new BTPage();
		reg->addChave(key,offset);
		insertReg(reg,0);
		root = 0;
	}else{
		root = atoi(IO->readPos(0,6).c_str());	
		insertReturn* result = (insertReturn*) malloc(sizeof(insertReturn));
		if(insere(key,offset,root,result)->status == PROMOTION){
			BTPage* reg = new BTPage();
			reg->addChave(result->promo_key,result->promo_offset);
			reg->addFilha(root,0);
			reg->addFilha(result->promo_r_child,1);
			int RRN = PED->getPosToWriteNewReg();
			root = 	RRN;
			insertReg(reg,RRN);
		}	
	}
	rootRaw << root << setfill(' ') << setw(3);	
	IO->writeAtPos(0,rootRaw.str());
	delete IO;
}
示例#2
0
int gerenciadorArvoreB::remove(int RRN,int chave){
	BTPage* reg = loadPTPage(RRN);
	int i,pos,res;
	int u=reg->totalChaves;
	while( u > 0 && chave < reg->chaves[u-1]){
		u--;	
	}

	for(i=0;i<reg->totalChaves;i++){
                if(reg->chaves[i]==chave){
			pos = reg->filha[i];
			break;
                }else if(reg->chaves[i] < chave){
                        pos = reg->filha[i+1];
                }else{
                        pos = reg->filha[i];
        	        break;
	        }
        }

	if(reg->chaves[i] != chave){
		if(pos!=-1){
			res= remove(pos,chave);
		}else{
			res= 0;
		}
	}else{
		if(pos==-1){
			reg->removeChave(chave);
			insertReg(reg,RRN);		
		}else{
			int posB = reg->filha[i+1];
			BTPage* aux;
			int posA;
			while (posB != -1){
				posA = posB; 
				aux = loadPTPage(posB);
				posB = aux->filha[0];	
			}
				
			reg->chaves[i] = aux->chaves[0];
			reg->offset[i] = aux->offset[0];
			insertReg(reg,RRN);
			remove(reg->filha[i+1],aux->chaves[0]);
		}
	
	}
	if(pos!=-1){
		BTPage* aux = loadPTPage(reg->filha[u]);
		if(aux->getTotalChaves() <= ((aux->D/2) - 1)){
			restore(reg,u,RRN);	
		}
	}
	return res;
}
示例#3
0
insertReturn* gerenciadorArvoreB::insere(int key,int offset,int RRN,insertReturn* result){

	int pos = 0;
	insertReturn* resval;	
	BTPage* reg;

	if(RRN == -1){
		result->promo_key = key;
		result->promo_offset = offset;
		result->promo_r_child = -1; 
		result->status = PROMOTION;	
		return result;
	}else{
		reg = loadPTPage(RRN);
		int i;
		for(i=0;i<reg->totalChaves;i++){
			if(reg->chaves[i]==key){
				result->status=ERRO;
				return result;
			}else if(reg->chaves[i] < key){
				pos = i+1;
			}else{
				pos = i;
				break;			
			}
		}
		resval = insere(key,offset,reg->filha[pos],result);
	}

	if(resval->status == NO_PROMOTION || resval->status == ERRO){
		return 	resval;
	}else{
		
		if(reg->totalChaves < (reg->D-1)){
			int pos = reg->addChave(result->promo_key,result->promo_offset);	
			reg->addFilha(result->promo_r_child,pos+1);
			result->status = NO_PROMOTION;
			insertReg(reg,RRN);
			return result;	
		}else{
			BTPage *newReg = new BTPage();
			split(result->promo_key,result->promo_offset,reg,result,newReg);
			insertReg(reg,RRN);
			insertReg(newReg,result->promo_r_child);
			result->status = PROMOTION;
			return result;
		}
	}
	delete reg;

}
示例#4
0
void gerenciadorArvoreB::combine(BTPage* reg,int i,int RRN){
	BTPage* right = loadPTPage(reg->filha[i]);
        BTPage* left = loadPTPage(reg->filha[i-1]);	
	int pos = left->addChave(reg->chaves[i-1],reg->offset[i-1]);
	reg->removeChave(reg->chaves[i-1]);
	left->filha[pos+1]=right->filha[0];
	int j;
	for(j=0;j<right->totalChaves;j++){
		int pos = left->addChave(right->chaves[j],right->offset[j]);
		left->addFilha(right->filha[j+1],pos+1);
	}

        insertReg(reg,RRN);
        insertReg(left,reg->filha[i-1]);

}
示例#5
0
void gerenciadorArvoreB::move_left(BTPage* reg,int i, int RRN){
	BTPage* right = loadPTPage(reg->filha[i]);
	BTPage* left = loadPTPage(reg->filha[i-1]);	
	
	int rc = right->chaves[0];
	int ro = right->offset[0];
	int rf = right->filha[0];
	
	right->removeChave(rc);
	int pos = left->addChave(reg->chaves[i-1],reg->offset[i-1]);
	left->filha[pos+1]=rf;

	reg->chaves[i-1]=rc;
	reg->offset[i-1]=ro;

	insertReg(reg,RRN);
	insertReg(right,reg->filha[i]);
	insertReg(left,reg->filha[i-1]);	
}
示例#6
0
void gerenciadorArvoreB::move_right(BTPage* reg,int i,int RRN){	
	BTPage* right = loadPTPage(reg->filha[i+1]);
	BTPage* left = loadPTPage(reg->filha[i]);
	
	int  lc = left->chaves[left->getTotalChaves()-1];
	int lo = left->offset[left->getTotalChaves()-1];
	int lf = left->filha[left->getTotalChaves()];
	
	int pos = right->addChave(reg->chaves[i],reg->offset[i]);
	right->addFilha(lf,pos);
	left->totalChaves -= 1;

	reg->chaves[i]=lc;
	reg->offset[i]=lo;

	insertReg(right,reg->filha[i+1]);
	insertReg(left,reg->filha[i]);
	insertReg(reg,RRN);
}
示例#7
0
int main(){
  char choice = 's';
  int input;

  startFileRead();
  while(choice != EXIT){
    scanf("%c", &choice);

    switch(choice){
      case INSERT:
        insertReg();
      break;

      case SEARCH:
        scanf("%d", &input);
        search(input);
      break;

      case REMOVE:
        scanf("%d", &input);
        removeRegister(input);
      break;

      case LISTALL:
        listAll();
      break;

      case ACCESS:
        counterAccess();
      break;

      case EXIT:
      break;

      default:
        printf("Entrada Inválida\n");
      break;
    }
    getchar();
  }

  return 0;
}