Columns are now enlarged if needed when setting a value
This commit is contained in:
@ -600,7 +600,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
|
||||
int column_dir_file_descriptor;
|
||||
size_t header_size;
|
||||
size_t data_size;
|
||||
size_t i, nb_elements;
|
||||
|
||||
new_column = NULL;
|
||||
|
||||
@ -732,41 +731,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
|
||||
strncpy(header->name, column_name, OBIDMS_MAX_COLNAME);
|
||||
|
||||
// Fill the data with NA values
|
||||
nb_elements = nb_lines*nb_elements_per_line;
|
||||
switch (type) {
|
||||
case OBI_VOID: // TODO;
|
||||
break;
|
||||
|
||||
case OBI_INT: for (i=0;i<nb_elements;i++)
|
||||
{
|
||||
*(((obiint_t*) (new_column->data)) + i) = OBIInt_NA;
|
||||
}
|
||||
break;
|
||||
|
||||
case OBI_FLOAT: for (i=0;i<nb_elements;i++)
|
||||
{
|
||||
*(((obifloat_t*) (new_column->data)) + i) = OBIFloat_NA;
|
||||
}
|
||||
break;
|
||||
|
||||
case OBI_BOOL: for (i=0;i<nb_elements;i++)
|
||||
{
|
||||
*(((obibool_t*) (new_column->data)) + i) = OBIBool_NA;
|
||||
}
|
||||
break;
|
||||
|
||||
case OBI_CHAR: for (i=0;i<nb_elements;i++)
|
||||
{
|
||||
*(((obichar_t*) (new_column->data)) + i) = OBIChar_NA;
|
||||
}
|
||||
break;
|
||||
|
||||
case OBI_IDX: for (i=0;i<nb_elements;i++)
|
||||
{
|
||||
*(((obiidx_t*) (new_column->data)) + i) = OBIIdx_NA;
|
||||
}
|
||||
break;
|
||||
}
|
||||
obi_ini_to_NA_values (new_column, 0, nb_lines);
|
||||
|
||||
free(column_file_name);
|
||||
close(column_file_descriptor);
|
||||
@ -1006,7 +971,7 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column)
|
||||
}
|
||||
|
||||
// Open the column file
|
||||
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR | O_CREAT);
|
||||
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR);
|
||||
if (column_file_descriptor < 0)
|
||||
{
|
||||
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
||||
@ -1066,6 +1031,139 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column)
|
||||
}
|
||||
|
||||
|
||||
int obi_enlarge_column(OBIDMS_column_p column)
|
||||
{
|
||||
size_t file_size;
|
||||
size_t old_data_size;
|
||||
size_t new_data_size;
|
||||
size_t header_size;
|
||||
size_t old_line_count;
|
||||
size_t new_line_count;
|
||||
int column_dir_file_descriptor;
|
||||
int column_file_descriptor;
|
||||
char* column_file_name;
|
||||
|
||||
// Get the file descriptor associated to the column directory
|
||||
column_dir_file_descriptor = dirfd((column->column_directory)->directory);
|
||||
if (column_dir_file_descriptor < 0)
|
||||
{
|
||||
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
||||
obidebug(1, "\nError getting the file descriptor for a column directory");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Get the column file name
|
||||
column_file_name = build_column_file_name((column->header)->name, (column->header)->version);
|
||||
if (column_file_name == NULL)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Open the column file
|
||||
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR | O_CREAT);
|
||||
if (column_file_descriptor < 0)
|
||||
{
|
||||
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
||||
obidebug(1, "\nError getting the file descriptor of a column file");
|
||||
free(column_file_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Calculate the new file size
|
||||
old_line_count = (column->header)->line_count;
|
||||
new_line_count = old_line_count * GROWTH_FACTOR;
|
||||
if (new_line_count > MAXIMUM_LINE_COUNT)
|
||||
{
|
||||
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
||||
obidebug(1, "\nError enlarging a column file: new line count greater than the maximum allowed");
|
||||
free(column_file_name);
|
||||
close(column_file_descriptor);
|
||||
return -1;
|
||||
}
|
||||
old_data_size = (column->header)->line_count * (column->header)->nb_elements_per_line * sizeof((column->header)->data_type);
|
||||
new_data_size = old_data_size * GROWTH_FACTOR;
|
||||
header_size = (column->header)->header_size;
|
||||
file_size = header_size + new_data_size;
|
||||
|
||||
// Unmap the data
|
||||
if (munmap(column->data, old_data_size) < 0)
|
||||
{
|
||||
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
||||
obidebug(1, "\nError munmapping the data of a column before enlarging");
|
||||
free(column_file_name);
|
||||
close(column_file_descriptor);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Unmap the header
|
||||
if (munmap(column->header, header_size) < 0)
|
||||
{
|
||||
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
||||
obidebug(1, "\nError munmapping the header of a column before enlarging");
|
||||
free(column_file_name);
|
||||
close(column_file_descriptor);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Enlarge the file
|
||||
if (ftruncate(column_file_descriptor, file_size) < 0)
|
||||
{
|
||||
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
||||
obidebug(1, "\nError enlarging a column file");
|
||||
free(column_file_name);
|
||||
close(column_file_descriptor);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Remap the header (TODO not sure if necessary??)
|
||||
column->header = mmap(NULL,
|
||||
header_size,
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED,
|
||||
column_file_descriptor,
|
||||
0
|
||||
);
|
||||
|
||||
if (column->header == MAP_FAILED)
|
||||
{
|
||||
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
||||
obidebug(1, "\nError mmapping the header of a column after enlarging file");
|
||||
close(column_file_descriptor);
|
||||
free(column_file_name);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Remap the data
|
||||
column->data = mmap(NULL,
|
||||
new_data_size,
|
||||
PROT_READ | PROT_WRITE,
|
||||
MAP_SHARED,
|
||||
column_file_descriptor,
|
||||
(column->header)->header_size
|
||||
);
|
||||
|
||||
if (column->data == MAP_FAILED)
|
||||
{
|
||||
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
||||
obidebug(1, "\nError re-mmapping the data of a column after enlarging the file");
|
||||
free(column_file_name);
|
||||
close(column_file_descriptor);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set new line count
|
||||
(column->header)->line_count = new_line_count;
|
||||
|
||||
// Initialize new data lines to NA
|
||||
obi_ini_to_NA_values(column, old_line_count, new_line_count - old_line_count);
|
||||
|
||||
free(column_file_name);
|
||||
close(column_file_descriptor);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int obi_truncate_and_close_column(OBIDMS_column_p column)
|
||||
{
|
||||
if (obi_truncate_column_to_lines_used(column) < 0)
|
||||
@ -1077,6 +1175,50 @@ int obi_truncate_and_close_column(OBIDMS_column_p column)
|
||||
}
|
||||
|
||||
|
||||
void obi_ini_to_NA_values(OBIDMS_column_p column, size_t start, size_t nb_lines)
|
||||
{
|
||||
size_t i, end, nb_elements;
|
||||
|
||||
nb_elements = nb_lines*((column->header)->nb_elements_per_line);
|
||||
end = start + nb_elements;
|
||||
|
||||
switch ((column->header)->data_type) {
|
||||
case OBI_VOID: // TODO;
|
||||
break;
|
||||
|
||||
case OBI_INT: for (i=start;i<end;i++)
|
||||
{
|
||||
*(((obiint_t*) (column->data)) + i) = OBIInt_NA;
|
||||
}
|
||||
break;
|
||||
|
||||
case OBI_FLOAT: for (i=start;i<end;i++)
|
||||
{
|
||||
*(((obifloat_t*) (column->data)) + i) = OBIFloat_NA;
|
||||
}
|
||||
break;
|
||||
|
||||
case OBI_BOOL: for (i=start;i<end;i++)
|
||||
{
|
||||
*(((obibool_t*) (column->data)) + i) = OBIBool_NA;
|
||||
}
|
||||
break;
|
||||
|
||||
case OBI_CHAR: for (i=start;i<end;i++)
|
||||
{
|
||||
*(((obichar_t*) (column->data)) + i) = OBIChar_NA;
|
||||
}
|
||||
break;
|
||||
|
||||
case OBI_IDX: for (i=start;i<end;i++)
|
||||
{
|
||||
*(((obiidx_t*) (column->data)) + i) = OBIIdx_NA;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void obi_column_make_unwritable(OBIDMS_column_p column)
|
||||
{
|
||||
column->writable = false;
|
||||
|
Reference in New Issue
Block a user