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
	// Add count column if it's a NUC_SEQ_VIEW with no count column (and there's no MERGED_sample column) // TODO discuss
	if ((!strcmp((view->infos)->view_type, VIEW_TYPE_NUC_SEQS)) && (!obi_view_column_exists(view, COUNT_COLUMN))
			&& (!obi_view_column_exists(view, "MERGED_sample")))  // TODO should eventually compute from merged samples?
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
	{
		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;
		}
	}

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

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

1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
	 // 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;
	}

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

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
	// 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;
		}
1095 1096
		if (column->writable)
			(column->header)->finished = true;
1097 1098
	}

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

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

	return 0;
}


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

	ret_value = 0;

	for (i=0; i < ((view->infos)->column_count); i++)
	{
1124 1125 1126 1127 1128 1129 1130 1131
		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)
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
		{
			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;
		}
	}

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

1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	// 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)
1168
{
1169 1170
	int   i;
	char* column_name = NULL;
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
	// 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)))
	{
1183 1184 1185 1186 1187 1188
		// 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;
		}
1189 1190
		if (column_name == NULL)
		{
1191 1192
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError trying to clone a column in a view: column alias not found from pointer");
1193 1194
			return -1;
		}
1195

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

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

	return 0;
}


1214
static int prepare_to_get_value_from_column(Obiview_p view, index_t* line_nb_p)
1215
{
1216
	if (((*line_nb_p)+1) > ((view->infos)->line_count))
1217 1218 1219 1220 1221 1222
	{
		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;
	}

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

	return 0;
}


1230 1231 1232

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

1233
static char* view_has_nuc_sequence_column(Obiview_p view)
1234
{
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
	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.");

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


1257
static char* view_has_quality_column(Obiview_p view)
1258
{
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
	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.");

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


1281
static char* view_has_id_column(Obiview_p view)
1282
{
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
	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.");

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

1304 1305

static char* view_has_definition_column(Obiview_p view)
1306
{
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
	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.");

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


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

1342 1343 1344
	// 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++)
1345
	{
1346 1347 1348 1349
		column = *((OBIDMS_column_p*)ll_get(view->columns, i));
		if (column == NULL)
		{
			obi_set_errno(OBIVIEW_ERROR);
1350
			obidebug(1, "\nError getting a column to clone from the linked list of column pointers of view %s", (view->infos)->name);
1351 1352 1353
			return NULL;
		}

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

1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390
			// 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;
			}

1391 1392 1393 1394
			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)
			{
1395
				obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1396 1397
				return NULL;
			}
1398

1399
			// Check each sequence and its quality
1400 1401 1402 1403 1404 1405 1406 1407 1408
			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
1409
						if ((size_t)qual_len != strlen(seq))
1410
						{
1411
							obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match (index %lld, seq=%s, quality length = %d).", (view->infos)->name, j, seq, qual_len);
1412 1413 1414 1415 1416 1417
							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)))
					{
1418
						obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1419 1420
						return NULL;
					}
1421
					free(seq);
1422 1423
				}
			}
1424

1425 1426
			obi_close_column(seq_column);
		}
1427
	}
1428

1429
	if (at_least_one_qual_col)
1430
	{
1431 1432 1433 1434 1435 1436 1437 1438
		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.");
1439
	}
1440
	else
1441
	{
1442 1443
		predicate = (char*) malloc(1 * sizeof(char));
		if (predicate == NULL)
1444
		{
1445 1446 1447
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for predicate character string.");
			return NULL;
1448
		}
1449
		strcpy(predicate, "");
1450
	}
1451

1452
	return predicate;
1453 1454 1455
}


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


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

	for (i=0; i < view->nb_predicates; i++)
	{
1469
		// Check predicate
1470 1471 1472
		predicate = view_check_one_predicate(view, (view->predicate_functions)[i]);
		if (predicate == NULL)
		{
1473 1474
			// TODO discuss what to do
			return -1;
1475 1476 1477
		}
		else
		{
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489
			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);
1490
		}
1491
	}
1492
	return 0;
1493 1494 1495 1496 1497 1498
}


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


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

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 1554
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
1555
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
1556
{
1557 1558 1559
	Obiview_p 		view;
	int 			i;
	index_t 		line_nb;
1560
	OBIDMS_column_p column;
1561
	int             comments_ok;
1562

1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578
	// 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;
	}

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

1587 1588 1589 1590 1591 1592 1593 1594
	// 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;
	}

1595
	// Allocate memory for view structure
Celine Mercier committed
1596 1597 1598 1599 1600 1601 1602 1603
	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;
	}

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

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

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

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

<