2015-05-22 17:54:34 +02:00
/****************************************************************************
2015-09-30 12:03:46 +02:00
* OBIDMS columns header file *
2015-05-22 17:54:34 +02:00
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**
* @ file obidmscolumn . h
2015-09-30 12:03:46 +02:00
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
2015-05-22 17:54:34 +02:00
* @ date 12 May 2015
2015-09-30 12:03:46 +02:00
* @ brief Header file for the functions and structures shared by all the OBIDMS columns .
2015-05-22 17:54:34 +02:00
*/
2015-09-30 12:03:46 +02:00
2015-05-22 17:54:34 +02:00
# ifndef OBIDMSCOLUMN_H_
# define OBIDMSCOLUMN_H_
# include <stdio.h>
# include <sys/types.h>
# include <unistd.h>
# include <stdbool.h>
2015-05-26 10:38:56 +02:00
# include <time.h>
2015-08-26 10:29:07 +02:00
# include <stdbool.h>
2015-05-22 17:54:34 +02:00
2015-06-17 16:51:16 +02:00
# include "obidms.h"
# include "obitypes.h"
# include "obierrno.h"
# include "obilittlebigman.h"
2015-06-26 17:53:03 +02:00
# include "obidmscolumndir.h"
2016-04-12 14:53:33 +02:00
# include "obiblob_indexer.h"
2015-05-22 17:54:34 +02:00
2015-09-30 12:03:46 +02:00
2017-01-31 16:48:06 +01:00
# define ELEMENTS_NAMES_MAX (2048) / **< The maximum length of the list of elements names. // TODO Discuss
*/
# define NB_ELTS_MAX_IF_DEFAULT_NAME (539) / **< The maximum number of elements per line if the default element names
* are used ( " 0 \01 \02 \0 ... \0 n " ) , considering ELEMENTS_NAMES_MAX .
*/
# define COLUMN_GROWTH_FACTOR (2) / **< The growth factor when a column is enlarged.
*/
2017-07-17 16:45:08 +02:00
# define MAXIMUM_LINE_COUNT (1000000000) / **< The maximum line count for the data of a column (1E9). //TODO
2017-01-31 16:48:06 +01:00
*/
# define COMMENTS_MAX_LENGTH (2048) / **< The maximum length for comments.
*/
2017-07-17 16:45:08 +02:00
# define FORMATTED_ELT_NAMES_SEPARATOR '\0' / **< The separator between elements names once formatted to be stored in columns.
2017-01-31 16:48:06 +01:00
*/
2017-07-17 16:45:08 +02:00
# define NOT_FORMATTED_ELT_NAMES_SEPARATOR ';' / **< The separator between elements names before being formatted to be stored in columns (e.g. as sent by the upper layer).
2017-01-31 16:48:06 +01:00
*/
2015-11-03 14:22:00 +01:00
2015-09-30 12:03:46 +02:00
2016-07-15 15:38:49 +02:00
/**
* @ brief Structure referencing a column by its name and its version .
*/
typedef struct Column_reference {
char column_name [ OBIDMS_COLUMN_MAX_NAME + 1 ] ; /**< Name of the column.
*/
obiversion_t version ; /**< Version of the column.
*/
} Column_reference_t , * Column_reference_p ;
2015-05-22 17:54:34 +02:00
/**
2015-09-30 12:03:46 +02:00
* @ brief OBIDMS column header structure .
2015-05-22 17:54:34 +02:00
*/
2015-06-10 15:19:02 +02:00
typedef struct OBIDMS_column_header {
2017-01-31 16:48:06 +01:00
size_t header_size ; /**< Size of the header in bytes.
*/
size_t data_size ; /**< Size of the data in bytes.
*/
index_t line_count ; /**< Number of lines of data allocated.
*/
2017-04-14 16:23:17 +02:00
index_t lines_used ; /**< Number of lines of data used (the highest index where data has been entered + 1).
2017-01-31 16:48:06 +01:00
*/
index_t nb_elements_per_line ; /**< Number of elements per line.
*/
char elements_names [ ELEMENTS_NAMES_MAX + 1 ] ; /**< Names of the line elements with '\0' as separator
* and ' \ 0 \ 0 ' as terminal flag .
* ( default are the indices : " 0 \01 \02 \0 ... \0 n \0 \0 " ) .
*/
int elements_names_length ; /**< Length of the character array where the elements names are stored.
*/
int elements_names_idx [ NB_ELTS_MAX_IF_DEFAULT_NAME ] ; /**< Index for the start of each element name in elements_names.
*/
int sorted_elements_idx [ NB_ELTS_MAX_IF_DEFAULT_NAME ] ; /**< Index for the sorted element names in elements_names_idx.
*/
OBIType_t returned_data_type ; /**< Type of the data that is returned when getting an
* element from the column .
*/
OBIType_t stored_data_type ; /**< Type of the data that is actually stored in the data
* part of the column .
*/
time_t creation_date ; /**< Date of creation of the file.
*/
obiversion_t version ; /**< Version of the column.
*/
obiversion_t cloned_from ; /**< Version of the column from which this column
* was cloned from ( - 1 if it was not created by cloning
* another column ) .
*/
char name [ OBIDMS_COLUMN_MAX_NAME + 1 ] ; /**< The column name as a NULL terminated string.
*/
char indexer_name [ INDEXER_MAX_NAME + 1 ] ; /**< If there is one, the indexer name as a NULL terminated string.
*/
Column_reference_t associated_column ; /**< If there is one, the reference to the associated column.
*/
char comments [ COMMENTS_MAX_LENGTH + 1 ] ; /**< Comments stored as a classical zero end C string.
*/
2015-06-10 15:19:02 +02:00
} OBIDMS_column_header_t , * OBIDMS_column_header_p ;
2015-05-22 17:54:34 +02:00
2015-06-23 18:35:34 +02:00
2015-05-26 10:38:56 +02:00
/**
2015-09-30 12:03:46 +02:00
* @ brief OBIDMS column structure .
2015-05-26 10:38:56 +02:00
*
* A data structure of this type is returned by the functions
2015-09-30 12:03:46 +02:00
* creating , opening or cloning an OBIDMS column .
2015-05-26 10:38:56 +02:00
*/
2015-06-10 15:19:02 +02:00
typedef struct OBIDMS_column {
2016-12-26 13:35:31 +01:00
OBIDMS_p dms ; /**< A pointer to the OBIDMS structure to
* which the column belongs .
2015-09-30 12:03:46 +02:00
*/
2016-12-26 13:35:31 +01:00
OBIDMS_column_directory_p column_directory ; /**< A pointer to the OBIDMS column directory
* structure to which the column belongs .
2015-09-30 12:03:46 +02:00
*/
OBIDMS_column_header_p header ; /**< A pointer to the header of the column.
*/
2016-12-26 13:35:31 +01:00
Obi_indexer_p indexer ; /**< A pointer to the blob indexer associated
* with the column if there is one .
2015-11-03 14:22:00 +01:00
*/
2015-09-30 12:03:46 +02:00
void * data ; /**< A `void` pointer to the beginning of the data.
2015-07-20 16:08:50 +02:00
*
2016-12-26 13:35:31 +01:00
* @ warning Never use this member directly outside
* of the code of the low level functions
* of the OBIDMS .
2015-07-20 16:08:50 +02:00
*/
2016-02-18 10:38:51 +01:00
bool writable ; /**< Indicates if the column is writable or not.
2015-07-20 16:08:50 +02:00
* - ` true ` the column is writable
* - ` false ` the column is read - only
*
* A column is writable only by its creator
* until it closes it .
*/
2016-12-26 13:35:31 +01:00
size_t counter ; /**< Indicates by how many threads/programs
* ( TODO ) the column is used .
2015-12-02 17:32:07 +01:00
*/
2015-06-10 15:19:02 +02:00
} OBIDMS_column_t , * OBIDMS_column_p ;
2015-05-26 10:38:56 +02:00
2015-07-31 18:03:48 +02:00
/**
2015-09-30 12:03:46 +02:00
* @ brief Returns the latest version number of a column in a column directory using the column directory structure .
*
* @ param column_directory A pointer as returned by obi_create_column_directory ( ) or obi_open_column_directory ( ) .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ returns The latest version number kept in the version file .
* @ retval - 1 if an error occurred .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ since May 2015
* @ author Eric Coissac ( eric . coissac @ metabarcoding . org )
2015-07-31 18:03:48 +02:00
*/
obiversion_t obi_get_latest_version_number ( OBIDMS_column_directory_p column_directory ) ;
/**
2015-09-30 12:03:46 +02:00
* @ brief Returns the latest version of a column in a column directory using the column name .
*
* @ param dms A pointer on an OBIDMS .
* @ param column_name The column name .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ returns The latest version number kept in the version file .
* @ retval - 1 if an error occurred .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ since May 2015
* @ author Eric Coissac ( eric . coissac @ metabarcoding . org )
2015-07-31 18:03:48 +02:00
*/
obiversion_t obi_column_get_latest_version_from_name ( OBIDMS_p dms , const char * column_name ) ;
2015-05-26 10:38:56 +02:00
/**
2015-06-10 15:19:02 +02:00
* @ brief Returns the header size in bytes of a column on this platform .
2015-05-26 10:38:56 +02:00
*
2015-06-17 16:51:16 +02:00
* The header size is defined as a multiple of the memory page size .
2015-09-30 12:03:46 +02:00
* As of now the header size is defined as one time the page size .
2015-05-26 10:38:56 +02:00
*
2015-11-06 17:55:15 +01:00
* @ returns The header size in bytes .
2015-05-26 10:38:56 +02:00
*
* @ since May 2015
* @ author Eric Coissac ( eric . coissac @ metabarcoding . org )
*/
size_t obi_get_platform_header_size ( ) ;
/**
2015-06-10 15:19:02 +02:00
* @ brief Creates a column .
2015-05-26 10:38:56 +02:00
*
2015-09-30 12:03:46 +02:00
* The minimum data size allocated is one memory page , and the data is initialized to the NA value of the OBIType .
2016-04-12 14:53:33 +02:00
* If there is an indexer associated with the column , it is opened or created if it does not already exist .
2015-09-30 12:03:46 +02:00
*
* @ warning If there is one element per line , elements_names should be equal to column_name . // TODO change this condition?
*
* @ param dms A pointer on an OBIDMS .
* @ param column_name The name of the new column .
* @ param data_type The OBIType code of the data .
2017-07-18 17:34:32 +02:00
* @ param nb_lines The number of lines to be stored ( can be 0 if not known ) .
* @ param nb_elements_per_line The number of elements per line .
2016-12-02 17:54:51 +01:00
* @ param elements_names The names of the elements with ' ; ' as separator ( no terminal ' ; ' ) ,
2017-01-31 16:48:06 +01:00
* NULL or " " if the default names are to be used ( " 0 \01 \02 \0 ... \0 n " ) .
2017-07-18 17:34:32 +02:00
* @ param elt_names_formatted Whether the separator for the elements names is ' ; ' ( false ) , or ' \0 ' ( true , as formatted by format_elements_names ( ) ) .
2016-04-12 14:53:33 +02:00
* @ param indexer_name The name of the indexer if there is one associated with the column .
2016-11-18 15:56:51 +01:00
* If NULL or " " , the indexer name is set as the column name .
2017-07-18 17:34:32 +02:00
* @ param associated_column_name The name of the associated column if there is one ( otherwise NULL or " " ) .
* @ param associated_column_version The version of the associated column if there is one ( otherwise - 1 ) .
* @ param comments Optional comments associated with the column ( NULL or " " if no comments associated ) .
2015-09-30 12:03:46 +02:00
*
* @ returns A pointer on the newly created column structure .
* @ retval NULL if an error occurred .
2015-05-26 10:38:56 +02:00
*
* @ since May 2015
2016-07-15 15:38:49 +02:00
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
2015-05-26 10:38:56 +02:00
*/
2016-12-05 11:18:29 +01:00
OBIDMS_column_p obi_create_column ( OBIDMS_p dms ,
const char * column_name ,
OBIType_t data_type ,
index_t nb_lines ,
index_t nb_elements_per_line ,
char * elements_names ,
2017-07-18 17:34:32 +02:00
bool elt_names_formatted ,
2016-12-05 11:18:29 +01:00
const char * indexer_name ,
2016-07-15 15:38:49 +02:00
const char * associated_column_name ,
obiversion_t associated_column_version ,
2017-07-18 17:34:32 +02:00
const char * comments
2016-02-18 10:38:51 +01:00
) ;
2015-05-26 10:38:56 +02:00
/**
2015-07-31 18:03:48 +02:00
* @ brief Opens a column in read - only mode .
2015-05-26 10:38:56 +02:00
*
2015-09-30 12:03:46 +02:00
* @ param dms A pointer on an OBIDMS .
* @ param column_name The name of the column .
* @ param version_number The version of the column that should be opened ( if - 1 , the latest version is retrieved ) .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ returns A pointer on the opened column structure .
* @ retval NULL if an error occurred .
2015-05-26 10:38:56 +02:00
*
2015-07-31 18:03:48 +02:00
* @ since July 2015
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
2015-05-26 10:38:56 +02:00
*/
2015-07-31 18:03:48 +02:00
OBIDMS_column_p obi_open_column ( OBIDMS_p dms , const char * column_name , obiversion_t version_number ) ;
2015-08-26 10:29:07 +02:00
/**
* @ brief Clones a column , and returns a pointer to the writable new column .
*
2015-09-30 12:03:46 +02:00
* @ param dms A pointer on an OBIDMS .
* @ param column_name The name of the column to clone .
* @ param version_number The version of the column that should be cloned ( if - 1 , the latest version is retrieved ) .
* @ param clone_data Whether the data should be copied or not .
2015-08-26 10:29:07 +02:00
*
2015-09-30 12:03:46 +02:00
* @ returns A pointer to the created column .
* @ retval NULL if an error occurred .
2015-08-26 10:29:07 +02:00
*
* @ since August 2015
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
2016-02-18 10:38:51 +01:00
OBIDMS_column_p obi_clone_column ( OBIDMS_p dms , OBIDMS_column_p line_selection , const char * column_name , obiversion_t version_number , bool clone_data ) ;
2015-08-26 10:29:07 +02:00
2016-11-09 16:48:00 +01:00
/**
* @ brief Clones a column indexer to have it writable .
*
* @ param column A pointer on an OBIDMS column .
*
* @ retval 0 if the operation was successfully completed .
* @ retval - 1 if an error occurred .
*
* @ since November 2016
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
int obi_clone_column_indexer ( OBIDMS_column_p column ) ;
2015-07-31 18:03:48 +02:00
/**
2016-04-14 15:13:30 +02:00
* @ brief Truncates a column to the number of lines used if it is not read - only and closes it .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ param column A pointer on an OBIDMS column .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ retval 0 if the operation was successfully completed .
* @ retval - 1 if an error occurred .
2015-07-31 18:03:48 +02:00
*
* @ since July 2015
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
int obi_close_column ( OBIDMS_column_p column ) ;
2015-08-26 17:01:54 +02:00
/**
2015-09-30 12:03:46 +02:00
* @ brief Truncates a column file to the number of lines used rounded to the nearest
* greater multiple of the page size .
2015-08-26 17:01:54 +02:00
*
2015-09-30 12:03:46 +02:00
* @ param column A pointer on an OBIDMS column .
2015-08-26 17:01:54 +02:00
*
2015-09-30 12:03:46 +02:00
* @ retval 0 if the operation was successfully completed .
* @ retval - 1 if an error occurred .
2015-08-26 17:01:54 +02:00
*
* @ since August 2015
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
2016-04-14 15:13:30 +02:00
int obi_truncate_column ( OBIDMS_column_p column ) ;
2015-08-26 17:01:54 +02:00
2015-09-01 17:38:08 +02:00
/**
* @ brief Enlarges a column file .
*
2015-09-30 12:03:46 +02:00
* @ param column A pointer on an OBIDMS column .
2015-09-01 17:38:08 +02:00
*
2015-09-30 12:03:46 +02:00
* @ retval 0 if the operation was successfully completed .
* @ retval - 1 if an error occurred .
2015-09-01 17:38:08 +02:00
*
* @ since August 2015
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
int obi_enlarge_column ( OBIDMS_column_p column ) ;
/*
2015-09-30 12:03:46 +02:00
* @ brief Sets the data in a column to the NA value of the data OBIType .
2015-09-01 17:38:08 +02:00
*
2015-09-30 12:03:46 +02:00
* @ param column A pointer on an OBIDMS column .
* @ param start The first line number of the block that should be set .
* @ param nb_lines The number of lines that should be set .
2015-09-01 17:38:08 +02:00
*
* @ since August 2015
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
2015-11-10 13:30:10 +01:00
void obi_ini_to_NA_values ( OBIDMS_column_p column , index_t first_line_nb , index_t nb_lines ) ; // TODO make private?
2015-09-01 17:38:08 +02:00
2015-07-31 18:03:48 +02:00
/**
2015-10-14 18:05:34 +02:00
* @ brief Recovers the header of an OBIDMS column from the column name .
*
* @ warning The header structure has to be munmapped by the caller .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ param dms A pointer on an OBIDMS .
* @ param column_name The name of an OBIDMS column .
2015-11-10 13:30:10 +01:00
* @ param version_number The version of the column from which the header should be
* retrieved ( - 1 : latest version ) .
2015-07-31 18:03:48 +02:00
*
2015-10-14 18:05:34 +02:00
* @ returns A pointer on the mmapped header of the column .
* @ retval NULL if an error occurred .
2015-07-31 18:03:48 +02:00
*
2015-10-14 18:05:34 +02:00
* @ since October 2015
2015-07-31 18:03:48 +02:00
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
2015-11-10 13:30:10 +01:00
OBIDMS_column_header_p obi_column_get_header_from_name ( OBIDMS_p dms , const char * column_name , obiversion_t version_number ) ;
2015-07-31 18:03:48 +02:00
2015-09-15 17:09:31 +02:00
/**
2015-10-14 18:05:34 +02:00
* @ brief Munmap a mmapped header as returned by obi_column_get_header_from_name ( ) .
2015-09-15 17:09:31 +02:00
*
2015-10-14 18:05:34 +02:00
* @ param header A pointer on the mmapped header structure .
2015-09-15 17:09:31 +02:00
*
2015-10-14 18:05:34 +02:00
* @ retval 0 if the operation was successfully completed .
* @ retval - 1 if an error occurred .
2015-09-15 17:09:31 +02:00
*
2015-10-14 18:05:34 +02:00
* @ since October 2015
2015-09-15 17:09:31 +02:00
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
2016-04-14 15:19:27 +02:00
int obi_close_header ( OBIDMS_column_header_p header ) ;
2015-09-15 17:09:31 +02:00
2015-07-31 18:03:48 +02:00
/**
2015-09-30 12:03:46 +02:00
* @ brief Recovers the index of an element in an OBIDMS column from the element ' s name .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ param column A pointer on an OBIDMS column .
* @ param element_name The name of the element .
2015-07-31 18:03:48 +02:00
*
2015-09-30 12:03:46 +02:00
* @ returns The index of the element in a line of the column .
2017-01-31 16:48:06 +01:00
* @ retval OBIIdx_NA if an error occurred .
2015-07-31 18:03:48 +02:00
*
* @ since July 2015
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
2015-11-06 17:55:15 +01:00
index_t obi_column_get_element_index_from_name ( OBIDMS_column_p column , const char * element_name ) ;
2015-07-31 18:03:48 +02:00
2017-01-31 16:48:06 +01:00
/**
* @ brief Recovers the elements names of the lines of a column , with ' ; ' as separator ( i . e . " 0;1;2;...;n \0 " ) .
*
* @ warning The returned pointer has to be freed by the caller .
*
* @ param column A pointer on an OBIDMS column .
*
* @ returns A pointer on a character array where the elements names are stored .
* @ retval NULL if an error occurred .
*
* @ since January 2017
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
char * obi_get_elements_names ( OBIDMS_column_p column ) ;
2016-04-22 17:55:53 +02:00
/**
* @ brief Prepares a column to set a value .
*
* @ param column A pointer on an OBIDMS column .
* @ param line_nb The number of the line at which the value will be set .
*
* @ retval 0 if the operation was successfully completed .
* @ retval - 1 if an error occurred .
*
* @ since April 2016
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
2017-07-05 14:49:20 +02:00
int obi_column_prepare_to_set_value ( OBIDMS_column_p column , index_t line_nb , index_t elt_idx ) ;
2016-04-13 15:10:24 +02:00
2016-04-22 17:55:53 +02:00
/**
* @ brief Prepares a column to recover a value .
2015-11-09 11:22:51 +01:00
*
2016-04-22 17:55:53 +02:00
* @ param column A pointer on an OBIDMS column .
* @ param line_nb The number of the line at which the value will be recovered .
2015-10-02 13:47:53 +02:00
*
2016-04-22 17:55:53 +02:00
* @ retval 0 if the operation was successfully completed .
* @ retval - 1 if an error occurred .
2015-10-02 13:47:53 +02:00
*
2016-04-22 17:55:53 +02:00
* @ since April 2016
2015-10-02 13:47:53 +02:00
* @ author Celine Mercier ( celine . mercier @ metabarcoding . org )
*/
2016-04-22 17:55:53 +02:00
int obi_column_prepare_to_get_value ( OBIDMS_column_p column , index_t line_nb ) ;
2015-10-02 13:47:53 +02:00
2015-05-22 17:54:34 +02:00
# endif /* OBIDMSCOLUMN_H_ */