X-Git-Url: https://git.llucax.com/z.facultad/75.52/treemulator.git/blobdiff_plain/b08d09c0c5ba065ea663371f3ad577a81948860d..89e087a53e76331615252ec4e79830f755dbed6a:/src/btree.cpp?ds=sidebyside diff --git a/src/btree.cpp b/src/btree.cpp index 7c7a46b..3027c9a 100644 --- a/src/btree.cpp +++ b/src/btree.cpp @@ -391,6 +391,8 @@ Clave* BTree::AddKeyOtherR (const Clave *k, uint node_num, uint &left_child, uin void BTree::DelKey (const Clave &k) { + std::string s = k; + std::cout << "========= Borrando " << s << " =================\n"; DelKeyR (new BTreeLeafData (k.Clone ()), 0, 0); } @@ -419,6 +421,17 @@ void BTree::DelKeyR (BTreeData *k, uint node_num, uint padre) /* La encontre!, retorno */ if (node_header.level == 0) { DelKeyFromLeaf (k->getClave (), node_num, padre); + } else { + uint left, right; + if (it == posterior) { + left = lchild->getChild (); + right = (*it)->getChild (); + } else { + left = (*ultima)->getChild (); + right = (*it)->getChild (); + } + std::cout << "Eliminar de Nodo con hijos : " << left << " y " << right << std::endl; + DelKeyFromNode (k->getClave (), node_num, padre, left, right); } return; } @@ -433,7 +446,7 @@ void BTree::DelKeyR (BTreeData *k, uint node_num, uint padre) * decir que no lo encontre */ if (node_header.level == 0) { - std::cout << "Clave no encontrada\n"; + std::cout << "*** Clave no encontrada ***\n"; return; } @@ -478,7 +491,8 @@ void BTree::DelKeyFromLeaf (Clave *k, uint node_num, uint padre) WriteBlock (node, node_num); /* Veo si se cumple la condiciĆ³n de minimalidad */ - if ((node_header.free_space <= (header.block_size/2)) && (node_num != 0)) { + uint min_free = (header.block_size-sizeof(BTreeNodeHeader))/2; + if ((node_header.free_space > min_free) && (node_num != 0)) { /* Oops! Debo pedir prestada clave */ uint hi, hd; Clave *pedida; @@ -501,6 +515,18 @@ void BTree::DelKeyFromLeaf (Clave *k, uint node_num, uint padre) node_keys.push_back (new BTreeLeafData (pedida)); } else { std::cout << "NADIE ME PUEDE PRESTAR, FUNDIR NODOS\n"; + uint join1, join2; + if (hi != 0) { + std::cout << "Join con Hermano Izquierdo\n"; + join1 = hi; + join2 = node_num; + } else { + std::cout << "Join con Hermano Derecho\n"; + join1 = node_num; + join2 = hd; + } + + JoinNodes (join1, join2, padre); return; } @@ -513,10 +539,123 @@ void BTree::DelKeyFromLeaf (Clave *k, uint node_num, uint padre) delete [] node; std::cout << "Borrado de una hoja listo\n"; } + +void BTree::JoinNodes (uint node1, uint node2, uint padre) +{ + uchar *n1, *n2, *npadre; + BTreeNodeHeader nh1, nh2, nhp; + std::list nk1, nk2, nkpadre; + + /* Leo los nodos */ + n1 = ReadBlock (node1); + n2 = ReadBlock (node2); + npadre = ReadBlock (padre); + + ReadNodoHeader (n1, &nh1); + ReadNodoHeader (n2, &nh2); + ReadNodoHeader (npadre, &nhp); + + /* Apunto de Unir */ + uint tmp = header.block_size - sizeof (BTreeNodeHeader); + uint l = tmp - nh1.free_space; + l += tmp - nh1.free_space; + l += 4; + + std::cout << "Espacio ocupado despues de unir : " << l << " de " << tmp << std::endl; + + nk1 = ReadKeys (n1, nh1); + nk2 = ReadKeys (n2, nh2); + nkpadre = ReadKeys (npadre, nhp); + + /* Busco la clave a juntar con los nodos */ + std::list::iterator it = nkpadre.begin (); + std::list::iterator borrar_padre; + std::list::iterator sig; + + Clave *cpadre; + BTreeData *lchild = (*it++); + + if (lchild->getChild () == node1) { + cpadre = (*it)->getClave (); + //nkpadre.erase (it); + borrar_padre = it; + } else { + while (it != nkpadre.end ()) { + if ((*it)->getChild () == node1) + break; + it++; + } + cpadre = (*it)->getClave (); + //nkpadre.erase (it); + borrar_padre = it; + } + if (it == nkpadre.end ()) { + std::cout << "PANIC : Me pase sin encontrar la clave!!\n"; + exit(1); + } + it++; + sig = it; + + std::list newkeys; + std::list::iterator i; + + i = nk1.begin (); + while (i != nk1.end ()) { + newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ())); + i++; + } + if (cpadre) + newkeys.push_back ( new BTreeLeafData (cpadre->Clone ())); + i = nk2.begin (); + while (i != nk2.end ()) { + newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ())); + i++; + } + + std::cout << "Espacio ocupado por las nuevas claves : " << (newkeys.size()*4) << std::endl; + if ((newkeys.size()*4) > tmp) { + std::cout << "PANIC : El nodo fundido no entra !!!\n"; + exit (1); + } + +/* if (padre == 0) { + nhp.level = 0; + WriteKeys (npadre, nhp, newkeys); + WriteNodoHeader (npadre, &nhp); + WriteBlock (npadre, padre);*/ + + /* TODO: Recuperar nodo1 y nodo2 */ +// } else { + WriteKeys (n1, nh1, newkeys); + WriteNodoHeader (n1, &nh1); + WriteBlock (n1, node1); + + /* TODO : Recuperar node2 */ + /* Actualizo punero al padre */ + (*sig)->setChild (node1); + nkpadre.erase (borrar_padre); + WriteKeys (npadre, nhp, nkpadre); + WriteNodoHeader (npadre, &nhp); + WriteBlock (npadre, padre); + //} + + DeleteKeys (nk1); + DeleteKeys (nk2); + DeleteKeys (nkpadre); + DeleteKeys (newkeys); + + delete [] n1; + delete [] n2; + delete [] npadre; +} + Clave *BTree::GetKey (uint node_num, char maxmin) { - if (node_num == 0) return NULL; + if (node_num == 0) { + std::cout << "Nodo no me puede prestar ... es NULL\n"; + return NULL; + } uchar *node; BTreeNodeHeader node_header; @@ -531,6 +670,21 @@ Clave *BTree::GetKey (uint node_num, char maxmin) if (node_header.level != 0) it++; Clave *k; + uint free = node_header.free_space; // + (*it)->Size (); + uint min_free = (header.block_size - sizeof (BTreeNodeHeader))/2; + if (free > min_free) { + std::cout << "No puedo prestar : Free = " << free << " Minimo = " << min_free << std::endl; + PrintNode (node_num); + WriteKeys (node, node_header, node_keys); + WriteNodoHeader (node, &node_header); + WriteBlock (node, node_num); + DeleteKeys (node_keys); + + delete [] node; + + return NULL; + } + if (maxmin == 0) { k = (*it)->getClave ()->Clone (); node_keys.erase (it); @@ -546,6 +700,8 @@ Clave *BTree::GetKey (uint node_num, char maxmin) WriteBlock (node, node_num); DeleteKeys (node_keys); + delete [] node; + return k; } @@ -641,8 +797,98 @@ Clave *BTree::ReplaceKeyInFather (uint node_num, uint padre, Clave *k) return ret; } -void BTree::DelKeyFromOther (const Clave &k, BTreeFindResult *r) +void BTree::DelKeyFromNode (Clave *k, uint node_num, uint padre, uint left, uint right) { + uint padre_hijo; + uchar *node; + BTreeNodeHeader node_header; + std::list node_keys; + + node = ReadBlock (padre); + ReadNodoHeader (node, &node_header); + node_keys = ReadKeys (node, node_header); + + if (right != 0) { + std::cout << "Busco para la derecha y luego todo a la izquierda\n"; + uchar *node_r; + BTreeNodeHeader node_hr; + std::list node_keyr; + + /* Busco la clave inmediatamente superior en el arbol */ + padre_hijo = node_num; + do { + node_r = ReadBlock (right); + ReadNodoHeader (node_r, &node_hr); + if (node_hr.level != 0) { + BTreeData *data_r; + node_keyr = ReadKeys (node_r, node_hr); + data_r = *(node_keyr.begin ()); + padre_hijo = right; + right = data_r->getChild (); + + DeleteKeys (node_keyr); + delete [] node_r; + } + } while (node_hr.level != 0); + + std::cout << "Voy a reemplazar en el nodo " << right << std::endl; + + /* Reemplazo la clave a borrar por la de la hoja */ + node_keyr = ReadKeys (node_r, node_hr); + BTreeData *reemplazar = *(node_keyr.begin ()); + + std::string ss = *reemplazar; + std::cout << "Voy a reemplazar por : " << ss << std::endl; + + BTreeData *data = new BTreeLeafData (k->Clone()); + + std::list::iterator it = node_keys.begin (); + while (it != node_keys.end ()) { + if ((*data) == (*(*it))) { + break; + } + it++; + } + if (it == node_keys.end ()) { + std::cout << "PANIC : No encontre la clave en el nodo!!!!\n"; + std::string s = *data; + std::cout << s << std::endl; + PrintNode (node_num); + exit (1); + } + (*it)->setClave (reemplazar->getClave ()); + reemplazar->setClave (k->Clone ()); + + std::cout << "Tengo todo reemplazado ...\n"; + + /* Grabo los nodos */ + WriteKeys (node, node_header, node_keys); + WriteNodoHeader (node, &node_header); + WriteBlock (node, padre); + DeleteKeys (node_keys); + delete [] node; + + WriteKeys (node_r, node_hr, node_keyr); + WriteNodoHeader (node_r, &node_hr); + WriteBlock (node_r, right); + DeleteKeys (node_keyr); + delete [] node_r; + + std::cout << "Grabe todo en disco ...\n"; + + /* Ahora debo eliminar la clave que puse en el nodo hoja */ + std::cout << "Borro la clave desde la hoja!\n"; + + DelKeyFromLeaf (k, right, padre_hijo); + + std::cout << "Listo, Listo!\n"; + } else if (left != 0) { + std::cout << "PANIC : Deberia poder reemplazar en la derecha!!!!!\n"; + exit (1); + } else { + std::cout << "PANIC : No tengo hijos para reemplazar!!!!\n"; + exit (1); + } } void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header) @@ -714,9 +960,12 @@ void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::listToArray (); + acumulado += d->Size (); + //std::cout << "WriteKeys :: Acumulado = " << acumulado << std::endl; memcpy (node, n, d->Size ()); delete [] n; node += d->Size ();