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 BTreeLeafData data (k->Clone ());
68 if (node_header.free_space > data.Size ()) {
69 /* TODO : Insertar ordenado */
70 node_keys = ReadKeys (node, node_header);
71 node_keys.push_back (data);
73 WriteKeys (node, node_header, node_keys);
74 WriteNodoHeader (node, &node_header);
75 WriteBlock (node, node_num);
78 /* Lo que debe pasar :
79 * 1) Separar N/2 claves en "nodo"
80 * 2) Separar N/2-1 claves en un nuevo nodo
81 * 3) kout = clave del medio y mandarla al padre
83 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 */
124 if (node_header.level == 0)
125 data = BTreeLeafData (node);
127 data = BTreeData (node);
128 node += data.Size ();
129 keys.push_back (data);
135 void BTree::WriteKeys (uchar *node, BTreeNodeHeader &node_header, std::list<BTreeData> &keys)
137 std::list<BTreeData>::iterator it = keys.begin ();
139 node += sizeof (BTreeNodeHeader);
141 node_header.item_count = 0;
142 node_header.free_space = header.block_size - sizeof (BTreeNodeHeader);
144 while (it != keys.end ()) {
146 memcpy (node, d.ToArray(), d.Size ());
148 node_header.free_space -= d.Size ();
149 node_header.item_count++;
153 /* TODO : incrementar node_header.item_count aca o fuera de este metodo? */