New OBIDMS method to list the columns of an OBIDMS

This commit is contained in:
Celine Mercier
2015-09-15 17:09:31 +02:00
parent 90bf15186e
commit 2698022aaf
7 changed files with 130 additions and 69 deletions

View File

@ -216,54 +216,6 @@ OBIDMS_p obi_dms(const char* dms_name)
}
int obi_list_columns(OBIDMS_p dms)
{
DIR *d;
struct dirent *dir;
char* dir_name;
char* extension;
OBIType_t data_type;
obiversion_t latest_version;
d = dms->directory;
dir = readdir(d);
if (dir == NULL)
{
obidebug(1, "\nError reading in the OBIDMS directory");
return -1;
}
fprintf(stderr, "Column name\tData type\tLatest version");
while (dir != NULL)
{
dir_name = strdup(dir->d_name);
if (dir_name == NULL)
{
obidebug(1, "\nError strdup-ing a directory name");
return -1;
}
dir_name = strtok(dir_name, ".");
extension = strtok(NULL, ".");
if ((extension != NULL) && (strcmp("obicol", extension) == 0))
// Found a column directory
{
data_type = obi_column_get_data_type_from_name(dms, dir_name);
latest_version = obi_column_get_latest_version_from_name(dms, dir_name);
fprintf(stderr, "\n%s\t%d\t%d", dir_name, data_type, latest_version);
}
dir = readdir(d);
}
rewinddir(d);
return 0;
}
int obi_close_dms(OBIDMS_p dms)
{
if (dms != NULL)

View File

@ -126,23 +126,6 @@ OBIDMS_p obi_open_dms(const char *dms_name);
OBIDMS_p obi_dms(const char *dms_name);
/**
* @brief Lists all the column directories in the OBIDMS, their data type and
* their latest version.
*
* @param dms a pointer as returned by obi_create_dms() or obi_open_dms().
*
* @return an integer value indicating the success of the operation.
*
* @retvalue 0 on success
* @retvalue -1 on failure and the `obi_errno` variable is set.
*
* @since July 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
int obi_list_columns(OBIDMS_p dms);
/**
* @brief Closes an opened OBITools Data Management instance (OBIDMS).
*

View File

@ -1347,13 +1347,99 @@ OBIType_t obi_column_get_data_type_from_name(OBIDMS_p dms, const char* column_na
}
size_t obi_column_get_line_count_from_name(OBIDMS_p dms, const char* column_name)
{
OBIDMS_column_header_p header;
OBIDMS_column_directory_p column_directory;
char* column_file_name;
int column_file_descriptor;
int column_dir_file_descriptor;
size_t header_size;
size_t line_count;
obiversion_t version_number;
// Get the column directory structure associated to the column
column_directory = obi_open_column_directory(dms, column_name);
if (column_directory == NULL)
{
obidebug(1, "\nError opening a column directory structure");
return -1;
}
// Get the file descriptor associated to the column directory
column_dir_file_descriptor = dirfd(column_directory->directory);
if (column_dir_file_descriptor < 0)
{
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
obidebug(1, "\nError getting the file descriptor of a column directory");
obi_close_column_directory(column_directory);
return -1;
}
// Calculate the header size
header_size = obi_get_platform_header_size();
// Get the latest version number
version_number = obi_get_latest_version_number(column_directory);
if (version_number < 0)
{
obidebug(1, "\nError getting the latest version number in a column directory");
return -1;
}
// Get the column file name
column_file_name = build_column_file_name(column_name, version_number);
if (column_file_name == NULL)
{
return -1;
}
// Open the column file (READ-ONLY)
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDONLY);
if (column_file_descriptor < 0)
{
obidebug(1, "\nError opening a column file");
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(column_file_name);
return -1;
}
// Fill the header structure
header = mmap(NULL,
header_size,
PROT_READ,
MAP_SHARED,
column_file_descriptor,
0
);
if (header == MAP_FAILED)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError mmapping the header of a column");
close(column_file_descriptor);
free(column_file_name);
return -1;
}
// Check endianness?
line_count = header->line_count;
free(column_file_name);
close(column_file_descriptor);
munmap(header, header_size);
return line_count;
}
const char* obi_column_get_elements_names(OBIDMS_column_p column)
{
return (column->header)->elements_names;
}
// to be rewritten in an optimized and safe way
// TODO to be rewritten in an optimized and safe way
size_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name)
{
char* elements_names;

View File

@ -312,6 +312,20 @@ OBIType_t obi_column_get_data_type(OBIDMS_column_p column);
OBIType_t obi_column_get_data_type_from_name(OBIDMS_p dms, const char* column_name);
/**
* @brief Recovers the line count of an OBIDMS column from the column name.
*
* @param dms a pointer on an OBIDMS
* @param column_name the name of an OBIDMS column
*
* @return the line count of the column
*
* @since September 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
size_t obi_column_get_line_count_from_name(OBIDMS_p dms, const char* column_name);
/**
* @brief Recovers the elements names of an OBIDMS column.
*