obiview.c 107 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 19
#include <inttypes.h>

Celine Mercier committed
20 21

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


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

53 54

/**
55
 * Internal function building the file name where the informations about a finished, read-only obiview are stored.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
 *
 * @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);


/**
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
 * 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);


/**
 * Internal function checking if a view (either finished or unfinished) with a given name already exists in a DMS.
88 89 90 91 92 93 94 95 96
 *
 * @param dms The DMS.
 * @param view_name The name of the view.
 *
 * @returns A boolean value indicating whether the view already exists or not.
 *
 * @since September 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
97
static bool view_exists(OBIDMS_p dms, const char* view_name);
98 99


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

Celine Mercier committed
110

111 112 113 114 115 116 117 118 119 120 121 122
/**
 * @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)
 */
123
static int enlarge_view_file(Obiview_p view, size_t new_size);
124 125


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


/**
 * @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.
 *
149 150 151 152 153 154 155 156
 * @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.
 *
157 158
 * @param view A pointer on the view.
 *
159 160 161
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
162 163 164
 * @since June 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
165
static int update_column_refs(Obiview_p view);
Celine Mercier committed
166 167


168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
/**
 * @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)
 */
184
static int create_column_dict(Obiview_p view);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202


/**
 * @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)
 */
203
static int update_column_dict(Obiview_p view);
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227


/**
 * @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)
 */
228
static int update_column_refs_and_dict(Obiview_p view);
229 230


231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
/**
 * @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)
 */
248
static int update_lines(Obiview_p view, index_t line_count);
249 250


251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
/**
 * @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)
 */
266 267 268 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
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);
331 332


333
/**
334
 * @brief Internal function preparing to set a value in a column, in the context of a view.
335 336 337 338 339 340 341 342 343 344 345 346 347 348
 *
 * 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)
 */
349
static int prepare_to_set_value_in_column(Obiview_p view, OBIDMS_column_p* column_pp, index_t* line_nb_p);
350 351


352
/**
353
 * @brief Internal function preparing to get a value from a column, in the context of a view.
354 355 356 357 358 359 360 361 362 363 364 365 366
 *
 * 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)
 */
367
static int prepare_to_get_value_from_column(Obiview_p view, index_t* line_nb_p);
368 369


370 371 372 373 374 375 376 377 378 379
/****** 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.
 *
380 381
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
382 383 384 385
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
386
static char* view_has_nuc_sequence_column(Obiview_p view);
387 388 389 390 391 392 393 394 395 396


/**
 * @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.
 *
397 398
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
399 400 401 402
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
403
static char* view_has_quality_column(Obiview_p view);
404 405 406 407 408 409 410 411 412 413


/**
 * @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.
 *
414 415
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
416 417 418 419
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
420
static char* view_has_id_column(Obiview_p view);
421 422 423 424 425 426 427 428 429 430


/**
 * @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.
 *
431 432
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
433 434 435 436
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
437
static char* view_has_definition_column(Obiview_p view);
438 439 440


/**
441
 * @brief Internal function checking that all the quality columns of a view and their associated sequence columns
442 443 444 445 446 447
 * 		  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.
 *
448 449
 * @returns A character string describing the predicate.
 * @retval NULL if the predicate is false or if there was an error.
450 451 452 453
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
454
static char* view_check_qual_match_seqs(Obiview_p view);
455 456 457 458 459 460 461 462


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


/**
 * @brief Internal function checking all the predicates associated with a view.
 *
 * @param view The view.
476
 * @param write Whether the verified predicates should be written in the view comments.
477
 *
478 479
 * @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.
480 481 482 483
 *
 * @since July 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
484
static int view_check_all_predicates(Obiview_p view, bool write);
485 486


Celine Mercier committed
487 488 489 490 491 492
/************************************************************************
 *
 * 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
 *
 ************************************************************************/

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

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


517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
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;
}


static bool view_exists(OBIDMS_p dms, const char* view_name)
541
{
542 543
	struct dirent* dp;
	char*		   file_name;
544

545
	// Check finished views
546 547 548 549 550
	// Create file name
	file_name = build_obiview_file_name(view_name);
	if (file_name == NULL)
		return -1;

551
	rewinddir(dms->view_directory);
552 553 554 555 556
	while ((dp = readdir(dms->view_directory)) != NULL)
	{
		if ((dp->d_name)[0] == '.')
			continue;
		if (strcmp(dp->d_name, file_name) == 0)
557 558 559 560 561 562 563 564 565 566 567 568 569 570
		{
			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;

571
	rewinddir(dms->view_directory);
572 573 574 575 576 577 578
	while ((dp = readdir(dms->view_directory)) != NULL)
	{
		if ((dp->d_name)[0] == '.')
			continue;
		if (strcmp(dp->d_name, file_name) == 0)
		{
			free(file_name);
579
			return true;
580
		}
581
	}
582 583 584

	free(file_name);

585 586 587 588
	return false;
}


589
static size_t get_platform_view_file_size()
Celine Mercier committed
590 591 592 593 594 595 596
{
	size_t obiview_size;
	size_t rounded_obiview_size;
	double multiple;

	obiview_size = sizeof(Obiview_infos_t);

597
	multiple = 	ceil((double) (obiview_size) / (double) getpagesize());
Celine Mercier committed
598 599 600 601 602 603 604

	rounded_obiview_size = multiple * getpagesize();

	return rounded_obiview_size;
}


605
static int enlarge_view_file(Obiview_p view, size_t new_size)
606
{
607
	int    obiview_file_descriptor;
608 609
	double multiple;
	size_t rounded_new_size;
610
	char*  file_name;
611

612
	// Create file name
613
	file_name = build_unfinished_obiview_file_name((view->infos)->name);
614 615
	if (file_name == NULL)
		return -1;
616 617

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

627
	free(file_name);
628 629 630 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

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

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

	return 0;
}


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

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

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

702
	free(file_name);
Celine Mercier committed
703

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

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

715 716 717 718 719 720 721 722 723
	// 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;
	}

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

731 732
	return 0;
}
Celine Mercier committed
733 734


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

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


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

	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);
			obidebug(1, "\nError: the name/alias identifying a column in a view is not unique");
			return -1;
		}

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

	return 0;
}


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

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

	return 0;
}


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


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

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

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

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

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

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

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

	return 0;
}


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

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

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

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

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

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

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

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

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

946
	return new_column;
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 973
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;
	}

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

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

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

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

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 1072 1073 1074
	}

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

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

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

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

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

	return 0;
}


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

	ret_value = 0;

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

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

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

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

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

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

	return 0;
}


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

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

	return 0;
}


1220 1221 1222

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

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

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


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

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


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

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

1294 1295

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

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


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

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

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

1357 1358 1359 1360
			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)
			{
1361
				obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1362 1363
				return NULL;
			}
1364

1365
			// Check each sequence and its quality
1366 1367 1368 1369 1370
			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);
1371

1372 1373 1374 1375
					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
1376
						if ((size_t)qual_len != strlen(seq))
1377
						{
1378
							obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1379 1380 1381 1382 1383 1384
							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)))
					{
1385
						obidebug(1, "\nError checking the predicate for view %s: The sequences and sequence quality arrays match.", (view->infos)->name);
1386 1387 1388 1389
						return NULL;
					}
				}
			}
1390

1391 1392
			obi_close_column(seq_column);
		}
1393
	}
1394

1395
	if (at_least_one_qual_col)
1396
	{
1397 1398 1399 1400 1401 1402 1403 1404
		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.");
1405
	}
1406
	else
1407
	{
1408 1409
		predicate = (char*) malloc(1 * sizeof(char));
		if (predicate == NULL)
1410
		{
1411 1412 1413
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for predicate character string.");
			return NULL;
1414
		}
1415
		strcpy(predicate, "");
1416
	}
1417

1418
	return predicate;
1419 1420 1421
}


1422
static char* view_check_one_predicate(Obiview_p view, char* (*predicate_function)(Obiview_p view))
1423 1424 1425 1426 1427
{
	return predicate_function(view);
}


1428
static int view_check_all_predicates(Obiview_p view, bool write)
1429
{
1430
	int    i;
1431
	char*  predicate = NULL;
1432 1433 1434

	for (i=0; i < view->nb_predicates; i++)
	{
1435
		// Check predicate
1436 1437 1438
		predicate = view_check_one_predicate(view, (view->predicate_functions)[i]);
		if (predicate == NULL)
		{
1439 1440
			// TODO discuss what to do
			return -1;
1441 1442 1443
		}
		else
		{
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455
			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);
1456
		}
1457
	}
1458
	return 0;
1459 1460 1461 1462 1463 1464
}


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


Celine Mercier committed
1465 1466 1467 1468 1469 1470
/**********************************************************************
 *
 * 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
 *
 **********************************************************************/

1471

Celine Mercier committed
1472
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
1473
{
1474 1475 1476
	Obiview_p 		view;
	int 			i;
	index_t 		line_nb;
1477
	OBIDMS_column_p column;
1478
	int             comments_ok;
1479

1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495
	// 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;
	}

1496
	// Check uniqueness of name
1497
	if (view_exists(dms, view_name))
1498
	{
1499 1500 1501
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nName of new view already exists");
		return NULL;
1502
	}
Celine Mercier committed
1503

1504 1505 1506 1507 1508 1509 1510 1511
	// 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;
	}

1512
	// Allocate memory for view structure
Celine Mercier committed
1513 1514 1515 1516 1517 1518 1519 1520
	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;
	}

1521
	view->dms = dms;
1522
	view->read_only = false;
1523 1524 1525 1526 1527 1528 1529 1530 1531

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

	// Map view file
1532
	view->infos = obi_view_map_file(dms, view_name, false);
1533 1534 1535 1536 1537 1538 1539
	if (view->infos == NULL)
	{
		obidebug(1, "\nError mapping the informations of a new view");
		free(view);
		return NULL;
	}

1540 1541 1542
	// Flag the view as being a work in progress
	(view->infos)->finished = false;

1543 1544 1545
	// Write used size in view file for initial structure
	(view->infos)->used_size = sizeof(Obiview_infos_t);

Celine Mercier committed
1546 1547 1548
	// Clone view to clone if there is one
	if (view_to_clone != NULL)
	{
1549
		if ((view_to_clone->infos)->finished == false)
Celine Mercier committed
1550 1551
		{
			obi_set_errno(OBIVIEW_ERROR);
1552
			obidebug(1, "\nA view can not be cloned if it is not finished");
1553
			obi_view_unmap_file(view->dms, view->infos);
Celine Mercier committed
1554 1555 1556 1557
			free(view);
			return NULL;
		}

1558 1559
		// If the view to clone has an associated line selection and there is no new line selection, open the associated line selection
		if ((view_to_clone->line_selection != NULL) && (line_selection == NULL))
Celine Mercier committed
1560 1561 1562
		{
			view->line_selection = obi_open_column(dms, ((view_to_clone->line_selection)->header)->name, ((view_to_clone->line_selection)->header)->version);
			if (view->line_selection == NULL)
1563 1564 1565
			{
				obi_view_unmap_file(view->dms, view->infos);
				free(view);
Celine Mercier committed
1566
				return NULL;