obiview.c 114 KB
Newer Older
Celine Mercier committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/********************************************************************
 * Obiview functions                                                 *
 ********************************************************************/

/**
 * @file obiview.c
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 * @date 16 December 2015
 * @brief Obiview functions.
 */


#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <fcntl.h>
#include <sys/mman.h>
18
#include <inttypes.h>
19
#include <math.h>
Celine Mercier committed
20
//#include <ctype.h>
Celine Mercier committed
21 22

#include "obiview.h"
23
#include "obidms.h"
Celine Mercier committed
24 25
#include "obidmscolumn.h"
#include "obidmscolumn_idx.h"
26
#include "obidmscolumn_blob.h"
27 28 29 30
#include "obidmscolumn_bool.h"
#include "obidmscolumn_char.h"
#include "obidmscolumn_float.h"
#include "obidmscolumn_int.h"
31
#include "obidmscolumn_qual.h"
32 33
#include "obidmscolumn_seq.h"
#include "obidmscolumn_str.h"
34
#include "obidmscolumn_array.h"
35 36 37
#include "obierrno.h"
#include "obidebug.h"
#include "obilittlebigman.h"
38
#include "hashtable.h"
39
#include "linked_list.h"
40
#include "utils.h"
41
#include "obiblob.h"
42
#include "libjson/json_utils.h"
Celine Mercier committed
43 44 45 46 47 48 49 50 51 52 53


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


/**************************************************************************
 *
 * 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
 *
 **************************************************************************/

54 55

/**
56
 * Internal function building the file name where the informations about a finished, read-only obiview are stored.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param view_name The name of the view.
 *
 * @returns A pointer to the file name.
 * @retval NULL if an error occurred.
 *
 * @since June 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static char* build_obiview_file_name(const char* view_name);


/**
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
 * Internal function building the file name where the informations about an unfinished, writable obiview are stored.
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param view_name The name of the view.
 *
 * @returns A pointer to the file name.
 * @retval NULL if an error occurred.
 *
 * @since February 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static char* build_unfinished_obiview_file_name(const char* view_name);


87 88 89 90 91 92
/**
 * Internal function checking if a view is finished.
 *
 * @param dms The DMS.
 * @param view_name The name of the view.
 *
93 94
 * @retval 1 if the view is finished.
 * @retval 0 if the view is not finished.
95 96 97 98 99
 * @retval -1 if the view does not exist or if an error occurred.
 *
 * @since October 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
100
static int view_is_finished(OBIDMS_p dms, const char* view_name);
101 102


103
/**
104
 * Internal function calculating the initial size of the file where the informations about an obiview are stored.
105
 *
106
 * @returns The initial size of the file in bytes, rounded to a multiple of page size.
107 108 109 110
 *
 * @since June 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
111
static size_t get_platform_view_file_size(void);
112

Celine Mercier committed
113

114 115 116 117 118 119 120 121 122 123 124 125
/**
 * @brief Internal function enlarging a view file.
 *
 * @param view A pointer on the view.
 * @param new_size The new size needed, in bytes (not necessarily rounded to a page size multiple).
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since August 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
126
static int enlarge_view_file(Obiview_p view, size_t new_size);
127 128


129 130
/**
 * @brief Internal function creating a file containing all the informations on a view.
Celine Mercier committed
131
 *
132
 * The file is named with the name of the view.
Celine Mercier committed
133
 *
134 135
 * @param dms The DMS to which the view belongs.
 * @param view_name The name of the view.
Celine Mercier committed
136 137 138 139
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
140
 * @since June 2016
Celine Mercier committed
141 142
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
143
static int create_obiview_file(OBIDMS_p dms, const char* view_name);
144 145 146 147 148 149 150 151


/**
 * @brief Internal function to update the column references of a view.
 *
 * The column references stored in the mapped view infos structures are updated
 * to match the columns opened in the opened view structure.
 *
152 153 154 155 156 157 158 159
 * @warning The column pointer array should be up to date before using this function.
 * @warning Aliases are not updated by this function and have to be edited separately.
 * 			This function simply reads the column pointer array associated with the view
 * 			and fills the column names and versions in the column reference array accordingly,
 * 			without touching the alias.
 *          That means that for example if there is a shift in the column pointer array, this
 *          function should not be used.
 *
160 161
 * @param view A pointer on the view.
 *
162 163 164
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
165 166 167
 * @since June 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
168
static int update_column_refs(Obiview_p view);
Celine Mercier committed
169 170


171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/**
 * @brief Internal function creating the column dictionary associated with a view.
 *
 * The column dictionary is built from the column references array, and associates each column alias
 * with the pointer on the column.
 *
 * @warning The column reference array and the column pointer array should be up to date before using this function.
 *
 * @param view A pointer on the view.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
187
static int create_column_dict(Obiview_p view);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205


/**
 * @brief Internal function updating the column dictionary associated with a view.
 *
 * The column dictionary is built from the column references array, and associates each column alias
 * with the pointer on the column.
 *
 * @warning The column reference array and the column pointer array should be up to date before using this function.
 *
 * @param view A pointer on the view.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
206
static int update_column_dict(Obiview_p view);
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230


/**
 * @brief Internal function updating the column reference array and the column dictionary associated with a view.
 *
 * The column reference array is updated from the column pointer array, then the column dictionary that
 * and associates each column alias with the pointer on the column is updated from the column reference array.
 *
 * @warning The column pointer array should be up to date before using this function.
 * @warning Aliases are not updated by this function and have to be edited separately.
 * 			This function simply reads the column pointer array associated with the view
 * 			and fills the column names and versions in the column reference array accordingly,
 * 			without touching the alias.
 *          That means that for example if there is a shift in the column pointer array, this
 *          function should not be used.
 *
 * @param view A pointer on the view.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
231
static int update_column_refs_and_dict(Obiview_p view);
232 233


234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
/**
 * @brief Internal function to update the line count in the context of a view.
 *
 * All columns of the view are enlarged to contain at least the new line count.
 *
 * @warning The view must be writable.
 *
 * @param view A pointer on the view.
 * @param line_count The new line count.
 *
 * @returns A value indicating the success of the operation.
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since February 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
251
static int update_lines(Obiview_p view, index_t line_count);
252 253


254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/**
 * @brief Internal function to clone a column in the context of a view.
 *
 * Clones with the right line selection and replaces the cloned columns with the new ones in the view.
 * If there is a line selection, all columns have to be cloned, otherwise only the column of interest is cloned.
 *
 * @param view A pointer on the view.
 * @param column_name The name of the column in the view that should be cloned.
 *
 * @returns A pointer on the new column.
 * @retval NULL if an error occurred.
 *
 * @since February 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
static OBIDMS_column_p clone_column_in_view(Obiview_p view, const char* column_name);


/**
 * @brief Saves a view, updating its informations in the view file.
 *
 * @warning The view must be writable.
 *
 * @param view A pointer on the view.
 *
 * @returns A value indicating the success of the operation.
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since February 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int save_view(Obiview_p view);


/**
 * @brief Rename a view file once the view is finished, replacing the '*.obiview_unfinished' extension with '*.obiview'.
 *
 * @param view A pointer on the view.
 *
 * @returns A value indicating the success of the operation.
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since February 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int rename_finished_view(Obiview_p view);


/**
 * @brief Finishes a view: check the predicates, save all the informations, rename the view file.
 *
 * @param view A pointer on the view.
 *
 * @returns A value indicating the success of the operation.
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since February 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int finish_view(Obiview_p view);

/**
 * @brief Closes an opened view.
 *
 * @warning Doesn't save the view.
 *
 * @param view A pointer on the view.
 *
 * @returns A value indicating the success of the operation.
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @see obi_save_and_close_view()
 * @since February 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int close_view(Obiview_p view);
334 335


336
/**
337
 * @brief Internal function preparing to set a value in a column, in the context of a view.
338 339 340 341 342 343 344 345 346 347 348 349 350 351
 *
 * The function checks that the view is not read-only, clones the column or all columns if needed,
 * and updates the line count if needed.
 *
 * @param view The view.
 * @param column_pp A pointer on the pointer on the column, to allow replacing the column if it is cloned.
 * @param line_nb_p A pointer on the index of the line that will be modified, to allow replacing it if needed.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
352
static int prepare_to_set_value_in_column(Obiview_p view, OBIDMS_column_p* column_pp, index_t* line_nb_p);
353 354


355
/**
356
 * @brief Internal function preparing to get a value from a column, in the context of a view.
357 358 359 360 361 362 363 364 365 366 367 368 369
 *
 * The function checks that the line index is not beyond the current line count of the view,
 * and modifies it if there is a line selection associated with the view.
 *
 * @param view The view.
 * @param line_nb_p A pointer on the index of the line, to allow replacing it if needed.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
370
static int prepare_to_get_value_from_column(Obiview_p view, index_t* line_nb_p);
371 372


373 374 375 376 377 378 379 380 381 382
/****** PREDICATE FUNCTIONS *******/

/**
 * @brief Internal function checking if a view has a NUC_SEQUENCE_COLUMN column.
 *
 * The function checks that the view has a column with the name attributed to obligatory
 * nucleotide sequence columns.
 *
 * @param view The view.
 *
383 384
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
385 386 387 388
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
389
static char* view_has_nuc_sequence_column(Obiview_p view);
390 391 392 393 394 395 396 397 398 399


/**
 * @brief Internal function checking if a view has a QUALITY_COLUMN column.
 *
 * The function checks that the view has a column with the name attributed to obligatory
 * quality columns.
 *
 * @param view The view.
 *
400 401
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
402 403 404 405
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
406
static char* view_has_quality_column(Obiview_p view);
407 408 409 410 411 412 413 414 415 416


/**
 * @brief Internal function checking if a view has a ID_COLUMN column.
 *
 * The function checks that the view has a column with the name attributed to obligatory
 * id columns.
 *
 * @param view The view.
 *
417 418
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
419 420 421 422
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
423
static char* view_has_id_column(Obiview_p view);
424 425 426 427 428 429 430 431 432 433


/**
 * @brief Internal function checking if a view has a DEFINITION_COLUMN column.
 *
 * The function checks that the view has a column with the name attributed to obligatory
 * definition columns.
 *
 * @param view The view.
 *
434 435
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
436 437 438 439
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
440
static char* view_has_definition_column(Obiview_p view);
441 442 443


/**
444
 * @brief Internal function checking that all the quality columns of a view and their associated sequence columns
445 446 447 448 449 450
 * 		  correspond properly:
 * 		  	- when a line is defined for either column, it must also be defined for the other column
 * 		  	- when a line is defined, the lengths of the sequence and of the quality must be equal
 *
 * @param view The view.
 *
451 452
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
453 454 455 456
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
457
static char* view_check_qual_match_seqs(Obiview_p view);
458 459 460 461 462 463 464 465


/**
 * @brief Internal function checking one predicate function on a view.
 *
 * @param view The view.
 * @param predicate_function The predicate function to use.
 *
466 467
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
468 469 470 471
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
472
static char* view_check_one_predicate(Obiview_p view, char* (*predicate_function)(Obiview_p view));
473 474 475 476 477 478


/**
 * @brief Internal function checking all the predicates associated with a view.
 *
 * @param view The view.
479
 * @param write Whether the verified predicates should be written in the view comments.
480
 *
481 482
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if at least one of the predicates is false or if there was an error.
483 484 485 486
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
487
static int view_check_all_predicates(Obiview_p view, bool write);
488 489


Celine Mercier committed
490 491 492 493 494 495
/************************************************************************
 *
 * 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
 *
 ************************************************************************/

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517

static char* build_obiview_file_name(const char* view_name)
{
	char* file_name;

	// Build file name
	file_name = (char*) malloc((strlen(view_name) + 8 + 1)*sizeof(char));
	if (file_name == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for a view file name");
		return NULL;
	}
	if (sprintf(file_name, "%s.obiview", view_name) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nProblem building an obiview file name");
		return NULL;
	}

	return file_name;
}
518 519


520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
static char* build_unfinished_obiview_file_name(const char* view_name)
{
	char* file_name;

	// Build file name
	file_name = (char*) malloc((strlen(view_name) + 19 + 1)*sizeof(char));
	if (file_name == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for a view file name");
		return NULL;
	}
	if (sprintf(file_name, "%s.obiview_unfinished", view_name) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nProblem building an unfinished obiview file name");
		return NULL;
	}

	return file_name;
}


543
static int view_is_finished(OBIDMS_p dms, const char* view_name)
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
{
	struct dirent* dp;
	char*		   file_name;

	// Check finished views
	// Create file name
	file_name = build_obiview_file_name(view_name);
	if (file_name == NULL)
		return -1;

	rewinddir(dms->view_directory);
	while ((dp = readdir(dms->view_directory)) != NULL)
	{
		if ((dp->d_name)[0] == '.')
			continue;
		if (strcmp(dp->d_name, file_name) == 0)
		{
			free(file_name);
			return true;
		}
	}

	free(file_name);

	// Check unfinished views
	// Create file name
	file_name = build_unfinished_obiview_file_name(view_name);
	if (file_name == NULL)
		return -1;

	rewinddir(dms->view_directory);
	while ((dp = readdir(dms->view_directory)) != NULL)
	{
		if ((dp->d_name)[0] == '.')
			continue;
		if (strcmp(dp->d_name, file_name) == 0)
		{
			free(file_name);
			return false;
		}
	}

	free(file_name);

	return -1;
}


592
static size_t get_platform_view_file_size()
Celine Mercier committed
593 594 595 596 597 598 599
{
	size_t obiview_size;
	size_t rounded_obiview_size;
	double multiple;

	obiview_size = sizeof(Obiview_infos_t);

600
	multiple = 	ceil((double) (obiview_size) / (double) getpagesize());
Celine Mercier committed
601 602 603 604 605 606 607

	rounded_obiview_size = multiple * getpagesize();

	return rounded_obiview_size;
}


608
static int enlarge_view_file(Obiview_p view, size_t new_size)
609
{
610
	int    obiview_file_descriptor;
611 612
	double multiple;
	size_t rounded_new_size;
613
	char*  file_name;
614

615
	// Create file name
616
	file_name = build_unfinished_obiview_file_name((view->infos)->name);
617 618
	if (file_name == NULL)
		return -1;
619 620

    // Open view file
621
	obiview_file_descriptor = openat((view->dms)->view_dir_fd, file_name, O_RDWR, 0777);
622 623 624 625
	if (obiview_file_descriptor < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError opening a view file");
626
		free(file_name);
627 628 629
		return -1;
	}

630
	free(file_name);
631 632 633 634 635 636 637 638 639 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

	// Round new size to a multiple of page size	// TODO make function in utils
	multiple = 	ceil((double) new_size / (double) getpagesize());
	rounded_new_size = multiple * getpagesize();

	// Enlarge the file
	if (ftruncate(obiview_file_descriptor, rounded_new_size) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError enlarging a view file");
		close(obiview_file_descriptor);
		return -1;
	}

	// Unmap and remap the file
	if (munmap(view->infos, (view->infos)->file_size) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError munmapping a view file when enlarging");
		close(obiview_file_descriptor);
		return -1;
	}

	view->infos = mmap(NULL,
					   rounded_new_size,
					   PROT_READ | PROT_WRITE,
					   MAP_SHARED,
					   obiview_file_descriptor,
					   0
					  );
	if (view->infos == MAP_FAILED)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError re-mmapping a view file after enlarging the file");
		close(obiview_file_descriptor);
		return -1;
	}

	// Set new size
	(view->infos)->file_size = rounded_new_size;

672 673 674 675 676 677
	if (close(obiview_file_descriptor) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError closing a view file");
		return -1;
	}
678 679 680 681 682

	return 0;
}


683
static int create_obiview_file(OBIDMS_p dms, const char* view_name)
Celine Mercier committed
684
{
685
	char* 				file_name;
Celine Mercier committed
686 687 688
	int 				obiview_file_descriptor;
	size_t  			file_size;

689
	// Create file name
690
	file_name = build_unfinished_obiview_file_name(view_name);
691 692
	if (file_name == NULL)
		return -1;
Celine Mercier committed
693 694

    // Create file
695
	obiview_file_descriptor = openat(dms->view_dir_fd, file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
Celine Mercier committed
696 697 698 699
	if (obiview_file_descriptor < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError creating an obiview file");
700
		free(file_name);
Celine Mercier committed
701 702 703
		return -1;
	}

704
	free(file_name);
Celine Mercier committed
705

706
	// Truncate file to the initial size
707
	file_size = get_platform_view_file_size();
Celine Mercier committed
708 709 710 711 712 713 714 715 716

	if (ftruncate(obiview_file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError truncating an obiview file to the right size");
		close(obiview_file_descriptor);
		return -1;
	}

717 718 719 720 721 722 723 724 725
	// Write file size
	if (write(obiview_file_descriptor, &file_size, sizeof(size_t)) < ((ssize_t) sizeof(size_t)))
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError writing the file size in an obiview file");
		close(obiview_file_descriptor);
		return -1;
	}

726 727 728 729 730 731
	if (close(obiview_file_descriptor) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError closing a view file");
		return -1;
	}
Celine Mercier committed
732

733 734
	return 0;
}
Celine Mercier committed
735 736


737
static int update_column_refs(Obiview_p view)
738 739
{
	int i;
740
	OBIDMS_column_p column;
741 742 743

	for (i=0; i < (view->infos)->column_count; i++)
	{
744 745 746 747 748 749 750 751 752
		column = *((OBIDMS_column_p*)ll_get(view->columns, i));
		if (column == NULL)
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError getting a column from the linked list of column pointers of a view");
			return -1;
		}
		strcpy(((((view->infos)->column_references)[i]).column_refs).column_name, (column->header)->name);
		((((view->infos)->column_references)[i]).column_refs).version = (column->header)->version;
753
	}
754
	return 0;
755 756 757
}


758
static int create_column_dict(Obiview_p view)
759 760
{
	int i;
761
	OBIDMS_column_p* column_pp;
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777

	view->column_dict = ht_create(MAX_NB_OPENED_COLUMNS);
	if (view->column_dict == NULL)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError creating a column dictionary");
		return -1;
	}

	// Rebuild the dictionary from the column references and the column pointer array associated with the view
	for (i=0; i < (view->infos)->column_count; i++)
	{
		// Check that each alias is unique
		if (ht_get(view->column_dict, (((view->infos)->column_references)[i]).alias) != NULL)
		{
			obi_set_errno(OBIVIEW_ERROR);
778
			obidebug(1, "\nError: the name/alias identifying a column in a view is not unique: %s", (((view->infos)->column_references)[i]).alias);
779 780 781
			return -1;
		}

782 783 784 785 786 787 788 789 790
		column_pp = (OBIDMS_column_p*) ll_get(view->columns, i);
		if (column_pp == NULL)
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError getting a column from the linked list of column pointers of a view when creating a column dictionary");
			return -1;
		}

		if (ht_set(view->column_dict, (((view->infos)->column_references)[i]).alias, column_pp) < 0)
791 792 793 794 795
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError adding a column in a column dictionary");
			return -1;
		}
796
	}
797 798 799 800 801

	return 0;
}


802
static int update_column_dict(Obiview_p view)
803 804 805 806 807 808 809 810 811 812 813
{
	// Re-initialize the dictionary to rebuild it from scratch
	ht_free(view->column_dict);

	if (create_column_dict(view) < 0)
		return -1;

	return 0;
}


814
static int update_column_refs_and_dict(Obiview_p view)
815
{
816 817
	if (update_column_refs(view) < 0)
		return -1;
818
	return update_column_dict(view);
Celine Mercier committed
819 820 821
}


822
static int update_lines(Obiview_p view, index_t line_count)
823
{
824 825
	int             i;
	OBIDMS_column_p column;
826 827 828 829 830 831 832 833

	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to update the line count of all columns in a read-only view");
		return -1;
	}
834

835
	for (i=0; i<((view->infos)->column_count); i++)
836
	{
837 838 839 840 841 842 843 844
		column = *((OBIDMS_column_p*)ll_get(view->columns, i));
		if (column == NULL)
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError getting a column from the linked list of column pointers of a view when updating view lines");
			return -1;
		}

845
		// Clone the column first if needed
846
		if (!(column->writable))
847
		{
848 849
			column = clone_column_in_view(view, (((view->infos)->column_references)[i]).alias);
			if (column == NULL)
850
			{
851 852
				obidebug(1, "\nError cloning a column in a view when updating its line count");
				return -1;
853
			}
854
		}
855

856
		// Enlarge the column if needed
857
		while (line_count > (column->header)->line_count)
858
		{
859
			if (obi_enlarge_column(column) < 0)
860 861
		    	return -1;
		}
862

863
		// Set the number of lines used to the new view line count
864
		(column->header)->lines_used = line_count;
865 866
	}

867
	(view->infos)->line_count = line_count;
868 869 870 871 872

	return 0;
}


873
static OBIDMS_column_p clone_column_in_view(Obiview_p view, const char* column_name)
874 875
{
	int i;
876
	OBIDMS_column_p column = NULL;
877
	OBIDMS_column_p new_column = NULL;
878 879 880 881 882 883 884 885 886 887
	OBIDMS_column_p column_buffer;

	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to delete a column in a read-only view");
		return NULL;
	}

888
	for (i=0; i<((view->infos)->column_count); i++)
889
	{
890
		if ((view->line_selection != NULL) || (!strcmp((((view->infos)->column_references)[i]).alias, column_name)))
891 892 893
		{ // Clone with the right line selection and replace (for all columns if there is a line selection)

			// Save pointer to close column after cloning
894 895 896 897 898 899 900
			column_buffer = *((OBIDMS_column_p*)ll_get(view->columns, i));
			if (column_buffer == NULL)
			{
				obi_set_errno(OBIVIEW_ERROR);
				obidebug(1, "\nError getting a column to clone from the linked list of column pointers of a view");
				return NULL;
			}
901 902

			// Clone and replace the column in the view
903 904
			column = obi_clone_column(view->dms, view->line_selection, (column_buffer->header)->name, (column_buffer->header)->version, true);
			if (column == NULL)
905 906 907 908 909 910
			{
				obi_set_errno(OBIVIEW_ERROR);
				obidebug(1, "\nError cloning a column to replace in a view");
				return NULL;
			}

911 912 913 914 915 916 917 918
			// Change the pointer in the linked list of column pointers
			if (ll_set(view->columns, i, column) < 0)
			{
				obi_set_errno(OBIVIEW_ERROR);
				obidebug(1, "\nError changing the column pointer of a cloned column in the linked list of column pointers of a view");
				return NULL;
			}

919 920 921
			// Close old cloned column
			obi_close_column(column_buffer);

922
			if (!strcmp((((view->infos)->column_references)[i]).alias, column_name))
923 924
				// Found the column to return
				new_column = column;
925 926 927
		}
	}

928
	// Close old line selection
929 930 931 932
	if (view->line_selection != NULL)
	{
		obi_close_column(view->line_selection);
		view->line_selection = NULL;
933 934 935
		// Update line selection reference
		(((view->infos)->line_selection).column_name)[0] = '\0';
		((view->infos)->line_selection).version = -1;
936 937
	}

938
	// Update column refs and dict
939 940 941 942 943
	if (update_column_refs_and_dict(view) < 0)
	{
		obidebug(1, "\nError updating columns references and dictionary after cloning a column in a view");
		return NULL;
	}
944

945
	return new_column;
946 947 948
}


949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
static int save_view(Obiview_p view)
{
	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to save a read-only view");
		return -1;
	}

	// Store reference for the line selection associated with that view if there is one
	if (view->line_selection != NULL)		// Unnecessary in theory, the line selection references are already saved
	{
		strcpy(((view->infos)->line_selection).column_name, ((view->line_selection)->header)->name);
		((view->infos)->line_selection).version = ((view->line_selection)->header)->version;
		(view->infos)->all_lines = false;
	}
	else	// Necessary because line selection could have been deleted if a column was cloned
	{
		(((view->infos)->line_selection).column_name)[0] = '\0';
		((view->infos)->line_selection).version = -1;
		(view->infos)->all_lines = true;
	}

973 974 975 976 977
	if (update_column_refs(view) < 0)
	{
		obidebug(1, "\nError updating column references when saving a view");
		return -1;
	}
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028

	return 0;
}


static int rename_finished_view(Obiview_p view)
{
	char* old_name;
	char* new_name;
	char* path_old_name;
	char* path_new_name;
	char* full_path_old_name;
	char* full_path_new_name;

	old_name = build_unfinished_obiview_file_name((view->infos)->name);
	new_name = build_obiview_file_name((view->infos)->name);

	path_old_name = malloc(MAX_PATH_LEN);
	path_new_name = malloc(MAX_PATH_LEN);

	strcpy(path_old_name, "VIEWS/");
	strcat(path_old_name, old_name);

	strcpy(path_new_name, "VIEWS/");
	strcat(path_new_name, new_name);

	full_path_old_name = obi_dms_get_full_path(view->dms, path_old_name);
	full_path_new_name = obi_dms_get_full_path(view->dms, path_new_name);

	if (rename(full_path_old_name, full_path_new_name) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError renaming the file of a finished view: %s", full_path_new_name);
		free(old_name);
		free(new_name);
		return -1;
	}

	free(old_name);
	free(new_name);
	free(path_new_name);
	free(path_old_name);
	free(full_path_old_name);
	free(full_path_new_name);

	return 0;
}


static int finish_view(Obiview_p view)
{
1029 1030
	int             i;
	OBIDMS_column_p column;
1031 1032 1033 1034 1035 1036 1037 1038 1039

	// Check that the view is not read-only
	if (view->read_only)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError trying to save a read-only view");
		return -1;
	}

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	// Add count column if it's a NUC_SEQ_VIEW with no count column // TODO discuss
	if ((!strcmp((view->infos)->view_type, VIEW_TYPE_NUC_SEQS)) && (!obi_view_column_exists(view, COUNT_COLUMN)))
	{
		if (obi_create_auto_count_column(view) < 0)
		{
			obidebug(1, "\nError creating an automatic count column when finishing a view");
			return -1;
		}
	}

	// Add id column if it's a NUC_SEQ_VIEW with no id column // TODO discuss
	if ((!strcmp((view->infos)->view_type, VIEW_TYPE_NUC_SEQS)) && (!obi_view_column_exists(view, ID_COLUMN)))
	{
		if (obi_create_auto_id_column(view, NULL) < 0)
		{
			obidebug(1, "\nError creating an automatic id column when finishing a view");
			return -1;
		}
	}

1060
	// Check predicates
1061
	if (view_check_all_predicates(view, true) < 0)
1062
	{
1063 1064 1065
		obidebug(1, "\nView predicates not respected, view rollbacked");
		obi_rollback_view(view);  // TODO discuss, maybe never call from C layer
		return -1;
1066 1067 1068 1069 1070
	}

	if (save_view(view) < 0)
		return -1;

1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
	 // Add the time in the view comments
	 time_t t;
	 t = time(&t);
	 if (obi_view_add_comment(view, "Date created", strtok(ctime(&t), "\n")) < 0)
	 {
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError adding the date of creation when finishing a view");
		return -1;
	}

1081 1082 1083
	if (rename_finished_view(view) < 0)
		return -1;

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
	// Flag the columns as finished
	for (i=0; i < ((view->infos)->column_count); i++)
	{
		column = *((OBIDMS_column_p*)ll_get(view->columns, i));
		if (column == NULL)
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError getting a column to flag it as finished when finishing a view");
			return -1;
		}
1094 1095
		if (column->writable)
			(column->header)->finished = true;
1096 1097
	}

1098 1099 1100 1101 1102 1103 1104 1105
	// Flag the line selection column as finished
	 if (view->line_selection != NULL)
	 {
		 column = view->line_selection;
     	 if (column->writable)
     		 (column->header)->finished = true;
	 }

1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	// Flag the view as finished
	(view->infos)->finished = true;

	return 0;
}


static int close_view(Obiview_p view)
{
	int i;
	int ret_value;
1117
	OBIDMS_column_p column;
1118 1119 1120 1121 1122

	ret_value = 0;

	for (i=0; i < ((view->infos)->column_count); i++)
	{
1123 1124 1125 1126 1127 1128 1129 1130
		column = *((OBIDMS_column_p*)ll_get(view->columns, i));
		if (column == NULL)
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError getting a column to close from the linked list of column pointers of a view");
			return -1;
		}
		if (obi_close_column(column) < 0)
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
		{
			obidebug(1, "\nError closing a column while closing a view");
			ret_value = -1;
		}
	}

	// Close line selection if there is one
	if (view->line_selection != NULL)
	{
		if (obi_close_column(view->line_selection) < 0)
		{
			obidebug(1, "\nError closing a line selection while closing a view");
			ret_value = -1;
		}
	}

1147 1148 1149
	// Free the linked list of column pointers
	ll_free(view->columns);

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
	// Free the column dictionary
	ht_free(view->column_dict);

	// Unmap view file
	if (obi_view_unmap_file(view->dms, view->infos) < 0)
	{
		obidebug(1, "\nError unmaping a view file while closing a view");
		ret_value = -1;
	}

	free(view);

	return ret_value;
}


static int prepare_to_set_value_in_column(Obiview_p view, OBIDMS_column_p* column_pp, index_t* line_nb_p)
1167
{
1168 1169
	int   i;
	char* column_name = NULL;
1170

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
	// Check that the view is not read-only
	if (view->read_only)
	{
		obidebug(1, "\nError trying to set a value in a column in a read-only view");
		return -1;
	}

	// If there is a line selection associated with the view or if the column
	// is read-only, all columns or this column respectively must be cloned
	if ((view->line_selection != NULL) || (!((*column_pp)->writable)))
	{
1182 1183 1184 1185 1186 1187
		// Get the name/alias of the column from the pointer
		for (i=0; i<((view->infos)->column_count); i++)
		{
			if (obi_view_get_column(view, (((view->infos)->column_references)[i]).alias) == *column_pp)
				column_name = (((view->infos)->column_references)[i]).alias;
		}
1188 1189
		if (column_name == NULL)
		{
1190 1191
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError trying to clone a column in a view: column alias not found from pointer");
1192 1193
			return -1;
		}
1194

1195
		(*column_pp) = clone_column_in_view(view, column_name);
1196 1197 1198 1199 1200 1201 1202
		if ((*column_pp) == NULL)
		{
			obidebug(1, "\nError trying to clone a column to modify it");
			return -1;
		}
	}

1203
	if (((*line_nb_p)+1) > (view->infos)->line_count)
1204
	{
1205
		if (update_lines(view, ((*line_nb_p)+1)) < 0)
1206 1207 1208 1209 1210 1211 1212
			return -1;
	}

	return 0;
}


1213
static int prepare_to_get_value_from_column(Obiview_p view, index_t* line_nb_p)
1214
{
1215
	if (((*line_nb_p)+1) > ((view->infos)->line_count))
1216 1217 1218 1219 1220 1221
	{
		obi_set_errno(OBICOL_UNKNOWN_ERROR);
		obidebug(1, "\nError trying to get a value that is beyond the current line count of the view");
		return -1;
	}

1222
	if (view->line_selection != NULL)
1223 1224 1225 1226 1227 1228
		(*line_nb_p) = *(((index_t*) ((view->line_selection)->data)) + (*line_nb_p));

	return 0;
}


1229 1230 1231

/****** PREDICATE FUNCTIONS *******/

1232
static char* view_has_nuc_sequence_column(Obiview_p view)
1233
{
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
	char* predicate;

	predicate = (char*) malloc((strlen("The view has an associated nucleotide sequence column.") + 1) * sizeof(char));
	if (predicate == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for predicate character string.");
		return NULL;
	}

	strcpy(predicate, "The view has an associated nucleotide sequence column.");

1246
	if (obi_view_get_column(view, NUC_SEQUENCE_COLUMN) != NULL)
1247
		return predicate;
1248
	else
1249 1250
	{
		obidebug(1, "\nError checking the predicate: %s", predicate);
1251
		return NULL;
1252
	}
1253 1254 1255
}


1256
static char* view_has_quality_column(Obiview_p view)
1257
{
1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
	char* predicate;

	predicate = (char*) malloc((strlen("The view has an associated sequence quality column.") + 1) * sizeof(char));
	if (predicate == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for predicate character string.");
		return NULL;
	}

	strcpy(predicate, "The view has an associated sequence quality column.");

1270
	if (obi_view_get_column(view, QUALITY_COLUMN) != NULL)
1271
		return predicate;
1272
	else
1273 1274
	{
		obidebug(1, "\nError checking the predicate: %s", predicate);
1275
		return NULL;
1276
	}
1277 1278 1279
}


1280
static char* view_has_id_column(Obiview_p view)
1281
{
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
	char* predicate;

	predicate = (char*) malloc((strlen("The view has an associated identifier column.") + 1) * sizeof(char));
	if (predicate == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for predicate character string.");
		return NULL;
	}

	strcpy(predicate, "The view has an associated identifier column.");

1294
	if (obi_view_get_column(view, ID_COLUMN) != NULL)
1295
		return predicate;
1296
	else
1297 1298
	{
		obidebug(1, "\nError checking the predicate: %s", predicate);
1299
		return NULL;
1300
	}
1301 1302
}

1303 1304

static char* view_has_definition_column(Obiview_p view)
1305
{
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
	char* predicate;

	predicate = (char*) malloc((strlen("The view has an associated definition column.") + 1) * sizeof(char));
	if (predicate == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for predicate character string.");
		return NULL;
	}

	strcpy(predicate, "The view has an associated definition column.");

1318
	if (obi_view_get_column(view, DEFINITION_COLUMN) != NULL)
1319
		return predicate;
1320
	else
1321 1322
	{
		obidebug(1, "\nError checking the predicate: %s", predicate);
1323
		return NULL;
1324
	}
1325 1326 1327
}


1328
static char* view_check_qual_match_seqs(Obiview_p view)
1329
{
1330
	index_t 		i, j, k;
1331 1332 1333 1334
	index_t			nb_elements_per_line;
	int	    		qual_len;
	const uint8_t*	qual;
	char* 			seq;
1335
	OBIDMS_column_p column;
1336 1337
	OBIDMS_column_p qual_column;
	OBIDMS_column_p seq_column;
1338
	char* 			predicate;
1339
	bool			at_least_one_qual_col;
1340

1341 1342 1343
	// Go through all columns in the view and check the predicate for all quality columns
	at_least_one_qual_col = false;
	for (i=0; i < ((view->infos)->column_count); i++)
1344
	{
1345 1346 1347 1348
		column = *((OBIDMS_column_p*)ll_get(view->columns, i));
		if (column == NULL)
		{
			obi_set_errno(OBIVIEW_ERROR);
1349
			obidebug(1, "\nError getting a column to clone from the linked list of column pointers of view %s", (view->infos)->name);
1350 1351 1352
			return NULL;
		}

1353
		// Check if it's a quality column
1354
		if ((column->header)->returned_data_type == OBI_QUAL)
1355 1356 1357
		{
			at_least_one_qual_col = true;
			// Check that the quality arrays match the sequences of the associated column
1358
			qual_column = column;
1359 1360 1361
			seq_column = obi_open_column(view->dms, ((qual_column->header)->associated_column).column_name, ((qual_column->header)->associated_column).version);
			if (seq_column == NULL)
			{
1362
				obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1363 1364
				return NULL;
			}
1365

1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
			// Close and reopen indexers to remap them properly, because in writable mode they are mostly unmapped
			if (obi_close_indexer(qual_column->indexer) < 0)
			{
				obidebug(1, "\nError closing the quality indexer when checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
				return NULL;
			}
			qual_column->indexer = obi_open_avl_group(view->dms, (qual_column->header)->indexer_name);
			if (qual_column->indexer == NULL)
			{
				obidebug(1, "\nError reopening the quality indexer when checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
				return NULL;
			}
			if (obi_close_indexer(seq_column->indexer) < 0)
			{
				obidebug(1, "\nError closing the sequence indexer when checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
				return NULL;
			}
			seq_column->indexer = obi_open_avl_group(view->dms, (seq_column->header)->indexer_name);
			if (seq_column->indexer == NULL)
			{
				obidebug(1, "\nError reopening the sequence indexer when checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
				return NULL;
			}

1390 1391 1392 1393
			nb_elements_per_line = (qual_column->header)->nb_elements_per_line;
			// Check that the quality and the sequence columns have the same number of elements per line
			if (nb_elements_per_line != (seq_column->header)->nb_elements_per_line)
			{
1394
				obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1395 1396
				return NULL;
			}
1397

1398
			// Check each sequence and its quality
1399 1400 1401 1402 1403 1404 1405 1406 1407
			for (j=0; j < (view->infos)->line_count; j++)
			{
				for (k=0; k < nb_elements_per_line; k++)
				{
					qual = obi_get_qual_int_with_elt_idx_and_col_p_in_view(view, qual_column, j, k, &qual_len);
					seq = obi_get_seq_with_elt_idx_and_col_p_in_view(view, seq_column, j, k);
					if ((qual != OBIQual_int_NA) && (seq != OBISeq_NA))
					{
						// Test that the lengths of the quality and the sequence are equal
1408
						if ((size_t)qual_len != strlen(seq))
1409
						{
1410
							obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1411 1412 1413 1414 1415 1416
							return NULL;
						}
					}
					// Test if one value is NA and not the other
					else if (((qual == OBIQual_int_NA) && (seq != OBISeq_NA)) || ((qual != OBIQual_int_NA) && (seq == OBISeq_NA)))
					{
1417
						obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1418 1419
						return NULL;
					}
1420
					free(seq);
1421 1422
				}
			}
1423

1424 1425
			obi_close_column(seq_column);
		}
1426
	}
1427

1428
	if (at_least_one_qual_col)
1429
	{
1430 1431 1432 1433 1434 1435 1436 1437
		predicate = (char*) malloc((strlen("The sequences and sequence quality arrays match.") + 1) * sizeof(char));
		if (predicate == NULL)
		{
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for predicate character string.");
			return NULL;
		}
		strcpy(predicate, "The sequences and sequence quality arrays match.");
1438
	}
1439
	else
1440
	{
1441 1442
		predicate = (char*) malloc(1 * sizeof(char));
		if (predicate == NULL)
1443
		{
1444 1445 1446
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for predicate character string.");
			return NULL;
1447
		}
1448
		strcpy(predicate, "");
1449
	}
1450

1451
	return predicate;
1452 1453 1454
}


1455
static char* view_check_one_predicate(Obiview_p view, char* (*predicate_function)(Obiview_p view))
1456 1457 1458 1459 1460
{
	return predicate_function(view);
}


1461
static int view_check_all_predicates(Obiview_p view, bool write)
1462
{
1463
	int    i;
1464
	char*  predicate = NULL;
1465 1466 1467

	for (i=0; i < view->nb_predicates; i++)
	{
1468
		// Check predicate
1469 1470 1471
		predicate = view_check_one_predicate(view, (view->predicate_functions)[i]);
		if (predicate == NULL)
		{
1472 1473
			// TODO discuss what to do
			return -1;
1474 1475 1476
		}
		else
		{
1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488
			if ((write) && (predicate[0]!='\0'))
			{
				// Add predicate in comments
				if (obi_view_add_comment(view, PREDICATE_KEY, predicate) < 0)
				{
					obi_set_errno(OBIVIEW_ERROR);
					obidebug(1, "\nError adding a verified predicate (%s) in the comments of a view.", predicate);
					free(predicate);
					return -1;
				}
			}
			free(predicate);
1489
		}
1490
	}
1491
	return 0;
1492 1493 1494 1495 1496 1497
}


// TODO predicate function that goes through each line / each elements


Celine Mercier committed
1498 1499 1500 1501 1502 1503
/**********************************************************************
 *
 * 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
 *
 **********************************************************************/

1504

1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553
bool obi_view_exists(OBIDMS_p dms, const char* view_name)
{
	struct dirent* dp;
	char*		   file_name;

	// Check finished views
	// Create file name
	file_name = build_obiview_file_name(view_name);
	if (file_name == NULL)
		return -1;

	rewinddir(dms->view_directory);
	while ((dp = readdir(dms->view_directory)) != NULL)
	{
		if ((dp->d_name)[0] == '.')
			continue;
		if (strcmp(dp->d_name, file_name) == 0)
		{
			free(file_name);
			return true;
		}
	}

	free(file_name);

	// Check unfinished views
	// Create file name
	file_name = build_unfinished_obiview_file_name(view_name);
	if (file_name == NULL)
		return -1;

	rewinddir(dms->view_directory);
	while ((dp = readdir(dms->view_directory)) != NULL)
	{
		if ((dp->d_name)[0] == '.')
			continue;
		if (strcmp(dp->d_name, file_name) == 0)
		{
			free(file_name);
			return true;
		}
	}

	free(file_name);

	return false;
}


Celine Mercier committed
1554
Obiview_p obi_new_view(OBIDMS_p dms, const char* view_name, Obiview_p view_to_clone, index_t* line_selection, const char* comments)
Celine Mercier committed
1555
{
1556 1557 1558
	Obiview_p 		view;
	int 			i;
	index_t 		line_nb;
1559
	OBIDMS_column_p column;
1560
	int             comments_ok;
1561

1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577
	// Check that the DMS is a valid pointer
	if (dms == NULL)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError creating a view: DMS pointer is NULL");
		return NULL;
	}

	// Check that the view name pointer is valid
	if (view_name == NULL)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError creating a view: view name is NULL");
		return NULL;
	}

1578
	// Check uniqueness of name
1579
	if (obi_view_exists(dms, view_name))
1580
	{
1581 1582
		obi_set_errno(OBIVIEW_ALREADY_EXISTS_ERROR);
		obidebug(1, "\nName of new view ('%s') already exists", view_name);
1583
		return NULL;
1584
	}
Celine Mercier committed
1585

1586 1587 1588 1589 1590 1591 1592 1593
	// Check that the view name is not 'taxonomy' (used for URIs)
	if (strcmp(view_name, "taxonomy") == 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nView name can not be 'taxonomy'");
		return NULL;
	}

1594
	// Allocate memory for view structure
Celine Mercier committed
1595 1596 1597 1598 1599 1600 1601 1602
	view = (Obiview_p) malloc(sizeof(Obiview_t));
	if (view == NULL)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError allocating memory for a view");
		return NULL;
	}

1603
	view->dms = dms;
1604
	view->read_only = false;
1605 1606 1607 1608 1609 1610 1611 1612 1613

	// Create view file
	if (create_obiview_file(dms, view_name) < 0)
	{
		free(view);
		return NULL;
	}

	// Map view file
1614
	view->infos = obi_view_map_file(dms, view_name, false);
1615 1616 1617 1618 1619 1620 1621
	if (view->infos == NULL)
	{
		obidebug(1, "\nError mapping the informations of a new view");
		free(view);
		return NULL;
	}

1622 1623 1624
	// Flag the view as being a work in progress
	(view->infos)->finished = false;

1625 1626 1627
	// Write used size in view file for initial structure
	(view->infos)->used_size = sizeof(Obiview_infos_t);