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 if (node_header.level != 0) {
67 /* No estoy en una hoja, asi que tengo que buscar
68 * para donde moverme para agregar la clave
74 /* Estoy en una hoja, veo si lo puedo agregar */
75 BTreeLeafData data (k->Clone ());
77 if (node_header.free_space > data.Size ()) {
78 /* TODO : Insertar ordenado */
81 node_keys = ReadKeys (node, node_header);
82 std::list<BTreeData>::iterator it = node_keys.begin ();
84 while (it != node_keys.end ()) {
87 /* Me pase, lo agrego aca! */
90 node_keys.insert (it, data);
91 WriteKeys (node, node_header, node_keys);
92 WriteNodoHeader (node, &node_header);
93 WriteBlock (node, node_num);
98 /* Lo que debe pasar :
99 * 1) Separar N/2 claves en "nodo"
100 * 2) Separar N/2-1 claves en un nuevo nodo
101 * 3) kout = clave del medio y mandarla al padre
103 std::cout << "SPLIT" << std::endl;
110 void BTree::DelKey (const Clave &k) {}
112 void BTree::ReadNodoHeader (uchar *node, BTreeNodeHeader *header)
114 memcpy (header, node, sizeof (BTreeNodeHeader));
117 void BTree::WriteNodoHeader (uchar *node, BTreeNodeHeader *header)
119 memcpy (node, header, sizeof (BTreeNodeHeader));
122 uchar *BTree::ReadBlock (uint num)
124 uchar *out = new uchar[header.block_size];
126 fseek (fp, num*header.block_size + sizeof (BTreeFileHeader), SEEK_SET);
127 fread (out, 1, header.block_size, fp);
132 std::list<BTreeData> BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_header)
134 std::list<BTreeData> keys;
135 node += sizeof (BTreeNodeHeader);
137 for (uint i=0; i<node_header.item_count; i++) {
138 /* TODO : El tipo de clave deberia ser usado
139 * dependiendo de algun dato en el header del
142 /* TODO : Detectar si estoy en una hoja */
144 if (node_header.level == 0)
145 data = BTreeLeafData (node);
147 data = BTreeData (node);
148 node += data.Size ();
149 keys.push_back (data);
155 void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<BTreeData> &keys)
157 std::list<BTreeData>::iterator it = keys.begin ();
159 node += sizeof (BTreeNodeHeader);
161 node_header.item_count = 0;
162 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
164 while (it != keys.end ()) {
166 memcpy (node, d.ToArray(), d.Size ());
168 node_header.free_space -= d.Size ();
169 node_header.item_count++;
173 /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */
176 void BTree::PrintNode (uint num)
178 uchar *node = ReadBlock (num);
179 BTreeNodeHeader node_header;
180 ReadNodoHeader (node, &node_header);
182 std::list<BTreeData> node_keys = ReadKeys (node, node_header);
183 std::list<BTreeData>::iterator it = node_keys.begin ();
185 std::cout << "Nodo : " << num << std::endl;
186 std::cout << "Items : " << node_header.item_count << std::endl;
187 std::cout << "Free : " << node_header.free_space << " (" << (header.block_size - sizeof (BTreeNodeHeader)) << ")" << std::endl;
188 while (it != node_keys.end ()) {
189 std::string s = (*it);
190 std::cout << "(" << s << ") ";
192 std::cout << std::endl;