obiview.c 111 KB
Newer Older
Celine Mercier's avatar
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's avatar
Celine Mercier committed
20 21

#include "obiview.h"
22
#include "obidms.h"
Celine Mercier's avatar
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's avatar
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
 * 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);


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


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

Celine Mercier's avatar
Celine Mercier committed
112

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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


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


Celine Mercier's avatar
Celine Mercier committed
489 490 491 492 493 494
/************************************************************************
 *
 * 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
 *
 ************************************************************************/

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

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


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


542
static int view_is_finished(OBIDMS_p dms, const char* view_name)
543 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
{
	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;
}


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

	obiview_size = sizeof(Obiview_infos_t);

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

	rounded_obiview_size = multiple * getpagesize();

	return rounded_obiview_size;
}


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

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

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

629
	free(file_name);
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 670 671

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


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

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

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

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

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

	return 0;
}


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

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

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

704
	free(file_name);
Celine Mercier's avatar
Celine Mercier committed
705

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

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

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

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

733 734
	return 0;
}
Celine Mercier's avatar
Celine Mercier committed
735 736


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

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


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

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

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

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

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

	return 0;
}


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

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

	return 0;
}


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


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

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

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

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

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

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

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

	return 0;
}


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

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

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

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

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

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

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

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

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

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

947
	return new_column;
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 974
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;
	}

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

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

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

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

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

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

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

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

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

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

	return 0;
}


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

	ret_value = 0;

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

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

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

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

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

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

	return 0;
}


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

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

	return 0;
}


1221 1222 1223

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

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

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


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

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


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

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