diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index 3f00fc9..f8cd66d 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -660,7 +660,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, return NULL; } } - else if ((nb_elements_per_line == 1) && (strcmp(elements_names, column_name) != 0)) + else if ((nb_elements_per_line == 1) && (strcmp(elements_names, column_name) != 0)) // TODO Discuss, maybe just make it the column name { obidebug(1, "\nCan't create column because the element name does not match the column name"); return NULL; @@ -1126,6 +1126,29 @@ int obi_close_column(OBIDMS_column_p column) } +int obi_clone_column_indexer(OBIDMS_column_p column) +{ + char* new_indexer_name; + + new_indexer_name = obi_build_indexer_name((column->header)->name, (column->header)->version); + if (new_indexer_name == NULL) + return -1; + + column->indexer = obi_clone_indexer(column->indexer, new_indexer_name); // TODO Need to lock this somehow? + if (column->indexer == NULL) + { + obidebug(1, "\nError cloning a column's indexer to make it writable"); + return -1; + } + + strcpy((column->header)->indexer_name, new_indexer_name); + + free(new_indexer_name); + + return 0; +} + + int obi_truncate_column(OBIDMS_column_p column) // TODO is it necessary to unmap/remap? { size_t file_size; diff --git a/src/obidmscolumn.h b/src/obidmscolumn.h index 4ecbcf7..ee075c9 100644 --- a/src/obidmscolumn.h +++ b/src/obidmscolumn.h @@ -237,6 +237,20 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, const char* column_name, obiversio 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); +/** + * @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); + + /** * @brief Truncates a column to the number of lines used if it is not read-only and closes it. * diff --git a/src/obidmscolumn_str.c b/src/obidmscolumn_str.c index c092323..872f045 100644 --- a/src/obidmscolumn_str.c +++ b/src/obidmscolumn_str.c @@ -27,7 +27,6 @@ int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, const char* value) { index_t idx; - char* new_indexer_name; if (obi_column_prepare_to_set_value(column, line_nb) < 0) return -1; @@ -44,14 +43,9 @@ int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, { if (obi_errno == OBI_READ_ONLY_INDEXER_ERROR) { - // TODO PUT IN A COLUMN FUNCTION // If the error is that the indexer is read-only, clone it - new_indexer_name = obi_build_indexer_name((column->header)->name, (column->header)->version); - if (new_indexer_name == NULL) + if (obi_clone_column_indexer(column) < 0) return -1; - column->indexer = obi_clone_indexer(column->indexer, new_indexer_name); // TODO Need to lock this somehow? - strcpy((column->header)->indexer_name, new_indexer_name); - free(new_indexer_name); obi_set_errno(0); // Add the value in the new indexer