From f0a17b3d23f3f277957d3e97ac1c7c1869d6d4cc Mon Sep 17 00:00:00 2001 From: Ricardo Markiewicz Date: Thu, 27 Oct 2005 23:49:24 +0000 Subject: [PATCH] Agrego emision de excepsiones en caso de error. --- src/btree.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++++---- src/btree.h | 1 + src/exception.h | 23 +++++++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 src/exception.h diff --git a/src/btree.cpp b/src/btree.cpp index 9fece76..fff8278 100644 --- a/src/btree.cpp +++ b/src/btree.cpp @@ -54,7 +54,7 @@ void BTree::WriteBlock (uchar *block, uint num) void BTree::AddKey (const Clave &k) { uint left, right; - Clave *kout = AddKeyR (&k, 0, left, right); + Clave *kout = AddKeyR (k.Clone (), 0, left, right); if (kout) { unsigned short level; @@ -103,10 +103,19 @@ Clave* BTree::AddKeyR (const Clave *k, uint node_num, uint &left_child, uint &ri ReadNodoHeader (node, &node_header); delete [] node; - if (node_header.level == 0) - return AddKeyLeafR (k, node_num, left_child, right_child); + if (node_header.level == 0) { + try { + return AddKeyLeafR (k, node_num, left_child, right_child); + } catch (Exception *e) { + throw e; + } + } - return AddKeyOtherR (k, node_num, left_child, right_child); + try { + return AddKeyOtherR (k, node_num, left_child, right_child); + } catch (Exception *e) { + throw e; + } } Clave* BTree::AddKeyLeafR (const Clave *k, uint node_num, uint &left_child, uint &right_child) @@ -128,6 +137,14 @@ Clave* BTree::AddKeyLeafR (const Clave *k, uint node_num, uint &left_child, uint while (it != node_keys.end ()) { datait = (*it); + if (tree_type == TYPE_UNIQUE) { + /* Verifico que la clave no existea ya en el arbol */ + if ((*data) == (*datait)) { + throw new AddException (); + return NULL; + } + } + if ((*data) < (*datait)) /* Me pase, lo agrego aca! */ break; @@ -161,6 +178,13 @@ 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_UNIQUE) { + /* Verifico que la clave no existea ya en el arbol */ + if ((*data) == (*datait)) { + throw new AddException (); + return NULL; + } + } if ((*data) < (*datait)) /* Me pase, lo agrego aca! */ break; @@ -254,6 +278,13 @@ Clave* BTree::AddKeyOtherR (const Clave *k, uint node_num, uint &left_child, uin posterior = it; while (it != node_keys.end ()) { + if (tree_type == TYPE_UNIQUE) { + /* Verifico que la clave no existea ya en el arbol */ + if ((*data) == (*(*it))) { + throw new AddException (); + return NULL; + } + } if ((*data) < (*(*it))) break; ultima = it; @@ -283,6 +314,13 @@ Clave* BTree::AddKeyOtherR (const Clave *k, uint node_num, uint &left_child, uin while (it != node_keys.end ()) { datait = (*it); + if (tree_type == TYPE_UNIQUE) { + /* Verifico que la clave no existea ya en el arbol */ + if ((*data) == (*datait)) { + throw new AddException (); + return NULL; + } + } if ((*data) < (*datait)) /* Me pase, lo agrego aca! */ break; @@ -318,6 +356,13 @@ 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_UNIQUE) { + /* Verifico que la clave no existea ya en el arbol */ + if ((*data) == (*datait)) { + throw new AddException (); + return NULL; + } + } if ((*data) < (*datait)) /* Me pase, lo agrego aca! */ break; diff --git a/src/btree.h b/src/btree.h index 7d64072..ef9f242 100644 --- a/src/btree.h +++ b/src/btree.h @@ -73,6 +73,7 @@ #include "clave_fija.h" #include "clave_variable.h" #include "btree_data.h" +#include "exception.h" /* alias para codear menos :) */ diff --git a/src/exception.h b/src/exception.h new file mode 100644 index 0000000..133485e --- /dev/null +++ b/src/exception.h @@ -0,0 +1,23 @@ + +#ifndef _EXCEPTION_H_ +#define _EXCEPTION_H_ + +#include + +class Exception { + public: + virtual std::string Message () { return "Error desconocido"; } +}; + +class AddException : public Exception { + public: + virtual std::string Message () { return "No se pudo agregar la clave."; } +}; + +class DelException : public Exception { + public: + virtual std::string Message () { return "No se pudo eliminar la clave."; } +}; + +#endif + -- 2.43.0