header.block_size = block_size;
header.tree_type = tt;
header.key_type = kt;
+ header.block_data_counter = 0;
+ strcpy (header.magic, "DILUMA");
+ header.magic[6] = '\0';
WriteFileHeader ();
/* Creo el primer bloque vacio */
fclose (fp);
}
+ del = filename + ".blockdel";
+
+ fp = fopen (del.c_str (), "wb");
+ if (fp != NULL) {
+ uint i;
+
+ while (fread (&i, 1, sizeof (uint), fp)) {
+ deleted_block_data.push_back (i);
+ }
+
+ fclose (fp);
+ }
+
fp = fopen (name.c_str(), "rb+");
if (!fp) {
/* TODO : mandar una exception ? */
return;
}
+ filename = name;
ReadFileHeader ();
}
it++;
}
+ del = filename + ".del";
+
+ fp = fopen (del.c_str (), "wb");
+ it = deleted_block_data.begin ();
+
+ while (it != deleted_block_data.end ()) {
+ uint i = *it;
+ fwrite (&i, 1, sizeof (uint), fp);
+ it++;
+ }
fclose (fp);
}
void BTree::AddKey (const Clave &k)
{
uint left, right;
- Clave *kout;
+ Clave *kout, *in;
+
+ in = k.Clone ();
+ in->SetBlockData ( GetNextBlockData () );
try {
- kout = AddKeyR (k.Clone (), 0, left, right);
+ kout = AddKeyR (in->Clone (), 0, left, right);
} catch (Exception *e) {
throw e;
}
+ delete in;
+
if (kout) {
unsigned short level;
/* Debo dejar la raiz en el nodo 0, por lo que paso el nodo
if ((*data) == (*(*it))) {
BTreeData *aborrar = (*it);
node_keys.erase (it);
+ deleted_block_data.push_back (aborrar->GetKey ()->GetBlockData ());
delete aborrar;
break;
}
BTreeNodeHeader nh;
std::list<uint>::iterator it;
- it = deleted_nodes.begin ();
- if (it != deleted_nodes.end ()) {
+ if (deleted_nodes.size ()) {
+ it = deleted_nodes.begin ();
num = *it;
deleted_nodes.erase (it);
} else {
{
return header.key_type;
}
+
+uint BTree::GetNextBlockData ()
+{
+ uint n;
+ if (deleted_block_data.size ()) {
+ std::list<uint>::iterator it = deleted_block_data.begin ();
+ n = *it;
+ deleted_block_data.erase (it);
+ } else {
+ n = header.block_data_counter++;
+ }
+
+ return n;
+}
+