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);
29 write_block (node, 0);
39 void BTree::write_tree_header ()
41 fseek (fp, 0L, SEEK_SET);
42 fwrite (&header, 1, sizeof (BTreeFileHeader), fp);
45 void BTree::write_block (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)
53 std::list<Clave *> node_keys;
55 /* Leo el nodo raiz para empezar a agregar */
56 uchar *root = ReadBlock (0);
57 BTreeNodeHeader root_header;
58 ReadNodoHeader (root, &root_header);
60 node_keys = ReadKeys (root, root_header);
61 std::cout << node_keys.size () << std::endl;
63 if (root_header.free_space >= k.Size ()) {
64 std::cout << "Hay lugar para meter la clave" << std::endl;
66 std::cout << "__NO__ hay lugar para meter la clave" << std::endl;
72 void BTree::DelKey (const Clave &k) {}
74 void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)
76 memcpy (header, node, sizeof (BTreeNodeHeader));
79 void BTree::WriteNodoHeader (uchar *node, BTreeNodeHeader *header)
81 memcpy (node, header, sizeof (BTreeNodeHeader));
84 uchar *BTree::ReadBlock (uint num)
86 uchar *out = new uchar[header.block_size];
88 fseek (fp, num*header.block_size + sizeof (BTreeFileHeader), SEEK_SET);
89 fread (out, 1, header.block_size, fp);
94 std::list<Clave *> BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_header)
96 std::list<Clave *> keys;
97 node += sizeof (BTreeNodeHeader);
99 for (uint i=0; i<node_header.item_count; i++) {
100 /* TODO : El tipo de clave deberia ser usado
101 * dependiendo de algun dato en el header del
104 ClaveFija *k = new ClaveFija (node);
112 void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<Clave *> &keys)
114 std::list<Clave *>::iterator it = keys.begin ();
116 node += sizeof (BTreeNodeHeader);
118 while (it != keys.end ()) {
120 memcpy (node, c->ToArray(), c->Size ());
125 /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */