obidms.h 16.8 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
	char					directory_path[MAX_PATH_LEN+1];   	/**< The absolute path of the directory
107 108
																 *   containing the DMS.
																 */
109 110 111 112 113 114
	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.
																 */
115
	DIR*					indexer_directory;					/**< A directory entry usable to
Celine Mercier committed
116
																 *   refer and scan the indexer directory.
117
																 */
118
	int						indexer_dir_fd;						/**< The file descriptor of the directory entry
Celine Mercier committed
119
																 *   usable to refer and scan the indexer directory.
120
																 */
121 122 123 124 125 126
	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.
																 */
127 128 129 130 131 132
	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.
																 */
133 134
	Opened_columns_list_p 	opened_columns;						/**< List of opened columns.
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
Celine Mercier committed
135
	Opened_indexers_list_p 	opened_indexers;					/**< List of opened indexers.
136
	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
137 138
	OBIDMS_infos_p 			infos;							    /**< A pointer on the mapped DMS information file.
 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 */
139 140 141
} OBIDMS_t, *OBIDMS_p;


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


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

164

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

187

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

202

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


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

238

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

257

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


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


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


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


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


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


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


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


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


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


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


470 471 472 473 474 475 476
/**
 * @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)
 */
477
void obi_close_atexit(void);
478 479


480
#endif /* OBIDMS_H_ */