Commit 1586956d by Celine Mercier

Added the lists of opened columns and arrays in the OBIDMS structure,

and a counter in the OBIDMS column structure; fixed some bugs and
created tests for referring columns that are bound to disappear anyway.
parent b45b496b
......@@ -79,7 +79,6 @@ cdef class OBIDMS :
cpdef close(self) :
#TODO close all columns (needs to be discussed)
if (obi_close_dms(self.pointer)) < 0 :
raise Exception("Problem closing an OBIDMS")
......
......@@ -233,9 +233,20 @@ static char* build_array_data_file_name(const char* array_name)
}
size_t get_array_header_size()
{
return getpagesize() * 1;
size_t header_size;
size_t rounded_header_size;
double multiple;
header_size = sizeof(OBIDMS_array_header_t);
multiple = ceil((double) header_size / (double) getpagesize());
rounded_header_size = multiple * getpagesize();
return rounded_header_size;
}
......@@ -247,7 +258,17 @@ size_t get_initial_array_size()
size_t get_array_data_header_size()
{
return getpagesize() * 1;
size_t header_size;
size_t rounded_header_size;
double multiple;
header_size = sizeof(OBIDMS_array_data_header_t);
multiple = ceil((double) header_size / (double) getpagesize());
rounded_header_size = multiple * getpagesize();
return rounded_header_size;
}
......@@ -261,17 +282,17 @@ int close_array_data(OBIDMS_array_data_p array_data)
{
int ret_val = 0;
if (munmap(array_data->header, (array_data->header)->header_size) < 0)
if (munmap(array_data->data, (array_data->header)->data_size_max) < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the header of an array data file");
obidebug(1, "\nError munmapping the data of an array data file");
ret_val = -1;
}
if (munmap(array_data->data, (array_data->header)->data_size_max) < 0)
if (munmap(array_data->header, (array_data->header)->header_size) < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the data of an array data file");
obidebug(1, "\nError munmapping the header of an array data file");
ret_val = -1;
}
......@@ -737,6 +758,7 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
return NULL;
}
array->dms = dms;
array->data = array_data;
array->directory = dms->array_directory;
array->dir_fd = array_dir_file_descriptor;
......@@ -753,6 +775,11 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
close(array_file_descriptor);
// Add in the list of opened arrays
*(((dms->opened_arrays)->arrays)+((dms->opened_arrays)->nb_opened_arrays)) = array;
((dms->opened_arrays)->nb_opened_arrays)++;
array->counter = 1;
return array;
}
......@@ -767,6 +794,17 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
int array_dir_file_descriptor;
OBIDMS_array_data_p array_data;
OBIDMS_array_p array;
size_t i;
// Check if the array is already in the list of opened arrays
for (i=0; i < ((dms->opened_arrays)->nb_opened_arrays); i++)
{
if (!strcmp(((*(((dms->opened_arrays)->arrays)+i))->header)->array_name, array_name))
{ // Found the array already opened
((*(((dms->opened_arrays)->arrays)+i))->counter)++;
return *(((dms->opened_arrays)->arrays)+i);
}
}
// Open the data file
......@@ -923,37 +961,65 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
return NULL;
}
array->dms = dms;
array->data = array_data;
array->directory = dms->array_directory;
array->dir_fd = array_dir_file_descriptor;
close(array_file_descriptor);
// Add in the list of opened arrays
*(((dms->opened_arrays)->arrays)+((dms->opened_arrays)->nb_opened_arrays)) = array;
((dms->opened_arrays)->nb_opened_arrays)++;
array->counter = 1;
return array;
}
int obi_close_array(OBIDMS_array_p array) // TODO when do we call it? (as multiple threads can be using the same array)
int obi_close_array(OBIDMS_array_p array)
{
int ret_val = 0;
size_t i;
Opened_arrays_list_p arrays_list;
OBIDMS_p dms;
ret_val = close_array_data(array->data);
dms = array->dms;
if (munmap(array->first, (((array->header)->nb_items_max) * sizeof(index_t))) < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the array of an array file");
ret_val = -1;
}
arrays_list = dms->opened_arrays;
(array->counter)--;
if (munmap(array->header, (array->header)->header_size) < 0)
if (array->counter == 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the header of an array file");
ret_val = -1;
}
// Delete from the list of opened arrays
for (i=0; i < (arrays_list->nb_opened_arrays); i++)
{
if (!strcmp(((*((arrays_list->arrays)+i))->header)->array_name, (array->header)->array_name))
{ // Found the array. Rearrange list
(arrays_list->nb_opened_arrays)--;
(arrays_list->arrays)[i] = (arrays_list->arrays)[arrays_list->nb_opened_arrays];
}
}
ret_val = close_array_data(array->data);
if (munmap(array->first, (((array->header)->nb_items_max) * sizeof(index_t))) < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the array of an array file");
ret_val = -1;
}
free(array);
if (munmap(array->header, (array->header)->header_size) < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the header of an array file");
ret_val = -1;
}
free(array);
}
return ret_val;
}
......
......@@ -91,6 +91,8 @@ typedef struct OBIDMS_array_header {
* @brief OBIDMS array structure.
*/
typedef struct OBIDMS_array {
OBIDMS_p dms; /**< A pointer to the OBIDMS structure to which the array belongs.
*/
OBIDMS_array_header_p header; /**< A pointer to the header of the array.
*/
index_t* first; /**< A pointer to the beginning of the array itself.
......@@ -104,6 +106,8 @@ typedef struct OBIDMS_array {
int dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the array directory.
*/
size_t counter; /**< Indicates by how many threads/programs (TODO) the array is used.
*/
} OBIDMS_array_t, *OBIDMS_array_p;
......
......@@ -413,6 +413,16 @@ OBIDMS_p obi_open_dms(const char* dms_name)
return NULL;
}
// Initialize the list of opened columns
dms->opened_columns = (Opened_columns_list_p) malloc(sizeof(Opened_columns_list_t));
(dms->opened_columns)->columns = (OBIDMS_column_p*) malloc(MAX_NB_OPENED_COLUMNS*sizeof(OBIDMS_column_p));
(dms->opened_columns)->nb_opened_columns = 0;
// Initialize the list of opened arrays
dms->opened_arrays = (Opened_arrays_list_p) malloc(sizeof(Opened_arrays_list_t));
(dms->opened_arrays)->arrays = (OBIDMS_array_p*) malloc(MAX_NB_OPENED_ARRAYS*sizeof(OBIDMS_array_p));
(dms->opened_arrays)->nb_opened_arrays = 0;
return dms;
}
......@@ -440,6 +450,11 @@ int obi_close_dms(OBIDMS_p dms)
{
if (dms != NULL)
{
// Close all columns
while ((dms->opened_columns)->nb_opened_columns > 0)
obi_close_column(*((dms->opened_columns)->columns));
// Close dms and array directories
if (closedir(dms->directory) < 0)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
......
......@@ -25,10 +25,30 @@
#include "obierrno.h"
#define OBIDMS_MAX_NAME (2048) /**< The maximum length of an OBIDMS name.
*/
#define ARRAYS_DIR_NAME "arrays" /**< The name of the arrays directory.
*/
#define OBIDMS_MAX_NAME (2048) /**< The maximum length of an OBIDMS name.
*/
#define ARRAYS_DIR_NAME "arrays" /**< The name of the arrays directory.
*/
#define MAX_NB_OPENED_COLUMNS (100) /**< The maximum number of columns open at the same time.
*/
#define MAX_NB_OPENED_ARRAYS (100) /**< The maximum number of arrays open at the same time.
*/
struct OBIDMS_column; // TODO
typedef struct Opened_columns_list {
size_t nb_opened_columns;
struct OBIDMS_column** columns;
} Opened_columns_list_t, *Opened_columns_list_p;
struct OBIDMS_array; // TODO
typedef struct Opened_arrays_list {
size_t nb_opened_arrays;
struct OBIDMS_array** arrays;
} Opened_arrays_list_t, *Opened_arrays_list_p;
/**
......@@ -38,23 +58,27 @@
* and opening of an OBITools Data Management System (DMS)
*/
typedef struct OBIDMS {
char directory_name[OBIDMS_MAX_NAME+1]; /**< The name of the directory
* containing the DMS.
*/
DIR* directory; /**< A directory entry usable to
* refer and scan the database directory.
*/
int dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the database directory.
*/
DIR* array_directory; /**< A directory entry usable to
* refer and scan the array directory.
*/
int array_dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the array directory.
*/
bool little_endian; /**< Endianness of the database.
*/
char directory_name[OBIDMS_MAX_NAME+1]; /**< The name of the directory
* containing the DMS.
*/
DIR* directory; /**< A directory entry usable to
* refer and scan the database directory.
*/
int dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the database directory.
*/
DIR* array_directory; /**< A directory entry usable to
* refer and scan the array directory.
*/
int array_dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the array directory.
*/
bool little_endian; /**< Endianness of the database.
*/
Opened_columns_list_p opened_columns; /**< List of opened columns.
*/
Opened_arrays_list_p opened_arrays; /**< List of opened arrays.
*/
} OBIDMS_t, *OBIDMS_p;
......
......@@ -732,9 +732,8 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
if (comments != NULL)
strncpy(header->comments, comments, COMMENTS_MAX_LENGTH);
// If the data type is OBI_STR or OBI_SEQ, and the column is not referring another,
// the associated obi_array is opened or created
if ((stored_data_type == OBI_STR) || (stored_data_type == OBI_SEQ))
// If the data type is OBI_STR or OBI_SEQ, the associated obi_array is opened or created
if ((returned_data_type == OBI_STR) || (returned_data_type == OBI_SEQ))
{
array = obi_array(dms, array_name);
if (array == NULL)
......@@ -754,6 +753,11 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
close(column_file_descriptor);
// Add in the list of opened columns
*(((dms->opened_columns)->columns)+((dms->opened_columns)->nb_opened_columns)) = new_column;
((dms->opened_columns)->nb_opened_columns)++;
new_column->counter = 1;
return new_column;
}
......@@ -768,6 +772,7 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms,
char* column_file_name;
int column_file_descriptor;
size_t header_size;
size_t i;
column = NULL;
......@@ -790,6 +795,17 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms,
}
}
// Check if the column is already in the list of opened columns
for (i=0; i < ((dms->opened_columns)->nb_opened_columns); i++)
{
if (!strcmp(((*(((dms->opened_columns)->columns)+i))->header)->name, column_name)
&& (((*(((dms->opened_columns)->columns)+i))->header)->version == version_number))
{ // Found the column already opened, increase its counter and return the column
((*(((dms->opened_columns)->columns)+i))->counter)++;
return *(((dms->opened_columns)->columns)+i);
}
}
// Get the column file name
column_file_name = build_column_file_name(column_name, version_number);
if (column_file_name == NULL)
......@@ -870,9 +886,8 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms,
column->writable = false;
// If the data type is OBI_STR or OBI_SEQ, and the column is not referring,
// the associated obi_array is opened
if (((column->header)->stored_data_type == OBI_STR) || ((column->header)->stored_data_type == OBI_SEQ))
// If the data type is OBI_STR or OBI_SEQ, the associated obi_array is opened
if (((column->header)->returned_data_type == OBI_STR) || ((column->header)->returned_data_type == OBI_SEQ))
{
array = obi_array(dms, (column->header)->array_name);
if (array == NULL)
......@@ -900,6 +915,11 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms,
close(column_file_descriptor);
// Add in the list of opened columns and set column counter to 1
*(((dms->opened_columns)->columns)+((dms->opened_columns)->nb_opened_columns)) = column;
((dms->opened_columns)->nb_opened_columns)++;
column->counter = 1;
return column;
}
......@@ -924,6 +944,14 @@ OBIDMS_column_p obi_clone_column(OBIDMS_p dms,
return NULL;
}
// A referring column can't be cloned
if ((column_to_clone->header)->referring)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError: can't clone a referring column");
return NULL;
}
data_type = (column_to_clone->header)->returned_data_type;
nb_elements_per_line = (column_to_clone->header)->returned_nb_elements_per_line;
......@@ -990,28 +1018,68 @@ OBIDMS_column_p obi_clone_column(OBIDMS_p dms,
int obi_close_column(OBIDMS_column_p column)
{
if ((column->header)->referring)
obi_close_column(column->referred_column);
size_t i;
bool close_dir;
Opened_columns_list_p columns_list;
// Munmap data
if (munmap(column->data, (column->header)->data_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError munmapping column data");
return -1;
}
columns_list = (column->dms)->opened_columns;
// Munmap header
if (munmap(column->header, (column->header)->header_size) < 0)
(column->counter)--;
if (column->counter == 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError munmapping a column header");
return -1;
}
// Delete from the list of opened columns
for (i=0; i < (columns_list)->nb_opened_columns; i++)
{
if (!strcmp(((*((columns_list->columns)+i))->header)->name, (column->header)->name)
&& (((*((columns_list->columns)+i))->header)->version == (column->header)->version))
{ // Found the column. Rearrange list
(columns_list->nb_opened_columns)--;
(columns_list->columns)[i] = (columns_list->columns)[columns_list->nb_opened_columns];
}
}
// Close column directory if it was the last column opened from that directory
close_dir = 1;
for (i=0; i < (columns_list->nb_opened_columns); i++)
{
if (!strcmp(((*((columns_list->columns)+i))->header)->name, (column->header)->name))
{ // Not the last column from that directory
close_dir = 0;
}
}
//obi_close_column_directory(column->column_directory); // TODO or not
if ((column->header)->referring)
obi_close_column(column->referred_column);
free(column);
// If the data type is OBI_STR or OBI_SEQ, the associated obi_array is closed
if (((column->header)->returned_data_type == OBI_STR) || ((column->header)->returned_data_type == OBI_SEQ))
{
if (obi_close_array(column->array) < 0)
return -1;
}
// Munmap data
if (munmap(column->data, (column->header)->data_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError munmapping column data");
return -1;
}
// Munmap header
if (munmap(column->header, (column->header)->header_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError munmapping a column header");
return -1;
}
free(column);
if (close_dir)
obi_close_column_directory(column->column_directory);
}
return 0;
}
......@@ -1409,7 +1477,7 @@ char* obi_column_format_date(time_t date)
}
// TODO put in separate file and needs to lock the dependency with the referred column but...
// TODO put in separate file ?
// warning for the dependency and for the fact that it's always added at the next line (or not cuz might not be a good idea?)
int obi_grep_line(OBIDMS_column_p referring_column, index_t line_to_grep)
{
......
......@@ -122,6 +122,8 @@ typedef struct OBIDMS_column {
* A column is writable only by its creator
* until it closes it.
*/
size_t counter; /**< Indicates by how many threads/programs (TODO) the column is used.
*/
} OBIDMS_column_t, *OBIDMS_column_p;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment