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)
56 Clave* BTree::AddKeyR (const Clave *k, uint node_num)
59 std::list<BTreeData> node_keys;
61 /* Leo el nodo raiz para empezar a agregar */
62 uchar *node = ReadBlock (node_num);
63 BTreeNodeHeader node_header;
64 ReadNodoHeader (node, &node_header);
66 node_keys = ReadKeys (node, node_header);
68 if (node_header.free_space >= k->Size ()) {
69 /* TODO : Insertar ordenado */
70 /* Siempre se agrega en una hoja */
71 node_keys.push_back (BTreeLeafData (k->Clone ()));
73 WriteKeys (node, node_header, node_keys);
74 WriteNodoHeader (node, &node_header);
75 WriteBlock (node, node_num);
79 /* Lo que debe pasar :
80 * 1) Separar N/2 claves en "nodo"
81 * 2) Separar N/2-1 claves en un nuevo nodo
82 * 3) kout = clave del medio y mandarla al padre
84 std::cout << "SPLIT" << std::endl;
90 void BTree::DelKey (const Clave &k) {}
92 void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)
94 memcpy (header, node, sizeof (BTreeNodeHeader));
97 void BTree::WriteNodoHeader (uchar *node, BTreeNodeHeader *header)
99 memcpy (node, header, sizeof (BTreeNodeHeader));
102 uchar *BTree::ReadBlock (uint num)
104 uchar *out = new uchar[header.block_size];
106 fseek (fp, num*header.block_size + sizeof (BTreeFileHeader), SEEK_SET);
107 fread (out, 1, header.block_size, fp);
112 std::list<BTreeData> BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_header)
114 std::list<BTreeData> keys;
115 node += sizeof (BTreeNodeHeader);
117 for (uint i=0; i<node_header.item_count; i++) {
118 /* TODO : El tipo de clave deberia ser usado
119 * dependiendo de algun dato en el header del
122 /* TODO : Detectar si estoy en una hoja */
123 BTreeData data(node);
124 node += data.Size ();
125 keys.push_back (data);
131 void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<BTreeData> &keys)
133 std::list<BTreeData>::iterator it = keys.begin ();
135 node += sizeof (BTreeNodeHeader);
137 node_header.item_count = 0;
138 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
140 while (it != keys.end ()) {
142 memcpy (node, d.ToArray(), d.Size ());
144 node_header.free_space -= d.Size ();
145 node_header.item_count++;
149 /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */