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 (Clave &k)
53 /* Leo el nodo raiz para empezar a agregar */
54 uchar *root = ReadBlock (0);
55 BTreeNodeHeader root_header;
56 ReadNodoHeader (root, &root_header);
58 if (root_header.free_space >= k.Size ()) {
59 std::cout << "Hay lugar para meter la clave" << std::endl;
61 std::cout << "__NO__ hay lugar para meter la clave" << std::endl;
67 void BTree::DelKey (Clave &k) {}
69 void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)
71 memcpy (header, node, sizeof (BTreeNodeHeader));
74 void BTree::WriteNodoHeader (uchar *node, BTreeNodeHeader *header)
76 memcpy (node, header, sizeof (BTreeNodeHeader));
79 uchar *BTree::ReadBlock (uint num)
81 uchar *out = new uchar[header.block_size];
83 fseek (fp, num*header.block_size + sizeof (BTreeFileHeader), SEEK_SET);
84 fread (out, 1, header.block_size, fp);