node_keys.push_back (new BTreeLeafData (pedida));
} else {
std::cout << "NADIE ME PUEDE PRESTAR, FUNDIR NODOS\n";
+ uint join1, join2;
+ if (hi != 0) {
+ join1 = hi;
+ join2 = node_num;
+ } else {
+ join1 = node_num;
+ join2 = hd;
+ }
+
+ JoinNodes (join1, join2, padre);
return;
}
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<BTreeData *> nk1, nk2, nkpadre;
+
+ /* Leo los nodos */
+ n1 = ReadBlock (node1);
+ n2 = ReadBlock (node2);
+ npadre = ReadBlock (padre);
+
+ ReadNodoHeader (n1, &nh1);
+ ReadNodoHeader (n2, &nh2);
+ ReadNodoHeader (npadre, &nhp);
+
+ nk1 = ReadKeys (n1, nh1);
+ nk2 = ReadKeys (n2, nh2);
+ nkpadre = ReadKeys (npadre, nhp);
+
+ /* Busco la clave a juntar con los nodos */
+ std::list<BTreeData *>::iterator it = nkpadre.begin ();
+ Clave *cpadre;
+ BTreeData *lchild = (*it++);
+
+ if (lchild->getChild () == node1) {
+ cpadre = (*it)->getClave ();
+ } else {
+ while (it != nkpadre.end ()) {
+ if ((*it)->getChild () == node1)
+ break;
+ it++;
+ }
+ cpadre = (*it)->getClave ();
+ }
+
+ std::list<BTreeData *> newkeys;
+ std::list<BTreeData *>::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++;
+ }
+
+ if (padre == 0) {
+ nhp.level = 0;
+ WriteKeys (npadre, nhp, newkeys);
+ WriteNodoHeader (npadre, &nhp);
+ WriteBlock (npadre, padre);
+ } else {
+ std::cout << "TODO : Fundir en NO PADRE" << std::endl;
+ }
+
+ 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_header.level != 0) it++;
Clave *k;
+ uint free = node_header.free_space + (*it)->Size ();
+ if (free > (header.block_size/2)) {
+ 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);
WriteBlock (node, node_num);
DeleteKeys (node_keys);
+ delete [] node;
+
return k;
}