obidms.h 17 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 91 92 93 94 95 96
/**
 * @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.
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
	char 			    	comments[];							/**< Comments, additional informations on the DMS including
																 *   the command line history.
 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
} OBIDMS_infos_t, *OBIDMS_infos_p;


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


145 146 147 148
/**
 * @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];
149
extern int global_opened_dms_counter_list[MAX_NB_OPENED_DMS+1];
150 151


152 153
/**
 * @brief Checks if an OBIDMS exists.
154
 *
155
 * @param dms_path A pointer to a C string containing the path to the database.
156
 *
157
 * @returns An integer value indicating the status of the database.
158 159 160
 * @retval 1 if the database exists.
 * @retval 0 if the database does not exist.
 * @retval -1 if an error occurred.
161 162 163 164
 *
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
165
int obi_dms_exists(const char* dms_path);
166

167

168
/**
169
 * @brief Creates a new OBITools Data Management instance (OBIDMS).
170
 *
171 172 173
 * 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.
174
 *
Celine Mercier committed
175
 * A directory to store Obiblob indexers is also created.
176
 *
177
 * @param dms_path A pointer to a C string containing the path to the database.
178 179
 *                 The actual directory name used to store the DMS will be
 *                 `<dms_name>.obidms`.
180
 *
181 182
 * @returns A pointer to an OBIDMS structure describing the newly created DMS.
 * @retval NULL if an error occurred.
183 184 185 186 187
 *
 * @see obi_close_dms()
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
188
OBIDMS_p obi_create_dms(const char* dms_name);
189

190

191
/**
192
 * @brief Opens an existing OBITools Data Management instance (OBIDMS).
193
 *
194
 * @param dms_path A pointer to a C string containing the path to the database.
195
 *
196 197
 * @returns A pointer to an OBIDMS structure describing the opened DMS.
 * @retval NULL if an error occurred.
198 199 200 201 202
 *
 * @see obi_close_dms()
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
203
OBIDMS_p obi_open_dms(const char* dms_path);
204

205

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
/**
 * @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);


224
/**
225
 * @brief Creates or opens a new OBIDMS instance.
226
 *
227 228
 * If the database already exists, this function opens it, otherwise it
 * creates a new database.
229
 *
230
 * @param dms_path A pointer to a C string containing the path to the database.
231
 *
232 233
 * @returns A pointer to an OBIDMS structure describing the OBIDMS.
 * @retval NULL if an error occurred.
234 235 236 237 238
 *
 * @see obi_close_dms()
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
239
OBIDMS_p obi_dms(const char* dms_path);
240

241

242
/**
243
 * @brief Closes an opened OBITools Data Management instance (OBIDMS).
244
 *
245
 * @param dms A pointer as returned by obi_create_dms() or obi_open_dms().
246
 * @param force Whether the DMS should be closed even if it is opened more than once.
247
 *
248 249 250 251
 * @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?-.
252 253 254 255 256 257
 *
 * @see obi_create_dms()
 * @see obi_open_dms()
 * @since May 2015
 * @author Eric Coissac (eric.coissac@metabarcoding.org)
 */
258
int obi_close_dms(OBIDMS_p dms, bool force);
259

260

261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
/**
 * @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);


299 300 301 302 303 304 305 306 307 308 309 310 311
/**
 * @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)
 */
312 313 314
OBIDMS_column_p obi_dms_get_column_from_list(OBIDMS_p dms, const char* column_name, obiversion_t version);


315 316 317
/**
 * @brief Adds a column identified by its name and its version number in the list of opened columns.
 *
318 319
 * @warning obi_dms_get_column_from_list() should be used first to check if the column isn't already listed.
 *
320 321 322 323 324 325
 * @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)
 */
326 327 328
void obi_dms_list_column(OBIDMS_p dms, OBIDMS_column_p column);


329 330 331 332 333 334 335 336 337
/**
 * @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)
 */
338 339 340
int obi_dms_unlist_column(OBIDMS_p dms, OBIDMS_column_p column);


341 342 343 344 345 346 347 348 349 350 351 352
/**
 * @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)
 */
353 354 355
Obi_indexer_p obi_dms_get_indexer_from_list(OBIDMS_p dms, const char* indexer_name);


356 357 358
/**
 * @brief Adds an indexer identified by its name in the list of opened indexers.
 *
359 360
 * @warning obi_dms_get_indexer_from_list() should be used first to check if the indexer isn't already listed.
 *
361 362 363 364 365 366
 * @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)
 */
367 368 369
void obi_dms_list_indexer(OBIDMS_p dms, Obi_indexer_p indexer);


370 371 372 373 374 375 376 377 378
/**
 * @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)
 */
379 380 381
int obi_dms_unlist_indexer(OBIDMS_p dms, Obi_indexer_p indexer);


382
/**
383 384 385 386 387 388 389 390 391 392 393
 * @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)
394
 */
395
char* obi_dms_get_dms_path(OBIDMS_p dms);
396 397


398 399 400
/**
 * @brief Gets the full path of a file or a directory from its
 *        path relative to the DMS.
401 402 403
 *
 * @warning The returned pointer has to be freed by the caller.
 *
404 405
 * @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.
406 407 408 409
 *
 * @returns A pointer to the full path.
 * @retval NULL if an error occurred.
 *
410
 * @since April 2016
411 412
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
413
char* obi_dms_get_full_path(OBIDMS_p dms, const char* path_name);
414 415 416


/**
417
 * @brief Opens a directory relative to the DMS.
418
 *
419 420
 * @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.
421
 *
422
 * @returns The directory stream of the opened directory.
423 424
 * @retval NULL if an error occurred.
 *
425
 * @since April 2016
426 427 428 429 430
 * @author Celine Mercier (celine.mercier@metabarcoding.org)
 */
DIR* opendir_in_dms(OBIDMS_p dms, const char* path_name);


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
/**
 * @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);


473 474 475 476 477 478 479
/**
 * @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)
 */
480
void obi_close_atexit(void);
481 482


483
#endif /* OBIDMS_H_ */