X-Git-Url: https://git.llucax.com/z.facultad/75.52/treemulator.git/blobdiff_plain/46f8786311d03a7d9869d814a3d6f020a3498ed4..baff628083af0bab49fafdab5ed0445a6407f2de:/src/btree.cpp?ds=sidebyside diff --git a/src/btree.cpp b/src/btree.cpp index 6771746..2701b3a 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 */ @@ -33,9 +36,88 @@ BTree::BTree (const std::string &name, unsigned int block_size, int tt, int kt, delete [] node; } +BTree* BTree::Open (const std::string &name) +{ + /* Leo los bloques recuperables */ + 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)) { + tree->deleted_nodes.push_back (i); + } + + fclose (fp); + } + + 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 ? */ + delete tree; + return NULL; + } + tree->ReadFileHeader (); + + if (strcmp ("DILUMA", tree->header.magic)) { + /* Arbol no tiene magic!! */ + delete tree; + return NULL; + } + + return tree; +} + BTree::~BTree () { fclose (fp); + + std::string del = filename + ".del"; + + fp = fopen (del.c_str (), "wb"); + std::list::iterator it = deleted_nodes.begin (); + + while (it != deleted_nodes.end ()) { + uint i = *it; + fwrite (&i, 1, sizeof (uint), fp); + 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::ReadFileHeader () +{ + fseek (fp, 0L, SEEK_SET); + fread (&header, 1, sizeof (BTreeFileHeader), fp); } void BTree::WriteFileHeader () @@ -54,14 +136,19 @@ void BTree::WriteBlock (uchar *block, uint num) 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 @@ -533,6 +620,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; } @@ -1138,9 +1226,9 @@ uchar *BTree::NewBlock (uint &num) BTreeNodeHeader nh; std::list::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 { @@ -1249,3 +1337,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; +} +