obidms.h 17.9 KB
Newer Older
1 2 3 4 5 6 7 8 9
/********************************************************************
 * OBIDMS header file                                               *
 ********************************************************************/

/**
 * @file obidmscolumn.h
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 * @date 23 May 2015
 * @brief Header file for the OBIDMS functions and structures.
10 11
 */

12

13 14 15
#ifndef OBIDMS_H_
#define OBIDMS_H_

16

17 18 19 20
#include <stdlib.h>
#include <sys/stat.h>
#include <errno.h>
#include <dirent.h>
Eric Coissac committed
21 22
#include <string.h>
#include <stdio.h>
23
#include <stdbool.h>
24

25
#include "obierrno.h"
26
#include "obitypes.h"
27

28

29
#define OBIDMS_MAX_NAME (247) 	    			/**< The maximum length of an OBIDMS name.
Celine Mercier committed
30 31 32
 	 	 	 	 	 	 	 	 	 	 	 	 */
#define INDEXER_DIR_NAME "OBIBLOB_INDEXERS"  	/**< The name of the Obiblob indexer directory.
                                	 	 	 	 */
33 34
#define VIEW_DIR_NAME "VIEWS"					/**< The name of the view directory.
												 */
Celine Mercier committed
35 36
#define TAXONOMY_DIR_NAME "TAXONOMY"    		/**< The name of the taxonomy directory.
                                	 	 	 	 */
37
#define MAX_NB_OPENED_COLUMNS (1000) 			/**< The maximum number of columns open at the same time.
Celine Mercier committed
38
                                	 	 	 	 */
39 40
#define MAX_NB_OPENED_DMS (1000) 			    /**< The maximum number of DMS open at the same time for a given program.
                                	 	 	 	 */
41
#define MAX_NB_OPENED_INDEXERS (1000) 			/**< The maximum number of indexers open at the same time.
Celine Mercier committed
42
                                	 	 	 	 */
43
#define MAX_PATH_LEN (1024)						/**< Maximum length for the character string defining a
44 45 46 47
 	 	 	 	 	 	 	 	 	 	 	 	 *	 file or directory path.
 	 	 	 	 	 	 	 	 	 	 	 	 */


48 49
struct OBIDMS_column;							/**< Declarations to avoid circular dependencies. */
typedef struct OBIDMS_column* OBIDMS_column_p;  /**< Declarations to avoid circular dependencies. */
50

51 52 53 54 55 56
/**
 * @brief Structure listing the columns opened in a DMS, identified by their name and version number.
 */
typedef struct Opened_columns_list {
	int	 		 			nb_opened_columns;	  				/**< Number of opened columns.
	 	   	   	   	   	   	   	   	   	   	   	   	   	   	   	 */
57
	OBIDMS_column_p		 	columns[MAX_NB_OPENED_COLUMNS+1]; 	/**< Array of pointers on the opened columns.
58
 	   	   	   	   	   	   	 	 	 	 	 	 	 	 	 	 */
59 60 61
} Opened_columns_list_t, *Opened_columns_list_p;


62 63 64 65
// TODO Need to find a way to not refer to AVLs specifically
struct OBIDMS_avl_group;								/**< Declarations to avoid circular dependencies. */
typedef struct OBIDMS_avl_group* OBIDMS_avl_group_p;	/**< Declarations to avoid circular dependencies. */
typedef OBIDMS_avl_group_p Obi_indexer_p;				/**< Declarations to avoid circular dependencies. */
66

67 68 69
/**
 * @brief Structure listing the indexers opened in a DMS, identified by their name.
 */
Celine Mercier committed
70
typedef struct Opened_indexers_list {
71 72
	int		 		 		nb_opened_indexers;					/**< Number of opened indexers.
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
73
	Obi_indexer_p		    indexers[MAX_NB_OPENED_INDEXERS+1];	/**< Array of pointers on the opened indexers.
74
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
Celine Mercier committed
75
} Opened_indexers_list_t, *Opened_indexers_list_p;
76

77

78 79 80 81 82 83 84 85 86 87 88 89 90
/**
 * @brief A structure stored in an information file and containing comments and additional informations on the DMS
 * including the command line history.
 *
 * A pointer on the comments is kept in the OBIDMS structure when a DMS is opened.
 */
typedef struct OBIDMS_infos {
	bool	                little_endian;                  	/** Whether the DMS is in little endian.
																 */
	size_t                  file_size;                          /** The size of the file in bytes.
																 */
	size_t			     	used_size;    						/**< Used size in bytes.
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
91 92
	bool					working; 							/**< If the DMS is currently working (being used by a process).
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
93 94 95 96 97 98
	char 			    	comments[];							/**< Comments, additional informations on the DMS including
																 *   the command line history.
 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
} OBIDMS_infos_t, *OBIDMS_infos_p;


99 100
/**
 * @brief A structure describing an OBIDMS instance
101 102 103 104 105
 *
 * A pointer to this structure is returned on creation
 * and opening of an OBITools Data Management System (DMS)
 */
typedef struct OBIDMS {
106 107
	char					dms_name[OBIDMS_MAX_NAME+1];		/** The name of the DMS.
																 */
108
	char					directory_path[MAX_PATH_LEN+1];   	/**< The absolute path of the directory
109 110
																 *   containing the DMS.
																 */
111 112 113 114 115 116
	DIR*					directory;							/**< A directory entry usable to
																 *   refer and scan the database directory.
																 */
	int     				dir_fd;								/**< The file descriptor of the directory entry
																 *   usable to refer and scan the database directory.
																 */
117
	DIR*					indexer_directory;					/**< A directory entry usable to
Celine Mercier committed
118
																 *   refer and scan the indexer directory.
119
																 */
120
	int						indexer_dir_fd;						/**< The file descriptor of the directory entry
Celine Mercier committed
121
																 *   usable to refer and scan the indexer directory.
122
																 */
123 124 125 126 127 128
	DIR*					view_directory;						/**< A directory entry usable to
																 *   refer and scan the view directory.
																 */
	int						view_dir_fd;						/**< The file descriptor of the directory entry
																 *   usable to refer and scan the view directory.
																 */
129 130 131 132 133 134
	DIR*					tax_directory;						/**< A directory entry usable to
																 *   refer and scan the taxonomy directory.
																 */
	int						tax_dir_fd;							/**< The file descriptor of the directory entry
																 *   usable to refer and scan the taxonomy directory.
																 */
135 136
	Opened_columns_list_p 	opened_columns;						/**< List of opened columns.
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
Celine Mercier committed
137
	Opened_indexers_list_p 	opened_indexers;					/**< List of opened indexers.
138
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
139 140
	OBIDMS_infos_p 			infos;							    /**< A pointer on the mapped DMS information file.
 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
141 142 143
} OBIDMS_t, *OBIDMS_p;


144 145 146 147
/**
 * @brief Global Array of DMS pointers listing all the DMS opened by a program.
 */
extern OBIDMS_p global_opened_dms_list[MAX_NB_OPENED_DMS+1];
148
extern int global_opened_dms_counter_list[MAX_NB_OPENED_DMS+1];
149 150


151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
/**
 * @brief Checks if an OBIDMS contains unfinished views or columns.
 *
 * @param dms A pointer on the DMS.
 *
 * @returns An integer value indicating whether the DMS is clean or not.
 * @retval 1 if the DMS is clean.
 * @retval 0 if the DMS contains unfinished views or columns.
 * @retval -1 if an error occurred.
 *
 * @since September 2019
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int obi_dms_is_clean(OBIDMS_p dms);


/**
 * @brief Cleans an OBIDMS that contains unfinished views or columns and tags it as not working
 *        (not being used by a process).
 *
 * @param dms_path A pointer to a C string containing the path to the DMS.
 *
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 *
 * @since September 2019
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int obi_clean_dms(const char* dms_path);


182 183
/**
 * @brief Checks if an OBIDMS exists.
184
 *
185
 * @param dms_path A pointer to a C string containing the path to the database.
186
 *
187
 * @returns An integer value indicating the status of the database.
188 189 190
 * @retval 1 if the database exists.
 * @retval 0 if the database does not exist.
 * @retval -1 if an error occurred.
191 192 193 194
 *
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
195
int obi_dms_exists(const char* dms_path);
196

197

198
/**
199
 * @brief Creates a new OBITools Data Management instance (OBIDMS).
200
 *
201 202 203
 * A `DMS` is implemented as a directory. This function checks
 * if a directory with this name does not already exist
 * before creating the new database.
204
 *
Celine Mercier committed
205
 * A directory to store Obiblob indexers is also created.
206
 *
207
 * @param dms_path A pointer to a C string containing the path to the database.
208 209
 *                 The actual directory name used to store the DMS will be
 *                 `<dms_name>.obidms`.
210
 *
211 212
 * @returns A pointer to an OBIDMS structure describing the newly created DMS.
 * @retval NULL if an error occurred.
213 214 215 216 217
 *
 * @see obi_close_dms()
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
218
OBIDMS_p obi_create_dms(const char* dms_name);
219

220

221
/**
222
 * @brief Opens an existing OBITools Data Management instance (OBIDMS).
223
 *
224
 * @param dms_path A pointer to a C string containing the path to the database.
225
 * @param cleaning A boolean indicating whether the DMS is being opened for cleaning unfinished views and columns.
226
 *
227 228
 * @returns A pointer to an OBIDMS structure describing the opened DMS.
 * @retval NULL if an error occurred.
229 230 231 232 233
 *
 * @see obi_close_dms()
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
234
OBIDMS_p obi_open_dms(const char* dms_path, bool cleaning);
235

236

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
/**
 * @brief Opens an existing OBITools Data Management instance (OBIDMS).
 *
 * @warning No error is printed or saved if the DMS does not exist. For it to be the case, use obi_open_dms().
 *
 * @param dms_path A pointer to a C string containing the path to the database.
 *
 * @returns A pointer to an OBIDMS structure describing the opened DMS.
 * @retval NULL if the DMS does not exist or if an error occurred.
 *
 * @see obi_open_dms()
 * @see obi_close_dms()
 * @since May 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
OBIDMS_p obi_test_open_dms(const char* dms_name);


255
/**
256
 * @brief Creates or opens a new OBIDMS instance.
257
 *
258 259
 * If the database already exists, this function opens it, otherwise it
 * creates a new database.
260
 *
261
 * @param dms_path A pointer to a C string containing the path to the database.
262
 *
263 264
 * @returns A pointer to an OBIDMS structure describing the OBIDMS.
 * @retval NULL if an error occurred.
265 266 267 268 269
 *
 * @see obi_close_dms()
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
270
OBIDMS_p obi_dms(const char* dms_path);
271

272

273
/**
274
 * @brief Closes an opened OBITools Data Management instance (OBIDMS).
275
 *
276
 * @param dms A pointer as returned by obi_create_dms() or obi_open_dms().
277
 * @param force Whether the DMS should be closed even if it is opened more than once.
278
 *
279 280 281 282
 * @returns An integer value indicating the success of the operation. Even on
 * 		    error, the `OBIDMS` structure is freed.
 * @retval 0 on success.
 * @retval -1 if an error occurred?-.
283 284 285 286 287 288
 *
 * @see obi_create_dms()
 * @see obi_open_dms()
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
289
int obi_close_dms(OBIDMS_p dms, bool force);
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
/**
 * @brief Internal function writing new comments in a DMS informations file.
 *
 * The new comments replace the pre-existing ones.
 * The informations file is enlarged if necessary.
 *
 * @param dms A pointer on the DMS.
 * @param comments The new comments that should be written.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since September 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int obi_dms_write_comments(OBIDMS_p dms, const char* comments);


/**
 * @brief Adds comments to a DMS informations file.
 *
 * This reads the comments in the JSON format and adds the key value pair.
 * If the key already exists, the value format is turned into an array and the new value is appended
 * if it is not already in the array.
 *
 * @param column A pointer on an OBIDMS column.
 * @param key The key.
 * @param value The value associated with the key.
 *
 * @retval 0 if the operation was successfully completed.
 * @retval -1 if an error occurred.
 *
 * @since September 2018
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int obi_dms_add_comment(OBIDMS_p dms, const char* key, const char* value);


330 331 332 333 334 335 336 337 338 339 340 341 342
/**
 * @brief Returns a column identified by its name and its version number from the list of opened columns.
 *
 * @param dms The OBIDMS.
 * @param column_name The column name that should be looked for.
 * @param version The version number of the column that should be looked for.
 *
 * @returns A pointer on the column if it was found in the list of opened columns.
 * @retval NULL if the column was not found in the list of opened columns.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
343 344 345
OBIDMS_column_p obi_dms_get_column_from_list(OBIDMS_p dms, const char* column_name, obiversion_t version);


346 347 348
/**
 * @brief Adds a column identified by its name and its version number in the list of opened columns.
 *
349 350
 * @warning obi_dms_get_column_from_list() should be used first to check if the column isn't already listed.
 *
351 352 353 354 355 356
 * @param dms The OBIDMS.
 * @param column A pointer on the column that should be added in the list of opened columns.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
357 358 359
void obi_dms_list_column(OBIDMS_p dms, OBIDMS_column_p column);


360 361 362 363 364 365 366 367 368
/**
 * @brief Removes a column identified by its name and its version number from the list of opened columns.
 *
 * @param dms The OBIDMS.
 * @param column A pointer on the column that should be removed from the list of opened columns.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
369 370 371
int obi_dms_unlist_column(OBIDMS_p dms, OBIDMS_column_p column);


372 373 374 375 376 377 378 379 380 381 382 383
/**
 * @brief Returns an indexer identified by its name from the list of opened indexers.
 *
 * @param dms The OBIDMS.
 * @param indexer_name The indexer name that should be looked for.
 *
 * @returns A pointer on the indexer if it was found in the list of opened indexers.
 * @retval NULL if the indexer was not found in the list of opened indexers.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
384 385 386
Obi_indexer_p obi_dms_get_indexer_from_list(OBIDMS_p dms, const char* indexer_name);


387 388 389
/**
 * @brief Adds an indexer identified by its name in the list of opened indexers.
 *
390 391
 * @warning obi_dms_get_indexer_from_list() should be used first to check if the indexer isn't already listed.
 *
392 393 394 395 396 397
 * @param dms The OBIDMS.
 * @param indexer A pointer on the indexer that should be added in the list of opened indexers.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
398 399 400
void obi_dms_list_indexer(OBIDMS_p dms, Obi_indexer_p indexer);


401 402 403 404 405 406 407 408 409
/**
 * @brief Removes an indexer identified by its name from the list of opened indexers.
 *
 * @param dms The OBIDMS.
 * @param column A pointer on the indexer that should be removed from the list of opened indexers.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
410 411 412
int obi_dms_unlist_indexer(OBIDMS_p dms, Obi_indexer_p indexer);


413
/**
414 415 416 417 418 419 420 421 422 423 424
 * @brief Gets the full path to the DMS.
 *
 * @warning The returned pointer has to be freed by the caller.
 *
 * @param dms The DMS.
 *
 * @returns A pointer to the full path.
 * @retval NULL if an error occurred.
 *
 * @since April 2016
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
425
 */
426
char* obi_dms_get_dms_path(OBIDMS_p dms);
427 428


429 430 431
/**
 * @brief Gets the full path of a file or a directory from its
 *        path relative to the DMS.
432 433 434
 *
 * @warning The returned pointer has to be freed by the caller.
 *
435 436
 * @param dms The DMS to which path_name is relative.
 * @param path_name The path name for the file or directory, relative to the DMS.
437 438 439 440
 *
 * @returns A pointer to the full path.
 * @retval NULL if an error occurred.
 *
441
 * @since April 2016
442 443
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
444
char* obi_dms_get_full_path(OBIDMS_p dms, const char* path_name);
445 446 447


/**
448
 * @brief Opens a directory relative to the DMS.
449
 *
450 451
 * @param dms The DMS to which path_name is relative.
 * @param path_name The path name for the directory to be opened, relative to the DMS.
452
 *
453
 * @returns The directory stream of the opened directory.
454 455
 * @retval NULL if an error occurred.
 *
456
 * @since April 2016
457 458 459 460 461
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
DIR* opendir_in_dms(OBIDMS_p dms, const char* path_name);


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
/**
 * @brief Imports a column, copying it from a DMS to another DMS, and returns the version of the column in the destination DMS.
 *
 * The eventual associated indexers are imported too. If an indexer with the same name already exists in the destination DMS,
 * they are not merged, the imported indexer is renamed.
 *
 * @warning The eventual associated column is not imported and needs to be imported separately.
 *
 * @param dms_path_1 The path to the source DMS (without the '.obidms' extension).
 * @param dms_path_2 The path to the destination DMS (without the '.obidms' extension). It is created if it doesn't already exist.
 * @param column_name The name of the column to import.
 * @param version_number The version of the column to import.
 *
 * @returns The new version of the column in the destination DMS.
 * @retval -1 if an error occurred.
 *
 * @since August 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
obiversion_t obi_import_column(const char* dms_path_1, const char* dms_path_2, const char* column_name, obiversion_t version_number);


/**
 * @brief Imports a view, copying it and all its associated columns from a DMS to another DMS.
 *
 * All the columns and the eventual associated indexers are imported. If an indexer with the same name already exists in the destination DMS,
 * they are not merged, the imported indexer is renamed.
 *
 * @param dms_path_1 The path to the source DMS (without the '.obidms' extension).
 * @param dms_path_2 The path to the destination DMS (without the '.obidms' extension). It is created if it doesn't already exist.
 * @param view_name_1 The name of the view to import.
 * @param view_name_2 The name of the imported view in the destination DMS.
 *
 * @retval 0 on success.
 * @retval -1 if an error occurred.
 *
 * @since August 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
int obi_import_view(const char* dms_path_1, const char* dms_path_2, const char* view_name_1, const char* view_name_2);


504 505 506 507 508 509 510
/**
 * @brief Closes all DMS in the global list of opened DMS.
 *        To be executed 'at exit' of programs.
 *
 * @since October 2017
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
511
void obi_close_atexit(void);
512 513


514
#endif /* OBIDMS_H_ */