obiview.c 111 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>
20

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 672

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

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

	return 0;
}


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

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

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

705
	free(file_name);
Celine Mercier committed
706

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

	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;
	}

718 719 720 721 722 723 724 725 726
	// 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;
	}

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

734 735
	return 0;
}
Celine Mercier committed
736 737


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

	for (i=0; i < (view->infos)->column_count; i++)
	{
745 746 747 748 749 750 751 752 753
		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;
754
	}
755
	return 0;
756 757 758
}


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

	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);
779
			obidebug(1, "\nError: the name/alias identifying a column in a view is not unique: %s", (((view->infos)->column_references)[i]).alias);
780 781 782
			return -1;
		}

783 784 785 786 787 788 789 790 791
		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)
792 793 794 795 796
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError adding a column in a column dictionary");
			return -1;
		}
797
	}
798 799 800 801 802

	return 0;
}


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

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

	return 0;
}


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


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

	// 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;
	}
835

836
	for (i=0; i<((view->infos)->column_count); i++)
837
	{
838 839 840 841 842 843 844 845
		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;
		}

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

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

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

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

	return 0;
}


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

	// 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;
	}

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

			// Save pointer to close column after cloning
897 898 899 900 901 902 903
			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;
			}
904 905

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

914 915 916 917 918 919 920 921
			// 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;
			}

922 923 924
			// Close old cloned column
			obi_close_column(column_buffer);

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

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

941
	// Update column refs and dict
942 943 944 945 946
	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;
	}
947

948
	return new_column;
949 950 951
}


952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
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;
	}

976 977 978 979 980
	if (update_column_refs(view) < 0)
	{
		obidebug(1, "\nError updating column references when saving a view");
		return -1;
	}
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 1029 1030 1031

	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)
{
1032 1033
	int             i;
	OBIDMS_column_p column;
1034 1035 1036 1037 1038 1039 1040 1041 1042

	// 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;
	}

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	// 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;
		}
	}

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

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

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

1077 1078 1079 1080 1081 1082 1083 1084 1085 1086
	// 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;
		}
1087 1088
		if (column->writable)
			(column->header)->finished = true;
1089 1090
	}

1091 1092 1093 1094 1095 1096 1097 1098
	// Flag the line selection column as finished
	 if (view->line_selection != NULL)
	 {
		 column = view->line_selection;
     	 if (column->writable)
     		 (column->header)->finished = true;
	 }

1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
	// Flag the view as finished
	(view->infos)->finished = true;

	return 0;
}


static int close_view(Obiview_p view)
{
	int i;
	int ret_value;
1110
	OBIDMS_column_p column;
1111 1112 1113 1114 1115

	ret_value = 0;

	for (i=0; i < ((view->infos)->column_count); i++)
	{
1116 1117 1118 1119 1120 1121 1122 1123
		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)
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
		{
			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;
		}
	}

1140 1141 1142
	// Free the linked list of column pointers
	ll_free(view->columns);

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	// 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)
1160
{
1161 1162
	int   i;
	char* column_name = NULL;
1163

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
	// 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)))
	{
1175 1176 1177 1178 1179 1180
		// 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;
		}
1181 1182
		if (column_name == NULL)
		{
1183 1184
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError trying to clone a column in a view: column alias not found from pointer");
1185 1186
			return -1;
		}
1187

1188
		(*column_pp) = clone_column_in_view(view, column_name);
1189 1190 1191 1192 1193 1194 1195
		if ((*column_pp) == NULL)
		{
			obidebug(1, "\nError trying to clone a column to modify it");
			return -1;
		}
	}

1196
	if (((*line_nb_p)+1) > (view->infos)->line_count)
1197
	{
1198
		if (update_lines(view, ((*line_nb_p)+1)) < 0)
1199 1200 1201 1202 1203 1204 1205
			return -1;
	}

	return 0;
}


1206
static int prepare_to_get_value_from_column(Obiview_p view, index_t* line_nb_p)
1207
{
1208
	if (((*line_nb_p)+1) > ((view->infos)->line_count))
1209 1210 1211 1212 1213 1214
	{
		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;
	}

1215
	if (view->line_selection != NULL)
1216 1217 1218 1219 1220 1221
		(*line_nb_p) = *(((index_t*) ((view->line_selection)->data)) + (*line_nb_p));

	return 0;
}


1222 1223 1224

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

1225
static char* view_has_nuc_sequence_column(Obiview_p view)
1226
{
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
	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.");

1239
	if (obi_view_get_column(view, NUC_SEQUENCE_COLUMN) != NULL)
1240
		return predicate;
1241
	else
1242 1243
	{
		obidebug(1, "\nError checking the predicate: %s", predicate);
1244
		return NULL;
1245
	}
1246 1247 1248
}


1249
static char* view_has_quality_column(Obiview_p view)
1250
{
1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
	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.");

1263
	if (obi_view_get_column(view, QUALITY_COLUMN) != NULL)
1264
		return predicate;
1265
	else
1266 1267
	{
		obidebug(1, "\nError checking the predicate: %s", predicate);
1268
		return NULL;
1269
	}
1270 1271 1272
}


1273
static char* view_has_id_column(Obiview_p view)
1274
{
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
	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.");

1287
	if (obi_view_get_column(view, ID_COLUMN) != NULL)
1288
		return predicate;
1289
	else
1290 1291
	{
		obidebug(1, "\nError checking the predicate: %s", predicate);
1292
		return NULL;
1293
	}
1294 1295
}

1296 1297

static char* view_has_definition_column(Obiview_p view)
1298
{
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
	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.");

1311
	if (obi_view_get_column(view, DEFINITION_COLUMN) != NULL)
1312
		return predicate;
1313
	else
1314 1315
	{
		obidebug(1, "\nError checking the predicate: %s", predicate);
1316
		return NULL;
1317
	}
1318 1319 1320
}


1321
static char* view_check_qual_match_seqs(Obiview_p view)
1322
{
1323
	index_t 		i, j, k;
1324 1325 1326 1327
	index_t			nb_elements_per_line;
	int	    		qual_len;
	const uint8_t*	qual;
	char* 			seq;
1328
	OBIDMS_column_p column;
1329 1330
	OBIDMS_column_p qual_column;
	OBIDMS_column_p seq_column;
1331
	char* 			predicate;
1332
	bool			at_least_one_qual_col;
1333

1334 1335 1336
	// 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++)
1337
	{
1338 1339 1340 1341
		column = *((OBIDMS_column_p*)ll_get(view->columns, i));
		if (column == NULL)
		{
			obi_set_errno(OBIVIEW_ERROR);
1342
			obidebug(1, "\nError getting a column to clone from the linked list of column pointers of view %s", (view->infos)->name);
1343 1344 1345
			return NULL;
		}

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

1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
			// 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;
			}

1383 1384 1385 1386
			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)
			{
1387
				obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1388 1389
				return NULL;
			}
1390

1391
			// Check each sequence and its quality
1392 1393 1394 1395 1396 1397 1398 1399 1400
			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
1401
						if ((size_t)qual_len != strlen(seq))
1402
						{
1403
							obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1404 1405 1406 1407 1408 1409
							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)))
					{
1410
						obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1411 1412
						return NULL;
					}
1413
					free(seq);
1414 1415
				}
			}
1416

1417 1418
			obi_close_column(seq_column);
		}
1419
	}
1420

1421
	if (at_least_one_qual_col)
1422
	{
1423 1424 1425 1426 1427 1428 1429 1430
		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.");
1431
	}
1432
	else
1433
	{
1434 1435
		predicate = (char*) malloc(1 * sizeof(char));
		if (predicate == NULL)
1436
		{
1437 1438 1439
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for predicate character string.");
			return NULL;
1440
		}
1441
		strcpy(predicate, "");
1442
	}
1443

1444
	return predicate;
1445 1446 1447
}


1448
static char* view_check_one_predicate(Obiview_p view, char* (*predicate_function)(Obiview_p view))
1449 1450 1451 1452 1453
{
	return predicate_function(view);
}


1454
static int view_check_all_predicates(Obiview_p view, bool write)
1455
{
1456
	int    i;
1457
	char*  predicate = NULL;
1458 1459 1460

	for (i=0; i < view->nb_predicates; i++)
	{
1461
		// Check predicate
1462 1463 1464
		predicate = view_check_one_predicate(view, (view->predicate_functions)[i]);
		if (predicate == NULL)
		{
1465 1466
			// TODO discuss what to do
			return -1;
1467 1468 1469
		}
		else
		{
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
			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);
1482
		}
1483
	}
1484
	return 0;
1485 1486 1487 1488 1489 1490
}


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


Celine Mercier committed
1491 1492 1493 1494 1495 1496
/**********************************************************************
 *
 * 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
 *
 **********************************************************************/

1497

1498 1499 1500 1501 1502 1503 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
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
1547
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
1548
{
1549 1550 1551
	Obiview_p 		view;
	int 			i;
	index_t 		line_nb;
1552
	OBIDMS_column_p column;
1553
	int             comments_ok;
1554

1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
	// 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;
	}

1571
	// Check uniqueness of name
1572
	if (obi_view_exists(dms, view_name))
1573
	{
1574 1575
		obi_set_errno(OBIVIEW_ALREADY_EXISTS_ERROR);
		obidebug(1, "\nName of new view ('%s') already exists", view_name);
1576
		return NULL;
1577
	}
Celine Mercier committed
1578

1579 1580 1581 1582 1583 1584 1585 1586
	// 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;
	}

1587
	// Allocate memory for view structure
Celine Mercier committed
1588 1589 1590 1591 1592 1593 1594 1595
	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;
	}

1596
	view->dms = dms;
1597
	view->read_only = false;
1598 1599 1600 1601 1602 1603 1604 1605 1606

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

	// Map view file
1607
	view->infos = obi_view_map_file(dms, view_name, false);
1608 1609 1610 1611 1612 1613 1614
	if (view->infos == NULL)
	{
		obidebug(1, "\nError mapping the informations of a new view");
		free(view);
		return NULL;
	}

1615 1616 1617
	// Flag the view as being a work in progress
	(view->infos)->finished = false;

1618 1619 1620
	// Write used size in view file for initial structure
	(view->infos)->used_size = sizeof(Obiview_infos_t);