4 BTree::BTree (const std::string &name, unsigned int block_size, bool create_new_file)
9 fp = fopen (name.c_str(), "wb+");
11 /* TODO : mandar una exception ? */
15 /* Nombre de archivo */
18 /* Inicializo el header */
19 header.block_size = block_size;
22 /* Creo el primer bloque vacio */
23 node = new uchar[block_size];
24 ReadNodoHeader (node, &nh);
26 nh.free_space = block_size - sizeof (BTreeNodeHeader);
28 WriteNodoHeader (node, &nh);
39 void BTree::WriteFileHeader ()
41 fseek (fp, 0L, SEEK_SET);
42 fwrite (&header, 1, sizeof (BTreeFileHeader), fp);
45 void BTree::WriteBlock (uchar *block, uint num)
47 fseek (fp, num*header.block_size + sizeof (BTreeFileHeader), SEEK_SET);
48 fwrite (block, 1, header.block_size, fp);
51 void BTree::AddKey (const Clave &k)
54 Clave *kout = AddKeyR (&k, 0, left, right);
57 std::cout << "Raiz fue spliteada!!" << std::endl;
61 Clave* BTree::AddKeyR (const Clave *k, uint node_num, uint &left_child, uint &right_child)
64 std::list<BTreeData> node_keys;
66 BTreeLeafData data (k->Clone ());
68 /* Leo el nodo raiz para empezar a agregar */
69 uchar *node = ReadBlock (node_num);
70 BTreeNodeHeader node_header;
71 ReadNodoHeader (node, &node_header);
73 if (node_header.level != 0) {
74 /* No estoy en una hoja, asi que tengo que buscar
75 * para donde moverme para agregar la clave
81 /* Estoy en una hoja, veo si lo puedo agregar */
83 if (node_header.free_space > data.Size ()) {
84 /* TODO : Insertar ordenado */
87 node_keys = ReadKeys (node, node_header);
88 std::list<BTreeData>::iterator it = node_keys.begin ();
90 while (it != node_keys.end ()) {
93 /* Me pase, lo agrego aca! */
97 node_keys.insert (it, data);
98 WriteKeys (node, node_header, node_keys);
99 WriteNodoHeader (node, &node_header);
100 WriteBlock (node, node_num);
102 PrintNode (node_num);
104 std::cout << "=============== SPLIT ================" << std::endl;
105 /* Split : Creo e inicializo el nuevo nodo */
106 std::list<BTreeData> new_node_keys;
107 std::list<BTreeData> old_node_keys;
108 BTreeNodeHeader new_node_header;
110 uchar *new_node = NewBlock (new_node_num);
111 ReadNodoHeader (new_node, &new_node_header);
113 node_keys = ReadKeys (node, node_header);
114 new_node_keys = ReadKeys (new_node, new_node_header);
116 /* Agrego la clave en la lista que ya tengo de manera ordenada */
117 std::list<BTreeData>::iterator it = node_keys.begin ();
119 while (it != node_keys.end ()) {
123 /* Me pase, lo agrego aca! */
127 if (it != node_keys.end ())
128 node_keys.insert (it, data);
130 node_keys.push_back (data);
132 /* Tengo que guardar claves hasta ocupar nodo size/2 en cada nodo
133 * y subir la clave del medio */
134 node_header.item_count = 0;
135 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
138 it = node_keys.begin ();
139 while (it != node_keys.end ()) {
142 total_size += datait.Size ();
144 /* Hack : Si me quedo con todas las claves, en el caso de ser
145 * del mismo tama#o se desbalancea. Hay que ver que efecto
146 * puede tener en el caso de claves de long. variable
148 if (it == node_keys.end ())
149 total_size -= datait.Size ();
152 it = node_keys.begin ();
154 while (used < total_size/2) {
156 std::cout << used << " " << total_size << std::endl;
157 old_node_keys.push_back (d);
161 kout = (*it++).getClave (); // Esta se retorna al "padre" para que se la agregue
162 while (it != node_keys.end ()) {
164 new_node_keys.push_back (d);
169 WriteKeys (node, node_header, old_node_keys);
170 WriteNodoHeader (node, &node_header);
171 WriteBlock (node, node_num);
172 WriteKeys (new_node, new_node_header, new_node_keys);
173 WriteNodoHeader (new_node, &new_node_header);
174 WriteBlock (new_node, new_node_num);
176 PrintNode (node_num);
177 PrintNode (new_node_num);
180 left_child = node_num;
181 right_child = new_node_num;
189 void BTree::DelKey (const Clave &k) {}
191 void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)
193 memcpy (header, node, sizeof (BTreeNodeHeader));
196 void BTree::WriteNodoHeader (uchar *node, BTreeNodeHeader *header)
198 memcpy (node, header, sizeof (BTreeNodeHeader));
201 uchar *BTree::ReadBlock (uint num)
203 uchar *out = new uchar[header.block_size];
205 fseek (fp, num*header.block_size + sizeof (BTreeFileHeader), SEEK_SET);
206 fread (out, 1, header.block_size, fp);
211 std::list<BTreeData> BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_header)
213 std::list<BTreeData> keys;
214 node += sizeof (BTreeNodeHeader);
216 for (uint i=0; i<node_header.item_count; i++) {
217 /* TODO : El tipo de clave deberia ser usado
218 * dependiendo de algun dato en el header del
221 /* TODO : Detectar si estoy en una hoja */
223 if (node_header.level == 0)
224 data = BTreeLeafData (node);
226 data = BTreeData (node);
227 node += data.Size ();
228 keys.push_back (data);
234 void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<BTreeData> &keys)
236 std::list<BTreeData>::iterator it = keys.begin ();
238 node += sizeof (BTreeNodeHeader);
240 node_header.item_count = 0;
241 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
243 while (it != keys.end ()) {
245 memcpy (node, d.ToArray(), d.Size ());
247 node_header.free_space -= d.Size ();
248 node_header.item_count++;
252 /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */
255 void BTree::PrintNode (uint num)
257 uchar *node = ReadBlock (num);
258 BTreeNodeHeader node_header;
259 ReadNodoHeader (node, &node_header);
261 std::list<BTreeData> node_keys = ReadKeys (node, node_header);
262 std::list<BTreeData>::iterator it = node_keys.begin ();
264 std::cout << "Nodo : " << num << std::endl;
265 std::cout << "Items : " << node_header.item_count << std::endl;
266 std::cout << "Free : " << node_header.free_space << " (" << (header.block_size - sizeof (BTreeNodeHeader)) << ")" << std::endl;
267 while (it != node_keys.end ()) {
268 std::string s = (*it);
269 std::cout << "(" << s << ") ";
272 std::cout << std::endl;
277 uchar *BTree::NewBlock (uint &num)
283 fseek (fp, 0, SEEK_END);
284 filelen = ftell (fp);
286 num = (filelen - sizeof (BTreeFileHeader))/header.block_size;
288 node = new uchar[header.block_size];
289 ReadNodoHeader (node, &nh);
291 nh.free_space = header.block_size - sizeof (BTreeNodeHeader);
293 WriteNodoHeader (node, &nh);
294 WriteBlock (node, 0);