X-Git-Url: https://git.llucax.com/z.facultad/75.52/treemulator.git/blobdiff_plain/aa158a0284c1bc32a979d50275aa1b24438d46ef..HEAD:/src/btree.cpp?ds=sidebyside diff --git a/src/btree.cpp b/src/btree.cpp index 77967bf..cce0759 100644 --- a/src/btree.cpp +++ b/src/btree.cpp @@ -19,6 +19,9 @@ BTree::BTree (const std::string &name, unsigned int block_size, int tt, int kt, 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 */ @@ -49,11 +52,25 @@ BTree::BTree (const std::string &name) 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 (); } @@ -72,6 +89,16 @@ 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); } @@ -100,8 +127,7 @@ void BTree::AddKey (const Clave &k) 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); @@ -582,6 +608,7 @@ void BTree::DelKeyFromLeaf (Clave *k, uint node_num, uint padre) if ((*data) == (*(*it))) { BTreeData *aborrar = (*it); node_keys.erase (it); + deleted_block_data.push_back (aborrar->GetKey ()->GetBlockData ()); delete aborrar; break; } @@ -1298,3 +1325,18 @@ int BTree::type () const { return header.key_type; } + +uint BTree::GetNextBlockData () +{ + uint n; + if (deleted_block_data.size ()) { + std::list::iterator it = deleted_block_data.begin (); + n = *it; + deleted_block_data.erase (it); + } else { + n = header.block_data_counter++; + } + + return n; +} +