Makes few cleanning and adds an obi_dms C constructor
This commit is contained in:
49
src/obidms.c
49
src/obidms.c
@ -7,6 +7,14 @@
|
|||||||
|
|
||||||
#include <obidms.h>
|
#include <obidms.h>
|
||||||
|
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
***********************************************************************/
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Internal function building the directory name from a OBIDMS name.
|
* Internal function building the directory name from a OBIDMS name.
|
||||||
*
|
*
|
||||||
@ -50,6 +58,33 @@ static char *build_directory_name(const char *name) {
|
|||||||
return dirdbname;
|
return dirdbname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***********************************************************************
|
||||||
|
*
|
||||||
|
* 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
|
||||||
|
*
|
||||||
|
***********************************************************************/
|
||||||
|
|
||||||
|
int obi_dms_exists(const char* name) {
|
||||||
|
struct stat buffer;
|
||||||
|
char *dirdbname;
|
||||||
|
int exist;
|
||||||
|
|
||||||
|
// Build and check the directory name
|
||||||
|
dirdbname = build_directory_name(name);
|
||||||
|
if (dirdbname==NULL)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
exist = stat(dirdbname,&buffer);
|
||||||
|
|
||||||
|
free(dirdbname);
|
||||||
|
|
||||||
|
if(exist == 0)
|
||||||
|
return 1;
|
||||||
|
else // -1
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
OBIDMS_p obi_create_dms(const char *name) {
|
OBIDMS_p obi_create_dms(const char *name) {
|
||||||
char *dirdbname;
|
char *dirdbname;
|
||||||
|
|
||||||
@ -127,6 +162,20 @@ OBIDMS_p obi_open_dms(const char *name) {
|
|||||||
return dms;
|
return dms;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
OBIDMS_p obi_dms(const char *name) {
|
||||||
|
int exist = obi_dms_exists(name);
|
||||||
|
|
||||||
|
switch (exist) {
|
||||||
|
case 0:
|
||||||
|
return obi_create_dms(name);
|
||||||
|
case 1:
|
||||||
|
return obi_open_dms(name);
|
||||||
|
};
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int obi_close_dms(OBIDMS_p dms) {
|
int obi_close_dms(OBIDMS_p dms) {
|
||||||
|
|
||||||
|
40
src/obidms.h
40
src/obidms.h
@ -61,6 +61,23 @@ typedef struct OBIDMS {
|
|||||||
*/
|
*/
|
||||||
/**@}*/
|
/**@}*/
|
||||||
|
|
||||||
|
/*@
|
||||||
|
* @brief Check if a OBIDMS exist
|
||||||
|
*
|
||||||
|
* @param name a pointer to a C string containing the name of the database
|
||||||
|
* the actual directory name used to store the DMS will be
|
||||||
|
* `<name>.obidms`
|
||||||
|
*
|
||||||
|
* @return an integer value indicating the status of the database
|
||||||
|
* @retvalue 1 the database exist
|
||||||
|
* @retvalue 0 the database does not exist
|
||||||
|
* @retvalue -1 an error occurs
|
||||||
|
*
|
||||||
|
* @see obi_close_dms()
|
||||||
|
* @since May 2015
|
||||||
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
||||||
|
*/
|
||||||
|
int obi_dms_exists(const char* name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Create a new OBITools Data Management instance (OBIDMS).
|
* @brief Create a new OBITools Data Management instance (OBIDMS).
|
||||||
@ -108,6 +125,29 @@ OBIDMS_p obi_create_dms(const char *name);
|
|||||||
*/
|
*/
|
||||||
OBIDMS_p obi_open_dms(const char *name);
|
OBIDMS_p obi_open_dms(const char *name);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Create a new OBIDMS instance.
|
||||||
|
*
|
||||||
|
* If the database already exist this function open it, otherwise it
|
||||||
|
* creates a new databse.
|
||||||
|
*
|
||||||
|
* @param name a pointer to a C string containing the name of the database
|
||||||
|
* the actual directory name used to store the DMS is
|
||||||
|
* `<name>.obidms`
|
||||||
|
*
|
||||||
|
* @return a pointer to an OBIDMS structure describing the newly created DMS
|
||||||
|
* @retval NULL on error and the `obi_errno`variable is set.
|
||||||
|
*
|
||||||
|
* ###Error values
|
||||||
|
* - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit.
|
||||||
|
* - OBIDMS_MEMORY_ERROR : something wrong occurs during memory allocation.
|
||||||
|
*
|
||||||
|
* @see obi_close_dms()
|
||||||
|
* @since May 2015
|
||||||
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
||||||
|
*/
|
||||||
|
OBIDMS_p obi_dms(const char *name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Close an opened OBITools Data Management instance (OBIDMS).
|
* @brief Close an opened OBITools Data Management instance (OBIDMS).
|
||||||
*
|
*
|
||||||
|
@ -579,36 +579,3 @@ OBIDMSColumn_p obi_create_column(OBIDMS_p dms,
|
|||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
||||||
/************************************************************************
|
|
||||||
************************************************************************
|
|
||||||
************************************************************************/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Reads a memory block from a file.
|
|
||||||
*
|
|
||||||
* * @param file The file that should be mapped.
|
|
||||||
* * @param size The number of bytes that should be mapped.
|
|
||||||
* * @param start The position at which the mapping should start.
|
|
||||||
* @return A pointer on the first element of the block.
|
|
||||||
*/
|
|
||||||
char* obi_map_read_only(int file, int start, int size)
|
|
||||||
{
|
|
||||||
char* map;
|
|
||||||
map = (char*) mmap(0, size, PROT_READ, MAP_PRIVATE, file, start);
|
|
||||||
if (map == MAP_FAILED)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "\nmmap read-only error\n");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
return(map);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Unmaps a memory block.
|
|
||||||
*
|
|
||||||
* * @param size The size of the block that should be unmapped.
|
|
||||||
*/
|
|
||||||
void obi_unmap(int size)
|
|
||||||
{
|
|
||||||
munmap(0, size);
|
|
||||||
}
|
|
||||||
|
@ -23,22 +23,6 @@
|
|||||||
#include <obierrno.h>
|
#include <obierrno.h>
|
||||||
#include <obilittlebigman.h>
|
#include <obilittlebigman.h>
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Value separator in OBIDMSColumn files.
|
|
||||||
*/
|
|
||||||
static const char SEPARATOR = '\n';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Header size in OBIDMSColumn files.
|
|
||||||
*/
|
|
||||||
static const int HEADER_SIZE = 4096;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Number of bytes to map
|
|
||||||
*/
|
|
||||||
static const int BUFFER_SIZE = 4;
|
|
||||||
|
|
||||||
#define OBIDMS_MAX_COLNAME (128) /**< The maximum length of an OBIDMS column name
|
#define OBIDMS_MAX_COLNAME (128) /**< The maximum length of an OBIDMS column name
|
||||||
*/
|
*/
|
||||||
@ -63,7 +47,7 @@ typedef struct OBIDMSColumnHeader {
|
|||||||
OBIType_t data_type; /**< type of the data */
|
OBIType_t data_type; /**< type of the data */
|
||||||
time_t creation_date; /**< date of creation of the file */
|
time_t creation_date; /**< date of creation of the file */
|
||||||
obiversion_t version; /**< version of the OBIColumn */
|
obiversion_t version; /**< version of the OBIColumn */
|
||||||
char name[OBIDMS_MAX_COLNAME]; /**< The column name as a NULL
|
char name[OBIDMS_MAX_COLNAME+1]; /**< The column name as a NULL
|
||||||
* terminated string.
|
* terminated string.
|
||||||
*/
|
*/
|
||||||
char comments[1]; /**< comments stored as a classical
|
char comments[1]; /**< comments stored as a classical
|
||||||
@ -145,9 +129,6 @@ OBIDMSColumn_p obi_create_column(OBIDMS_p dms,
|
|||||||
*/
|
*/
|
||||||
obiversion_t obi_latest_version(OBIDMS_p dms,char *name);
|
obiversion_t obi_latest_version(OBIDMS_p dms,char *name);
|
||||||
|
|
||||||
char* obi_map_read_only(int file, int start, int size);
|
|
||||||
|
|
||||||
void obi_unmap(int size);
|
|
||||||
|
|
||||||
|
|
||||||
#endif /* OBIDMSCOLUMN_H_ */
|
#endif /* OBIDMSCOLUMN_H_ */
|
||||||
|
Reference in New Issue
Block a user