Character string indexer API

This commit is contained in:
Celine Mercier
2016-04-12 17:21:01 +02:00
parent 04c9470f7d
commit 5ec2d8842e
14 changed files with 165 additions and 80 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

80
src/char_str_indexer.c Normal file
View File

@ -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 <stdlib.h>
#include <stdio.h>
#include <math.h>
#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);
}

61
src/char_str_indexer.h Normal file
View File

@ -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 <stdlib.h>
#include <stdio.h>
#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_ */

View File

@ -16,7 +16,6 @@
#include <math.h>
#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

View File

@ -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_ */

View File

@ -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);
}