diff --git a/python/obitools3/obidms/_obidms.cfiles b/python/obitools3/obidms/_obidms.cfiles index 50d9879..8a385f6 100644 --- a/python/obitools3/obidms/_obidms.cfiles +++ b/python/obitools3/obidms/_obidms.cfiles @@ -1,5 +1,7 @@ ../../../src/bloom.h ../../../src/bloom.c +../../../src/char_str_indexer.h +../../../src/char_str_indexer.c ../../../src/crc64.h ../../../src/crc64.c ../../../src/dna_seq_indexer.h diff --git a/python/obitools3/obidms/_obidmscolumn_bool.cfiles b/python/obitools3/obidms/_obidmscolumn_bool.cfiles index 277bc9e..768624d 100644 --- a/python/obitools3/obidms/_obidmscolumn_bool.cfiles +++ b/python/obitools3/obidms/_obidmscolumn_bool.cfiles @@ -2,6 +2,8 @@ ../../../src/obidmscolumn_bool.h ../../../src/bloom.h ../../../src/bloom.c +../../../src/char_str_indexer.h +../../../src/char_str_indexer.c ../../../src/crc64.h ../../../src/crc64.c ../../../src/dna_seq_indexer.h diff --git a/python/obitools3/obidms/_obidmscolumn_char.cfiles b/python/obitools3/obidms/_obidmscolumn_char.cfiles index 05d09a0..f1e152e 100644 --- a/python/obitools3/obidms/_obidmscolumn_char.cfiles +++ b/python/obitools3/obidms/_obidmscolumn_char.cfiles @@ -2,6 +2,8 @@ ../../../src/obidmscolumn_char.h ../../../src/bloom.h ../../../src/bloom.c +../../../src/char_str_indexer.h +../../../src/char_str_indexer.c ../../../src/crc64.h ../../../src/crc64.c ../../../src/dna_seq_indexer.h diff --git a/python/obitools3/obidms/_obidmscolumn_float.cfiles b/python/obitools3/obidms/_obidmscolumn_float.cfiles index 3619c44..c8f9cc5 100644 --- a/python/obitools3/obidms/_obidmscolumn_float.cfiles +++ b/python/obitools3/obidms/_obidmscolumn_float.cfiles @@ -1,5 +1,7 @@ ../../../src/obidmscolumn_float.c ../../../src/obidmscolumn_float.h +../../../src/char_str_indexer.h +../../../src/char_str_indexer.c ../../../src/bloom.h ../../../src/bloom.c ../../../src/crc64.h diff --git a/python/obitools3/obidms/_obidmscolumn_int.cfiles b/python/obitools3/obidms/_obidmscolumn_int.cfiles index 9c0776f..a67627a 100644 --- a/python/obitools3/obidms/_obidmscolumn_int.cfiles +++ b/python/obitools3/obidms/_obidmscolumn_int.cfiles @@ -1,5 +1,7 @@ ../../../src/obidmscolumn_int.c ../../../src/obidmscolumn_int.h +../../../src/char_str_indexer.h +../../../src/char_str_indexer.c ../../../src/bloom.h ../../../src/bloom.c ../../../src/crc64.h diff --git a/python/obitools3/obidms/_obidmscolumn_seq.cfiles b/python/obitools3/obidms/_obidmscolumn_seq.cfiles index 7ab8ac1..4582a53 100644 --- a/python/obitools3/obidms/_obidmscolumn_seq.cfiles +++ b/python/obitools3/obidms/_obidmscolumn_seq.cfiles @@ -2,6 +2,8 @@ ../../../src/obidmscolumn_seq.h ../../../src/bloom.h ../../../src/bloom.c +../../../src/char_str_indexer.h +../../../src/char_str_indexer.c ../../../src/crc64.h ../../../src/crc64.c ../../../src/dna_seq_indexer.h diff --git a/python/obitools3/obidms/_obidmscolumn_str.cfiles b/python/obitools3/obidms/_obidmscolumn_str.cfiles index b987965..9a65e77 100644 --- a/python/obitools3/obidms/_obidmscolumn_str.cfiles +++ b/python/obitools3/obidms/_obidmscolumn_str.cfiles @@ -2,6 +2,8 @@ ../../../src/obidmscolumn_str.h ../../../src/bloom.h ../../../src/bloom.c +../../../src/char_str_indexer.h +../../../src/char_str_indexer.c ../../../src/crc64.h ../../../src/crc64.c ../../../src/dna_seq_indexer.h diff --git a/python/obitools3/obidms/_obiseq.cfiles b/python/obitools3/obidms/_obiseq.cfiles index 50d9879..8a385f6 100644 --- a/python/obitools3/obidms/_obiseq.cfiles +++ b/python/obitools3/obidms/_obiseq.cfiles @@ -1,5 +1,7 @@ ../../../src/bloom.h ../../../src/bloom.c +../../../src/char_str_indexer.h +../../../src/char_str_indexer.c ../../../src/crc64.h ../../../src/crc64.c ../../../src/dna_seq_indexer.h diff --git a/python/obitools3/obidms/_obitaxo.cfiles b/python/obitools3/obidms/_obitaxo.cfiles index 50d9879..8a385f6 100644 --- a/python/obitools3/obidms/_obitaxo.cfiles +++ b/python/obitools3/obidms/_obitaxo.cfiles @@ -1,5 +1,7 @@ ../../../src/bloom.h ../../../src/bloom.c +../../../src/char_str_indexer.h +../../../src/char_str_indexer.c ../../../src/crc64.h ../../../src/crc64.c ../../../src/dna_seq_indexer.h diff --git a/src/char_str_indexer.c b/src/char_str_indexer.c new file mode 100644 index 0000000..d6f528a --- /dev/null +++ b/src/char_str_indexer.c @@ -0,0 +1,80 @@ +/**************************************************************************** + * Character string indexing functions * + ****************************************************************************/ + +/** + * @file char_str_indexer.c + * @author Celine Mercier + * @date April 12th 2016 + * @brief Functions handling the indexing and retrieval of character strings. + */ + + +#include +#include +#include + +#include "obiblob.h" +#include "obiblob_indexer.h" +#include "obidebug.h" +#include "obitypes.h" + + +#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?) + + +Obi_blob_p obi_str_to_blob(const char* value) +{ + Obi_blob_p value_b; + int32_t length; + + // Compute the number of bytes on which the value will be encoded + length = strlen(value) + 1; // +1 to store \0 at the end (makes retrieving faster) + + value_b = obi_blob((byte_t*)value, ELEMENT_SIZE_STR, length, length); + if (value_b == NULL) + { + obidebug(1, "\nError encoding a character string in a blob"); + return NULL; + } + + return value_b; +} + + +char* obi_blob_to_str(Obi_blob_p value_b) +{ + return value_b->value; +} + + +index_t obi_index_char_str(Obi_indexer_p indexer, const char* value) +{ + Obi_blob_p value_b; + index_t idx; + + // Encode value + value_b = obi_str_to_blob(value); + if (value_b == NULL) + return -1; + + // Add in the indexer + idx = obi_indexer_add(indexer, value_b); + + free(value_b); + + return idx; +} + + +char* obi_retrieve_char_str(Obi_indexer_p indexer, index_t idx) +{ + Obi_blob_p value_b; + + // Get encoded value + value_b = obi_indexer_get(indexer, idx); + + // Return decoded character string + return obi_blob_to_str(value_b); +} + diff --git a/src/char_str_indexer.h b/src/char_str_indexer.h new file mode 100644 index 0000000..08a3179 --- /dev/null +++ b/src/char_str_indexer.h @@ -0,0 +1,61 @@ +/**************************************************************************** + * DNA sequence indexer header file * + ****************************************************************************/ + +/** + * @file dna_seq_indexer.h + * @author Celine Mercier + * @date April 12th 2016 + * @brief Header file for the functions handling the indexing of DNA sequences. + */ + + +#ifndef CHAR_STR_INDEXER_H_ +#define CHAR_STR_INDEXER_H_ + + +#include +#include + +#include "obitypes.h" +#include "obiblob.h" +#include "obiblob_indexer.h" + + +/** + * @brief Converts a character string to a blob. + * + * @warning The blob must be freed by the caller. + * + * @param value The character string to convert. + * + * @returns A pointer to the blob created. + * @retval NULL if an error occurred. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +Obi_blob_p obi_str_to_blob(char* value); + + +/** + * @brief Converts a blob to a character string. + * + * @param value_b The blob to convert. + * + * @returns A pointer to the character string contained in the blob. + * + * @since October 2015 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +const char* obi_blob_to_str(Obi_blob_p value_b); + + +// TODO doc +index_t obi_index_char_str(Obi_indexer_p indexer, const char* value); + +char* obi_retrieve_char_str(Obi_indexer_p indexer, index_t idx); + + +#endif /* CHAR_STR_INDEXER_H_ */ + diff --git a/src/obiblob.c b/src/obiblob.c index aa438ab..4e0b623 100644 --- a/src/obiblob.c +++ b/src/obiblob.c @@ -16,7 +16,6 @@ #include #include "obiblob.h" -#include "encode.h" #include "obierrno.h" #include "obitypes.h" // For byte_t type #include "obidebug.h" @@ -56,31 +55,3 @@ Obi_blob_p obi_blob(byte_t* encoded_value, uint8_t element_size, int32_t length_ return blob; } - -Obi_blob_p obi_str_to_blob(char* value) -{ - Obi_blob_p value_b; - int32_t length; - - // Compute the number of bytes on which the value will be encoded - length = strlen(value) + 1; // +1 to store \0 at the end (makes retrieving faster) - - value_b = obi_blob(value, ELEMENT_SIZE_STR, length, length); - if (value_b == NULL) - { - obidebug(1, "\nError encoding a character string in a blob"); - return NULL; - } - - return value_b; -} - - -const char* obi_blob_to_str(Obi_blob_p value_b) -{ - return value_b->value; -} - - -// TODO same for int - diff --git a/src/obiblob.h b/src/obiblob.h index f39b9c8..5b34197 100644 --- a/src/obiblob.h +++ b/src/obiblob.h @@ -50,34 +50,5 @@ typedef struct Obi_blob { Obi_blob_p obi_blob(byte_t* encoded_value, uint8_t element_size, int32_t length_encoded_value, int32_t length_decoded_value); -/** - * @brief Converts a character string to a blob. - * - * @warning The blob must be freed by the caller. - * - * @param value The character string to convert. - * - * @returns A pointer to the blob created. - * @retval NULL if an error occurred. - * - * @since October 2015 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -Obi_blob_p obi_str_to_blob(char* value); - - -/** - * @brief Converts a blob to a character string. - * - * @param value_b The blob to convert. - * - * @returns A pointer to the character string contained in the blob. - * - * @since October 2015 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -const char* obi_blob_to_str(Obi_blob_p value_b); - - #endif /* OBIBLOB_H_ */ diff --git a/src/obidmscolumn_str.c b/src/obidmscolumn_str.c index 8742295..ee1a429 100644 --- a/src/obidmscolumn_str.c +++ b/src/obidmscolumn_str.c @@ -18,7 +18,7 @@ #include "obitypes.h" #include "obierrno.h" #include "obidebug.h" -#include "obiblob_indexer.h" +#include "char_str_indexer.h" #define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?) @@ -32,7 +32,6 @@ int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx, char* value) { - Obi_blob_p value_b; index_t idx; // Check that the line number is not greater than the maximum allowed @@ -55,21 +54,14 @@ int obi_column_set_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, if ((line_nb+1) > (column->header)->lines_used) (column->header)->lines_used = line_nb+1; - // Encode the value on a byte array with a header - value_b = obi_str_to_blob(value); - if (value_b == NULL) - return -1; - // Add in the indexer - idx = obi_indexer_add(column->indexer, value_b); + idx = obi_index_char_str(column->indexer, value); if (idx == -1) return -1; // Add the value's index in the column *(((index_t*) (column->data)) + (line_nb * ((column->header)->nb_elements_per_line)) + element_idx) = idx; - free(value_b); - return 0; } @@ -104,17 +96,13 @@ int obi_column_set_obistr_with_elt_idx_in_view(Obiview_p view, OBIDMS_column_p c return -1; } - if (obi_column_set_obistr_with_elt_idx(column, line_nb, element_idx, value) < 0) - return -1; - - return 0; + return obi_column_set_obistr_with_elt_idx(column, line_nb, element_idx, value); } const char* obi_column_get_obistr_with_elt_idx(OBIDMS_column_p column, index_t line_nb, index_t element_idx) { index_t idx; - Obi_blob_p value_b; if ((line_nb+1) > ((column->header)->line_count)) { @@ -129,9 +117,7 @@ const char* obi_column_get_obistr_with_elt_idx(OBIDMS_column_p column, index_t l if (idx == OBIIdx_NA) return OBIStr_NA; - value_b = obi_indexer_get(column->indexer, idx); - - return obi_blob_to_str(value_b); + return obi_retrieve_char_str(column->indexer, idx); } @@ -157,8 +143,7 @@ int obi_column_set_obistr_with_elt_name(OBIDMS_column_p column, index_t line_nb, element_idx = obi_column_get_element_index_from_name(column, element_name); if (element_idx == OBIIdx_NA) return -1; - obi_column_set_obistr_with_elt_idx(column, line_nb, element_idx, value); - return 0; + return obi_column_set_obistr_with_elt_idx(column, line_nb, element_idx, value); } @@ -168,8 +153,7 @@ int obi_column_set_obistr_with_elt_name_in_view(Obiview_p view, OBIDMS_column_p element_idx = obi_column_get_element_index_from_name(column, element_name); if (element_idx == OBIIdx_NA) return -1; - obi_column_set_obistr_with_elt_idx_in_view(view, column, line_nb, element_idx, value); - return 0; + return obi_column_set_obistr_with_elt_idx_in_view(view, column, line_nb, element_idx, value); }