4 BTree::BTree (const std::string &name, unsigned int block_size, int kt, bool create_new_file)
10 fp = fopen (name.c_str(), "wb+");
12 /* TODO : mandar una exception ? */
16 /* Nombre de archivo */
19 /* Inicializo el header */
20 header.block_size = block_size;
23 /* Creo el primer bloque vacio */
24 node = new uchar[block_size];
25 ReadNodoHeader (node, &nh);
27 nh.free_space = block_size - sizeof (BTreeNodeHeader);
29 WriteNodoHeader (node, &nh);
40 void BTree::WriteFileHeader ()
42 fseek (fp, 0L, SEEK_SET);
43 fwrite (&header, 1, sizeof (BTreeFileHeader), fp);
46 void BTree::WriteBlock (uchar *block, uint num)
49 fseek (fp, num*header.block_size, SEEK_SET);
50 fwrite (block, 1, header.block_size, fp);
53 void BTree::AddKey (const Clave &k)
56 Clave *kout = AddKeyR (k.Clone (), 0, left, right);
60 /* Debo dejar la raiz en el nodo 0, por lo que paso el nodo
61 * que esta usando el hijo izquierdo a un nuevo nodo */
62 std::list<BTreeData *> node_keys;
63 BTreeNodeHeader node_header;
64 uchar *node = ReadBlock (left);
65 ReadNodoHeader (node, &node_header);
66 node_keys = ReadKeys (node, node_header);
67 level = node_header.level + 1;
69 uchar *new_node = NewBlock (left);
70 delete [] new_node; /* No me interesa, voy a usar lo leio antes */
72 WriteKeys (node, node_header, node_keys);
73 WriteNodoHeader (node, &node_header);
74 WriteBlock (node, left);
75 DeleteKeys (node_keys);
78 /* Leo y actualizo la Raiz */
80 ReadNodoHeader (node, &node_header);
81 node_keys = std::list<BTreeData *>();
83 node_keys.push_back (new BTreeChildData (left));
84 node_keys.push_back (new BTreeData (kout, right));
86 node_header.level = level;
87 node_header.item_count = 1;
89 WriteKeys (node, node_header, node_keys);
90 WriteNodoHeader (node, &node_header);
93 DeleteKeys (node_keys);
98 Clave* BTree::AddKeyR (const Clave *k, uint node_num, uint &left_child, uint &right_child)
100 uchar *node = ReadBlock (node_num);
101 BTreeNodeHeader node_header;
102 ReadNodoHeader (node, &node_header);
105 if (node_header.level == 0)
106 return AddKeyLeafR (k, node_num, left_child, right_child);
108 return AddKeyOtherR (k, node_num, left_child, right_child);
111 Clave* BTree::AddKeyLeafR (const Clave *k, uint node_num, uint &left_child, uint &right_child)
114 std::list<BTreeData *> node_keys;
116 BTreeData *data = new BTreeLeafData (k->Clone ());
118 /* Leo el nodo raiz para empezar a agregar */
119 uchar *node = ReadBlock (node_num);
120 BTreeNodeHeader node_header;
121 ReadNodoHeader (node, &node_header);
123 if (node_header.free_space > data->Size ()) {
125 node_keys = ReadKeys (node, node_header);
126 std::list<BTreeData *>::iterator it = node_keys.begin ();
128 while (it != node_keys.end ()) {
130 if ((*data) < (*datait))
131 /* Me pase, lo agrego aca! */
135 node_keys.insert (it, data);
136 WriteKeys (node, node_header, node_keys);
137 WriteNodoHeader (node, &node_header);
138 WriteBlock (node, node_num);
139 DeleteKeys (node_keys);
142 PrintNode (node_num);
144 /* Split : Creo e inicializo el nuevo nodo */
145 std::list<BTreeData *> new_node_keys;
146 std::list<BTreeData *> old_node_keys;
147 BTreeNodeHeader new_node_header;
149 uchar *new_node = NewBlock (new_node_num);
150 ReadNodoHeader (new_node, &new_node_header);
151 new_node_header.level = node_header.level;
153 node_keys = ReadKeys (node, node_header);
154 new_node_keys = ReadKeys (new_node, new_node_header);
156 /* Agrego la clave en la lista que ya tengo de manera ordenada */
157 std::list<BTreeData *>::iterator it = node_keys.begin ();
158 std::list<BTreeData *>::iterator previt = node_keys.begin ();
160 while (it != node_keys.end ()) {
163 if ((*data) < (*datait))
164 /* Me pase, lo agrego aca! */
169 if (it != node_keys.end ())
170 node_keys.insert (it, data);
172 node_keys.push_back (data);
174 /* Tengo que guardar claves hasta ocupar nodo size/2 en cada nodo
175 * y subir la clave del medio */
176 node_header.item_count = 0;
177 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
180 it = node_keys.begin ();
181 while (it != node_keys.end ()) {
184 total_size += datait->Size ();
186 /* Hack : Si me quedo con todas las claves, en el caso de ser
187 * del mismo tama#o se desbalancea. Hay que ver que efecto
188 * puede tener en el caso de claves de long. variable
190 if (it == node_keys.end ())
191 total_size -= datait->Size ();
194 it = node_keys.begin ();
196 while (used < total_size/2) {
197 BTreeData *d = (*it);
198 old_node_keys.push_back (d);
202 kout = (*it++)->getClave (); // Esta se retorna al "padre" para que se la agregue
204 while (it != node_keys.end ()) {
205 BTreeData *d = (*it);
206 new_node_keys.push_back (d);
211 WriteKeys (node, node_header, old_node_keys);
212 WriteNodoHeader (node, &node_header);
213 WriteBlock (node, node_num);
214 WriteKeys (new_node, new_node_header, new_node_keys);
215 WriteNodoHeader (new_node, &new_node_header);
216 WriteBlock (new_node, new_node_num);
217 DeleteKeys (old_node_keys);
218 DeleteKeys (new_node_keys);
220 PrintNode (node_num);
221 PrintNode (new_node_num);
224 left_child = node_num;
225 right_child = new_node_num;
233 Clave* BTree::AddKeyOtherR (const Clave *k, uint node_num, uint &left_child, uint &right_child)
236 std::list<BTreeData *> node_keys;
238 BTreeData *data = new BTreeLeafData (k->Clone ());
240 /* Leo el nodo raiz para empezar a agregar */
241 uchar *node = ReadBlock (node_num);
242 BTreeNodeHeader node_header;
243 ReadNodoHeader (node, &node_header);
245 node_keys = ReadKeys (node, node_header);
247 std::list<BTreeData *>::iterator it = node_keys.begin ();
248 std::list<BTreeData *>::iterator posterior;
249 std::list<BTreeData *>::iterator ultima;
251 /* Se supone que la primera es un hijo :) */
252 BTreeData *lchild = (*it++);
255 while (it != node_keys.end ()) {
256 if ((*data) < (*(*it)))
262 if (it == posterior) {
263 k = AddKeyR (k, lchild->getChild (), left_child, right_child);
265 k = AddKeyR (k, (*ultima)->getChild (), left_child, right_child);
267 DeleteKeys (node_keys);
270 if (data) delete data;
276 data = new BTreeData (k->Clone (), right_child);
278 if (node_header.free_space > data->Size ()) {
280 node_keys = ReadKeys (node, node_header);
281 std::list<BTreeData *>::iterator it = node_keys.begin ();
283 while (it != node_keys.end ()) {
285 if ((*data) < (*datait))
286 /* Me pase, lo agrego aca! */
290 node_keys.insert (it, data);
291 WriteKeys (node, node_header, node_keys);
292 WriteNodoHeader (node, &node_header);
293 WriteBlock (node, node_num);
294 DeleteKeys (node_keys);
297 PrintNode (node_num);
299 /* Split : Creo e inicializo el nuevo nodo */
300 std::list<BTreeData *> new_node_keys;
301 std::list<BTreeData *> old_node_keys;
302 BTreeNodeHeader new_node_header;
304 uchar *new_node = NewBlock (new_node_num);
305 ReadNodoHeader (new_node, &new_node_header);
306 new_node_header.level = node_header.level;
308 node_keys = ReadKeys (node, node_header);
309 new_node_keys = ReadKeys (new_node, new_node_header);
311 /* Agrego la clave en la lista que ya tengo de manera ordenada */
312 std::list<BTreeData *>::iterator it = node_keys.begin ();
313 std::list<BTreeData *>::iterator previt = node_keys.begin ();
317 while (it != node_keys.end ()) {
320 if ((*data) < (*datait))
321 /* Me pase, lo agrego aca! */
326 if (it != node_keys.end ())
327 node_keys.insert (it, data);
329 node_keys.push_back (data);
331 /* Tengo que guardar claves hasta ocupar nodo size/2 en cada nodo
332 * y subir la clave del medio */
333 node_header.item_count = 0;
334 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
337 it = node_keys.begin ();
338 while (it != node_keys.end ()) {
341 total_size += datait->Size ();
343 /* Hack : Si me quedo con todas las claves, en el caso de ser
344 * del mismo tama#o se desbalancea. Hay que ver que efecto
345 * puede tener en el caso de claves de long. variable
347 if (it == node_keys.end ())
348 total_size -= datait->Size ();
351 it = node_keys.begin ();
353 while (used < total_size/2) {
354 BTreeData *d = (*it);
355 old_node_keys.push_back (d);
359 kout = (*it)->getClave (); // Esta se retorna al "padre" para que se la agregue
361 new_node_keys.push_back ( new BTreeChildData ((*it)->getChild ()));
363 while (it != node_keys.end ()) {
364 BTreeData *d = (*it);
365 new_node_keys.push_back (d);
370 WriteKeys (node, node_header, old_node_keys);
371 WriteNodoHeader (node, &node_header);
372 WriteBlock (node, node_num);
373 WriteKeys (new_node, new_node_header, new_node_keys);
374 WriteNodoHeader (new_node, &new_node_header);
375 WriteBlock (new_node, new_node_num);
376 DeleteKeys (old_node_keys);
377 DeleteKeys (new_node_keys);
379 PrintNode (node_num);
380 PrintNode (new_node_num);
383 left_child = node_num;
384 right_child = new_node_num;
392 void BTree::DelKey (const Clave &k)
395 std::cout << "========= Borrando " << s << " =================\n";
396 DelKeyR (new BTreeLeafData (k.Clone ()), 0, 0);
399 void BTree::DelKeyR (BTreeData *k, uint node_num, uint padre)
401 std::list<BTreeData *> node_keys;
402 BTreeNodeHeader node_header;
405 node = ReadBlock (node_num);
406 ReadNodoHeader (node, &node_header);
407 node_keys = ReadKeys (node, node_header);
409 std::list<BTreeData *>::iterator it = node_keys.begin ();
410 std::list<BTreeData *>::iterator ultima;
411 std::list<BTreeData *>::iterator posterior;
414 if (node_header.level != 0) {
419 while (it != node_keys.end ()) {
420 if ((*k) == (*(*it))) {
421 /* La encontre!, retorno */
422 if (node_header.level == 0) {
423 DelKeyFromLeaf (k->getClave (), node_num, padre);
426 if (it == posterior) {
427 left = lchild->getChild ();
428 right = (*it)->getChild ();
430 left = (*ultima)->getChild ();
431 right = (*it)->getChild ();
433 std::cout << "Eliminar de Nodo con hijos : " << left << " y " << right << std::endl;
434 DelKeyFromNode (k->getClave (), node_num, padre, left, right);
445 /* Si llego aca y estoy en nivel 0 (una hoja) quiere
446 * decir que no lo encontre
448 if (node_header.level == 0) {
449 std::cout << "*** Clave no encontrada ***\n";
453 /* TODO: Aca faltaria liberar memoria */
454 if (it == posterior) {
455 DelKeyR (k, lchild->getChild (), node_num);
457 DelKeyR (k, (*ultima)->getChild (), node_num);
461 void BTree::DelKeyFromLeaf (Clave *k, uint node_num, uint padre)
465 BTreeNodeHeader node_header;
466 std::list<BTreeData *> node_keys;
468 node = ReadBlock (node_num);
469 ReadNodoHeader (node, &node_header);
470 node_keys = ReadKeys (node, node_header);
472 data = new BTreeLeafData (k);
474 std::list<BTreeData *>::iterator it;
475 it = node_keys.begin ();
476 while (it != node_keys.end ()) {
477 if ((*data) == (*(*it))) {
478 node_keys.erase (it);
486 /* TODO : que pasa cuando el nodo se queda vacio o con
489 WriteKeys (node, node_header, node_keys);
490 WriteNodoHeader (node, &node_header);
491 WriteBlock (node, node_num);
493 /* Veo si se cumple la condición de minimalidad */
494 uint min_free = (header.block_size-sizeof(BTreeNodeHeader))/2;
495 if ((node_header.free_space > min_free) && (node_num != 0)) {
496 /* Oops! Debo pedir prestada clave */
500 FindBrothers (node_num, padre, hi, hd);
502 if ((pedida = GetKey (hi, 1)) != NULL) {
503 std::string s = *pedida;
504 std::cout << "Clave Pedida : " << s << std::endl;
506 pedida = ReplaceKeyInFather (node_num, padre, pedida);
508 node_keys.insert (node_keys.begin (), new BTreeLeafData (pedida));
509 } else if ((pedida = GetKey (hd, 0)) != NULL) {
510 std::string s = *pedida;
511 std::cout << "Clave Pedida : " << s << std::endl;
513 pedida = ReplaceKeyInFather (node_num, padre, pedida);
515 node_keys.push_back (new BTreeLeafData (pedida));
517 std::cout << "NADIE ME PUEDE PRESTAR, FUNDIR NODOS\n";
520 std::cout << "Join con Hermano Izquierdo\n";
524 std::cout << "Join con Hermano Derecho\n";
529 JoinNodes (join1, join2, padre);
533 WriteKeys (node, node_header, node_keys);
534 WriteNodoHeader (node, &node_header);
535 WriteBlock (node, node_num);
538 DeleteKeys (node_keys);
540 std::cout << "Borrado de una hoja listo\n";
543 void BTree::JoinNodes (uint node1, uint node2, uint padre)
545 uchar *n1, *n2, *npadre;
546 BTreeNodeHeader nh1, nh2, nhp;
547 std::list<BTreeData *> nk1, nk2, nkpadre;
550 n1 = ReadBlock (node1);
551 n2 = ReadBlock (node2);
552 npadre = ReadBlock (padre);
554 ReadNodoHeader (n1, &nh1);
555 ReadNodoHeader (n2, &nh2);
556 ReadNodoHeader (npadre, &nhp);
559 uint tmp = header.block_size - sizeof (BTreeNodeHeader);
560 uint l = tmp - nh1.free_space;
561 l += tmp - nh1.free_space;
564 std::cout << "Espacio ocupado despues de unir : " << l << " de " << tmp << std::endl;
566 nk1 = ReadKeys (n1, nh1);
567 nk2 = ReadKeys (n2, nh2);
568 nkpadre = ReadKeys (npadre, nhp);
570 /* Busco la clave a juntar con los nodos */
571 std::list<BTreeData *>::iterator it = nkpadre.begin ();
572 std::list<BTreeData *>::iterator borrar_padre;
573 std::list<BTreeData *>::iterator sig;
576 BTreeData *lchild = (*it++);
578 if (lchild->getChild () == node1) {
579 cpadre = (*it)->getClave ();
580 //nkpadre.erase (it);
583 while (it != nkpadre.end ()) {
584 if ((*it)->getChild () == node1)
588 cpadre = (*it)->getClave ();
589 //nkpadre.erase (it);
592 if (it == nkpadre.end ()) {
593 std::cout << "PANIC : Me pase sin encontrar la clave!!\n";
599 std::list<BTreeData *> newkeys;
600 std::list<BTreeData *>::iterator i;
603 while (i != nk1.end ()) {
604 newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ()));
608 newkeys.push_back ( new BTreeLeafData (cpadre->Clone ()));
610 while (i != nk2.end ()) {
611 newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ()));
615 std::cout << "Espacio ocupado por las nuevas claves : " << (newkeys.size()*4) << std::endl;
616 if ((newkeys.size()*4) > tmp) {
617 std::cout << "PANIC : El nodo fundido no entra !!!\n";
623 WriteKeys (npadre, nhp, newkeys);
624 WriteNodoHeader (npadre, &nhp);
625 WriteBlock (npadre, padre);*/
627 /* TODO: Recuperar nodo1 y nodo2 */
629 WriteKeys (n1, nh1, newkeys);
630 WriteNodoHeader (n1, &nh1);
631 WriteBlock (n1, node1);
633 /* TODO : Recuperar node2 */
634 /* Actualizo punero al padre */
635 (*sig)->setChild (node1);
637 nkpadre.erase (borrar_padre);
638 WriteKeys (npadre, nhp, nkpadre);
639 WriteNodoHeader (npadre, &nhp);
640 WriteBlock (npadre, padre);
645 DeleteKeys (nkpadre);
646 DeleteKeys (newkeys);
653 Clave *BTree::GetKey (uint node_num, char maxmin)
656 std::cout << "Nodo no me puede prestar ... es NULL\n";
661 BTreeNodeHeader node_header;
662 std::list<BTreeData *> node_keys;
664 node = ReadBlock (node_num);
665 ReadNodoHeader (node, &node_header);
666 node_keys = ReadKeys (node, node_header);
668 std::list<BTreeData *>::iterator it = node_keys.begin ();
670 if (node_header.level != 0) it++;
673 uint free = node_header.free_space; // + (*it)->Size ();
674 uint min_free = (header.block_size - sizeof (BTreeNodeHeader))/2;
675 if (free > min_free) {
676 std::cout << "No puedo prestar : Free = " << free << " Minimo = " << min_free << std::endl;
677 PrintNode (node_num);
678 WriteKeys (node, node_header, node_keys);
679 WriteNodoHeader (node, &node_header);
680 WriteBlock (node, node_num);
681 DeleteKeys (node_keys);
689 k = (*it)->getClave ()->Clone ();
690 node_keys.erase (it);
692 it = node_keys.end ();
694 k = (*it)->getClave ()->Clone ();
695 node_keys.erase (it);
698 WriteKeys (node, node_header, node_keys);
699 WriteNodoHeader (node, &node_header);
700 WriteBlock (node, node_num);
701 DeleteKeys (node_keys);
708 void BTree::FindBrothers (uint node_num, uint padre, uint &left, uint &right)
711 BTreeNodeHeader node_header;
712 std::list<BTreeData *> node_keys;
714 node = ReadBlock (padre);
715 ReadNodoHeader (node, &node_header);
716 node_keys = ReadKeys (node, node_header);
718 std::list<BTreeData *>::iterator it = node_keys.begin ();
719 std::list<BTreeData *>::iterator anterior = node_keys.begin ();
720 std::list<BTreeData *>::iterator siguiente;
722 BTreeData *lchild = (*it++);
724 if (lchild->getChild () == node_num) {
725 /* Solo tengo hermano derecho */
726 std::cout << "Hermano Izquierdo : NO TENGO" << std::endl;
728 std::cout << "Hermano Derecho : " << (*it)->getChild () << std::endl;
729 right = (*it)->getChild ();
733 while (it != node_keys.end ()) {
734 if ((*it)->getChild () == node_num)
741 std::cout << "Hermano Izquierdo : " << (*anterior)->getChild () << std::endl;
742 left = (*anterior)->getChild ();
743 if (siguiente != node_keys.end ()) {
744 right = (*siguiente)->getChild ();
745 std::cout << "Hermano Derecho : " << (*siguiente)->getChild () << std::endl;
748 std::cout << "Hermano Derecho : NO TENGO" << std::endl;
752 Clave *BTree::ReplaceKeyInFather (uint node_num, uint padre, Clave *k)
755 BTreeNodeHeader node_header;
756 std::list<BTreeData *> node_keys;
758 node = ReadBlock (padre);
759 ReadNodoHeader (node, &node_header);
760 node_keys = ReadKeys (node, node_header);
762 std::list<BTreeData *>::iterator it = node_keys.begin ();
763 std::list<BTreeData *>::iterator anterior = node_keys.begin ();
764 std::list<BTreeData *>::iterator siguiente;
766 BTreeData *lchild = (*it++);
768 if (lchild->getChild () == node_num) {
769 Clave *ret = (*it)->getClave ();
772 WriteKeys (node, node_header, node_keys);
773 WriteNodoHeader (node, &node_header);
774 WriteBlock (node, padre);
775 DeleteKeys (node_keys);
781 while (it != node_keys.end ()) {
782 if ((*it)->getChild () == node_num)
788 Clave *ret = (*it)->getClave ();
791 WriteKeys (node, node_header, node_keys);
792 WriteNodoHeader (node, &node_header);
793 WriteBlock (node, padre);
794 DeleteKeys (node_keys);
800 void BTree::DelKeyFromNode (Clave *k, uint node_num, uint padre, uint left, uint right)
804 BTreeNodeHeader node_header;
805 std::list<BTreeData *> node_keys;
807 node = ReadBlock (padre);
808 ReadNodoHeader (node, &node_header);
809 node_keys = ReadKeys (node, node_header);
812 std::cout << "Busco para la derecha y luego todo a la izquierda\n";
814 BTreeNodeHeader node_hr;
815 std::list<BTreeData *> node_keyr;
817 /* Busco la clave inmediatamente superior en el arbol */
818 padre_hijo = node_num;
820 node_r = ReadBlock (right);
821 ReadNodoHeader (node_r, &node_hr);
822 if (node_hr.level != 0) {
824 node_keyr = ReadKeys (node_r, node_hr);
825 data_r = *(node_keyr.begin ());
827 right = data_r->getChild ();
829 DeleteKeys (node_keyr);
832 } while (node_hr.level != 0);
834 std::cout << "Voy a reemplazar en el nodo " << right << std::endl;
836 /* Reemplazo la clave a borrar por la de la hoja */
837 node_keyr = ReadKeys (node_r, node_hr);
838 BTreeData *reemplazar = *(node_keyr.begin ());
840 std::string ss = *reemplazar;
841 std::cout << "Voy a reemplazar por : " << ss << std::endl;
843 BTreeData *data = new BTreeLeafData (k->Clone());
845 std::list<BTreeData *>::iterator it = node_keys.begin ();
846 while (it != node_keys.end ()) {
847 if ((*data) == (*(*it))) {
852 if (it == node_keys.end ()) {
853 std::cout << "PANIC : No encontre la clave en el nodo!!!!\n";
854 std::string s = *data;
855 std::cout << s << std::endl;
856 PrintNode (node_num);
859 (*it)->setClave (reemplazar->getClave ());
860 reemplazar->setClave (k->Clone ());
862 std::cout << "Tengo todo reemplazado ...\n";
864 /* Grabo los nodos */
865 WriteKeys (node, node_header, node_keys);
866 WriteNodoHeader (node, &node_header);
867 WriteBlock (node, padre);
868 DeleteKeys (node_keys);
871 WriteKeys (node_r, node_hr, node_keyr);
872 WriteNodoHeader (node_r, &node_hr);
873 WriteBlock (node_r, right);
874 DeleteKeys (node_keyr);
877 std::cout << "Grabe todo en disco ...\n";
879 /* Ahora debo eliminar la clave que puse en el nodo hoja */
880 std::cout << "Borro la clave desde la hoja!\n";
882 DelKeyFromLeaf (k, right, padre_hijo);
884 std::cout << "Listo, Listo!\n";
885 } else if (left != 0) {
886 std::cout << "PANIC : Deberia poder reemplazar en la derecha!!!!!\n";
889 std::cout << "PANIC : No tengo hijos para reemplazar!!!!\n";
894 void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)
896 memcpy (header, node, sizeof (BTreeNodeHeader));
899 void BTree::WriteNodoHeader (uchar *node, BTreeNodeHeader *header)
901 memcpy (node, header, sizeof (BTreeNodeHeader));
904 uchar *BTree::ReadBlock (uint num)
906 /* Como el bloque 0 se usa para el header, el Nodo "num"
907 * está en el bloque "num+1"
911 uchar *out = new uchar[header.block_size];
913 fseek (fp, num*header.block_size, SEEK_SET);
914 fread (out, 1, header.block_size, fp);
919 std::list<BTreeData *> BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_header)
921 std::list<BTreeData *> keys;
922 node += sizeof (BTreeNodeHeader);
923 uint count = node_header.item_count;
925 if (node_header.item_count == 0) return keys;
927 if (node_header.level != 0) {
928 /* Si no es una hoja, lo primero que tengo es un BTreeChildData */
929 BTreeChildData *d = new BTreeChildData (node);
935 for (uint i=0; i<count; i++) {
936 /* TODO : El tipo de clave deberia ser usado
937 * dependiendo de algun dato en el header del
940 /* TODO : Detectar si estoy en una hoja */
942 if (node_header.level == 0) {
943 data = new BTreeLeafData (node, key_type);
945 data = new BTreeData (node, key_type);
947 node += data->Size ();
948 keys.push_back (data);
954 void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<BTreeData *> &keys)
956 std::list<BTreeData *>::iterator it = keys.begin ();
958 node += sizeof (BTreeNodeHeader);
960 node_header.item_count = 0;
961 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
964 while (it != keys.end ()) {
965 BTreeData *d = (*it);
966 uchar *n = d->ToArray ();
967 acumulado += d->Size ();
968 //std::cout << "WriteKeys :: Acumulado = " << acumulado << std::endl;
969 memcpy (node, n, d->Size ());
972 node_header.free_space -= d->Size ();
973 node_header.item_count++;
977 /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */
980 void BTree::PrintNode (uint num)
982 uchar *node = ReadBlock (num);
983 BTreeNodeHeader node_header;
984 ReadNodoHeader (node, &node_header);
986 std::list<BTreeData *> node_keys = ReadKeys (node, node_header);
987 std::list<BTreeData *>::iterator it = node_keys.begin ();
989 std::cout << "Nodo : " << num << std::endl;
990 std::cout << "Level : " << node_header.level << std::endl;
991 std::cout << "Items : " << node_header.item_count << std::endl;
992 std::cout << "Free : " << node_header.free_space << " (" << (header.block_size - sizeof (BTreeNodeHeader)) << ")" << std::endl;
993 while (it != node_keys.end ()) {
994 std::string s = *(*it);
995 std::cout << s << " ";
998 std::cout << std::endl;
1001 DeleteKeys (node_keys);
1004 uchar *BTree::NewBlock (uint &num)
1010 fseek (fp, 0, SEEK_END);
1011 filelen = ftell (fp);
1013 num = filelen/header.block_size - 1;
1015 node = new uchar[header.block_size];
1016 ReadNodoHeader (node, &nh);
1018 nh.free_space = header.block_size - sizeof (BTreeNodeHeader);
1020 WriteNodoHeader (node, &nh);
1021 WriteBlock (node, num);
1026 BTreeFindResult *BTree::FindKey (const Clave &k)
1028 return FindKeyR (&k, 0);
1031 BTreeFindResult *BTree::FindKeyR (const Clave *k, uint node_num)
1033 std::list<BTreeData *> node_keys;
1034 BTreeNodeHeader node_header;
1036 /* Leo el nodo raiz para empezar a agregar */
1037 uchar *node = ReadBlock (node_num);
1038 ReadNodoHeader (node, &node_header);
1039 node_keys = ReadKeys (node, node_header);
1041 std::list<BTreeData *>::iterator it = node_keys.begin ();
1042 std::list<BTreeData *>::iterator posterior;
1043 std::list<BTreeData *>::iterator ultima;
1045 /* Se supone que la primera es un hijo :) */
1047 if (node_header.level != 0) {
1053 if (node_header.level == 0)
1054 data = new BTreeLeafData ((Clave *)k);
1056 data = new BTreeData ((Clave *)k, 0);
1058 while (it != node_keys.end ()) {
1059 if ((*data) == (*(*it))) {
1060 /* La encontre!, retorno */
1062 DeleteKeys (node_keys);
1063 BTreeFindResult *result = new BTreeFindResult ();
1064 result->node = node_num;
1065 result->header = node_header;
1069 if ((*data) < (*(*it)))
1075 /* TODO: Aca faltaria liberar memoria */
1076 if (it == posterior)
1077 return FindKeyR (k, lchild->getChild ());
1079 return FindKeyR (k, (*ultima)->getChild ());
1082 void BTree::DeleteKeys (std::list<BTreeData *> &keys)
1084 std::list<BTreeData *>::iterator it = keys.begin ();
1086 while (it != keys.end ()) {
1087 BTreeData *d = (*it);