#include "btree.h"
-BTree::BTree (const char *name, unsigned int block_size, bool create_new_file)
+BTree::BTree (const std::string &name, unsigned int block_size, bool create_new_file)
{
uchar *node;
BTreeNodeHeader nh;
- fp = fopen (name, "wb");
+ fp = fopen (name.c_str(), "wb+");
if (!fp) {
/* TODO : mandar una exception ? */
return;
/* Inicializo el header */
header.block_size = block_size;
- write_tree_header ();
+ WriteFileHeader ();
/* Creo el primer bloque vacio */
node = new uchar[block_size];
nh.free_space = block_size - sizeof (BTreeNodeHeader);
nh.item_count = 0;
WriteNodoHeader (node, &nh);
- write_block (node, 0);
+ WriteBlock (node, 0);
delete [] node;
}
fclose (fp);
}
-void BTree::write_tree_header ()
+void BTree::WriteFileHeader ()
{
fseek (fp, 0L, SEEK_SET);
fwrite (&header, 1, sizeof (BTreeFileHeader), fp);
}
-void BTree::write_block (uchar *block, uint num)
+void BTree::WriteBlock (uchar *block, uint num)
{
fseek (fp, num*header.block_size + sizeof (BTreeFileHeader), SEEK_SET);
fwrite (block, 1, header.block_size, fp);
}
-void BTree::AddKey (Clave &k)
+void BTree::AddKey (const Clave &k)
{
+ uint left, right;
+ Clave *kout = AddKeyR (&k, 0, left, right);
+
+ if (kout) {
+ unsigned short level;
+ /* Debo dejar la raiz en el nodo 0, por lo que paso el nodo
+ * que esta usando el hijo izquierdo a un nuevo nodo */
+ std::list<BTreeData *> node_keys;
+ BTreeNodeHeader node_header;
+ uchar *node = ReadBlock (left);
+ ReadNodoHeader (node, &node_header);
+ node_keys = ReadKeys (node, node_header);
+ level = node_header.level + 1;
+
+ uchar *new_node = NewBlock (left);
+ delete [] new_node; /* No me interesa, voy a usar lo leio antes */
+
+ WriteKeys (node, node_header, node_keys);
+ WriteNodoHeader (node, &node_header);
+ WriteBlock (node, left);
+ delete [] node;
+
+ /* Leo y actualizo la Raiz */
+ node = ReadBlock (0);
+ ReadNodoHeader (node, &node_header);
+ node_keys = std::list<BTreeData *>();
+
+ node_keys.push_back (new BTreeChildData (left));
+ node_keys.push_back (new BTreeData (kout, right));
+
+ node_header.level = level;
+ node_header.item_count = 1;
+
+ WriteKeys (node, node_header, node_keys);
+ WriteNodoHeader (node, &node_header);
+ WriteBlock (node, 0);
+ PrintNode (0);
+ }
+}
+
+Clave* BTree::AddKeyR (const Clave *k, uint node_num, uint &left_child, uint &right_child)
+{
+ Clave *kout = NULL;
+ std::list<BTreeData *> node_keys;
+
+ BTreeData *data = new BTreeLeafData (k->Clone ());
+
/* Leo el nodo raiz para empezar a agregar */
- uchar *root = ReadBlock (0);
- BTreeNodeHeader root_header;
- ReadNodoHeader (root, &root_header);
+ uchar *node = ReadBlock (node_num);
+ BTreeNodeHeader node_header;
+ ReadNodoHeader (node, &node_header);
+
+ if (node_header.level != 0) {
+ std::cout << "Entrando nodo level = " << node_header.level << std::endl;
+ /* No estoy en una hoja, asi que tengo que buscar
+ * para donde moverme para agregar la clave
+ */
+ std::cout << "Leo Claves" << std::endl;
+ node_keys = ReadKeys (node, node_header);
+
+ std::list<BTreeData *>::iterator it = node_keys.begin ();
+ std::list<BTreeData *>::iterator posterior;
+ std::list<BTreeData *>::iterator ultima;
+
+ /* Se supone que la primera es un hijo :) */
+ BTreeData *lchild = (*it++);
+ posterior = it;
+
+ while (it != node_keys.end ()) {
+ if ((*data) < (*(*it)))
+ break;
+ ultima = it;
+ it++;
+ }
+
+ if (it == posterior) {
+ std::cout << "==== Me voy al nodo " << lchild->getChild () << std::endl;
+ k = AddKeyR (k, lchild->getChild (), left_child, right_child);
+ } else {
+ std::cout << "==== Me voy al nodo " << (*ultima)->getChild () << std::endl;
+ k = AddKeyR (k, (*ultima)->getChild (), left_child, right_child);
+ }
+ if (k)
+ data = new BTreeData (k->Clone (), right_child);
+ }
+
+
+ /* Nada que hacer */
+ if (!k) return NULL;
+
+ /* Estoy en una hoja, veo si lo puedo agregar */
+
+ if (node_header.free_space > data->Size ()) {
+ /* TODO : Insertar ordenado */
+ BTreeData *datait;
+ node_keys = ReadKeys (node, node_header);
+ std::list<BTreeData *>::iterator it = node_keys.begin ();
- if (root_header.free_space >= k.Size ()) {
- std::cout << "Hay lugar para meter la clave" << std::endl;
+ while (it != node_keys.end ()) {
+ datait = (*it);
+ if ((*data) < (*datait))
+ /* Me pase, lo agrego aca! */
+ break;
+ it++;
+ }
+ node_keys.insert (it, data);
+ WriteKeys (node, node_header, node_keys);
+ WriteNodoHeader (node, &node_header);
+ WriteBlock (node, node_num);
+
+ PrintNode (node_num);
} else {
- std::cout << "__NO__ hay lugar para meter la clave" << std::endl;
+ std::cout << "=============== SPLIT ================" << std::endl;
+ /* Split : Creo e inicializo el nuevo nodo */
+ std::list<BTreeData *> new_node_keys;
+ std::list<BTreeData *> old_node_keys;
+ BTreeNodeHeader new_node_header;
+ uint new_node_num;
+ uchar *new_node = NewBlock (new_node_num);
+ ReadNodoHeader (new_node, &new_node_header);
+
+ node_keys = ReadKeys (node, node_header);
+ new_node_keys = ReadKeys (new_node, new_node_header);
+
+ /* Agrego la clave en la lista que ya tengo de manera ordenada */
+ std::list<BTreeData *>::iterator it = node_keys.begin ();
+
+ while (it != node_keys.end ()) {
+ BTreeData *datait;
+ datait = (*it);
+ if ((*data) < (*datait))
+ /* Me pase, lo agrego aca! */
+ break;
+ it++;
+ }
+ if (it != node_keys.end ())
+ node_keys.insert (it, data);
+ else
+ node_keys.push_back (data);
+
+ /* Tengo que guardar claves hasta ocupar nodo size/2 en cada nodo
+ * y subir la clave del medio */
+ node_header.item_count = 0;
+ node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
+
+ uint total_size = 0;
+ it = node_keys.begin ();
+ while (it != node_keys.end ()) {
+ BTreeData *datait;
+ datait = (*it);
+ total_size += datait->Size ();
+ it++;
+ /* Hack : Si me quedo con todas las claves, en el caso de ser
+ * del mismo tama#o se desbalancea. Hay que ver que efecto
+ * puede tener en el caso de claves de long. variable
+ */
+ if (it == node_keys.end ())
+ total_size -= datait->Size ();
+ }
+
+ it = node_keys.begin ();
+ uint used = 0;
+ while (used < total_size/2) {
+ BTreeData *d = (*it);
+ old_node_keys.push_back (d);
+ used += d->Size ();
+ it++;
+ }
+ kout = (*it++)->getClave (); // Esta se retorna al "padre" para que se la agregue
+ while (it != node_keys.end ()) {
+ BTreeData *d = (*it);
+ new_node_keys.push_back (d);
+ it++;
+ }
+
+ /* Guardo */
+ WriteKeys (node, node_header, old_node_keys);
+ WriteNodoHeader (node, &node_header);
+ WriteBlock (node, node_num);
+ WriteKeys (new_node, new_node_header, new_node_keys);
+ WriteNodoHeader (new_node, &new_node_header);
+ WriteBlock (new_node, new_node_num);
+
+ PrintNode (node_num);
+ PrintNode (new_node_num);
+
+ /* Paso los hijos */
+ left_child = node_num;
+ right_child = new_node_num;
+ delete [] new_node;
+ delete [] node;
}
- delete [] root;
+ return kout;
}
-void BTree::DelKey (Clave &k) {}
+void BTree::DelKey (const Clave &k) {}
void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)
{
return out;
}
+std::list<BTreeData *> BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_header)
+{
+ std::list<BTreeData *> keys;
+ node += sizeof (BTreeNodeHeader);
+ uint count = node_header.item_count;
+
+ if (node_header.item_count == 0) return keys;
+
+ if (node_header.level != 0) {
+ /* Si no es una hoja, lo primero que tengo es un BTreeChildData */
+ BTreeChildData *d = new BTreeChildData (node);
+ node += d->Size ();
+ keys.push_back (d);
+ count--;
+ }
+
+ for (uint i=0; i<count; i++) {
+ /* TODO : El tipo de clave deberia ser usado
+ * dependiendo de algun dato en el header del
+ * arbol
+ */
+ /* TODO : Detectar si estoy en una hoja */
+ BTreeData *data;
+ if (node_header.level == 0) {
+ data = new BTreeLeafData (node);
+ } else {
+ data = new BTreeData (node);
+ }
+ node += data->Size ();
+ keys.push_back (data);
+ }
+
+ return keys;
+}
+
+void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<BTreeData *> &keys)
+{
+ std::list<BTreeData *>::iterator it = keys.begin ();
+
+ node += sizeof (BTreeNodeHeader);
+
+ node_header.item_count = 0;
+ node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
+
+ while (it != keys.end ()) {
+ BTreeData *d = (*it);
+ memcpy (node, d->ToArray(), d->Size ());
+ node += d->Size ();
+ node_header.free_space -= d->Size ();
+ node_header.item_count++;
+ it++;
+ }
+
+ /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */
+}
+
+void BTree::PrintNode (uint num)
+{
+ uchar *node = ReadBlock (num);
+ BTreeNodeHeader node_header;
+ ReadNodoHeader (node, &node_header);
+
+ std::list<BTreeData *> node_keys = ReadKeys (node, node_header);
+ std::list<BTreeData *>::iterator it = node_keys.begin ();
+
+ std::cout << "Nodo : " << num << std::endl;
+ std::cout << "Level : " << node_header.level << std::endl;
+ std::cout << "Items : " << node_header.item_count << std::endl;
+ std::cout << "Free : " << node_header.free_space << " (" << (header.block_size - sizeof (BTreeNodeHeader)) << ")" << std::endl;
+ while (it != node_keys.end ()) {
+ std::string s = *(*it);
+ std::cout << s << " ";
+ it++;
+ }
+ std::cout << std::endl;
+
+ delete [] node;
+}
+
+uchar *BTree::NewBlock (uint &num)
+{
+ long filelen;
+ uchar *node;
+ BTreeNodeHeader nh;
+
+ fseek (fp, 0, SEEK_END);
+ filelen = ftell (fp);
+
+ num = (filelen - sizeof (BTreeFileHeader))/header.block_size;
+
+ node = new uchar[header.block_size];
+ ReadNodoHeader (node, &nh);
+ nh.level = 0;
+ nh.free_space = header.block_size - sizeof (BTreeNodeHeader);
+ nh.item_count = 0;
+ WriteNodoHeader (node, &nh);
+ WriteBlock (node, 0);
+
+ return node;
+}
+