+Clave *BTree::GetKey (uint node_num, char maxmin)
+{
+ if (node_num == 0) {
+ std::cout << "Nodo no me puede prestar ... es NULL\n";
+ return NULL;
+ }
+
+ uchar *node;
+ BTreeNodeHeader node_header;
+ std::list<BTreeData *> node_keys;
+
+ node = ReadBlock (node_num);
+ ReadNodoHeader (node, &node_header);
+ node_keys = ReadKeys (node, node_header);
+
+ std::list<BTreeData *>::iterator it = node_keys.begin ();
+
+ 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)->GetKey ()->Clone ();
+ node_keys.erase (it);
+ } else {
+ it = node_keys.end ();
+ it--;
+ k = (*it)->GetKey ()->Clone ();
+ node_keys.erase (it);
+ }
+
+ WriteKeys (node, node_header, node_keys);
+ WriteNodoHeader (node, &node_header);
+ WriteBlock (node, node_num);
+ DeleteKeys (node_keys);
+
+ delete [] node;
+
+ return k;
+}
+
+void BTree::FindBrothers (uint node_num, uint padre, uint &left, uint &right)
+{
+ uchar *node;
+ BTreeNodeHeader node_header;
+ std::list<BTreeData *> node_keys;
+
+ node = ReadBlock (padre);
+ ReadNodoHeader (node, &node_header);
+ node_keys = ReadKeys (node, node_header);
+
+ std::list<BTreeData *>::iterator it = node_keys.begin ();
+ std::list<BTreeData *>::iterator anterior = node_keys.begin ();
+ std::list<BTreeData *>::iterator siguiente;
+
+ BTreeData *lchild = (*it++);
+
+ if (lchild->GetChild () == node_num) {
+ /* Solo tengo hermano derecho */
+ std::cout << "Hermano Izquierdo : NO TENGO" << std::endl;
+ left = 0;
+ std::cout << "Hermano Derecho : " << (*it)->GetChild () << std::endl;
+ right = (*it)->GetChild ();
+ return;
+ }
+
+ while (it != node_keys.end ()) {
+ if ((*it)->GetChild () == node_num)
+ break;
+ anterior = it;
+ it++;
+ }
+ siguiente = it++;
+
+ std::cout << "Hermano Izquierdo : " << (*anterior)->GetChild () << std::endl;
+ left = (*anterior)->GetChild ();
+ if (siguiente != node_keys.end ()) {
+ right = (*siguiente)->GetChild ();
+ std::cout << "Hermano Derecho : " << (*siguiente)->GetChild () << std::endl;
+ } else {
+ right = 0;
+ std::cout << "Hermano Derecho : NO TENGO" << std::endl;
+ }
+}
+
+Clave *BTree::ReplaceKeyInFather (uint node_num, uint padre, Clave *k)
+{
+ uchar *node;
+ BTreeNodeHeader node_header;
+ std::list<BTreeData *> node_keys;
+
+ node = ReadBlock (padre);
+ ReadNodoHeader (node, &node_header);
+ node_keys = ReadKeys (node, node_header);
+
+ std::list<BTreeData *>::iterator it = node_keys.begin ();
+ std::list<BTreeData *>::iterator anterior = node_keys.begin ();
+ std::list<BTreeData *>::iterator siguiente;
+
+ BTreeData *lchild = (*it++);
+
+ if (lchild->GetChild () == node_num) {
+ Clave *ret = (*it)->GetKey ();
+ (*it)->SetKey (k);
+
+ WriteKeys (node, node_header, node_keys);
+ WriteNodoHeader (node, &node_header);
+ WriteBlock (node, padre);
+ DeleteKeys (node_keys);
+
+ delete [] node;
+ return ret;
+ }
+
+ while (it != node_keys.end ()) {
+ if ((*it)->GetChild () == node_num)
+ break;
+ anterior = it;
+ it++;
+ }
+
+ Clave *ret = (*it)->GetKey ();
+ (*it)->SetKey (k);
+
+ WriteKeys (node, node_header, node_keys);
+ WriteNodoHeader (node, &node_header);
+ WriteBlock (node, padre);
+ DeleteKeys (node_keys);
+
+ delete [] node;
+ return ret;
+}
+
+void BTree::DelKeyFromNode (Clave *k, uint node_num, uint padre, uint left, uint right)
+{
+ uint padre_hijo;
+ uchar *node;
+ BTreeNodeHeader node_header;
+ std::list<BTreeData *> node_keys;
+
+ node = ReadBlock (node_num);
+ 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<BTreeData *> 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<BTreeData *>::iterator it = node_keys.begin ();
+ while (it != node_keys.end ()) {
+ std::string ss1, ss2;
+ ss1 = *data;
+ ss2 = *(*it);
+ std::cout << ss1 << " == " << ss2 << std::endl;
+ 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)->SetKey (reemplazar->GetKey ());
+ reemplazar->SetKey (k->Clone ());
+
+ std::cout << "Tengo todo reemplazado ...\n";
+
+ /* Grabo los nodos */
+ WriteKeys (node, node_header, node_keys);
+ WriteNodoHeader (node, &node_header);
+ WriteBlock (node, node_num);
+ 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";
+ PrintNode (node_num);
+ PrintNode (right);
+ /* 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);
+ }
+}