obidms.c 42.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
/********************************************************************
 * OBIDMS functions                                                 *
 ********************************************************************/

/**
 * @file obidms.c
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 * @date 23 May 2015
 * @brief OBIDMS functions.
10 11
 */

12

13 14
#include <stdlib.h>
#include <stdio.h>
15
#include <string.h>
16
#include <stdbool.h>
17
#include <sys/stat.h>
18
#include <fcntl.h>
19 20
#include <sys/types.h>
#include <dirent.h>
21
#include <unistd.h>
22
#include <libgen.h>   /* <EC> : Added July 28th 2017 to include basename */
23
#include <sys/mman.h>
24
#include <math.h>
25 26

#include "obidms.h"
27
#include "obierrno.h"
28 29
#include "obidebug.h"
#include "obidmscolumn.h"
30
#include "obiview.h"
31
#include "obiblob_indexer.h"
32
#include "utils.h"
33
#include "obilittlebigman.h"
34
#include "libjson/json_utils.h"
35

36

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

39

40
// Initialize global list of opened DMS and their associated counters
41
OBIDMS_p global_opened_dms_list[MAX_NB_OPENED_DMS+1] = { 0 };
42 43
int global_opened_dms_counter_list[MAX_NB_OPENED_DMS+1] = { 0 };

44 45


46
/**************************************************************************
47
 *
48
 * 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
49
 *
50
 **************************************************************************/
51 52


53
/**
54
 * Internal function building the OBIDMS directory name from an OBIDMS name.
55
 *
56
 * The function builds the directory name corresponding to an OBIDMS.
57
 * It also checks that the name is not too long.
58 59 60
 *
 * @warning The returned pointer has to be freed by the caller.
 *
61
 * @param dms_name The name of the OBIDMS.
62
 *
63 64
 * @returns A pointer to the directory name.
 * @retval NULL if an error occurred.
65 66 67 68
 *
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
69
static char* build_directory_name(const char* dms_name);
70 71


72 73 74 75 76 77 78 79 80
/**
 * Internal function building the informations file name from an OBIDMS name.
 *
 * The function builds the file name for the informations file of an OBIDMS.
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param dms_name The name of the OBIDMS.
 *
81
 * @returns A pointer on the file name.
82 83 84 85 86 87 88 89
 * @retval NULL if an error occurred.
 *
 * @since November 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static char* build_infos_file_name(const char* dms_name);


90 91 92 93 94 95 96 97
/**
 * Internal function calculating the initial size of the file where the informations about a DMS are stored.
 *
 * @returns The initial size of the file in bytes, rounded to a multiple of page size.
 *
 * @since September 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
98
static size_t get_platform_infos_file_size(void);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147


/**
 * @brief Internal function enlarging a DMS information file.
 *
 * @param dms A pointer on the DMS.
 * @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 September 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int enlarge_infos_file(OBIDMS_p dms, size_t new_size);


/**
 * @brief Internal function mapping a DMS information file and returning the mapped structure stored in it.
 *
 * The function checks that endianness of the platform and of the DMS match.
 *
 * @param dms_file_descriptor The file descriptor of the DMS directory.
 * @param dms_name The base name of the DMS.
 *
 * @returns A pointer on the mapped DMS infos structure.
 * @retval NULL if an error occurred.
 *
 * @since September 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static OBIDMS_infos_p map_infos_file(int dms_file_descriptor, const char* dms_name);


/**
 * @brief Unmaps a DMS information file.
 *
 * @param dms A pointer on the OBIDMS.
 *
 * @returns A value indicating the success of the operation.
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since October 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int unmap_infos_file(OBIDMS_p dms);


148 149 150
/**
 * Internal function creating the file containing basic informations on the OBIDMS.
 *
151
 * This file contains an OBIDMS_infos structure.
152 153 154
 *
 * @warning The returned pointer has to be freed by the caller.
 *
155 156
 * @param dms_file_descriptor The file descriptor of the DMS directory.
 * @param dms_name The base name of the DMS.
157 158 159 160 161 162 163
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since November 2015
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
164
static int create_dms_infos_file(int dms_file_descriptor, const char* dms_name);
165 166


167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
/**
 * Internal function adding a DMS in the global list of DMS opened by a program.
 *
 * @param dms A pointer on the DMS to add.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since October 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int list_dms(OBIDMS_p dms);


/**
 * Internal function removing a DMS from the global list of DMS opened by a program.
 *
 * @param dms A pointer on the DMS to remove.
185 186 187 188
 * @param force Whether the DMS should be unlisted even if it is opened more than once.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if the DMS was not found.
189 190 191 192
 *
 * @since October 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
static int unlist_dms(OBIDMS_p dms, bool force);


/**
 * Internal function checking if a DMS is already in the global list of DMS opened by the program.
 *
 * @param full_dms_path The absolute path to the DMS directory.
 *
 * @returns A pointer on the DMS if it is already opened, or NULL if it was not opened.
 *
 * @since November 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static OBIDMS_p check_if_dms_in_list(const char* full_dms_path);


/**
 * Internal function returning the count for an opened DMS (how many times the DMS is opened) in the global list of DMS opened by the program.
 *
 * @param dms A pointer on the DMS.
 *
 * @returns The count indicating how many times the DMS has been opened by the program.
 *
 * @retval -1 if the DMS was not found.
 *
 * @since November 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
static int dms_count_in_list(OBIDMS_p dms);

223 224


225
/************************************************************************
226
 *
227
 * 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
228
 *
229
 ************************************************************************/
230

231
static char* build_directory_name(const char* dms_name)
232
{
233
	char* directory_name;
234 235

	// Build the database directory name
Celine Mercier committed
236 237
	directory_name = (char*) malloc((strlen(dms_name) + 8)*sizeof(char));
	if (sprintf(directory_name, "%s.obidms", dms_name) < 0)
238 239
	{
		obi_set_errno(OBIDMS_MEMORY_ERROR);
240
		obidebug(1, "\nProblem building an OBIDMS directory name");
241 242 243 244
		return NULL;
	}

	// Test if the database name is not too long
245
	if (strlen(directory_name) >= OBIDMS_MAX_NAME)
246 247
	{
		obi_set_errno(OBIDMS_LONG_NAME_ERROR);
248
		obidebug(1, "\nProblem building an OBIDMS directory name");
249
		free(directory_name);
250 251 252
		return NULL;
	}

253
	return directory_name;
254 255
}

256

257 258 259 260 261
static char* build_infos_file_name(const char* dms_name)
{
	char* file_name;

	// Build file name
Celine Mercier committed
262 263
	file_name = (char*) malloc((strlen(dms_name) + 7)*sizeof(char));
	if (sprintf(file_name, "%s_infos", dms_name) < 0)
264 265 266 267 268 269 270 271 272 273
	{
		obi_set_errno(OBIDMS_MEMORY_ERROR);
		obidebug(1, "\nProblem building an informations file name");
		return NULL;
	}

	return file_name;
}


274
static size_t get_platform_infos_file_size()
275
{
276 277 278
	size_t infos_size;
	size_t rounded_infos_size;
	double multiple;
279

280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	infos_size = sizeof(OBIDMS_infos_t);

	multiple = 	ceil((double) (infos_size) / (double) getpagesize());

	rounded_infos_size = multiple * getpagesize();

	return rounded_infos_size;
}


static int enlarge_infos_file(OBIDMS_p dms, size_t new_size)
{
	int    infos_file_descriptor;
	double multiple;
	size_t rounded_new_size;
	char*  file_name;
296 297

	// Create file name
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
	file_name = build_infos_file_name(dms->dms_name);
	if (file_name == NULL)
		return -1;

    // Open infos file
	infos_file_descriptor = openat(dms->dir_fd, file_name, O_RDWR, 0777);
	if (infos_file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening a DMS information file");
		free(file_name);
		return -1;
	}

	free(file_name);

	// 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(infos_file_descriptor, rounded_new_size) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError enlarging a DMS information file");
		close(infos_file_descriptor);
		return -1;
	}

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

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

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

	if (close(infos_file_descriptor) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError closing a DMS information file");
		return -1;
	}

	return 0;
}


static OBIDMS_infos_p map_infos_file(int dms_file_descriptor, const char* dms_name)
{
	char*				file_name;
	OBIDMS_infos_p		dms_infos;
	int 				file_descriptor;
	size_t  			min_size;
	size_t  			full_size;
	bool                little_endian_platform;

	// Build file name
	file_name = build_infos_file_name(dms_name);
	if (file_name == NULL)
		return NULL;

    // Open file
	file_descriptor = openat(dms_file_descriptor,
							 file_name,
							 O_RDWR, 0777);
	if (file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening a DMS information file (DMS %s)", dms_name);
		free(file_name);
		return NULL;
	}

	free(file_name);

	// Map minimum size to read endianness and file size
	// (fread() fails to read properly from the structure in the file because of padding)
	min_size = get_platform_infos_file_size();

	dms_infos = mmap(NULL,
					 min_size,
					 PROT_READ | PROT_WRITE,
					 MAP_SHARED,
					 file_descriptor,
					 0
	   	   	 	 	);
	if (dms_infos == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError mapping a DMS information file");
		return NULL;
	}

	// Check endianness of the platform and DMS
	little_endian_platform = obi_is_little_endian();
	if (little_endian_platform != dms_infos->little_endian)
	{
		obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR);
		obidebug(1, "\nError: The DMS and the platform have different endianness");
		close(file_descriptor);
		return NULL;
	}

	// Read actual file size
	full_size = dms_infos->file_size;

	// Unmap the minimum size and remap the file with the full size
	if (munmap(dms_infos, min_size) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError munmapping a DMS information file");
		close(file_descriptor);
		return NULL;
	}

	dms_infos = mmap(NULL,
				 	 full_size,
					 PROT_READ | PROT_WRITE,
					 MAP_SHARED,
					 file_descriptor,
					 0
	   	   	 	 	);
	if (dms_infos == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError mapping a DMS information file");
		return NULL;
	}

	if (close(file_descriptor) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError closing a DMS information file");
		return NULL;
	}

	return dms_infos;
}


static int unmap_infos_file(OBIDMS_p dms)
{
	OBIDMS_infos_p dms_infos;
	char* 	       file_name;
	int 	       file_descriptor;
	size_t         file_size;

	dms_infos = dms->infos;

	// Build file name
	file_name = build_infos_file_name(dms->dms_name);
	if (file_name == NULL)
		return -1;

    // Open file
	file_descriptor = openat(dms->dir_fd,
							 file_name,
							 O_RDWR, 0777);
	if (file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening a DMS information file (DMS %s) to unmap it", dms->dms_name);
		free(file_name);
		return -1;
	}

	free(file_name);

	// Unmap the DMS infos structure
	file_size = dms_infos->file_size;
	if (munmap(dms_infos, file_size) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError unmapping a DMS information file");
		close(file_descriptor);
		return -1;
	}

	if (close(file_descriptor) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError closing a DMS information file");
		return -1;
	}

	return 0;
}


static int create_dms_infos_file(int dms_file_descriptor, const char* dms_name)
{
	char*				file_name;
	int 				infos_file_descriptor;
	size_t  			file_size;

	// Build file name
514 515 516 517 518
	file_name = build_infos_file_name(dms_name);
	if (file_name == NULL)
		return -1;

    // Create file
519 520
	infos_file_descriptor = openat(dms_file_descriptor,
								   file_name,
521 522
								   O_RDWR | O_CREAT | O_EXCL,
								   0777);
523 524 525
	if (infos_file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
526
		obidebug(1, "\nError creating a DMS information file (DMS %s)", dms_name);
527 528 529 530 531 532
		free(file_name);
		return -1;
	}

	free(file_name);

533 534 535
	// Truncate file to the initial size
	file_size = get_platform_infos_file_size();

536 537 538
	if (ftruncate(infos_file_descriptor, file_size) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
539
		obidebug(1, "\nError truncating a DMS information file to the right size");
540 541 542 543
		close(infos_file_descriptor);
		return -1;
	}

544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
	// Map the DMS infos structure
	OBIDMS_infos_p infos = mmap(NULL,
				 	 file_size,
					 PROT_READ | PROT_WRITE,
					 MAP_SHARED,
					 infos_file_descriptor,
					 0
	   	   	 	 	);

	// Initialize values
	infos->little_endian = obi_is_little_endian();
	infos->file_size = file_size;
	infos->used_size = 0;
	infos->comments[0] = '\0';

	// Unmap the infos file
	if (munmap(infos, file_size) < 0)
561
	{
562 563
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nError munmapping a DMS information file");
564 565 566
		return -1;
	}

567 568 569
	if (close(infos_file_descriptor) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
570
		obidebug(1, "\nError closing a view file");
571 572 573
		return -1;
	}

574 575 576
	return 0;
}

577

578 579 580
static int list_dms(OBIDMS_p dms)
{
	int i = 0;
581
	while ((global_opened_dms_list[i] != NULL) && (global_opened_dms_list[i] != dms))
582 583 584 585 586 587 588
		i++;
	if (i == (MAX_NB_OPENED_DMS-1))
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening a DMS: maximum number of DMS opened by a program reached");
		return -1;
	}
589 590 591
	if (global_opened_dms_list[i] == NULL)
	{    // new dms
		global_opened_dms_list[i] = dms;
592
		global_opened_dms_counter_list[i] = 1;
593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	}
	else
	{    // already opened dms
		(global_opened_dms_counter_list[i])++;
	}

	return 0;
}


static int unlist_dms(OBIDMS_p dms, bool force)
{
	int i = 0;
	while ((global_opened_dms_list[i] != dms) && (i <= MAX_NB_OPENED_DMS))
		i++;
	if (i == MAX_NB_OPENED_DMS)
		return -1; // DMS not found
	// If opened more than once, and the unlisting is not forced, decrement counter
	if ((global_opened_dms_counter_list[i] > 1) && (! force))
		(global_opened_dms_counter_list[i])--;
	else
	{	// If opened once or forced unlisting, delete and unlist by shifting the list
		while (global_opened_dms_list[i] != NULL)
		{
			global_opened_dms_list[i] = global_opened_dms_list[i+1];
			i++;
		}
	}
621 622 623 624
	return 0;
}


625
static OBIDMS_p check_if_dms_in_list(const char* full_dms_path)
626 627
{
	int i = 0;
628
	while ((i <= MAX_NB_OPENED_DMS) && (global_opened_dms_list[i] != NULL) && (strcmp(global_opened_dms_list[i]->directory_path, full_dms_path) != 0))
629
		i++;
630 631 632 633 634 635 636 637 638 639 640 641
	if (i == MAX_NB_OPENED_DMS)
		return NULL; // DMS not found
	if (global_opened_dms_list[i] != NULL)
		return global_opened_dms_list[i];
	return NULL;
}


static int dms_count_in_list(OBIDMS_p dms)
{
	int i = 0;
	while ((i <= MAX_NB_OPENED_DMS) && (global_opened_dms_list[i] != dms))
642
		i++;
643 644 645
	if (i == MAX_NB_OPENED_DMS)
		return -1; // DMS not found
	return global_opened_dms_counter_list[i];
646 647 648
}


649
/**********************************************************************
650
 *
651
 * 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
652
 *
653
 **********************************************************************/
654

655
int obi_dms_exists(const char* dms_path)
656
{
657
    struct stat buffer;
Celine Mercier committed
658 659
	char*       directory_name;
    int 		check_dir;
660 661

	// Build and check the directory name
662
    directory_name = build_directory_name(dms_path);
663
	if (directory_name == NULL)
664 665
		return -1;

666
	check_dir = stat(directory_name, &buffer);
667

668
	free(directory_name);
669

670
    if (check_dir == 0)
671
        return 1;
672 673
    else
    	return 0;
674 675 676
}


677
OBIDMS_p obi_create_dms(const char* dms_path)
678
{
679 680 681 682
	char*  			directory_name;
	DIR*  			dms_dir;
	int    			dms_file_descriptor;
	OBIDMS_p        dms;
683 684

	// Build and check the directory name
685
	directory_name = build_directory_name(dms_path);
686
	if (directory_name == NULL)
687 688 689
		return NULL;

	// Try to create the directory
690
	if (mkdir(directory_name, 00777) < 0)
691
	{
692
		if (errno == EEXIST)
693
		{
694
			obi_set_errno(OBIDMS_EXIST_ERROR);
695 696
			obidebug(1, "\nAn OBIDMS directory with the same name already exists in this directory.");
		}
697 698
		else
			obi_set_errno(OBIDMS_UNKNOWN_ERROR);
699
		obidebug(1, "\nProblem creating an OBIDMS directory");
700
		free(directory_name);
701 702 703
		return NULL;
	}

704
	// Get file descriptor of DMS directory to create other directories
705 706 707 708 709 710 711 712
	dms_dir = opendir(directory_name);
	if (dms_dir == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nProblem opening a newly created OBIDMS directory");
		free(directory_name);
		return NULL;
	}
713 714 715

	free(directory_name);

716 717 718 719 720 721 722 723
	dms_file_descriptor = dirfd(dms_dir);
	if (dms_file_descriptor < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nProblem getting the file descriptor of a newly created OBIDMS directory");
		return NULL;
	}

Celine Mercier committed
724 725
	// Create the indexer directory
	if (mkdirat(dms_file_descriptor, INDEXER_DIR_NAME, 00777) < 0)
726
	{
Celine Mercier committed
727
		obi_set_errno(OBI_INDEXER_ERROR);
728
		obidebug(1, "\nProblem creating the indexer directory");
729 730 731
		return NULL;
	}

732 733 734 735
	// Create the view directory
	if (mkdirat(dms_file_descriptor, VIEW_DIR_NAME, 00777) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
736 737 738 739 740 741 742 743 744
		obidebug(1, "\nProblem creating the view directory");
		return NULL;
	}

	// Create the taxonomy directory
	if (mkdirat(dms_file_descriptor, TAXONOMY_DIR_NAME, 00777) < 0)
	{
		obi_set_errno(OBIVIEW_ERROR);
		obidebug(1, "\nProblem creating the taxonomy directory");
745 746 747
		return NULL;
	}

748 749 750 751 752 753 754
	// Create the information file
	if (create_dms_infos_file(dms_file_descriptor, basename((char*)dms_path)) < 0)
		return NULL;

	// Open DMS
	dms = obi_open_dms(dms_path);
	if (dms == NULL)
755
	{
756 757
		obidebug(1, "\nProblem opening a DMS");
		return NULL;
758 759
	}

760 761 762 763
	// Write empty json string in comments (Python json lib doesn't like empty "")
	if (obi_dms_write_comments(dms, "{}") < 0)
	{
		obidebug(1, "\nProblem initializing empty json string in new DMS comments");
764
		return NULL;
765
	}
766

767 768 769 770 771 772 773
	if (closedir(dms_dir) < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError closing a directory");
		return NULL;
	}

774
	return dms;
775 776
}

777

778
OBIDMS_p obi_open_dms(const char* dms_path)
779 780
{
	OBIDMS_p	dms;
781 782
	char*	 	relative_dms_path;
	char*	 	absolute_dms_path;
783 784

	dms = NULL;
785

786 787 788
	// Build and check the directory name including the relative path
	relative_dms_path = build_directory_name(dms_path);
	if (relative_dms_path == NULL)
789 790
		return NULL;

791 792 793
	// Get and store the absolute path to the DMS directory
	absolute_dms_path = realpath(relative_dms_path, NULL);
	if (absolute_dms_path == NULL)
794
	{
795 796 797 798
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError getting the absolute path to the DMS directory (DMS does not exist)");
		free(relative_dms_path);
		return NULL;
799 800
	}

801 802 803 804 805
	free(relative_dms_path);

	// Check if the DMS is already opened
	dms = check_if_dms_in_list(absolute_dms_path);
	if (dms != NULL)
806
	{
807 808 809
		list_dms(dms);
		free(absolute_dms_path);
		return dms;
810 811
	}

812 813 814
	// Allocate the data structure
	dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t));
	if (dms == NULL)
815
	{
816 817
		obi_set_errno(OBIDMS_MEMORY_ERROR);
		obidebug(1, "\nError allocating the memory for the OBIDMS structure");
818
		return NULL;
819
	}
820

821 822
	strcpy(dms->dms_name, basename((char*)dms_path));
	strcpy(dms->directory_path, absolute_dms_path);
823

824
	// Try to open the directory
825
	dms->directory = opendir(dms->directory_path);
826
	if (dms->directory == NULL)
827 828 829 830 831 832
	{
		switch (errno)
		{
			case ENOENT:
				obi_set_errno(OBIDMS_NOT_EXIST_ERROR);
				break;
833

834 835 836
			case EACCES:
				obi_set_errno(OBIDMS_ACCESS_ERROR);
				break;
837

838 839 840
			case ENOMEM:
				obi_set_errno(OBIDMS_MEMORY_ERROR);
				break;
841

842 843
			default:
				obi_set_errno(OBIDMS_UNKNOWN_ERROR);
844
		}
845
		obidebug(1, "\nCan't open OBIDMS directory");
846
		free(dms);
847 848 849
		return NULL;
	}

850
	// Get and store file descriptor of DMS directory
851 852
	dms->dir_fd = dirfd(dms->directory);
	if (dms->dir_fd < 0)
853 854 855
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError getting the file descriptor for a newly created OBIDMS directory");
856 857
		closedir(dms->directory);
		free(dms);
858 859 860
		return NULL;
	}

861 862 863
	// Open the information file
	dms->infos = map_infos_file(dms->dir_fd, dms->dms_name);
	if (dms->infos == NULL)
864
	{
865
		obidebug(1, "\nError opening a DMS information file");
866 867 868 869 870
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

Celine Mercier committed
871 872 873
	// Open the indexer directory
	dms->indexer_directory = opendir_in_dms(dms, INDEXER_DIR_NAME);
	if (dms->indexer_directory == NULL)
874 875
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
Celine Mercier committed
876
		obidebug(1, "\nError opening the indexer directory");
877
		unmap_infos_file(dms);
878 879
		closedir(dms->directory);
		free(dms);
880 881 882
		return NULL;
	}

Celine Mercier committed
883 884 885
	// Store the indexer directory's file descriptor
	dms->indexer_dir_fd = dirfd(dms->indexer_directory);
	if (dms->indexer_dir_fd < 0)
886 887
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
Celine Mercier committed
888
		obidebug(1, "\nError getting the file descriptor of the indexer directory");
889
		unmap_infos_file(dms);
Celine Mercier committed
890
		closedir(dms->indexer_directory);
891
		closedir(dms->directory);
892 893 894
		free(dms);
		return NULL;
	}
895

896 897 898 899 900 901
	// Open the view directory
	dms->view_directory = opendir_in_dms(dms, VIEW_DIR_NAME);
	if (dms->view_directory == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening the view directory");
902
		unmap_infos_file(dms);
903 904 905 906 907 908 909 910 911 912 913 914
		closedir(dms->indexer_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

	// Store the view directory's file descriptor
	dms->view_dir_fd = dirfd(dms->view_directory);
	if (dms->view_dir_fd < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError getting the file descriptor of the view directory");
915
		unmap_infos_file(dms);
916 917 918 919 920 921
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

922 923 924 925 926 927
	// Open the taxonomy directory
	dms->tax_directory = opendir_in_dms(dms, TAXONOMY_DIR_NAME);
	if (dms->tax_directory == NULL)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError opening the taxonomy directory");
928
		unmap_infos_file(dms);
929 930 931 932 933 934 935 936 937 938 939 940 941
		closedir(dms->indexer_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

	// Store the taxonomy directory's file descriptor
	dms->tax_dir_fd = dirfd(dms->tax_directory);
	if (dms->tax_dir_fd < 0)
	{
		obi_set_errno(OBIDMS_UNKNOWN_ERROR);
		obidebug(1, "\nError getting the file descriptor of the taxonomy directory");
942
		unmap_infos_file(dms);
943
		closedir(dms->indexer_directory);
944 945 946 947 948 949 950 951 952 953 954
		closedir(dms->tax_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

	// Clean unfinished views
	if (obi_clean_unfinished_views(dms) < 0)
	{
		obidebug(1, "\nError cleaning unfinished views when opening an OBIDMS");
955
		unmap_infos_file(dms);
956 957 958 959 960 961 962 963 964 965 966 967
		closedir(dms->indexer_directory);
		closedir(dms->tax_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

	// Clean unfinished columns
	if (obi_clean_unfinished_columns(dms) < 0)
	{
		obidebug(1, "\nError cleaning unfinished columns when opening an OBIDMS");
968
		unmap_infos_file(dms);
969
		closedir(dms->indexer_directory);
970 971 972 973 974 975 976
		closedir(dms->tax_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms);
		return NULL;
	}

977 978 979 980
	// Initialize the list of opened columns
	dms->opened_columns = (Opened_columns_list_p) malloc(sizeof(Opened_columns_list_t));
	(dms->opened_columns)->nb_opened_columns = 0;

981
	// Initialize the list of opened indexers
Celine Mercier committed
982 983
	dms->opened_indexers = (Opened_indexers_list_p) malloc(sizeof(Opened_indexers_list_t));
	(dms->opened_indexers)->nb_opened_indexers = 0;
984

985
	// Add in the global list of opened DMS
986 987 988
	if (list_dms(dms) < 0)
	{
		obidebug(1, "\nError cleaning unfinished columns when opening an OBIDMS");
989
		unmap_infos_file(dms);
990 991 992 993 994 995 996 997 998
		closedir(dms->indexer_directory);
		closedir(dms->tax_directory);
		closedir(dms->view_directory);
		closedir(dms->directory);
		free(dms->opened_columns);
		free(dms->opened_indexers);
		free(dms);
		return NULL;
	}
999

1000 1001 1002
	return dms;
}

1003

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
OBIDMS_p obi_test_open_dms(const char* dms_name)
{
	int exists;

	exists = obi_dms_exists(dms_name);

	switch (exists)
	{
		case 0:
			return NULL;
		case 1:
			return obi_open_dms(dms_name);
	};

	obidebug(1, "\nError checking if an OBIDMS directory exists");
	return NULL;
}


1023 1024
OBIDMS_p obi_dms(const char* dms_name)
{
1025
	int exists;
1026

1027 1028 1029 1030
	exists = obi_dms_exists(dms_name);

	switch (exists)
	{
1031
		case 0:
1032
			return obi_create_dms(dms_name);
1033
		case 1:
1034
			return obi_open_dms(dms_name);
1035 1036
	};

1037
	obidebug(1, "\nError checking if an OBIDMS directory exists");
1038 1039 1040
	return NULL;
}

1041

1042
int obi_close_dms(OBIDMS_p dms, bool force)
1043
{
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	int dms_counter;

	if (!force)
	{
		dms_counter = dms_count_in_list(dms);
		if (dms_counter < 0)
			obidebug(1, "\nError checking the counter of an OBIDMS in the global list of opened OBIDMS");

		if (dms_counter > 1)	// Don't close if the DMS is opened more than once
		{
			if (unlist_dms(dms, force) < 0)
			{
				obidebug(1, "\nError decrementing the counter of an OBIDMS in the global list of opened OBIDMS");
				return -1;
			}
			return 0;
		}
	}

1063 1064
	if (dms != NULL)
	{
1065 1066 1067 1068 1069 1070 1071 1072
		// Unmap information file
		if (unmap_infos_file(dms) < 0)
		{
			obidebug(1, "\nError unmaping a DMS information file while closing a DMS");
			free(dms);
			return -1;
		}

1073 1074 1075 1076
		// Close all columns
		while ((dms->opened_columns)->nb_opened_columns > 0)
			obi_close_column(*((dms->opened_columns)->columns));

1077
		// Close dms, and view, indexer and taxonomy directories
1078
		if (closedir(dms->indexer_directory) < 0)
1079
		{
Celine Mercier committed
1080 1081
			obi_set_errno(OBI_INDEXER_ERROR);
			obidebug(1, "\nError closing an indexer directory");
1082 1083 1084
			free(dms);
			return -1;
		}
1085 1086 1087 1088 1089 1090 1091
		if (closedir(dms->view_directory) < 0)
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError closing a view directory");
			free(dms);
			return -1;
		}
1092 1093 1094 1095 1096 1097 1098
		if (closedir(dms->tax_directory) < 0)
		{
			obi_set_errno(OBIVIEW_ERROR);
			obidebug(1, "\nError closing a taxonomy directory");
			free(dms);
			return -1;
		}
1099 1100 1101 1102 1103 1104 1105
		if (closedir(dms->directory) < 0)
		{
			obi_set_errno(OBIDMS_MEMORY_ERROR);
			obidebug(1, "\nError closing an OBIDMS directory");
			free(dms);
			return -1;
		}
1106 1107

		// Remove DMS from global list of opened DMS
1108 1109 1110 1111 1112 1113
		if (unlist_dms(dms, force) < 0)
		{
			obidebug(1, "\nError removing an OBIDMS from the global list of opened OBIDMS when closing it");
			free(dms);
			return -1;
		}
1114

1115 1116 1117 1118 1119 1120
		free(dms);
	}

	return 0;
}

1121

1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
int obi_dms_write_comments(OBIDMS_p dms, const char* comments)
{
	size_t new_size;

	if (comments == NULL)
		return 0;	// TODO or error? discuss

	new_size = sizeof(OBIDMS_infos_t) + strlen(comments) + 1;

	// Check if the file has to be enlarged
	if (new_size >= (dms->infos)->file_size)
	{
		if (enlarge_infos_file(dms, new_size) < 0)
			return -1;
	}

	strcpy((dms->infos)->comments, comments);

	(dms->infos)->used_size = new_size;

	return 0;
}


int obi_dms_add_comment(OBIDMS_p dms, const char* key, const char* value)
{
	char* new_comments = NULL;

	new_comments = obi_add_comment((dms->infos)->comments, key, value);
	if (new_comments == NULL)
	{
		obidebug(1, "\nError adding a comment to a dms, key: %s, value: %s", key, value);
		return -1;
	}

	if (obi_dms_write_comments(dms, new_comments) < 0)
	{
		obidebug(1, "\nError adding a comment to a dms, key: %s, value: %s", key, value);
		return -1;
	}

	free(new_comments);

	return 0;
}


1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
OBIDMS_column_p obi_dms_get_column_from_list(OBIDMS_p dms, const char* column_name, obiversion_t version)
{
	int i;

	for (i=0; i < ((dms->opened_columns)->nb_opened_columns); i++)
	{
		if (!strcmp(((*(((dms->opened_columns)->columns)+i))->header)->name, column_name)
				&& (((*(((dms->opened_columns)->columns)+i))->header)->version == version))
		{	// Found the column already opened, return it
			return *(((dms->opened_columns)->columns)+i);
		}
	}
	// Didn't find the column
	return NULL;
}


1186
void obi_dms_list_column(OBIDMS_p dms, OBIDMS_column_p column)	// TODO add check if column already in list?
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
{
	*(((dms->opened_columns)->columns)+((dms->opened_columns)->nb_opened_columns)) = column;
	((dms->opened_columns)->nb_opened_columns)++;
}


int obi_dms_unlist_column(OBIDMS_p dms, OBIDMS_column_p column)
{
	int i;
	Opened_columns_list_p columns_list;

	columns_list = dms->opened_columns;

	for (i=0; i < columns_list->nb_opened_columns; i++)
	{
		if (!strcmp(((*((columns_list->columns)+i))->header)->name, (column->header)->name)
				&& (((*((columns_list->columns)+i))->header)->version == (column->header)->version))
		{	// Found the column. Rearrange list
			(columns_list->nb_opened_columns)--;
			(columns_list->columns)[i] = (columns_list->columns)[columns_list->nb_opened_columns];
			return 0;
		}
	}

	obidebug(1, "\nCould not find the column to delete from list of open columns");
	return -1;
}


Obi_indexer_p obi_dms_get_indexer_from_list(OBIDMS_p dms, const char* indexer_name)
{
	int i;
	Opened_indexers_list_p indexers_list;

	indexers_list = dms->opened_indexers;

	for (i=0; i < (indexers_list->nb_opened_indexers); i++)
	{
1225
		if (!strcmp(obi_indexer_get_name((indexers_list->indexers)[i]), indexer_name))
1226 1227 1228 1229 1230 1231 1232 1233 1234
		{	// Found the indexer already opened, return it
			return (indexers_list->indexers)[i];
		}
	}
	// Didn't find the indexer
	return NULL;
}


1235
void obi_dms_list_indexer(OBIDMS_p dms, Obi_indexer_p indexer)  	// TODO add check if indexer already in list?
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
{
	*(((dms->opened_indexers)->indexers)+((dms->opened_indexers)->nb_opened_indexers)) = indexer;
	((dms->opened_indexers)->nb_opened_indexers)++;
}


int obi_dms_unlist_indexer(OBIDMS_p dms, Obi_indexer_p indexer)
{
	int i;
	Opened_indexers_list_p indexers_list;

	indexers_list = dms->opened_indexers;

	for (i=0; i < indexers_list->nb_opened_indexers; i++)
	{
1251
		if (!strcmp(obi_indexer_get_name((indexers_list->indexers)[i]), indexer->name))
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
		{	// Found the indexer. Rearrange list
			(indexers_list->nb_opened_indexers)--;
			(indexers_list->indexers)[i] = (indexers_list->indexers)[indexers_list->nb_opened_indexers];
			return 0;
		}
	}

	obidebug(1, "\nCould not find the indexer to delete from list of open indexers");
	return -1;
}


1264
char* obi_dms_get_dms_path(OBIDMS_p dms)
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
{
	char* full_path;

	full_path = (char*) malloc((MAX_PATH_LEN)*sizeof(char));
	if (full_path == NULL)
	{
		obi_set_errno(OBI_MALLOC_ERROR);
		obidebug(1, "\nError allocating memory for the char* path to a file or directory");
		return NULL;
	}

1276
	strcpy(full_path, dms->directory_path);
1277 1278 1279 1280 1281 1282 1283 1284 1285 1286

	return full_path;
}


char* obi_dms_get_full_path(OBIDMS_p dms, const char* path_name)
{
	char* full_path;

	full_path = obi_dms_get_dms_path(dms);
1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298
	strcat(full_path, "/");
	strcat(full_path, path_name);

	return full_path;
}


DIR* opendir_in_dms(OBIDMS_p dms, const char* path_name)
{
	char* full_path;
	DIR* directory;

1299
	full_path = obi_dms_get_full_path(dms, path_name);
1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
	if (full_path == NULL)
		return NULL;

	directory = opendir(full_path);
	if (directory == NULL)
	{
		obi_set_errno(OBI_UTILS_ERROR);
		obidebug(1, "\nError opening a directory");
	}

	free(full_path);

	return directory;
}
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380


// TODO move somewhere else maybe
// TODO discuss arguments
obiversion_t obi_import_column(const char* dms_path_1, const char* dms_path_2, const char* column_name, obiversion_t version_number)
{
	OBIDMS_p				dms_1;
	OBIDMS_p 				dms_2;
	OBIDMS_column_p 		column_1;
	OBIDMS_column_p 	   	column_2;
	OBIDMS_column_header_p 	header_1;
	OBIDMS_column_header_p 	header_2;
	int 					avl_exists;
	const char* 			avl_name;
	char* 					new_avl_name;
	obiversion_t 			new_version;
	int 					i;
	int 					avl_count;
	char*		 			avl_name_1;
	char* 					avl_name_2;
	char* 					avl_file_path_1;
	char* 					avl_file_path_2;
	char* 					avl_data_file_path_1;
	char* 					avl_data_file_path_2;
	char* 					complete_avl_name;
	Obi_indexer_p 			avl_group;

	dms_1 = obi_open_dms(dms_path_1);
	if (dms_1 == NULL)
	{
		obidebug(1, "\nError opening a DMS to import a column from it");
		return -1;
	}

	dms_2 = obi_dms(dms_path_2);
	if (dms_2 == NULL)
	{
		obidebug(1, "\nError opening or creating a DMS to import a column into it");
		return -1;
	}

	column_1 = obi_open_column(dms_1, column_name, version_number);
	if (column_1 == NULL)
	{
		obidebug(1, "\nError opening a column to import in another DMS");
		return -1;
	}

	header_1 = column_1->header;

	// Check if associated indexer exists BEFORE creating the new column as that will automatically create it if it doesn't already exist
	avl_name = header_1->indexer_name;
	avl_exists = obi_indexer_exists(dms_2, avl_name);
	if (avl_exists == -1)
	{
		obidebug(1, "\nError checking if an indexer exists while importing a column");
		return -1;
	}
	if (avl_exists)
		// Use automatic name
		new_avl_name = NULL;
	else
		// Name can stay the same
		new_avl_name = header_1->indexer_name;

	// Create new column
	column_2 = obi_create_column(dms_2, column_name, header_1->returned_data_type, header_1->line_count,
1381
								 header_1->nb_elements_per_line, header_1->elements_names, true, header_1->tuples,
1382 1383
								 header_1->to_eval, new_avl_name, (header_1->associated_column).column_name,
								 (header_1->associated_column).version, header_1->comments);
1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430

	if (column_2 == NULL)
	{
		obidebug(1, "\nError creating the new column while importing a column");
		return -1;
	}

	header_2 = column_2->header;

	// Get the new version to return
	new_version = header_2->version;

	// Copy lines_used informations
	header_2->lines_used = header_1->lines_used;

	// Copy data TODO check how much time and memory that costs, eventually use write() instead
	memcpy(column_2->data, column_1->data, header_1->data_size);

	// Copy the AVL files if there are some (overwriting the automatically created files)
	if ((header_1->returned_data_type == OBI_STR) || (header_1->returned_data_type == OBI_SEQ) || (header_1->returned_data_type == OBI_QUAL))
	{
		avl_name_1 = (char*) malloc((strlen(header_1->indexer_name) + 1)  * sizeof(char));
		if (avl_name_1 == NULL)
		{
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for an AVL name when importing a column");
			return -1;
		}
		strcpy(avl_name_1, header_1->indexer_name);

		avl_name_2 = (char*) malloc((strlen(header_2->indexer_name) + 1)  * sizeof(char));
		if (avl_name_2 == NULL)
		{
			obi_set_errno(OBI_MALLOC_ERROR);
			obidebug(1, "\nError allocating memory for an AVL name when importing a column");
			return -1;
		}
		strcpy(avl_name_2, header_2->indexer_name);

		avl_count = (column_1->indexer->last_avl_idx) + 1;

		// Close column to manipulate AVL files safely (but not multithreading safe) (TODO not sure how important this is, can't find informations about conflicts when using write() on mmapped files)
		if (obi_close_column(column_1) < 0)
		{
			obidebug(1, "\nError closing an imported column");
			return -1;
		}
1431 1432

		(column_2->header)->finished = true;  // note: this is normally handled by the view that created the column
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
		if (obi_close_column(column_2) < 0)
		{
			obidebug(1, "\nError closing an imported column");
			return -1;
		}

		for (i=0; i < avl_count; i++)
		{
			avl_file_path_1 = obi_get_full_path_of_avl_file_name(dms_1, avl_name_1, i);
			if (avl_file_path_1 == NULL)
			{
				obidebug(1, "\nError getting an AVL file path while importing a column");
				return -1;
			}

			avl_data_file_path_1 = obi_get_full_path_of_avl_data_file_name(dms_1, avl_name_1, i);
			if (avl_data_file_path_1 == NULL)
			{
				obidebug(1, "\nError getting an AVL file path while importing a column");
				return -1;
			}
			avl_file_path_2 = obi_get_full_path_of_avl_file_name(dms_2, avl_name_2, i);
			if (avl_file_path_2 == NULL)
			{
				obidebug(1, "\nError getting an AVL file path while importing a column");
				return -1;
			}
			avl_data_file_path_2 = obi_get_full_path_of_avl_data_file_name(dms_2, avl_name_2, i);
			if (avl_data_file_path_2 == NULL)
			{
				obidebug(1, "\nError getting an AVL file path while importing a column");
				return -1;
			}

			// Copy AVL file
			if (copy_file(avl_file_path_1, avl_file_path_2) < 0)
			{
				obidebug(1, "\nError copying an AVL file while importing a column");
				return -1;
			}

			// Copy AVL data file
			if (copy_file(avl_data_file_path_1, avl_data_file_path_2) < 0)
			{
				obidebug(1, "\nError copying a data AVL file while importing a column");
				return -1;
			}

			free(avl_file_path_1);
			free(avl_file_path_2);
			free(avl_data_file_path_1);
			free(avl_data_file_path_2);
		}

		// Update AVL names in headers
		avl_group = obi_open_indexer(dms_2, avl_name_2);
		for (i=0; i < avl_count; i++)
		{
			complete_avl_name = obi_build_avl_name_with_idx(avl_name_2, i);
			strcpy((((avl_group->sub_avls)[i])->header)->avl_name, complete_avl_name);
			strcpy(((((avl_group->sub_avls)[i])->data)->header)->avl_name, complete_avl_name);
			free(complete_avl_name);
		}

		free(avl_name_1);
		free(avl_name_2);
	}
	else
	{
		if (obi_close_column(column_1) < 0)
		{
			obidebug(1, "\nError closing an imported column");
			return -1;
		}
1507 1508

		(column_2->header)->finished = true;  // note: this is normally handled by the view that created the column
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
		if (obi_close_column(column_2) < 0)
		{
			obidebug(1, "\nError closing an imported column");
			return -1;
		}
	}

	// Copy associated column (update version)
	//new_associated_col_version = import_column(dms_path_1, dms_path_2, header_1->associated_column_name, header_1->associated_column_version);
	// TODO no? because if iterating over all columns in a view etc.... iterate and change associated columns version refs afterwards?

	// Close the DMS
1521 1522
	obi_close_dms(dms_1, false);
	obi_close_dms(dms_2, false);
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536

	return new_version;
}


// TODO move somewhere else maybe
// TODO discuss arguments
int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char* view_name_1, const char* view_name_2)
{
	OBIDMS_p 		dms_1;
	OBIDMS_p	 	dms_2;
	Obiview_p 		view_1;
	Obiview_p 		view_2;
	obiversion_t 	new_version;
1537 1538 1539
	int 			i, j;
	OBIDMS_column_header_p header = NULL;
	OBIDMS_column_header_p header_2 = NULL;
1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559

	dms_1 = obi_open_dms(dms_path_1);
	if (dms_1 == NULL)
	{
		obidebug(1, "\nError opening a DMS to import a view from it");
		return -1;
	}

	dms_2 = obi_dms(dms_path_2);
	if (dms_2 == NULL)
	{
		obidebug(1, "\nError opening or creating a DMS to import a view into it");
		return -1;
	}

	// Open view to import
	view_1 = obi_open_view(dms_1, view_name_1);

	// Create new view
	if (strcmp((view_1->infos)->view_type, VIEW_TYPE_NUC_SEQS) == 0)
1560
		view_2 = obi_new_view_nuc_seqs(dms_2, view_name_2, NULL, NULL, (view_1->infos)->comments, false, false);
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604
	else	// Non-typed view
		view_2 = obi_new_view(dms_2, view_name_2, NULL, NULL, (view_1->infos)->comments);

	// Import line count
	view_2->infos->line_count = view_1->infos->line_count;

	// Import the line selection column if there is one
	if (! view_1->infos->all_lines)
	{
		view_2->infos->all_lines = false;
		new_version = obi_import_column(dms_path_1, dms_path_2, ((view_1->infos)->line_selection).column_name, ((view_1->infos)->line_selection).version);
		if (new_version == -1)
		{
			obidebug(1, "\nError importing a line selection column while importing a view");
			return -1;
		}
		strcpy(((view_2->infos)->line_selection).column_name, ((view_1->infos)->line_selection).column_name);
		((view_2->infos)->line_selection).version = new_version;
		view_2->line_selection = obi_open_column(dms_2, ((view_2->infos)->line_selection).column_name, ((view_2->infos)->line_selection).version);
		if (view_2->line_selection == NULL)
		{
			obidebug(1, "\nError opening a line selection column while importing a view");
			return -1;
		}
	}

	// Import each column and update with the new version number
	for (i=0; i < (view_1->infos->column_count); i++)
	{
		new_version = obi_import_column(dms_path_1, dms_path_2, ((((view_1->infos)->column_references)[i]).column_refs).column_name, ((((view_1->infos)->column_references)[i]).column_refs).version);
		if (new_version == -1)
		{
			obidebug(1, "\nError importing a column while importing a view");
			return -1;
		}

		if (obi_view_add_column(view_2,
							    ((((view_1->infos)->column_references)[i]).column_refs).column_name,
							    new_version,
							    (((view_1->infos)->column_references)[i]).alias,
							    0,
								0,
								0,
								NULL,
1605
								false,
1606 1607
								false,
								false,
1608 1609 1610 1611 1612
								NULL,
								NULL,
								-1,
								NULL,
								false) < 0)
1613 1614 1615 1616
		{
			obidebug(1, "\nError adding a column to a view while importing it");
			return -1;
		}
1617 1618
	}

1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660
	// Go through columns again to update associated columns
	for (i=0; i < (view_1->infos->column_count); i++)
	{
		header = obi_column_get_header_from_name(dms_1, ((((view_1->infos)->column_references)[i]).column_refs).column_name, ((((view_1->infos)->column_references)[i]).column_refs).version);
		if (header == NULL)
		{
			obidebug(1, "\nError opening a column header while importing a view");
			return -1;
		}

		if (header->returned_data_type == OBI_QUAL)   // TODO maybe there should be a bool in column headers
		{
			// Look for the index of the associated column in the list
			for (j=0; j < (view_1->infos->column_count); j++)
			{
				if ((strcmp((header->associated_column).column_name, ((((view_1->infos)->column_references)[j]).column_refs).column_name) == 0) &&
						((header->associated_column).version == ((((view_1->infos)->column_references)[j]).column_refs).version))
					break;
			}

			header_2 = obi_column_get_header_from_name(dms_2, ((((view_2->infos)->column_references)[i]).column_refs).column_name, ((((view_2->infos)->column_references)[i]).column_refs).version);
			if (header_2 == NULL)
			{
				obidebug(1, "\nError opening a column header while importing a view");
				return -1;
			}

			// Update version of associated column
			(header_2->associated_column).version = ((((view_2->infos)->column_references)[j]).column_refs).version;
			if (obi_close_header(header_2) < 0)
			{
				obidebug(1, "\nError closing a column header while importing a view");
				return -1;
			}
		}
		if (obi_close_header(header) < 0)
		{
			obidebug(1, "\nError closing a column header while importing a view");
			return -1;
		}
	}

1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
	// Close the views
	if (obi_save_and_close_view(view_1) < 0)
	{
		obidebug(1, "\nError closing a view after importing from it");
		return -1;
	}
	if (obi_save_and_close_view(view_2) < 0)
	{
		obidebug(1, "\nError closing a view after importing it");
		return -1;
	}

	// Close the DMS
1674 1675
	obi_close_dms(dms_1, false);
	obi_close_dms(dms_2, false);
1676 1677 1678 1679 1680

	return 0;
}


1681 1682 1683
void obi_close_atexit()
{
	int i=0;
1684

1685 1686
	while (global_opened_dms_list[i] != NULL)
	{
1687
        obi_close_dms(global_opened_dms_list[i], true);
1688 1689 1690
		i++;
	}
}
1691