X-Git-Url: https://git.llucax.com/z.facultad/75.52/treemulator.git/blobdiff_plain/59b7476481f01388fcfd13492adbba7ef841755c..555cf8e7b36faa768e40d09665781c468424a91c:/src/btree.cpp?ds=inline diff --git a/src/btree.cpp b/src/btree.cpp index 81af642..e78a91e 100644 --- a/src/btree.cpp +++ b/src/btree.cpp @@ -3,8 +3,6 @@ BTree::BTree (const std::string &name, unsigned int block_size, int tt, int kt, bool create_new_file) { - key_type = kt; - tree_type = tt; uchar *node; BTreeNodeHeader nh; @@ -19,6 +17,8 @@ BTree::BTree (const std::string &name, unsigned int block_size, int tt, int kt, /* Inicializo el header */ header.block_size = block_size; + header.tree_type = tt; + header.key_type = kt; WriteFileHeader (); /* Creo el primer bloque vacio */ @@ -33,9 +33,52 @@ BTree::BTree (const std::string &name, unsigned int block_size, int tt, int kt, delete [] node; } +BTree::BTree (const std::string &name) +{ + /* Leo los bloques recuperables */ + std::string del = filename + ".del"; + + fp = fopen (del.c_str (), "wb"); + if (fp != NULL) { + uint i; + + while (fread (&i, 1, sizeof (uint), fp)) { + deleted_nodes.push_back (i); + } + + fclose (fp); + } + + fp = fopen (name.c_str(), "rb+"); + if (!fp) { + /* TODO : mandar una exception ? */ + return; + } + ReadFileHeader (); +} + 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++; + } + + fclose (fp); +} + +void BTree::ReadFileHeader () +{ + fseek (fp, 0L, SEEK_SET); + fread (&header, 1, sizeof (BTreeFileHeader), fp); } void BTree::WriteFileHeader () @@ -143,7 +186,7 @@ Clave* BTree::AddKeyLeafR (const Clave *k, uint node_num, uint &left_child, uint while (it != node_keys.end ()) { datait = (*it); - if (tree_type == TYPE_IDENTIFICACION) { + if (header.tree_type == TYPE_IDENTIFICACION) { /* Verifico que la clave no existea ya en el arbol */ if ((*data) == (*datait)) { throw new AddException (); @@ -184,7 +227,7 @@ Clave* BTree::AddKeyLeafR (const Clave *k, uint node_num, uint &left_child, uint while (it != node_keys.end ()) { BTreeData *datait; datait = (*it); - if (tree_type == TYPE_IDENTIFICACION) { + if (header.tree_type == TYPE_IDENTIFICACION) { /* Verifico que la clave no existea ya en el arbol */ if ((*data) == (*datait)) { throw new AddException (); @@ -284,7 +327,7 @@ Clave* BTree::AddKeyOtherR (const Clave *k, uint node_num, uint &left_child, uin posterior = it; while (it != node_keys.end ()) { - if (tree_type == TYPE_IDENTIFICACION) { + if (header.tree_type == TYPE_IDENTIFICACION) { /* Verifico que la clave no existea ya en el arbol */ if ((*data) == (*(*it))) { throw new AddException (); @@ -320,7 +363,7 @@ Clave* BTree::AddKeyOtherR (const Clave *k, uint node_num, uint &left_child, uin while (it != node_keys.end ()) { datait = (*it); - if (tree_type == TYPE_IDENTIFICACION) { + if (header.tree_type == TYPE_IDENTIFICACION) { /* Verifico que la clave no existea ya en el arbol */ if ((*data) == (*datait)) { throw new AddException (); @@ -362,7 +405,7 @@ Clave* BTree::AddKeyOtherR (const Clave *k, uint node_num, uint &left_child, uin while (it != node_keys.end ()) { BTreeData *datait; datait = (*it); - if (tree_type == TYPE_IDENTIFICACION) { + if (header.tree_type == TYPE_IDENTIFICACION) { /* Verifico que la clave no existea ya en el arbol */ if ((*data) == (*datait)) { throw new AddException (); @@ -1037,9 +1080,9 @@ std::list BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_heade for (uint i=0; iSize (); keys.push_back (data); @@ -1052,7 +1095,7 @@ std::list BTree::ReadKeys (uchar *node, BTreeNodeHeader &node_heade void BTree::AbrevKey (std::list &lst) { /* Claves Fijas No se abrevian */ - if (key_type == KEY_FIXED) return; + if (header.key_type == KEY_FIXED) return; BTreeData *primera = NULL; std::list::iterator it = lst.begin (); @@ -1067,7 +1110,7 @@ void BTree::AbrevKey (std::list &lst) void BTree::DeAbrevKey (std::list &lst) { /* Claves Fijas No se abrevian */ - if (key_type == KEY_FIXED) return; + if (header.key_type == KEY_FIXED) return; BTreeData *primera = NULL; std::list::iterator it = lst.begin (); @@ -1138,9 +1181,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 { @@ -1247,5 +1290,5 @@ void BTree::DeleteKeys (std::list &keys) int BTree::type () const { - return key_type; + return header.key_type; }