#include "btree.h"
-BTree::BTree (const std::string &name, unsigned int block_size, int kt, bool create_new_file)
+BTree::BTree (const std::string &name, unsigned int block_size, int tt, int kt, bool create_new_file)
{
- key_type = kt;
uchar *node;
BTreeNodeHeader nh;
/* Inicializo el header */
header.block_size = block_size;
+ header.tree_type = tt;
+ header.key_type = kt;
+ header.block_data_counter = 0;
WriteFileHeader ();
/* Creo el primer bloque vacio */
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);
+ }
+
+ 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;
+ }
+ ReadFileHeader ();
+}
+
BTree::~BTree ()
{
fclose (fp);
+
+ std::string del = filename + ".del";
+
+ fp = fopen (del.c_str (), "wb");
+ std::list<uint>::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 ()
void BTree::AddKey (const Clave &k)
{
uint left, right;
- Clave *kout = AddKeyR (&k, 0, left, right);
+ Clave *kout, *in;
+
+ in = k.Clone ();
+ in->SetBlockData ( GetNextBlockData () );
+
+ try {
+ kout = AddKeyR (in->Clone (), 0, left, right);
+ } catch (Exception *e) {
+ throw e;
+ }
+
+ delete in;
if (kout) {
unsigned short level;
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)
while (it != node_keys.end ()) {
datait = (*it);
+ if (header.tree_type == TYPE_IDENTIFICACION) {
+ /* 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;
while (it != node_keys.end ()) {
BTreeData *datait;
datait = (*it);
+ if (header.tree_type == TYPE_IDENTIFICACION) {
+ /* 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;
posterior = it;
while (it != node_keys.end ()) {
+ if (header.tree_type == TYPE_IDENTIFICACION) {
+ /* Verifico que la clave no existea ya en el arbol */
+ if ((*data) == (*(*it))) {
+ throw new AddException ();
+ return NULL;
+ }
+ }
if ((*data) < (*(*it)))
break;
ultima = it;
while (it != node_keys.end ()) {
datait = (*it);
+ if (header.tree_type == TYPE_IDENTIFICACION) {
+ /* 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;
while (it != node_keys.end ()) {
BTreeData *datait;
datait = (*it);
+ if (header.tree_type == TYPE_IDENTIFICACION) {
+ /* 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;
if ((*data) == (*(*it))) {
BTreeData *aborrar = (*it);
node_keys.erase (it);
+ deleted_block_data.push_back (aborrar->GetKey ()->GetBlockData ());
delete aborrar;
break;
}
WriteNodoHeader (npadre, &nhp);
WriteBlock (npadre, padre);
- /* TODO: Recuperar nodo1 y nodo2 */
+ deleted_nodes.push_back (node1);
+ deleted_nodes.push_back (node2);
} else {
WriteKeys (n1, nh1, newkeys);
WriteNodoHeader (n1, &nh1);
WriteBlock (n1, node1);
- /* TODO : Recuperar node2 */
+ deleted_nodes.push_back (node2);
+
/* Actualizo punero al padre */
(*anterior)->SetChild (node1);
for (uint i=0; i<count; i++) {
BTreeData *data;
if (node_header.level == 0) {
- data = new BTreeLeafData (node, key_type);
+ data = new BTreeLeafData (node, header.key_type);
} else {
- data = new BTreeData (node, key_type);
+ data = new BTreeData (node, header.key_type);
}
node += data->Size ();
keys.push_back (data);
void BTree::AbrevKey (std::list<BTreeData *> &lst)
{
/* Claves Fijas No se abrevian */
- if (key_type == KEY_FIXED) return;
+ if (header.key_type == KEY_FIXED) return;
BTreeData *primera = NULL;
std::list<BTreeData *>::iterator it = lst.begin ();
void BTree::DeAbrevKey (std::list<BTreeData *> &lst)
{
/* Claves Fijas No se abrevian */
- if (key_type == KEY_FIXED) return;
+ if (header.key_type == KEY_FIXED) return;
BTreeData *primera = NULL;
std::list<BTreeData *>::iterator it = lst.begin ();
uchar *node;
BTreeNodeHeader nh;
- fseek (fp, 0, SEEK_END);
- filelen = ftell (fp);
+ std::list<uint>::iterator it;
- num = filelen/header.block_size - 1;
+ if (deleted_nodes.size ()) {
+ it = deleted_nodes.begin ();
+ num = *it;
+ deleted_nodes.erase (it);
+ } else {
+ fseek (fp, 0, SEEK_END);
+ filelen = ftell (fp);
+ num = filelen/header.block_size - 1;
+ }
node = new uchar[header.block_size];
ReadNodoHeader (node, &nh);
nh.level = 0;
it++;
}
}
+
+int BTree::type () const
+{
+ 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;
+}
+