Added the possibility to clone a column, with or without its data

This commit is contained in:
Celine Mercier
2015-08-26 10:29:07 +02:00
parent e6d96d999a
commit 9ad31fddff
4 changed files with 110 additions and 35 deletions

View File

@ -723,6 +723,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
header->data_type = type;
header->creation_date = time(NULL);
header->version = version_number;
header->cloned_from = -1;
header->comments[0] = 0x0;
obi_column_set_elements_names(new_column, elements_names);
@ -813,7 +814,7 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, const char* column_name, obiversio
column->header = mmap(NULL,
header_size,
PROT_READ,
MAP_SHARED,
MAP_PRIVATE,
column_file_descriptor,
0
);
@ -836,7 +837,7 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, const char* column_name, obiversio
column->data = mmap(NULL,
data_size,
PROT_READ,
MAP_SHARED,
MAP_PRIVATE,
column_file_descriptor,
header_size
);
@ -861,9 +862,59 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, const char* column_name, obiversio
}
OBIDMS_column_p obi_clone_column(OBIDMS_p dms, const char* column_name, obiversion_t version_number, bool clone_data)
{
OBIDMS_column_p column_to_clone;
OBIDMS_column_p new_column;
size_t nb_lines;
size_t nb_elements_per_line;
OBIType_t data_type;
column_to_clone = obi_open_column(dms, column_name, version_number);
if (column_to_clone == NULL)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError opening the column to clone");
return NULL;
}
if (clone_data)
nb_lines = (column_to_clone->header)->line_count;
else
nb_lines = INITIAL_LINE_COUNT;
nb_elements_per_line = (column_to_clone->header)->nb_elements_per_line;
data_type = (column_to_clone->header)->data_type;
new_column = obi_create_column(dms,
column_name,
data_type,
nb_lines,
nb_elements_per_line,
(column_to_clone->header)->elements_names);
if (new_column == NULL)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError creating the new column when cloning a column");
// TODO the new file should be deleted
return NULL;
}
(new_column->header)->cloned_from = version_number;
// TODO copy header->comments?
if (clone_data)
memcpy(new_column->data, column_to_clone->data, nb_lines*nb_elements_per_line*sizeof(data_type));
return new_column;
}
int obi_close_column(OBIDMS_column_p column)
{
//munmap? TODO
//truncate to lines used TODO
free(column);
return 0;
}

View File

@ -17,6 +17,7 @@
#include <unistd.h>
#include <stdbool.h>
#include <time.h>
#include <stdbool.h>
#include "obidms.h"
#include "obitypes.h"
@ -25,6 +26,7 @@
#include "obidmscolumndir.h"
#define ELEMENTS_NAMES_MAX (2048)
#define INITIAL_LINE_COUNT (1000)
typedef int32_t obiversion_t; /**< Used to store the column version number
@ -49,6 +51,7 @@ typedef struct OBIDMS_column_header {
OBIType_t data_type; /**< type of the data */
time_t creation_date; /**< date of creation of the file */
obiversion_t version; /**< version of the OBIColumn */
obiversion_t cloned_from; /**< version of the OBIColumn from which the column was cloned from (-1 if it does not come from cloning).*/
char name[OBIDMS_MAX_COLNAME+1]; /**< The column name as a NULL
* terminated string.
*/
@ -148,7 +151,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
*
* @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
* @param version_number the version of the column that should be opened (if -1, the latest version number is retrieved)
*
* @return a pointer to the opened column
*
@ -158,6 +161,22 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
OBIDMS_column_p obi_open_column(OBIDMS_p dms, const char* column_name, obiversion_t version_number);
/**
* @brief Clones a column, and returns a pointer to the writable new column.
*
* @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 number is retrieved)
* @param clone_data whether the data should be copied or not
*
* @return a pointer to the created column
*
* @since August 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
OBIDMS_column_p obi_clone_column(OBIDMS_p dms, const char* column_name, obiversion_t version_number, bool clone_data);
/**
* @brief Closes a column.
*