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);
432 /* Si llego aca y estoy en nivel 0 (una hoja) quiere
433 * decir que no lo encontre
435 if (node_header.level == 0) {
436 std::cout << "Clave no encontrada\n";
440 /* TODO: Aca faltaria liberar memoria */
441 if (it == posterior) {
442 DelKeyR (k, lchild->getChild (), node_num);
444 DelKeyR (k, (*ultima)->getChild (), node_num);
448 void BTree::DelKeyFromLeaf (Clave *k, uint node_num, uint padre)
452 BTreeNodeHeader node_header;
453 std::list<BTreeData *> node_keys;
455 node = ReadBlock (node_num);
456 ReadNodoHeader (node, &node_header);
457 node_keys = ReadKeys (node, node_header);
459 data = new BTreeLeafData (k);
461 std::list<BTreeData *>::iterator it;
462 it = node_keys.begin ();
463 while (it != node_keys.end ()) {
464 if ((*data) == (*(*it))) {
465 node_keys.erase (it);
473 /* TODO : que pasa cuando el nodo se queda vacio o con
476 WriteKeys (node, node_header, node_keys);
477 WriteNodoHeader (node, &node_header);
478 WriteBlock (node, node_num);
480 /* Veo si se cumple la condición de minimalidad */
481 if ((node_header.free_space <= (header.block_size/2)) && (node_num != 0)) {
482 /* Oops! Debo pedir prestada clave */
486 FindBrothers (node_num, padre, hi, hd);
488 if ((pedida = GetKey (hi, 1)) != NULL) {
489 std::string s = *pedida;
490 std::cout << "Clave Pedida : " << s << std::endl;
492 pedida = ReplaceKeyInFather (node_num, padre, pedida);
494 node_keys.insert (node_keys.begin (), new BTreeLeafData (pedida));
495 } else if ((pedida = GetKey (hd, 0)) != NULL) {
496 std::string s = *pedida;
497 std::cout << "Clave Pedida : " << s << std::endl;
499 pedida = ReplaceKeyInFather (node_num, padre, pedida);
501 node_keys.push_back (new BTreeLeafData (pedida));
503 std::cout << "NADIE ME PUEDE PRESTAR, FUNDIR NODOS\n";
513 JoinNodes (join1, join2, padre);
517 WriteKeys (node, node_header, node_keys);
518 WriteNodoHeader (node, &node_header);
519 WriteBlock (node, node_num);
522 DeleteKeys (node_keys);
524 std::cout << "Borrado de una hoja listo\n";
527 void BTree::JoinNodes (uint node1, uint node2, uint padre)
529 uchar *n1, *n2, *npadre;
530 BTreeNodeHeader nh1, nh2, nhp;
531 std::list<BTreeData *> nk1, nk2, nkpadre;
534 n1 = ReadBlock (node1);
535 n2 = ReadBlock (node2);
536 npadre = ReadBlock (padre);
538 ReadNodoHeader (n1, &nh1);
539 ReadNodoHeader (n2, &nh2);
540 ReadNodoHeader (npadre, &nhp);
542 nk1 = ReadKeys (n1, nh1);
543 nk2 = ReadKeys (n2, nh2);
544 nkpadre = ReadKeys (npadre, nhp);
546 /* Busco la clave a juntar con los nodos */
547 std::list<BTreeData *>::iterator it = nkpadre.begin ();
548 std::list<BTreeData *>::iterator sig;
551 BTreeData *lchild = (*it++);
553 if (lchild->getChild () == node1) {
554 cpadre = (*it)->getClave ();
557 while (it != nkpadre.end ()) {
558 if ((*it)->getChild () == node1)
562 cpadre = (*it)->getClave ();
567 std::list<BTreeData *> newkeys;
568 std::list<BTreeData *>::iterator i;
571 while (i != nk1.end ()) {
572 newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ()));
576 newkeys.push_back ( new BTreeLeafData (cpadre->Clone ()));
578 while (i != nk2.end ()) {
579 newkeys.push_back ( new BTreeLeafData ((*i)->getClave ()->Clone ()));
585 WriteKeys (npadre, nhp, newkeys);
586 WriteNodoHeader (npadre, &nhp);
587 WriteBlock (npadre, padre);
589 /* TODO: Recuperar nodo1 y nodo2 */
591 WriteKeys (n1, nh1, newkeys);
592 WriteNodoHeader (n1, &nh1);
593 WriteBlock (n1, node1);
595 /* TODO : Recuperar node2 */
596 /* Actualizo punero al padre */
597 (*sig)->setChild (node1);
598 WriteKeys (npadre, nhp, nkpadre);
599 WriteNodoHeader (npadre, &nhp);
600 WriteBlock (npadre, padre);
605 DeleteKeys (nkpadre);
606 DeleteKeys (newkeys);
613 Clave *BTree::GetKey (uint node_num, char maxmin)
615 if (node_num == 0) return NULL;
618 BTreeNodeHeader node_header;
619 std::list<BTreeData *> node_keys;
621 node = ReadBlock (node_num);
622 ReadNodoHeader (node, &node_header);
623 node_keys = ReadKeys (node, node_header);
625 std::list<BTreeData *>::iterator it = node_keys.begin ();
627 if (node_header.level != 0) it++;
630 uint free = node_header.free_space + (*it)->Size ();
631 if (free > (header.block_size/2)) {
632 WriteKeys (node, node_header, node_keys);
633 WriteNodoHeader (node, &node_header);
634 WriteBlock (node, node_num);
635 DeleteKeys (node_keys);
643 k = (*it)->getClave ()->Clone ();
644 node_keys.erase (it);
646 it = node_keys.end ();
648 k = (*it)->getClave ()->Clone ();
649 node_keys.erase (it);
652 WriteKeys (node, node_header, node_keys);
653 WriteNodoHeader (node, &node_header);
654 WriteBlock (node, node_num);
655 DeleteKeys (node_keys);
662 void BTree::FindBrothers (uint node_num, uint padre, uint &left, uint &right)
665 BTreeNodeHeader node_header;
666 std::list<BTreeData *> node_keys;
668 node = ReadBlock (padre);
669 ReadNodoHeader (node, &node_header);
670 node_keys = ReadKeys (node, node_header);
672 std::list<BTreeData *>::iterator it = node_keys.begin ();
673 std::list<BTreeData *>::iterator anterior = node_keys.begin ();
674 std::list<BTreeData *>::iterator siguiente;
676 BTreeData *lchild = (*it++);
678 if (lchild->getChild () == node_num) {
679 /* Solo tengo hermano derecho */
680 std::cout << "Hermano Izquierdo : NO TENGO" << std::endl;
682 std::cout << "Hermano Derecho : " << (*it)->getChild () << std::endl;
683 right = (*it)->getChild ();
687 while (it != node_keys.end ()) {
688 if ((*it)->getChild () == node_num)
695 std::cout << "Hermano Izquierdo : " << (*anterior)->getChild () << std::endl;
696 left = (*anterior)->getChild ();
697 if (siguiente != node_keys.end ()) {
698 right = (*siguiente)->getChild ();
699 std::cout << "Hermano Derecho : " << (*siguiente)->getChild () << std::endl;
702 std::cout << "Hermano Derecho : NO TENGO" << std::endl;
706 Clave *BTree::ReplaceKeyInFather (uint node_num, uint padre, Clave *k)
709 BTreeNodeHeader node_header;
710 std::list<BTreeData *> node_keys;
712 node = ReadBlock (padre);
713 ReadNodoHeader (node, &node_header);
714 node_keys = ReadKeys (node, node_header);
716 std::list<BTreeData *>::iterator it = node_keys.begin ();
717 std::list<BTreeData *>::iterator anterior = node_keys.begin ();
718 std::list<BTreeData *>::iterator siguiente;
720 BTreeData *lchild = (*it++);
722 if (lchild->getChild () == node_num) {
723 Clave *ret = (*it)->getClave ();
726 WriteKeys (node, node_header, node_keys);
727 WriteNodoHeader (node, &node_header);
728 WriteBlock (node, padre);
729 DeleteKeys (node_keys);
735 while (it != node_keys.end ()) {
736 if ((*it)->getChild () == node_num)
742 Clave *ret = (*it)->getClave ();
745 WriteKeys (node, node_header, node_keys);
746 WriteNodoHeader (node, &node_header);
747 WriteBlock (node, padre);
748 DeleteKeys (node_keys);
754 void BTree::DelKeyFromOther (const Clave &k, BTreeFindResult *r)
758 void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)
760 memcpy (header, node, sizeof (BTreeNodeHeader));
763 void BTree::WriteNodoHeader (uchar *node, BTreeNodeHeader *header)
765 memcpy (node, header, sizeof (BTreeNodeHeader));
768 uchar *BTree::ReadBlock (uint num)
770 /* Como el bloque 0 se usa para el header, el Nodo "num"
771 * está en el bloque "num+1"
775 uchar *out = new uchar[header.block_size];
777 fseek (fp, num*header.block_size, SEEK_SET);
778 fread (out, 1, header.block_size, fp);
783 std::list<BTreeData *> BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_header)
785 std::list<BTreeData *> keys;
786 node += sizeof (BTreeNodeHeader);
787 uint count = node_header.item_count;
789 if (node_header.item_count == 0) return keys;
791 if (node_header.level != 0) {
792 /* Si no es una hoja, lo primero que tengo es un BTreeChildData */
793 BTreeChildData *d = new BTreeChildData (node);
799 for (uint i=0; i<count; i++) {
800 /* TODO : El tipo de clave deberia ser usado
801 * dependiendo de algun dato en el header del
804 /* TODO : Detectar si estoy en una hoja */
806 if (node_header.level == 0) {
807 data = new BTreeLeafData (node, key_type);
809 data = new BTreeData (node, key_type);
811 node += data->Size ();
812 keys.push_back (data);
818 void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<BTreeData *> &keys)
820 std::list<BTreeData *>::iterator it = keys.begin ();
822 node += sizeof (BTreeNodeHeader);
824 node_header.item_count = 0;
825 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
827 while (it != keys.end ()) {
828 BTreeData *d = (*it);
829 uchar *n = d->ToArray ();
830 memcpy (node, n, d->Size ());
833 node_header.free_space -= d->Size ();
834 node_header.item_count++;
838 /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */
841 void BTree::PrintNode (uint num)
843 uchar *node = ReadBlock (num);
844 BTreeNodeHeader node_header;
845 ReadNodoHeader (node, &node_header);
847 std::list<BTreeData *> node_keys = ReadKeys (node, node_header);
848 std::list<BTreeData *>::iterator it = node_keys.begin ();
850 std::cout << "Nodo : " << num << std::endl;
851 std::cout << "Level : " << node_header.level << std::endl;
852 std::cout << "Items : " << node_header.item_count << std::endl;
853 std::cout << "Free : " << node_header.free_space << " (" << (header.block_size - sizeof (BTreeNodeHeader)) << ")" << std::endl;
854 while (it != node_keys.end ()) {
855 std::string s = *(*it);
856 std::cout << s << " ";
859 std::cout << std::endl;
862 DeleteKeys (node_keys);
865 uchar *BTree::NewBlock (uint &num)
871 fseek (fp, 0, SEEK_END);
872 filelen = ftell (fp);
874 num = filelen/header.block_size - 1;
876 node = new uchar[header.block_size];
877 ReadNodoHeader (node, &nh);
879 nh.free_space = header.block_size - sizeof (BTreeNodeHeader);
881 WriteNodoHeader (node, &nh);
882 WriteBlock (node, num);
887 BTreeFindResult *BTree::FindKey (const Clave &k)
889 return FindKeyR (&k, 0);
892 BTreeFindResult *BTree::FindKeyR (const Clave *k, uint node_num)
894 std::list<BTreeData *> node_keys;
895 BTreeNodeHeader node_header;
897 /* Leo el nodo raiz para empezar a agregar */
898 uchar *node = ReadBlock (node_num);
899 ReadNodoHeader (node, &node_header);
900 node_keys = ReadKeys (node, node_header);
902 std::list<BTreeData *>::iterator it = node_keys.begin ();
903 std::list<BTreeData *>::iterator posterior;
904 std::list<BTreeData *>::iterator ultima;
906 /* Se supone que la primera es un hijo :) */
908 if (node_header.level != 0) {
914 if (node_header.level == 0)
915 data = new BTreeLeafData ((Clave *)k);
917 data = new BTreeData ((Clave *)k, 0);
919 while (it != node_keys.end ()) {
920 if ((*data) == (*(*it))) {
921 /* La encontre!, retorno */
923 DeleteKeys (node_keys);
924 BTreeFindResult *result = new BTreeFindResult ();
925 result->node = node_num;
926 result->header = node_header;
930 if ((*data) < (*(*it)))
936 /* TODO: Aca faltaria liberar memoria */
938 return FindKeyR (k, lchild->getChild ());
940 return FindKeyR (k, (*ultima)->getChild ());
943 void BTree::DeleteKeys (std::list<BTreeData *> &keys)
945 std::list<BTreeData *>::iterator it = keys.begin ();
947 while (it != keys.end ()) {
948 BTreeData *d = (*it);