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)
394 DelKeyR (new BTreeLeafData (k.Clone ()), 0, 0);
397 void BTree::DelKeyR (BTreeData *k, uint node_num, uint padre)
399 std::list<BTreeData *> node_keys;
400 BTreeNodeHeader node_header;
403 node = ReadBlock (node_num);
404 ReadNodoHeader (node, &node_header);
405 node_keys = ReadKeys (node, node_header);
407 std::list<BTreeData *>::iterator it = node_keys.begin ();
408 std::list<BTreeData *>::iterator ultima;
409 std::list<BTreeData *>::iterator posterior;
412 if (node_header.level != 0) {
417 while (it != node_keys.end ()) {
418 if ((*k) == (*(*it))) {
419 /* La encontre!, retorno */
420 if (node_header.level == 0) {
421 DelKeyFromLeaf (k->getClave (), node_num, padre);
424 if (it == posterior) {
425 left = lchild->getChild ();
426 right = (*it)->getChild ();
428 left = (*ultima)->getChild ();
429 right = (*it)->getChild ();
431 std::cout << "Eliminar de Nodo con hijos : " << left << " y " << right << std::endl;
432 DelKeyFromNode (k->getClave (), node_num, padre, left, right);
443 /* Si llego aca y estoy en nivel 0 (una hoja) quiere
444 * decir que no lo encontre
446 if (node_header.level == 0) {
447 std::cout << "Clave no encontrada\n";
451 /* TODO: Aca faltaria liberar memoria */
452 if (it == posterior) {
453 DelKeyR (k, lchild->getChild (), node_num);
455 DelKeyR (k, (*ultima)->getChild (), node_num);
459 void BTree::DelKeyFromLeaf (Clave *k, uint node_num, uint padre)
463 BTreeNodeHeader node_header;
464 std::list<BTreeData *> node_keys;
466 node = ReadBlock (node_num);
467 ReadNodoHeader (node, &node_header);
468 node_keys = ReadKeys (node, node_header);
470 data = new BTreeLeafData (k);
472 std::list<BTreeData *>::iterator it;
473 it = node_keys.begin ();
474 while (it != node_keys.end ()) {
475 if ((*data) == (*(*it))) {
476 node_keys.erase (it);
484 /* TODO : que pasa cuando el nodo se queda vacio o con
487 WriteKeys (node, node_header, node_keys);
488 WriteNodoHeader (node, &node_header);
489 WriteBlock (node, node_num);
491 /* Veo si se cumple la condición de minimalidad */
492 if ((node_header.free_space <= ((header.block_size-sizeof(BTreeNodeHeader))/2)) && (node_num != 0)) {
493 /* Oops! Debo pedir prestada clave */
497 FindBrothers (node_num, padre, hi, hd);
499 if ((pedida = GetKey (hi, 1)) != NULL) {
500 std::string s = *pedida;
501 std::cout << "Clave Pedida : " << s << std::endl;
503 pedida = ReplaceKeyInFather (node_num, padre, pedida);
505 node_keys.insert (node_keys.begin (), new BTreeLeafData (pedida));
506 } else if ((pedida = GetKey (hd, 0)) != NULL) {
507 std::string s = *pedida;
508 std::cout << "Clave Pedida : " << s << std::endl;
510 pedida = ReplaceKeyInFather (node_num, padre, pedida);
512 node_keys.push_back (new BTreeLeafData (pedida));
514 std::cout << "NADIE ME PUEDE PRESTAR, FUNDIR NODOS\n";
524 JoinNodes (join1, join2, padre);
528 WriteKeys (node, node_header, node_keys);
529 WriteNodoHeader (node, &node_header);
530 WriteBlock (node, node_num);
533 DeleteKeys (node_keys);
535 std::cout << "Borrado de una hoja listo\n";
538 void BTree::JoinNodes (uint node1, uint node2, uint padre)
540 uchar *n1, *n2, *npadre;
541 BTreeNodeHeader nh1, nh2, nhp;
542 std::list<BTreeData *> nk1, nk2, nkpadre;
545 n1 = ReadBlock (node1);
546 n2 = ReadBlock (node2);
547 npadre = ReadBlock (padre);
549 ReadNodoHeader (n1, &nh1);
550 ReadNodoHeader (n2, &nh2);
551 ReadNodoHeader (npadre, &nhp);
553 nk1 = ReadKeys (n1, nh1);
554 nk2 = ReadKeys (n2, nh2);
555 nkpadre = ReadKeys (npadre, nhp);
557 /* Busco la clave a juntar con los nodos */
558 std::list<BTreeData *>::iterator it = nkpadre.begin ();
559 std::list<BTreeData *>::iterator sig;
562 BTreeData *lchild = (*it++);
564 if (lchild->getChild () == node1) {
565 cpadre = (*it)->getClave ();
568 while (it != nkpadre.end ()) {
569 if ((*it)->getChild () == node1)
573 cpadre = (*it)->getClave ();
578 std::list<BTreeData *> newkeys;
579 std::list<BTreeData *>::iterator i;
582 while (i != nk1.end ()) {
583 newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ()));
587 newkeys.push_back ( new BTreeLeafData (cpadre->Clone ()));
589 while (i != nk2.end ()) {
590 newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ()));
596 WriteKeys (npadre, nhp, newkeys);
597 WriteNodoHeader (npadre, &nhp);
598 WriteBlock (npadre, padre);
600 /* TODO: Recuperar nodo1 y nodo2 */
602 WriteKeys (n1, nh1, newkeys);
603 WriteNodoHeader (n1, &nh1);
604 WriteBlock (n1, node1);
606 /* TODO : Recuperar node2 */
607 /* Actualizo punero al padre */
608 (*sig)->setChild (node1);
609 WriteKeys (npadre, nhp, nkpadre);
610 WriteNodoHeader (npadre, &nhp);
611 WriteBlock (npadre, padre);
616 DeleteKeys (nkpadre);
617 DeleteKeys (newkeys);
624 Clave *BTree::GetKey (uint node_num, char maxmin)
626 if (node_num == 0) return NULL;
629 BTreeNodeHeader node_header;
630 std::list<BTreeData *> node_keys;
632 node = ReadBlock (node_num);
633 ReadNodoHeader (node, &node_header);
634 node_keys = ReadKeys (node, node_header);
636 std::list<BTreeData *>::iterator it = node_keys.begin ();
638 if (node_header.level != 0) it++;
641 uint free = node_header.free_space + (*it)->Size ();
642 if (free > ((header.block_size - sizeof (BTreeNodeHeader))/2)) {
643 WriteKeys (node, node_header, node_keys);
644 WriteNodoHeader (node, &node_header);
645 WriteBlock (node, node_num);
646 DeleteKeys (node_keys);
654 k = (*it)->getClave ()->Clone ();
655 node_keys.erase (it);
657 it = node_keys.end ();
659 k = (*it)->getClave ()->Clone ();
660 node_keys.erase (it);
663 WriteKeys (node, node_header, node_keys);
664 WriteNodoHeader (node, &node_header);
665 WriteBlock (node, node_num);
666 DeleteKeys (node_keys);
673 void BTree::FindBrothers (uint node_num, uint padre, uint &left, uint &right)
676 BTreeNodeHeader node_header;
677 std::list<BTreeData *> node_keys;
679 node = ReadBlock (padre);
680 ReadNodoHeader (node, &node_header);
681 node_keys = ReadKeys (node, node_header);
683 std::list<BTreeData *>::iterator it = node_keys.begin ();
684 std::list<BTreeData *>::iterator anterior = node_keys.begin ();
685 std::list<BTreeData *>::iterator siguiente;
687 BTreeData *lchild = (*it++);
689 if (lchild->getChild () == node_num) {
690 /* Solo tengo hermano derecho */
691 std::cout << "Hermano Izquierdo : NO TENGO" << std::endl;
693 std::cout << "Hermano Derecho : " << (*it)->getChild () << std::endl;
694 right = (*it)->getChild ();
698 while (it != node_keys.end ()) {
699 if ((*it)->getChild () == node_num)
706 std::cout << "Hermano Izquierdo : " << (*anterior)->getChild () << std::endl;
707 left = (*anterior)->getChild ();
708 if (siguiente != node_keys.end ()) {
709 right = (*siguiente)->getChild ();
710 std::cout << "Hermano Derecho : " << (*siguiente)->getChild () << std::endl;
713 std::cout << "Hermano Derecho : NO TENGO" << std::endl;
717 Clave *BTree::ReplaceKeyInFather (uint node_num, uint padre, Clave *k)
720 BTreeNodeHeader node_header;
721 std::list<BTreeData *> node_keys;
723 node = ReadBlock (padre);
724 ReadNodoHeader (node, &node_header);
725 node_keys = ReadKeys (node, node_header);
727 std::list<BTreeData *>::iterator it = node_keys.begin ();
728 std::list<BTreeData *>::iterator anterior = node_keys.begin ();
729 std::list<BTreeData *>::iterator siguiente;
731 BTreeData *lchild = (*it++);
733 if (lchild->getChild () == node_num) {
734 Clave *ret = (*it)->getClave ();
737 WriteKeys (node, node_header, node_keys);
738 WriteNodoHeader (node, &node_header);
739 WriteBlock (node, padre);
740 DeleteKeys (node_keys);
746 while (it != node_keys.end ()) {
747 if ((*it)->getChild () == node_num)
753 Clave *ret = (*it)->getClave ();
756 WriteKeys (node, node_header, node_keys);
757 WriteNodoHeader (node, &node_header);
758 WriteBlock (node, padre);
759 DeleteKeys (node_keys);
765 void BTree::DelKeyFromNode (Clave *k, uint node_num, uint padre, uint left, uint right)
769 BTreeNodeHeader node_header;
770 std::list<BTreeData *> node_keys;
772 node = ReadBlock (padre);
773 ReadNodoHeader (node, &node_header);
774 node_keys = ReadKeys (node, node_header);
777 std::cout << "Busco para la derecha y luego todo a la izquierda\n";
779 BTreeNodeHeader node_hr;
780 std::list<BTreeData *> node_keyr;
782 /* Busco la clave inmediatamente superior en el arbol */
783 padre_hijo = node_num;
785 node_r = ReadBlock (right);
786 ReadNodoHeader (node_r, &node_hr);
787 if (node_hr.level != 0) {
789 node_keyr = ReadKeys (node_r, node_hr);
790 data_r = *(node_keyr.begin ());
792 right = data_r->getChild ();
794 DeleteKeys (node_keyr);
797 } while (node_hr.level != 0);
799 std::cout << "Voy a reemplazar en el nodo " << right << std::endl;
801 /* Reemplazo la clave a borrar por la de la hoja */
802 node_keyr = ReadKeys (node_r, node_hr);
803 BTreeData *reemplazar = *(node_keyr.begin ());
805 std::string ss = *reemplazar;
806 std::cout << "Voy a reemplazar por : " << ss << std::endl;
808 BTreeData *data = new BTreeLeafData (k->Clone());
810 std::list<BTreeData *>::iterator it = node_keys.begin ();
811 while (it != node_keys.end ()) {
812 if ((*data) == (*(*it))) {
817 (*it)->setClave (reemplazar->getClave ());
818 reemplazar->setClave (k->Clone ());
820 std::cout << "Tengo todo reemplazado ...\n";
822 /* Grabo los nodos */
823 WriteKeys (node, node_header, node_keys);
824 WriteNodoHeader (node, &node_header);
825 WriteBlock (node, padre);
826 DeleteKeys (node_keys);
829 WriteKeys (node_r, node_hr, node_keyr);
830 WriteNodoHeader (node_r, &node_hr);
831 WriteBlock (node_r, right);
832 DeleteKeys (node_keyr);
835 std::cout << "Grabe todo en disco ...\n";
837 /* Ahora debo eliminar la clave que puse en el nodo hoja */
838 std::cout << "Borro la clave desde la hoja!\n";
840 DelKeyFromLeaf (k, right, padre_hijo);
842 std::cout << "Listo, Listo!\n";
843 } else if (left != 0) {
845 std::cout << "PANIC : No tengo hijos para reemplazar!!!!\n";
849 void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)
851 memcpy (header, node, sizeof (BTreeNodeHeader));
854 void BTree::WriteNodoHeader (uchar *node, BTreeNodeHeader *header)
856 memcpy (node, header, sizeof (BTreeNodeHeader));
859 uchar *BTree::ReadBlock (uint num)
861 /* Como el bloque 0 se usa para el header, el Nodo "num"
862 * está en el bloque "num+1"
866 uchar *out = new uchar[header.block_size];
868 fseek (fp, num*header.block_size, SEEK_SET);
869 fread (out, 1, header.block_size, fp);
874 std::list<BTreeData *> BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_header)
876 std::list<BTreeData *> keys;
877 node += sizeof (BTreeNodeHeader);
878 uint count = node_header.item_count;
880 if (node_header.item_count == 0) return keys;
882 if (node_header.level != 0) {
883 /* Si no es una hoja, lo primero que tengo es un BTreeChildData */
884 BTreeChildData *d = new BTreeChildData (node);
890 for (uint i=0; i<count; i++) {
891 /* TODO : El tipo de clave deberia ser usado
892 * dependiendo de algun dato en el header del
895 /* TODO : Detectar si estoy en una hoja */
897 if (node_header.level == 0) {
898 data = new BTreeLeafData (node, key_type);
900 data = new BTreeData (node, key_type);
902 node += data->Size ();
903 keys.push_back (data);
909 void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<BTreeData *> &keys)
911 std::list<BTreeData *>::iterator it = keys.begin ();
913 node += sizeof (BTreeNodeHeader);
915 node_header.item_count = 0;
916 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
918 while (it != keys.end ()) {
919 BTreeData *d = (*it);
920 uchar *n = d->ToArray ();
921 memcpy (node, n, d->Size ());
924 node_header.free_space -= d->Size ();
925 node_header.item_count++;
929 /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */
932 void BTree::PrintNode (uint num)
934 uchar *node = ReadBlock (num);
935 BTreeNodeHeader node_header;
936 ReadNodoHeader (node, &node_header);
938 std::list<BTreeData *> node_keys = ReadKeys (node, node_header);
939 std::list<BTreeData *>::iterator it = node_keys.begin ();
941 std::cout << "Nodo : " << num << std::endl;
942 std::cout << "Level : " << node_header.level << std::endl;
943 std::cout << "Items : " << node_header.item_count << std::endl;
944 std::cout << "Free : " << node_header.free_space << " (" << (header.block_size - sizeof (BTreeNodeHeader)) << ")" << std::endl;
945 while (it != node_keys.end ()) {
946 std::string s = *(*it);
947 std::cout << s << " ";
950 std::cout << std::endl;
953 DeleteKeys (node_keys);
956 uchar *BTree::NewBlock (uint &num)
962 fseek (fp, 0, SEEK_END);
963 filelen = ftell (fp);
965 num = filelen/header.block_size - 1;
967 node = new uchar[header.block_size];
968 ReadNodoHeader (node, &nh);
970 nh.free_space = header.block_size - sizeof (BTreeNodeHeader);
972 WriteNodoHeader (node, &nh);
973 WriteBlock (node, num);
978 BTreeFindResult *BTree::FindKey (const Clave &k)
980 return FindKeyR (&k, 0);
983 BTreeFindResult *BTree::FindKeyR (const Clave *k, uint node_num)
985 std::list<BTreeData *> node_keys;
986 BTreeNodeHeader node_header;
988 /* Leo el nodo raiz para empezar a agregar */
989 uchar *node = ReadBlock (node_num);
990 ReadNodoHeader (node, &node_header);
991 node_keys = ReadKeys (node, node_header);
993 std::list<BTreeData *>::iterator it = node_keys.begin ();
994 std::list<BTreeData *>::iterator posterior;
995 std::list<BTreeData *>::iterator ultima;
997 /* Se supone que la primera es un hijo :) */
999 if (node_header.level != 0) {
1005 if (node_header.level == 0)
1006 data = new BTreeLeafData ((Clave *)k);
1008 data = new BTreeData ((Clave *)k, 0);
1010 while (it != node_keys.end ()) {
1011 if ((*data) == (*(*it))) {
1012 /* La encontre!, retorno */
1014 DeleteKeys (node_keys);
1015 BTreeFindResult *result = new BTreeFindResult ();
1016 result->node = node_num;
1017 result->header = node_header;
1021 if ((*data) < (*(*it)))
1027 /* TODO: Aca faltaria liberar memoria */
1028 if (it == posterior)
1029 return FindKeyR (k, lchild->getChild ());
1031 return FindKeyR (k, (*ultima)->getChild ());
1034 void BTree::DeleteKeys (std::list<BTreeData *> &keys)
1036 std::list<BTreeData *>::iterator it = keys.begin ();
1038 while (it != keys.end ()) {
1039 BTreeData *d = (*it);