obiavl.c 55.6 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 * OBIDMS AVL tree functions	                                            *
 ****************************************************************************/

/**
 * @file obiavl.c
 * @author Celine Mercier
 * @date December 3rd 2015
9
 * @brief Functions handling AVL trees for storing and retrieving blobs.
10 11 12 13 14 15 16 17 18 19 20 21
 */


#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <math.h>

22
#include "bloom.h"
23
#include "crc64.h"
24
#include "obiavl.h"
25
#include "obiblob.h"
26 27 28 29
#include "obierrno.h"
#include "obitypes.h"
#include "obidebug.h"
#include "encode.h"
30
#include "utils.h"
31 32 33 34 35


#define DEBUG_LEVEL 0	// TODO has to be defined somewhere else (cython compil flag?)


36

37 38 39 40 41 42
/**************************************************************************
 *
 * D E C L A R A T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
 *
 **************************************************************************/

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/**
 * @brief Internal function building the complete AVL name for an AVL with an associated index (for AVL groups).
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param avl_name The base name of the AVL tree.
 * @param avl_idx The index associated with that AVL.
 *
 * @returns A pointer to the complete name of the AVL.
 * @retval NULL if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static char* build_avl_name_with_idx(const char* avl_name, int avl_idx);


/**
 * @brief Internal function building the full path of an AVL directory containing an AVL or an AVL group.
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param dms A pointer to the OBIDMS to which the AVL tree belongs.
 * @param avl_name The name of the AVL tree or the base name of the AVL tree group.
 *
 * @returns A pointer to the full path of the AVL directory.
 * @retval NULL if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
char* get_full_path_of_avl_dir(OBIDMS_p dms, const char* avl_name);


78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * @brief Internal function building the file name for an AVL tree file.
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param avl_name The name of the AVL tree.
 *
 * @returns A pointer to the name of the file where the AVL tree is stored.
 * @retval NULL if an error occurred.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static char* build_avl_file_name(const char* avl_name);


/**
 * @brief Internal function building the file name for an AVL tree file.
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param avl_name The name of the AVL tree.
 *
 * @returns A pointer to the name of the file where the data referred to by the AVL tree is stored.
 * @retval NULL if an error occurred.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static char* build_avl_data_file_name(const char* avl_name);


/**
111 112
 * @brief Internal function returning the size of an AVL tree header on this platform,
 *        including the size of the bloom filter associated with the AVL tree.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
 *
 * @returns The size of an AVL tree header in bytes.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
size_t get_avl_header_size();


/**
 * @brief Internal function returning the initial size of an AVL tree on this platform.
 *
 * @returns The initial size of an AVL tree in bytes.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
size_t get_initial_avl_size();


/**
 * @brief Internal function returning the size, on this platform, of the header of the data
 *        referred to by an AVL tree.
 *
 * @returns The size of an AVL data header in bytes.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
size_t get_avl_data_header_size();


/**
 * @brief Internal function returning the initial size, on this platform, of the data
 *        referred to by an AVL tree.
 *
 * @returns The initial size of an AVL data array in bytes.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
size_t get_initial_avl_data_size();


/**
158
 * @brief Internal function truncating an AVL tree file to the minimum size that is a multiple of the page size.
159
 *
160
 * @param avl A pointer to the AVL tree structure.
161 162 163 164
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
165
 * @since April 2016
166 167
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
int truncate_avl_to_size_used(OBIDMS_avl_p avl);


/**
 * @brief Internal function truncating an AVL tree data file to the minimum size that is a multiple of the page size.
 *
 * @param avl A pointer to the the data structure referred to by an AVL tree.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int truncate_avl_data_to_size_used(OBIDMS_avl_data_p avl_data);
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209


/**
 * @brief Internal function enlarging an AVL tree.
 *
 * @param avl A pointer to the AVL tree structure.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int grow_avl(OBIDMS_avl_p avl);


/**
 * @brief Internal function enlarging the data array referred to by an AVL tree.
 *
 * @param avl A pointer to the AVL tree structure.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
210
int grow_avl_data(OBIDMS_avl_data_p avl_data);
211 212 213


/**
214
 * @brief Internal function closing an AVL data structure where the data referred to by an AVL tree is stored.
215
 *
216
 * @param avl_data A pointer to the data structure referred to by an AVL tree.
217
 *
218
 * @retval 0 if the operation was successfully completed.
219 220 221 222 223
 * @retval -1 if an error occurred.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
int close_avl_data(OBIDMS_avl_data_p avl_data);


/**
 * @brief Internal function unmapping the tree and data parts of an AVL tree structure.
 *
 * @param avl A pointer to the AVL tree structure.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int unmap_an_avl(OBIDMS_avl_p avl);


/**
 * @brief Internal function (re)mapping the tree and data parts of an AVL tree structure.
 *
 * @param avl A pointer to the AVL tree structure.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int remap_an_avl(OBIDMS_avl_p avl);


/**
 * @brief Internal function (re)mapping the tree and data parts of an AVL tree structure.
 *
 * @param avl A pointer to the AVL tree group structure.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int add_new_avl_in_group(OBIDMS_avl_group_p avl_group);


/**
 * @brief Internal function testing if a value might already be stored in an AVL tree.
 *
 * The function checks a bloom filter. No false negatives, possible false positives.
 *
 * @param avl A pointer to the AVL tree structure.
275
 * @param value A pointer to the blob structure.
276 277 278 279 280 281 282
 *
 * @retval 0 if the value is definitely not already stored in the AVL tree.
 * @retval 1 if the value might already be stored in the AVL tree.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
283
int maybe_in_avl(OBIDMS_avl_p avl, Obi_blob_p value);
284 285


286
/**
287
 * @brief Internal function storing a value (blob) in the data array referred to by an AVL tree.
288 289
 *
 * @param avl A pointer to the AVL tree structure.
290
 * @param value A pointer to the value (blob structure).
291 292 293 294 295 296 297
 *
 * @returns The index of the stored value.
 * @retval -1 if an error occurred.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
298
index_t avl_add_value_in_data_array(OBIDMS_avl_p avl, Obi_blob_p value);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318


/**
 * @brief Internal function initializing a node in an AVL tree.
 *
 * @param avl A pointer to the AVL tree structure.
 * @param node_idx The index of the node to initialize in the mmapped AVL tree.
 *
 * @returns The node structure initialized.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
AVL_node_p avl_create_node(OBIDMS_avl_p avl, index_t node_idx);


/**
 * @brief Internal function updating the balance factors in an AVL tree
 *        after adding a node, only in the subtree that will have to be balanced.
 *        That subtree is found using the avl->path_idx array and the directions taken
319
 *        down the tree to add the new node are stored in the avl->path_dir array.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
 *
 * @param avl A pointer to the AVL tree structure.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
void avl_update_balance_factors(OBIDMS_avl_p avl);


/**
 * @brief Internal function rotating a node with a "left left rotation".
 *
 * @param avl A pointer to the AVL tree structure.
 * @param node A pointer to the node that has to be rotated.
 * @param node_idx The index of the node that has to be rotated.
 *
 * @returns The new root of the subtree.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
index_t avl_rotate_leftleft(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx);

/**
 * @brief Internal function rotating a node with a "left right rotation".
 *
 * @param avl A pointer to the AVL tree structure.
 * @param node A pointer to the node that has to be rotated.
 * @param node_idx The index of the node that has to be rotated.
 *
 * @returns The new root of the subtree.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
index_t avl_rotate_leftright(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx);


/**
 * @brief Internal function rotating a node with a "right left rotation".
 *
 * @param avl A pointer to the AVL tree structure.
 * @param node A pointer to the node that has to be rotated.
 * @param node_idx The index of the node that has to be rotated.
 *
 * @returns The new root of the subtree.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
index_t avl_rotate_rightleft(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx);


/**
 * @brief Internal function rotating a node with a "right right rotation".
 *
 * @param avl A pointer to the AVL tree structure.
 * @param node A pointer to the node that has to be rotated.
 * @param node_idx The index of the node that has to be rotated.
 *
 * @returns The new root of the subtree.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
index_t avl_rotate_rightright(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx);


/**
 * @brief Internal function balancing one node.
 *
 * @param avl A pointer to the AVL tree structure.
 * @param node A pointer to the node that has to be balanced.
 * @param node_idx The index of the node that has to be balanced.
 *
 * @returns The new root of the subtree.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
index_t avl_balance_node(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx);


/**
 * @brief Internal function balancing the nodes of an AVL tree after adding a node,
 *        only in the subtree that eventually has to be balanced.
 *        That subtree is found using the avl->path_idx array.
 *
 * @param avl A pointer to the AVL tree structure.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
void avl_balance(OBIDMS_avl_p avl);


/**
 * @brief Internal function printing a depth first traverse of a node.
 *
 * @param avl A pointer to the AVL tree structure.
 * @param node A pointer to the node.
 * @param node_idx The index of the node.
 * @param depth The depth of the node.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
void avl_print_node(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx, int depth);


/**
 * @brief Internal function printing a depth first traverse of an AVL tree.
 *
 * @param avl A pointer to the AVL tree structure.
 *
 * @since December 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
void avl_print(OBIDMS_avl_p avl);


/************************************************************************
 *
 * D E F I N I T I O N   O F   T H E   P R I V A T E   F U N C T I O N S
 *
 ************************************************************************/

447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
static char* build_avl_name_with_idx(const char* avl_name, int avl_idx)
{
	char* avl_name_with_idx;
	int   avl_idx_length;

	if (avl_idx < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError building an AVL tree name with index: index < 0");
		return NULL;
	}

	avl_idx_length = avl_idx == 0 ? 1 : (int)(log10(avl_idx)+1);
	avl_name_with_idx = malloc((strlen(avl_name) + avl_idx_length + 2)*sizeof(char));
	if (avl_name_with_idx == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for an AVL name");
		return NULL;
	}
	if (sprintf(avl_name_with_idx, "%s_%u", avl_name, avl_idx) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError building an AVL tree name with index");
		return NULL;
	}

	return avl_name_with_idx;
}


char* get_full_path_of_avl_dir(OBIDMS_p dms, const char* avl_name)
{
	char* avl_dir_name;

482
	avl_dir_name = obi_dms_get_full_path(dms, INDEXER_DIR_NAME);
483 484 485 486 487 488 489 490 491 492 493 494
	if (avl_dir_name == NULL)
	{
		obidebug(1, "\nError getting path for the DMS AVL directory");
		return NULL;
	}
	strcat(avl_dir_name, "/");
	strcat(avl_dir_name, avl_name);

	return avl_dir_name;
}


495 496 497 498
static char* build_avl_file_name(const char* avl_name)
{
	char* file_name;

499 500
	// Test if the AVL name is not too long
	if (strlen(avl_name) >= AVL_MAX_NAME)
501 502
	{
		obi_set_errno(OBI_AVL_ERROR);
503
		obidebug(1, "\nError due to AVL tree name too long");
504 505 506
		return NULL;
	}

507 508 509 510 511 512 513 514 515
	// Build the file name
	file_name = (char*) malloc((strlen(avl_name) + 5)*sizeof(char));
	if (file_name == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for an AVL file name");
		return NULL;
	}
	if (sprintf(file_name,"%s.oda", avl_name) < 0)
516 517
	{
		obi_set_errno(OBI_AVL_ERROR);
518
		obidebug(1, "\nError building an AVL tree file name");
519 520 521 522 523 524 525 526 527 528 529 530 531
		free(file_name);
		return NULL;
	}

	return file_name;
}


static char* build_avl_data_file_name(const char* avl_name)
{
	char* file_name;

	// Build the file name
Celine Mercier committed
532
	file_name = (char*) malloc((strlen(avl_name) + 5)*sizeof(char));
533 534 535 536 537 538
	if (file_name == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for an AVL data file name");
		return NULL;
	}
Celine Mercier committed
539
	if (sprintf(file_name,"%s.odd", avl_name) < 0)
540 541
	{
		obi_set_errno(OBI_AVL_ERROR);
542
		obidebug(1, "\nError building an AVL tree data file name");
543 544 545 546 547 548 549 550 551 552 553 554 555
		return NULL;
	}

	return file_name;
}


size_t get_avl_header_size()
{
	size_t header_size;
	size_t rounded_header_size;
	double multiple;

556
	header_size = sizeof(OBIDMS_avl_header_t) + bloom_filter_size(MAX_NODE_COUNT_PER_AVL, BLOOM_FILTER_ERROR_RATE);
557

558
	multiple = ceil((double) header_size / (double) getpagesize());
559 560 561 562 563 564 565 566 567

	rounded_header_size = multiple * getpagesize();

	return rounded_header_size;
}


size_t get_initial_avl_size()
{
568 569 570 571 572 573 574
	size_t s;
	size_t m;

	m = 1;
	s = getpagesize() * m;

	return s;
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
}


size_t get_avl_data_header_size()
{
	size_t header_size;
	size_t rounded_header_size;
	double multiple;

	header_size = sizeof(OBIDMS_avl_data_header_t);

	multiple = 	ceil((double) header_size / (double) getpagesize());

	rounded_header_size = multiple * getpagesize();

	return rounded_header_size;
}


size_t get_initial_avl_data_size()
{
596 597 598 599 600 601 602
	size_t s;
	size_t m;

	m = 1;
	s = getpagesize() * m;

	return s;
603 604 605
}


606
int truncate_avl_to_size_used(OBIDMS_avl_p avl)	// TODO is it necessary to unmap/remap?
607
{
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639
	size_t  			file_size;
	size_t  			new_data_size;
	double  			multiple;
	int 				file_descriptor;

	// Compute the new size: used size rounded to the nearest greater multiple of page size greater than 0
	multiple = ceil((double) (ONE_IF_ZERO((avl->header)->nb_items * sizeof(AVL_node_t))) / (double) getpagesize());
	new_data_size = ((int) multiple) * getpagesize();

	// Check that it is actually greater than the current size of the file, otherwise no need to truncate
	if ((avl->header)->avl_size == new_data_size)
		return 0;

	// Get the file descriptor
	file_descriptor = avl->avl_fd;

	// Unmap the tree before truncating the file
	if (munmap(avl->tree, (avl->header)->avl_size) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError munmapping the tree of an AVL before truncating");
		return -1;
	}

	// Truncate the file
	file_size = (avl->header)->header_size + new_data_size;
	if (ftruncate(file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError truncating an AVL file");
		return -1;
	}
640

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
	// Remap the data
	avl->tree = mmap(NULL,
			         new_data_size,
					 PROT_READ | PROT_WRITE,
					 MAP_SHARED,
					 file_descriptor,
					 (avl->header)->header_size
					);

	if (avl->tree == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError re-mmapping the tree of an AVL after truncating");
		return -1;
	}

	// Set new data size and new max node count
	(avl->header)->avl_size     = new_data_size;
	(avl->header)->nb_items_max = floor(new_data_size / sizeof(AVL_node_t));

	return 0;
}


int truncate_avl_data_to_size_used(OBIDMS_avl_data_p avl_data)	// TODO is it necessary to unmap/remap?
{
	size_t  			file_size;
	index_t  			new_data_size;
	double  			multiple;
	int 				file_descriptor;

	// Compute the new size: used size rounded to the nearest greater multiple of page size greater than 0
	multiple = ceil((double) (ONE_IF_ZERO((avl_data->header)->data_size_used)) / (double) getpagesize());
	new_data_size = ((int) multiple) * getpagesize();

	// Check that it is actually greater than the current size of the file, otherwise no need to truncate
	if ((avl_data->header)->data_size_max == new_data_size)
		return 0;

	// Get the file descriptor
	file_descriptor = avl_data->data_fd;

	// Unmap the data before truncating the file
684 685 686
	if (munmap(avl_data->data, (avl_data->header)->data_size_max) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
687 688
		obidebug(1, "\nError munmapping the data of an AVL before truncating");
		return -1;
689 690
	}

691 692 693
	// Truncate the file
	file_size = (avl_data->header)->header_size + new_data_size;
	if (ftruncate(file_descriptor, file_size) < 0)
694 695
	{
		obi_set_errno(OBI_AVL_ERROR);
696 697
		obidebug(1, "\nError truncating an AVL data file");
		return -1;
698 699
	}

700 701 702 703 704 705 706 707
	// Remap the data
	avl_data->data = mmap(NULL,
					      new_data_size,
						  PROT_READ | PROT_WRITE,
						  MAP_SHARED,
						  file_descriptor,
						  (avl_data->header)->header_size
					     );
708

709 710 711 712 713 714 715 716 717 718 719
	if (avl_data->data == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError re-mmapping the data of an AVL after truncating");
		return -1;
	}

	// Set new data size
	(avl_data->header)->data_size_max = new_data_size;

	return 0;
720 721 722 723 724 725 726 727 728 729 730
}


int grow_avl(OBIDMS_avl_p avl)		// TODO Lock when needed
{
	size_t	file_size;
	size_t 	old_data_size;
	size_t 	new_data_size;
	size_t 	header_size;
	int 	avl_file_descriptor;

731
	avl_file_descriptor = avl->avl_fd;
732 733 734 735 736 737 738 739 740 741 742

	// Calculate the new file size
	old_data_size = (avl->header)->avl_size;
	new_data_size = old_data_size * AVL_GROWTH_FACTOR;
	header_size = (avl->header)->header_size;
	file_size = header_size + new_data_size;

	// Enlarge the file
	if (ftruncate(avl_file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
743
		obidebug(1, "\nError enlarging an AVL tree file");
744 745 746 747 748 749 750 751 752
		close(avl_file_descriptor);
		return -1;
	}

	// Unmap and re-map the data

	if (munmap(avl->tree, old_data_size) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
753
		obidebug(1, "\nError munmapping the tree of an AVL tree file before enlarging");
754 755 756 757 758
		close(avl_file_descriptor);
		return -1;
	}

	avl->tree = mmap(NULL,
759 760 761 762 763 764
					 new_data_size,
					 PROT_READ | PROT_WRITE,
					 MAP_SHARED,
					 avl_file_descriptor,
					 header_size
					);
765 766 767 768

	if (avl->tree == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
769
		obidebug(1, "\nError re-mmapping the tree of an AVL tree file after enlarging the file");
770 771 772 773 774 775 776 777 778 779 780 781 782 783
		close(avl_file_descriptor);
		return -1;
	}

	// Set new maximum number of items
	(avl->header)->nb_items_max = floor(((double) new_data_size) / ((double) sizeof(AVL_node_t)));

	// Set the new avl size
	(avl->header)->avl_size = new_data_size;

	return 0;
}


784
int grow_avl_data(OBIDMS_avl_data_p avl_data)		// TODO Lock when needed
785 786 787 788 789 790 791
{
	size_t 	file_size;
	index_t old_data_size;
	index_t new_data_size;
	size_t 	header_size;
	int 	avl_data_file_descriptor;

792
	avl_data_file_descriptor = avl_data->data_fd;
793 794

	// Calculate the new file size
795
	old_data_size = (avl_data->header)->data_size_max;
796
	new_data_size = old_data_size * AVL_GROWTH_FACTOR;
797
	header_size = (avl_data->header)->header_size;
798 799 800 801 802 803
	file_size = header_size + new_data_size;

	// Enlarge the file
	if (ftruncate(avl_data_file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
804
		obidebug(1, "\nError enlarging an AVL tree data file");
805 806 807 808 809 810
		close(avl_data_file_descriptor);
		return -1;
	}

	// Unmap and re-map the data

811
	if (munmap(avl_data->data, old_data_size) < 0)
812 813
	{
		obi_set_errno(OBI_AVL_ERROR);
814
		obidebug(1, "\nError munmapping the data of an AVL tree data file before enlarging");
815 816 817 818
		close(avl_data_file_descriptor);
		return -1;
	}

819 820 821 822 823 824 825
	avl_data->data = mmap(NULL,
						  new_data_size,
						  PROT_READ | PROT_WRITE,
						  MAP_SHARED,
						  avl_data_file_descriptor,
						  header_size
						 );
826

827
	if (avl_data->data == MAP_FAILED)
828 829
	{
		obi_set_errno(OBI_AVL_ERROR);
830
		obidebug(1, "\nError re-mmapping the data of an AVL tree data file after enlarging the file");
831 832 833 834 835
		close(avl_data_file_descriptor);
		return -1;
	}

	// Set new data size
836
	(avl_data->header)->data_size_max = new_data_size;
837

838
	// Initialize new data to 0
839
	memset((avl_data->data)+old_data_size, 0, new_data_size - old_data_size);
840 841 842 843 844

	return 0;
}


845
int close_avl_data(OBIDMS_avl_data_p avl_data)
846
{
847
	int ret_val = 0;
848

849
	ret_val = truncate_avl_data_to_size_used(avl_data);
850

851
	if (munmap(avl_data->data, (avl_data->header)->data_size_max) < 0)
852
	{
853 854 855
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError munmapping the data of an AVL tree data file");
		ret_val = -1;
856 857
	}

858 859 860 861 862 863
	if (munmap(avl_data->header, (avl_data->header)->header_size) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError munmapping the header of an AVL tree data file");
		ret_val = -1;
	}
864

865
	close(avl_data->data_fd);
866

867
	free(avl_data);
868

869
	return ret_val;
870 871 872
}


873
int unmap_an_avl(OBIDMS_avl_p avl)
874
{
875 876 877 878 879 880 881 882 883 884 885 886 887 888
	if (munmap((avl->data)->data, ((avl->data)->header)->data_size_max) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError unmapping the data of an AVL tree");
		return -1;
	}
	if (munmap(avl->tree, (((avl->header)->nb_items_max) * sizeof(AVL_node_t))) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError unmapping the tree of an AVL tree");
		return -1;
	}
	return 0;
}
889 890


891 892 893 894 895 896 897 898 899 900 901 902 903 904
int remap_an_avl(OBIDMS_avl_p avl)
{
	(avl->data)->data = mmap(NULL,
							 ((avl->data)->header)->data_size_max,
							 PROT_READ,
							 MAP_SHARED,
							 (avl->data)->data_fd,
							 ((avl->data)->header)->header_size);
	if ((avl->data)->data == NULL)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError mapping the data of an AVL tree");
		return -1;
	}
905

906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
	avl->tree = mmap(NULL,
					 ((avl->header)->nb_items_max) * sizeof(AVL_node_t),
					 PROT_READ,
					 MAP_SHARED,
					 avl->avl_fd,
					 (avl->header)->header_size);
	if (avl->tree == NULL)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError mapping the tree of an AVL tree");
		return -1;
	}

	return 0;
}
921 922


923 924 925 926
int add_new_avl_in_group(OBIDMS_avl_group_p avl_group)
{
	// Check that maximum number of AVLs in a group was not reached
	if (avl_group->current_avl_idx == (MAX_NB_OF_AVLS_IN_GROUP-1))
927
	{
928 929 930 931 932 933 934 935 936 937 938
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError: Trying to add new AVL in AVL group but maximum number of AVLs in a group reached");
		return -1;
	}

	// Unmap the previous AVL
	if (unmap_an_avl((avl_group->sub_avls)[avl_group->current_avl_idx]) < 0)
		return -1;

	// Increment current AVL index
	(avl_group->current_avl_idx)++;
939

940
	// Create the new AVL
941
	(avl_group->sub_avls)[avl_group->current_avl_idx] = obi_create_avl(avl_group->dms, avl_group->name, avl_group->current_avl_idx);
942 943 944 945
	if ((avl_group->sub_avls)[avl_group->current_avl_idx] == NULL)
	{
		obidebug(1, "\nError creating a new AVL tree in a group");
		return -1;
946 947
	}

948 949 950 951
	return 0;
}


952
int maybe_in_avl(OBIDMS_avl_p avl, Obi_blob_p value)
953
{
954
	return (bloom_check(&((avl->header)->bloom_filter), value, obi_blob_sizeof(value)));
955 956 957
}


958
index_t avl_add_value_in_data_array(OBIDMS_avl_p avl, Obi_blob_p value)
959 960
{
	index_t value_idx;
961
	int value_size;
962 963 964 965

	value_idx = ((avl->data)->header)->data_size_used;

	// Grow the data if needed
966
	value_size = obi_blob_sizeof(value);
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
	while (((avl->data)->header)->data_size_max < (value_idx + value_size))
	{
		if (grow_avl_data(avl->data) < 0)
			return -1;
	}

	// Store the value itself at the end of the data
	memcpy((((avl->data)->data)+value_idx), value, value_size);

	// Update the data size
	((avl->data)->header)->data_size_used = value_idx + value_size;

	// Update the number of items
	(((avl->data)->header)->nb_items)++;

	return value_idx;
983 984 985 986 987 988 989 990 991
}


AVL_node_p avl_create_node(OBIDMS_avl_p avl, index_t node_idx)
{
	AVL_node_p node;

	node = (avl->tree)+node_idx;

992 993
	node->left_child     = -1;
	node->right_child    = -1;
994
	node->balance_factor = 0;
995 996
	node->value          = -1;
	node->crc64          = 0; 	// TODO no NA value
997 998 999 1000 1001 1002 1003 1004

	return node;
}


// Update the balance factors of the nodes from the node that will need balancing
void avl_update_balance_factors(OBIDMS_avl_p avl)
{
1005
	uint8_t    n;
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
	AVL_node_p node;

	// Update balance factors from the node where balancing might be needed
	node=(avl->tree)+((avl->path_idx)[1]);

	for (n=1; (avl->path_dir)[n] != -1; n++)
	{
		if ((avl->path_dir)[n]) // Went right
		{
			(node->balance_factor)--;
			node=RIGHT_CHILD(node);
		}
		else 	// Went left
		{
			(node->balance_factor)++;
			node=LEFT_CHILD(node);
		}
	}
}


// Left Left Rotate
index_t avl_rotate_leftleft(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx)
{
	AVL_node_p left_child = LEFT_CHILD(node);
	index_t left_child_idx = node->left_child;

	node->left_child = left_child->right_child;
	left_child->right_child = node_idx;

	node->balance_factor = 0;
	left_child->balance_factor = 0;

	return left_child_idx;
}


// Left Right Rotate
index_t avl_rotate_leftright(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx)
{
	AVL_node_p left_child = LEFT_CHILD(node);
	index_t left_child_idx = node->left_child;
	AVL_node_p rc_of_lc = RIGHT_CHILD(left_child);
	index_t rc_of_lc_idx = left_child->right_child;

	node->left_child = rc_of_lc->right_child;
	left_child->right_child = rc_of_lc->left_child;
	rc_of_lc->left_child = left_child_idx;
	rc_of_lc->right_child = node_idx;

	if (rc_of_lc->balance_factor == -1)
	{
		left_child->balance_factor = 1;
		node->balance_factor = 0;
	}
	else if (rc_of_lc->balance_factor == 0)
	{
		left_child->balance_factor = 0;
		node->balance_factor = 0;
	}
	else	// if (rc_of_lc->balance_factor == 1)
	{
		left_child->balance_factor = 0;
		node->balance_factor = -1;
	}

	rc_of_lc->balance_factor = 0;

	return rc_of_lc_idx;
}


// Right Left Rotate
index_t avl_rotate_rightleft(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx)
{
	AVL_node_p right_child = RIGHT_CHILD(node);
	index_t right_child_idx = node->right_child;;
	AVL_node_p lc_of_rc = LEFT_CHILD(right_child);
	index_t lc_of_rc_idx = right_child->left_child;

	node->right_child = lc_of_rc->left_child;
	right_child->left_child = lc_of_rc->right_child;
	lc_of_rc->right_child = right_child_idx;
	lc_of_rc->left_child = node_idx;

	if (lc_of_rc->balance_factor == 1)
	{
		right_child->balance_factor = 1;
		node->balance_factor = 0;
	}
	else if (lc_of_rc->balance_factor == 0)
	{
		right_child->balance_factor = 0;
		node->balance_factor = 0;
	}
	else	// if (lc_of_rc->balance_factor == -1)
	{
		right_child->balance_factor = 0;
		node->balance_factor = 1;
	}

	lc_of_rc->balance_factor = 0;

	return lc_of_rc_idx;
}


// Right Right Rotate
index_t avl_rotate_rightright(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx)
{
	AVL_node_p right_child = RIGHT_CHILD(node);
	index_t right_child_idx = node->right_child;

	node->right_child = right_child->left_child;
	right_child->left_child = node_idx;

	node->balance_factor = 0;
	right_child->balance_factor = 0;

	return right_child_idx;
}


// Balance a given node
index_t avl_balance_node(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx)
{
	index_t new_root = 0;

	if (node->balance_factor == 2)
	{ // Left Heavy
		if ((LEFT_CHILD(node))->balance_factor == -1)
			new_root = avl_rotate_leftright(avl, node, node_idx);
		else
			new_root = avl_rotate_leftleft(avl, node, node_idx);
	}
	else if (node->balance_factor == -2)
	{ // Right Heavy
		if ((RIGHT_CHILD(node))->balance_factor == 1)
			new_root = avl_rotate_rightleft(avl, node, node_idx);
		else
			new_root = avl_rotate_rightright(avl, node, node_idx);
	}
	else
	// Node is balanced
		new_root = node_idx;

	return new_root;
}


// Balance a given tree
void avl_balance(OBIDMS_avl_p avl)
{
1159 1160
	index_t    new_root;
	index_t    node_index;
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198
	AVL_node_p node_to_balance;
	AVL_node_p parent_of_node_to_balance;

	node_index = (avl->path_idx)[1];
	node_to_balance = (avl->tree)+node_index;
	parent_of_node_to_balance = (avl->tree)+((avl->path_idx)[0]);

	// Balance the 2nd node stored in the path (the first is only kept to connect the new root
	// of the subtree if needed).
	new_root = avl_balance_node(avl, node_to_balance, node_index);

	if (new_root != node_index)
	// If the root of the subtree has changed
	{
		// If the subtree's root is the tree's root, store the new root
		if (node_index == (avl->header)->root_idx)
			(avl->header)->root_idx = new_root;
		// Else, connect the new subtree's root to the parent of the subtree
		else if ((avl->path_dir)[0])	   // Subtree is the right child of its parent
			parent_of_node_to_balance->right_child = new_root;
		else	   // Subtree is the left child of its parent
			parent_of_node_to_balance->left_child = new_root;
	}
}


// Print a depth first traverse of a node
void avl_print_node(OBIDMS_avl_p avl, AVL_node_p node, index_t node_idx, int depth)
{
	int i = 0;

	if (node->left_child != -1)
		avl_print_node(avl, LEFT_CHILD(node), node->left_child, depth+2);

	for (i = 0; i < depth; i++)
		putchar(' ');

	fprintf(stderr, "Node idx: %lld, Value idx: %lld, Left child: %lld, Right child: %lld, "
1199
			"Balance factor: %d, CRC: %llu\n", node_idx, node->value, node->left_child, node->right_child, node->balance_factor, node->crc64);
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221

	if (node->right_child != -1)
		avl_print_node(avl, RIGHT_CHILD(node), node->right_child, depth+2);
}


// Print a depth first traverse of a tree
void avl_print(OBIDMS_avl_p avl)
{
	fprintf(stderr, "\nRoot index: %lld\n", (avl->header)->root_idx);
	avl_print_node(avl, (avl->tree)+((avl->header)->root_idx), (avl->header)->root_idx, 0);
}


/**********************************************************************
 *
 * D E F I N I T I O N   O F   T H E   P U B L I C   F U N C T I O N S
 *
 **********************************************************************/

int obi_avl_exists(OBIDMS_p dms, const char* avl_name)
{
1222
	struct stat buffer;
1223
	char* 	    avl_dir_path;
1224
	int 	    check_dir;
1225

Celine Mercier committed
1226
	// Build the AVL tree file path
1227
	avl_dir_path = get_full_path_of_avl_dir(dms, avl_name);
1228
	if (avl_dir_path == NULL)
1229 1230
		return -1;

1231
	check_dir = stat(avl_dir_path, &buffer);
1232

1233
	free(avl_dir_path);
1234 1235 1236 1237 1238 1239 1240 1241

	if (check_dir == 0)
		return 1;
	else
		return 0;
}


1242
OBIDMS_avl_p obi_create_avl(OBIDMS_p dms, const char* avl_name, int avl_idx)
1243
{
1244 1245
	char* 				complete_avl_name;
	char*				avl_dir_name;
1246 1247 1248 1249 1250 1251 1252
	char* 				avl_file_name;
	char* 				avl_data_file_name;
	size_t				header_size;
	size_t				data_size;
	size_t				file_size;
	int					avl_file_descriptor;
	int					avl_data_file_descriptor;
1253
	int					avl_dir_fd;
1254 1255
	OBIDMS_avl_data_p   avl_data;
	OBIDMS_avl_p		avl;
1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
 	DIR*				directory;
    struct stat 		buffer;
    int 				check_dir;

	// Get complete name of AVL if index
	if (avl_idx >= 0)
	{
		complete_avl_name = build_avl_name_with_idx(avl_name, avl_idx);
		if (complete_avl_name == NULL)
			return NULL;
	}
	else
	{
		complete_avl_name = (char*) malloc((strlen(avl_name)+1)*sizeof(char));
		if (complete_avl_name == NULL)
		{
    		obi_set_errno(OBI_MALLOC_ERROR);
    		obidebug(1, "\nError allocating memory for an AVL name");
			return NULL;
		}
		strcpy(complete_avl_name, avl_name);
	}

	// Create that AVL's directory if needed
	avl_dir_name = get_full_path_of_avl_dir(dms, avl_name);
	if (avl_dir_name == NULL)
		return NULL;
	// Check if the AVL's directory already exists
	check_dir = stat(avl_dir_name, &buffer);
	// Create that AVL's directory if it doesn't already exist
   	if (check_dir < 0)
   	{
Celine Mercier committed
1288
		if (mkdirat(dms->indexer_dir_fd, avl_dir_name, 00777) < 0)
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
		{
			obi_set_errno(OBI_AVL_ERROR);
			obidebug(1, "\nError creating an AVL directory");
			if (avl_idx >= 0)
				free(complete_avl_name);
			free(avl_dir_name);
			return NULL;
		}
   	}
    // Open the AVL directory
    directory = opendir(avl_dir_name);
	if (directory == NULL)
	{
		obidebug(1, "\nError opening an AVL directory");
		if (avl_idx >= 0)
			free(complete_avl_name);
		free(avl_dir_name);
		return NULL;
	}
	free(avl_dir_name);
    avl_dir_fd = dirfd(directory);
	if (avl_dir_fd < 0)
	{
		obidebug(1, "\nError getting an AVL directory file descriptor");
		if (avl_idx >= 0)
			free(complete_avl_name);
		return NULL;
	}
1317 1318 1319 1320

	// Create the data file

		// Build file name
1321
	avl_data_file_name = build_avl_data_file_name(complete_avl_name);
1322
	if (avl_data_file_name == NULL)
1323 1324 1325
	{
		if (avl_idx >= 0)
			free(complete_avl_name);
1326
		return NULL;
1327
	}
1328 1329

		// Create file
1330
	avl_data_file_descriptor = openat(avl_dir_fd, avl_data_file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
1331 1332 1333
	if (avl_data_file_descriptor < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
1334
		obidebug(1, "\nError creating an AVL tree data file");
1335 1336
		if (avl_idx >= 0)
			free(complete_avl_name);
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
		free(avl_data_file_name);
		return NULL;
	}
	free(avl_data_file_name);

		// Calculate the size needed
	header_size = get_avl_data_header_size();
	data_size   = get_initial_avl_data_size();
	file_size   = header_size + data_size;

1347
		// Truncate the AVL tree data file to the right size
1348 1349 1350
	if (ftruncate(avl_data_file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
1351
		obidebug(1, "\nError truncating an AVL tree data file to the right size");
1352 1353
		if (avl_idx >= 0)
			free(complete_avl_name);
1354 1355 1356 1357
		close(avl_data_file_descriptor);
		return NULL;
	}

1358
		// Allocate the memory for the AVL tree data structure
1359 1360 1361
	avl_data = (OBIDMS_avl_data_p) malloc(sizeof(OBIDMS_avl_data_t));
	if (avl_data == NULL)
	{
1362
		obi_set_errno(OBI_MALLOC_ERROR);
1363
		obidebug(1, "\nError allocating the memory for the AVL tree data structure");
1364 1365
		if (avl_idx >= 0)
			free(complete_avl_name);
1366 1367 1368 1369
		close(avl_data_file_descriptor);
		return NULL;
	}

1370
		// Fill the AVL tree data structure
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
	avl_data->header = mmap(NULL,
			                header_size,
			                PROT_READ | PROT_WRITE,
			                MAP_SHARED,
			                avl_data_file_descriptor,
			                0
			               );
	if (avl_data->header == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
1381
		obidebug(1, "\nError mmapping the header of an AVL tree data file");
1382 1383
		if (avl_idx >= 0)
			free(complete_avl_name);
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
		close(avl_data_file_descriptor);
		free(avl_data);
		return NULL;
	}

	avl_data->data   = mmap(NULL,
			                data_size,
			                PROT_READ | PROT_WRITE,
			                MAP_SHARED,
			                avl_data_file_descriptor,
			                header_size
			               );
	if (avl_data->data == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
1399
		obidebug(1, "\nError mmapping the data of an AVL tree data file");
1400 1401
		if (avl_idx >= 0)
			free(complete_avl_name);
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
		munmap(avl_data->header, header_size);
		close(avl_data_file_descriptor);
		free(avl_data);
		return NULL;
	}

	(avl_data->header)->header_size 	 = header_size;
	(avl_data->header)->data_size_max	 = data_size;
	(avl_data->header)->data_size_used   = 0;
	(avl_data->header)->nb_items	 	 = 0;
	(avl_data->header)->creation_date    = time(NULL);
1413 1414 1415
	strcpy((avl_data->header)->avl_name, complete_avl_name);

	avl_data->data_fd = avl_data_file_descriptor;
1416 1417 1418 1419 1420

	// Initialize all bits to 0
	memset(avl_data->data, 0, (avl_data->header)->data_size_max);


1421
	// Create the AVL tree file
1422 1423

		// Build file name
1424
	avl_file_name = build_avl_file_name(complete_avl_name);
1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436
	if (avl_file_name == NULL)
	{
		close_avl_data(avl_data);
		return NULL;
	}

		// Calculate the size needed
	header_size = get_avl_header_size();
	data_size = get_initial_avl_size();
	file_size = header_size + data_size;

		// Create file
1437
	avl_file_descriptor = openat(avl_dir_fd, avl_file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
1438 1439 1440
	if (avl_file_descriptor < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
1441
		obidebug(1, "\nError creating an AVL tree file");
1442 1443
		if (avl_idx >= 0)
			free(complete_avl_name);
1444 1445 1446 1447 1448 1449
		close_avl_data(avl_data);
		free(avl_file_name);
		return NULL;
	}
	free(avl_file_name);

1450
		// Truncate the AVL tree file to the right size
1451 1452 1453
	if (ftruncate(avl_file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
1454
		obidebug(1, "\nError truncating an AVL tree file to the right size");
1455 1456
		if (avl_idx >= 0)
			free(complete_avl_name);
1457 1458 1459 1460 1461
		close_avl_data(avl_data);
		close(avl_file_descriptor);
		return NULL;
	}

1462
		// Allocate the memory for the AVL tree structure
1463 1464 1465
	avl = (OBIDMS_avl_p) malloc(sizeof(OBIDMS_avl_t));
	if (avl == NULL)
	{
1466
		obi_set_errno(OBI_MALLOC_ERROR);
1467
		obidebug(1, "\nError allocating the memory for the AVL tree structure");
1468 1469
		if (avl_idx >= 0)
			free(complete_avl_name);
1470 1471 1472 1473 1474
		close_avl_data(avl_data);
		close(avl_file_descriptor);
		return NULL;
	}

1475
		// Fill the AVL tree structure
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
	avl->header = mmap(NULL,
			           header_size,
			           PROT_READ | PROT_WRITE,
			           MAP_SHARED,
			           avl_file_descriptor,
			           0
			          );
	if (avl->header == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
1486
		obidebug(1, "\nError mmapping the header of an AVL tree file");
1487 1488
		if (avl_idx >= 0)
			free(complete_avl_name);
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504
		close_avl_data(avl_data);
		close(avl_file_descriptor);
		free(avl);
		return NULL;
	}

	avl->tree  = mmap(NULL,
			          data_size,
			          PROT_READ | PROT_WRITE,
			          MAP_SHARED,
			          avl_file_descriptor,
			          header_size
			         );
	if (avl->tree == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
1505
		obidebug(1, "\nError mmapping the data of an AVL tree file");
1506 1507
		if (avl_idx >= 0)
			free(complete_avl_name);
1508 1509 1510 1511 1512 1513 1514
		close_avl_data(avl_data);
		munmap(avl->header, header_size);
		close(avl_file_descriptor);
		free(avl);
		return NULL;
	}

1515 1516
	avl->dms       = dms;
	avl->data      = avl_data;
Celine Mercier committed
1517
	avl->directory = dms->indexer_directory;
1518 1519
	avl->dir_fd    = avl_dir_fd;
	avl->avl_fd    = avl_file_descriptor;
1520 1521 1522 1523 1524

	(avl->header)->header_size 	 = header_size;
	(avl->header)->avl_size 	 = data_size;
	(avl->header)->nb_items	 	 = 0;
	(avl->header)->nb_items_max	 = (index_t) floor(((double) get_initial_avl_size()) / ((double) sizeof(AVL_node_t)));
1525
	(avl->header)->root_idx      = -1;
1526
	(avl->header)->creation_date = time(NULL);
1527
	strcpy((avl->header)->avl_name, complete_avl_name);
1528 1529

	// Bloom filter
1530
	bloom_init(&((avl->header)->bloom_filter), MAX_NODE_COUNT_PER_AVL);
1531

1532 1533 1534
	if (avl_idx >= 0)
		free(complete_avl_name);

1535 1536 1537 1538
	return avl;
}


1539
OBIDMS_avl_p obi_open_avl(OBIDMS_p dms, const char* avl_name, int avl_idx)
1540 1541
{
	char* 	 			avl_file_name;
1542 1543
	char*				complete_avl_name;
	char*				avl_dir_name;
1544
	char* 				avl_data_file_name;
1545
	DIR*				directory;
1546 1547 1548 1549 1550 1551
	size_t				header_size;
	int					avl_file_descriptor;
	int					avl_data_file_descriptor;
	int					avl_dir_file_descriptor;
	OBIDMS_avl_data_p   avl_data;
	OBIDMS_avl_p		avl;
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570

	// Get complete name of AVL if index
	if (avl_idx >= 0)
	{
		complete_avl_name = build_avl_name_with_idx(avl_name, avl_idx);
		if (complete_avl_name == NULL)
			return NULL;
	}
	else
	{
		complete_avl_name = (char*) malloc((strlen(avl_name)+1)*sizeof(char));
		if (complete_avl_name == NULL)
		{
    		obi_set_errno(OBI_MALLOC_ERROR);
    		obidebug(1, "\nError allocating memory for an AVL name");
			return NULL;
		}
		strcpy(complete_avl_name, avl_name);
	}
1571

1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599
    // Open the AVL directory
	avl_dir_name = get_full_path_of_avl_dir(dms, avl_name);
	if (avl_dir_name == NULL)
	{
		if (avl_idx >= 0)
			free(complete_avl_name);
		return NULL;
	}
    directory = opendir(avl_dir_name);
	if (directory == NULL)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError opening an AVL directory");
		if (avl_idx >= 0)
			free(complete_avl_name);
		free(avl_dir_name);
		return NULL;
	}
	free(avl_dir_name);
	avl_dir_file_descriptor = dirfd(directory);
	if (avl_dir_file_descriptor < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError getting the file descriptor of an AVL directory");
		if (avl_idx >= 0)
			free(complete_avl_name);
		return NULL;
	}
1600

1601 1602

	// Open the data file
1603 1604

		// Build file name
1605
	avl_data_file_name = build_avl_data_file_name(complete_avl_name);
1606 1607 1608 1609
	if (avl_data_file_name == NULL)
		return NULL;

		// Open file
1610
	avl_data_file_descriptor = openat(avl_dir_file_descriptor, avl_data_file_name, O_RDONLY, 0777);
1611 1612 1613
	if (avl_data_file_descriptor < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
1614
		obidebug(1, "\nError opening an AVL tree data file");
1615 1616
		if (avl_idx >= 0)
			free(complete_avl_name);
1617 1618 1619 1620 1621
		free(avl_data_file_name);
		return NULL;
	}
	free(avl_data_file_name);

1622
		// Allocate the memory for the AVL tree data structure
1623 1624 1625
	avl_data = (OBIDMS_avl_data_p) malloc(sizeof(OBIDMS_avl_data_t));
	if (avl_data == NULL)
	{
1626
		obi_set_errno(OBI_MALLOC_ERROR);
1627
		obidebug(1, "\nError allocating the memory for the AVL tree data structure");
1628 1629
		if (avl_idx >= 0)
			free(complete_avl_name);
1630 1631 1632 1633 1634 1635 1636 1637
		close(avl_data_file_descriptor);
		return NULL;
	}

	// Read the header size
	if (read(avl_data_file_descriptor, &header_size, sizeof(size_t)) < ((ssize_t) sizeof(size_t)))
	{
		obi_set_errno(OBI_AVL_ERROR);
1638
		obidebug(1, "\nError reading the header size to open an AVL tree data file");
1639 1640
		if (avl_idx >= 0)
			free(complete_avl_name);
1641 1642 1643 1644 1645 1646 1647
		close(avl_data_file_descriptor);
		return NULL;
	}

		// Fill the avl data structure
	avl_data->header = mmap(NULL,
			                header_size,
1648
			                PROT_READ,
1649 1650 1651 1652 1653 1654 1655
			                MAP_SHARED,
			                avl_data_file_descriptor,
			                0
			               );
	if (avl_data->header == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
1656
		obidebug(1, "\nError mmapping the header of an AVL tree data file");
1657 1658
		if (avl_idx >= 0)
			free(complete_avl_name);
1659 1660 1661 1662 1663 1664 1665
		close(avl_data_file_descriptor);
		free(avl_data);
		return NULL;
	}

	avl_data->data   = mmap(NULL,
			                (avl_data->header)->data_size_max,
1666
			                PROT_READ,
1667 1668 1669 1670 1671 1672 1673
			                MAP_SHARED,
			                avl_data_file_descriptor,
			                header_size
			               );
	if (avl_data->data == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
1674
		obidebug(1, "\nError mmapping the data of an AVL tree data file");
1675 1676
		if (avl_idx >= 0)
			free(complete_avl_name);
1677 1678 1679 1680 1681 1682
		munmap(avl_data->header, header_size);
		close(avl_data_file_descriptor);
		free(avl_data);
		return NULL;
	}

1683
	avl_data->data_fd = avl_data_file_descriptor;
1684 1685


1686
	// Open the AVL tree file
1687 1688

		// Build file name
1689
	avl_file_name = build_avl_file_name(complete_avl_name);
1690 1691 1692 1693 1694 1695 1696
	if (avl_file_name == NULL)
	{
		close_avl_data(avl_data);
		return NULL;
	}

		// Open file
1697
	avl_file_descriptor = openat(avl_dir_file_descriptor, avl_file_name, O_RDONLY, 0777);
1698 1699 1700
	if (avl_file_descriptor < 0)
	{
		obi_set_errno(OBI_AVL_ERROR);
1701
		obidebug(1, "\nError opening an AVL tree file");
1702 1703
		if (avl_idx >= 0)
			free(complete_avl_name);
1704 1705 1706 1707 1708 1709
		close_avl_data(avl_data);
		free(avl_file_name);
		return NULL;
	}
	free(avl_file_name);

1710
		// Allocate the memory for the AVL tree structure
1711 1712 1713
	avl = (OBIDMS_avl_p) malloc(sizeof(OBIDMS_avl_t));
	if (avl == NULL)
	{
1714
		obi_set_errno(OBI_MALLOC_ERROR);
1715
		obidebug(1, "\nError allocating the memory for the AVL tree structure");
1716 1717
		if (avl_idx >= 0)
			free(complete_avl_name);
1718 1719 1720 1721 1722 1723 1724 1725 1726
		close_avl_data(avl_data);
		close(avl_file_descriptor);
		return NULL;
	}

		// Read the header size
	if (read(avl_file_descriptor, &header_size, sizeof(size_t)) < ((ssize_t) sizeof(size_t)))
	{
		obi_set_errno(OBI_AVL_ERROR);
1727
		obidebug(1, "\nError reading the header size to open an AVL tree");
1728 1729
		if (avl_idx >= 0)
			free(complete_avl_name);
1730 1731 1732 1733 1734 1735 1736
		close(avl_file_descriptor);
		return NULL;
	}

		// Fill the avl structure
	avl->header = mmap(NULL,
			           header_size,
1737
			           PROT_READ,
1738 1739 1740 1741 1742 1743 1744
			           MAP_SHARED,
			           avl_file_descriptor,
			           0
			          );
	if (avl->header == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
1745
		obidebug(1, "\nError mmapping the header of an AVL tree file");
1746 1747
		if (avl_idx >= 0)
			free(complete_avl_name);
1748 1749 1750 1751 1752 1753 1754 1755
		close_avl_data(avl_data);
		close(avl_file_descriptor);
		free(avl);
		return NULL;
	}

	avl->tree = mmap(NULL,
					 (((avl->header)->nb_items_max) * sizeof(AVL_node_t)),
1756
			         PROT_READ,
1757 1758 1759 1760 1761 1762 1763
			         MAP_SHARED,
			         avl_file_descriptor,
			         header_size
			        );
	if (avl->tree == MAP_FAILED)
	{
		obi_set_errno(OBI_AVL_ERROR);
1764
		obidebug(1, "\nError mmapping the data of an AVL tree file");
1765 1766
		if (avl_idx >= 0)
			free(complete_avl_name);
1767 1768 1769 1770 1771 1772 1773
		close_avl_data(avl_data);
		munmap(avl->header, header_size);
		close(avl_file_descriptor);
		free(avl);
		return NULL;
	}

1774 1775
	avl->dms       = dms;
	avl->data      = avl_data;
Celine Mercier committed
1776
	avl->directory = dms->indexer_directory;
1777 1778
	avl->dir_fd    = avl_dir_file_descriptor;
	avl->avl_fd    = avl_file_descriptor;
1779

1780 1781 1782
	if (avl_idx >= 0)
		free(complete_avl_name);

1783 1784 1785 1786
	return avl;
}


1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826
OBIDMS_avl_group_p obi_avl_group(OBIDMS_p dms, const char* avl_name)
{
	int exists;

	exists = obi_avl_exists(dms, avl_name);

	switch (exists)
	{
		case 0:
			return obi_create_avl_group(dms, avl_name);
		case 1:
			return obi_open_avl_group(dms, avl_name);
	};

	obidebug(1, "\nError checking if an AVL tree already exists");
	return NULL;
}


OBIDMS_avl_group_p obi_create_avl_group(OBIDMS_p dms, const char* avl_name)
{
	OBIDMS_avl_group_p avl_group;

	avl_group = (OBIDMS_avl_group_p) malloc(sizeof(OBIDMS_avl_group_t));
	if (avl_group == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating the memory for an AVL group");
		return NULL;
	}

	// Create 1st avl
	(avl_group->sub_avls)[0] = obi_create_avl(dms, avl_name, 0);
	if ((avl_group->sub_avls)[0] == NULL)
	{
		obidebug(1, "\nError creating the first AVL of an AVL group");
		return NULL;
	}

	avl_group->current_avl_idx = 0;
1827
	strcpy(avl_group->name, avl_name);
1828 1829 1830

	avl_group->dms = dms;

1831 1832 1833 1834 1835 1836
	// Add in the list of open indexers
	obi_dms_list_indexer(dms, avl_group);

	// Set counter to 1
	avl_group->counter = 1;

1837 1838 1839
	// Set as writable
	avl_group->writable = true;

1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
	return avl_group;
}


OBIDMS_avl_group_p obi_open_avl_group(OBIDMS_p dms, const char* avl_name)
{
	OBIDMS_avl_group_p  avl_group;
	char*				avl_dir_name;
	int					avl_count;
	int					i;

1851 1852 1853 1854 1855 1856 1857 1858
	// Check if the group isn't already open
	avl_group = obi_dms_get_indexer_from_list(dms, avl_name);
	if (avl_group != NULL)	// Found it
	{
		// Increment counter
		(avl_group->counter)++;
		return avl_group;
	}
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886

	avl_group = (OBIDMS_avl_group_p) malloc(sizeof(OBIDMS_avl_group_t));
	if (avl_group == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating the memory for an AVL group");
		return NULL;
	}

	// Count the AVLs
	avl_dir_name = get_full_path_of_avl_dir(dms, avl_name);
	if (avl_dir_name == NULL)
		return NULL;
	avl_count = count_dir(avl_dir_name) / 2;
	if (avl_count < 0)
	{
		obidebug(1, "\nError counting the AVLs in an AVL directory: %s", avl_name);
		return NULL;
	}

	// Open the AVLs
	for (i=0; i<avl_count; i++)
	{
		(avl_group->sub_avls)[i] = obi_open_avl(dms, avl_name, i);
		if ((avl_group->sub_avls)[i] == NULL)
			return NULL;
	}
	avl_group->current_avl_idx = avl_count-1;			// TODO latest. discuss
1887
	strcpy(avl_group->name, avl_name);
1888 1889 1890

	avl_group->dms = dms;

1891 1892 1893 1894 1895 1896
	// Add in the list of open indexers
	obi_dms_list_indexer(dms, avl_group);

	// Set counter to 1
	avl_group->counter = 1;

1897 1898 1899
	// Set as read-only
	avl_group->writable = false;

1900 1901 1902 1903
	return avl_group;
}


1904 1905
int obi_close_avl(OBIDMS_avl_p avl)
{
1906
	int ret_val = 0;
1907

1908
	ret_val = close_avl_data(avl->data);
1909

1910
	ret_val = truncate_avl_to_size_used(avl);
1911

1912
	if (munmap(avl->tree, (((avl->header)->nb_items_max) * sizeof(AVL_node_t))) < 0)
1913
	{
1914 1915 1916 1917
		obi_set_errno(OBI_AVL_ERROR);
		obidebug(1, "\nError munmapping the tree of an AVL tree file");
		ret_val = -1;
	}