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 */
delete [] node;
}
-BTree::BTree (const std::string &name)
+BTree* BTree::Open (const std::string &name)
{
/* Leo los bloques recuperables */
- std::string del = filename + ".del";
+ FILE *fp;
+ BTree *tree = new BTree ();
+
+ std::string del = name + ".del";
fp = fopen (del.c_str (), "wb");
if (fp != NULL) {
uint i;
while (fread (&i, 1, sizeof (uint), fp)) {
- deleted_nodes.push_back (i);
+ tree->deleted_nodes.push_back (i);
}
fclose (fp);
}
- fp = fopen (name.c_str(), "rb+");
+ del = name + ".blockdel";
+
+ fp = fopen (del.c_str (), "wb");
+ if (fp != NULL) {
+ uint i;
+
+ while (fread (&i, 1, sizeof (uint), fp)) {
+ tree->deleted_block_data.push_back (i);
+ }
+
+ fclose (fp);
+ }
+
+ tree->filename = name;
+ tree->fp = fopen (name.c_str(), "rb+");
if (!fp) {
/* TODO : mandar una exception ? */
- return;
+ delete tree;
+ return NULL;
}
- ReadFileHeader ();
+ tree->ReadFileHeader ();
+
+ if (strcmp ("DILUMA", tree->header.magic)) {
+ /* Arbol no tiene magic!! */
+ delete tree;
+ return NULL;
+ }
+
+ return tree;
}
BTree::~BTree ()
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);
}
Clave *kout, *in;
in = k.Clone ();
- /* TODO : Hacer un contador con recuperacion */
- in->SetBlockData (0);
+ in->SetBlockData ( GetNextBlockData () );
try {
kout = AddKeyR (in->Clone (), 0, left, right);
if ((*data) == (*(*it))) {
BTreeData *aborrar = (*it);
node_keys.erase (it);
+ deleted_block_data.push_back (aborrar->GetKey ()->GetBlockData ());
delete aborrar;
break;
}
{
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;
+}
+