1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-23 04:43:57 +01:00

Merge branch 'master' into change_h5rw_datasets

This commit is contained in:
Evgeny Posenitskiy 2021-03-04 20:21:08 +01:00 committed by GitHub
commit e624be1435
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 214 additions and 52 deletions

View File

@ -120,15 +120,11 @@ int test_h5read() {
file2 = trexio_open(file_name2, 'r', TREXIO_HDF5);
assert (file2 == NULL);
// test appending non-existing file, should create it
const char* file_name3 = "test_append.h5";
// test appending non-existing file, should fail and return NULL
trexio_t* file3 = NULL;
file3 = trexio_open(file_name3, 'a', TREXIO_HDF5);
assert (file3 != NULL);
rc = trexio_close(file3);
assert (rc == TREXIO_SUCCESS);
file3 = trexio_open(file_name2, 'a', TREXIO_HDF5);
assert (file3 == NULL);
free(coord);

View File

@ -31,6 +31,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
trexio_t* result = NULL;
/* Allocate data structures */
switch (back_end) {
case TREXIO_TEXT:
@ -47,8 +48,10 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
*/
}
/* TODO: Error handling */
assert (result != NULL);
assert (result != NULL); /* TODO: Error handling */
/* Data for the parent type */
result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char));
strcpy(result->file_name, file_name);
@ -57,7 +60,11 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
int irc = pthread_mutex_init ( &(result->thread_lock), NULL);
assert (irc == 0);
trexio_exit_code rc = TREXIO_FAILURE;
trexio_exit_code rc;
/* Back end initialization */
rc = TREXIO_FAILURE;
switch (back_end) {
@ -81,6 +88,32 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
return NULL;
}
/* File locking */
rc = TREXIO_FAILURE;
switch (back_end) {
case TREXIO_TEXT:
rc = trexio_text_lock(result);
break;
case TREXIO_HDF5:
rc = TREXIO_SUCCESS;
break;
/*
case TREXIO_JSON:
rc = trexio_json_lock(result);
break;
*/
}
if (rc != TREXIO_SUCCESS) {
free(result->file_name);
free(result);
return NULL;
}
return result;
}
@ -90,6 +123,7 @@ trexio_exit_code trexio_close(trexio_t* file) {
trexio_exit_code rc;
/* Terminate the back end */
switch (file->back_end) {
case TREXIO_TEXT:
@ -108,16 +142,44 @@ trexio_exit_code trexio_close(trexio_t* file) {
assert (1 == 0); /* Impossible case */
}
if (rc != TREXIO_SUCCESS) {
free(file->file_name);
free(file);
return TREXIO_FAILURE;
}
/* File unlocking */
rc = TREXIO_FAILURE;
switch (file->back_end) {
case TREXIO_TEXT:
rc = trexio_text_unlock(file);
break;
case TREXIO_HDF5:
rc = TREXIO_SUCCESS;
break;
/*
case TREXIO_JSON:
rc = trexio_json_unlock(file);
break;
*/
}
/* Terminate front end */
free(file->file_name);
file->file_name = NULL;
int irc = pthread_mutex_destroy( &(file->thread_lock) );
assert (irc == 0);
free(file);
if (rc != TREXIO_SUCCESS) return rc;
free(file);
if (irc != 0) return TREXIO_ERRNO;
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
return TREXIO_SUCCESS;
}

View File

@ -21,6 +21,7 @@ typedef int32_t trexio_exit_code;
#define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 )
#define TREXIO_END ( (trexio_exit_code) 10 )
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 )

View File

@ -78,6 +78,7 @@ typedef int32_t trexio_exit_code;
#define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 )
#define TREXIO_END ( (trexio_exit_code) 10 )
#define TREXIO_READONLY ( (trexio_exit_code) 11 )
#define TREXIO_ERRNO ( (trexio_exit_code) 12 )
#define TREXIO_INVALID_ID ( (trexio_exit_code) 20 )
#define TREXIO_INVALID_NUM ( (trexio_exit_code) 21 )
#+end_src
@ -170,6 +171,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
trexio_t* result = NULL;
/* Allocate data structures */
switch (back_end) {
case TREXIO_TEXT:
@ -186,8 +188,10 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
,*/
}
/* TODO: Error handling */
assert (result != NULL);
assert (result != NULL); /* TODO: Error handling */
/* Data for the parent type */
result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char));
strcpy(result->file_name, file_name);
@ -196,7 +200,11 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
int irc = pthread_mutex_init ( &(result->thread_lock), NULL);
assert (irc == 0);
trexio_exit_code rc = TREXIO_FAILURE;
trexio_exit_code rc;
/* Back end initialization */
rc = TREXIO_FAILURE;
switch (back_end) {
@ -220,6 +228,32 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b
return NULL;
}
/* File locking */
rc = TREXIO_FAILURE;
switch (back_end) {
case TREXIO_TEXT:
rc = trexio_text_lock(result);
break;
case TREXIO_HDF5:
rc = TREXIO_SUCCESS;
break;
/*
case TREXIO_JSON:
rc = trexio_json_lock(result);
break;
*/
}
if (rc != TREXIO_SUCCESS) {
free(result->file_name);
free(result);
return NULL;
}
return result;
}
#+end_src
@ -237,6 +271,7 @@ trexio_exit_code trexio_close(trexio_t* file) {
trexio_exit_code rc;
/* Terminate the back end */
switch (file->back_end) {
case TREXIO_TEXT:
@ -255,17 +290,47 @@ trexio_exit_code trexio_close(trexio_t* file) {
assert (1 == 0); /* Impossible case */
}
if (rc != TREXIO_SUCCESS) {
free(file->file_name);
free(file);
return TREXIO_FAILURE;
}
/* File unlocking */
rc = TREXIO_FAILURE;
switch (file->back_end) {
case TREXIO_TEXT:
rc = trexio_text_unlock(file);
break;
case TREXIO_HDF5:
rc = TREXIO_SUCCESS;
break;
/*
case TREXIO_JSON:
rc = trexio_json_unlock(file);
break;
*/
}
/* Terminate front end */
free(file->file_name);
file->file_name = NULL;
int irc = pthread_mutex_destroy( &(file->thread_lock) );
assert (irc == 0);
free(file);
if (rc != TREXIO_SUCCESS) return rc;
free(file);
if (irc != 0) return TREXIO_ERRNO;
if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE;
return TREXIO_SUCCESS;
}
}
#+end_src
** Reading/writing data
@ -629,5 +694,4 @@ trexio_exit_code trexio_buffered_write_rdm_two_e(trexio_t* file, const int64_t o
- [ ] Error handling with errno
- [ ] HDF5 back-end
- [ ] JSON back-end
- [ ] File locking with flock
- [ ] Caching of the struct saving last modification date in structs

View File

@ -95,10 +95,10 @@ trexio_exit_code trexio_hdf5_init(trexio_t* file) {
switch (file->mode) {
case 'r':
case 'a':
// reading non-existing file -> error
// reading or appending non-existing file -> error
return TREXIO_FAILURE;
case 'w':
// appending or writing non-existing file -> create it
// writing non-existing file -> create it
f->file_id = H5Fcreate(file->file_name, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
break;
}
@ -107,10 +107,11 @@ trexio_exit_code trexio_hdf5_init(trexio_t* file) {
/* Create or open groups in the hdf5 file assuming that they exist if file exists */
switch (file->mode) {
// the switch for 'r'/'a' is reached only if file exists
case 'r':
case 'a':
f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT);
//f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT);
//f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT);
break;
case 'w':
f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
@ -370,7 +371,7 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6
if (nucleus->num != 0) {
printf("%ld -> %ld %s \n", num, nucleus->num,
"This variable alreasy exists. Overwriting it is not supported");
"This variable already exists. Overwriting it is not supported");
trexio_hdf5_free_nucleus(nucleus);
return TREXIO_FAILURE;
}

View File

@ -186,10 +186,10 @@ trexio_exit_code trexio_hdf5_init(trexio_t* file) {
switch (file->mode) {
case 'r':
case 'a':
// reading non-existing file -> error
// reading or appending non-existing file -> error
return TREXIO_FAILURE;
case 'w':
// appending or writing non-existing file -> create it
// writing non-existing file -> create it
f->file_id = H5Fcreate(file->file_name, H5F_ACC_EXCL, H5P_DEFAULT, H5P_DEFAULT);
break;
}
@ -198,10 +198,11 @@ trexio_exit_code trexio_hdf5_init(trexio_t* file) {
/* Create or open groups in the hdf5 file assuming that they exist if file exists */
switch (file->mode) {
// the switch for 'r'/'a' is reached only if file exists
case 'r':
case 'a':
f->nucleus_group = H5Gopen(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT);
//f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT);
//f->electron_group = H5Gopen(f->file_id, ELECTRON_GROUP_NAME, H5P_DEFAULT);
break;
case 'w':
f->nucleus_group = H5Gcreate(f->file_id, NUCLEUS_GROUP_NAME, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
@ -521,7 +522,7 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6
if (nucleus->num != 0) {
printf("%ld -> %ld %s \n", num, nucleus->num,
"This variable alreasy exists. Overwriting it is not supported");
"This variable already exists. Overwriting it is not supported");
trexio_hdf5_free_nucleus(nucleus);
return TREXIO_FAILURE;
}

View File

@ -38,7 +38,18 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
assert (f->lock_file > 0);
free(file_name);
/* Lock the directory for the current process */
f->nucleus = NULL;
f->electron= NULL;
f->rdm = NULL;
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_lock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
struct flock fl;
fl.l_type = F_WRLCK;
@ -50,20 +61,12 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
int rc = fcntl(f->lock_file, F_SETLKW, &fl);
if (rc == -1) return TREXIO_FAILURE;
trexio_text_t* f_child = (trexio_text_t*) file;
f_child->nucleus = NULL;
f_child->electron= NULL;
f_child->rdm = NULL;
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_finalize(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
trexio_exit_code rc;
rc = trexio_text_free_nucleus( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
@ -71,7 +74,14 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) {
rc = trexio_text_free_rdm( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
/* Release lock */
return TREXIO_SUCCESS;
}
trexio_exit_code trexio_text_unlock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
struct flock fl;
fl.l_type = F_WRLCK;

View File

@ -56,8 +56,12 @@ typedef struct trexio_text_s {
trexio_exit_code trexio_text_init(trexio_t* file);
trexio_exit_code trexio_text_lock(trexio_t* file);
trexio_exit_code trexio_text_finalize(trexio_t* file);
trexio_exit_code trexio_text_unlock(trexio_t* file);
nucleus_t* trexio_text_read_nucleus(trexio_text_t* file);
trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file);

View File

@ -137,7 +137,25 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
assert (f->lock_file > 0);
free(file_name);
/* Lock the directory for the current process */
f->nucleus = NULL;
f->electron= NULL;
f->rdm = NULL;
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle trexio_text.h
trexio_exit_code trexio_text_lock(trexio_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_lock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
struct flock fl;
fl.l_type = F_WRLCK;
@ -149,12 +167,6 @@ trexio_exit_code trexio_text_init(trexio_t* file) {
int rc = fcntl(f->lock_file, F_SETLKW, &fl);
if (rc == -1) return TREXIO_FAILURE;
trexio_text_t* f_child = (trexio_text_t*) file;
f_child->nucleus = NULL;
f_child->electron= NULL;
f_child->rdm = NULL;
return TREXIO_SUCCESS;
}
@ -169,8 +181,6 @@ trexio_exit_code trexio_text_finalize(trexio_t* file);
trexio_exit_code trexio_text_finalize(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
trexio_exit_code rc;
rc = trexio_text_free_nucleus( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
@ -178,7 +188,20 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) {
rc = trexio_text_free_rdm( (trexio_text_t*) file);
assert (rc == TREXIO_SUCCESS);
/* Release lock */
return TREXIO_SUCCESS;
}
#+end_src
#+begin_src c :tangle trexio_text.h
trexio_exit_code trexio_text_unlock(trexio_t* file);
#+end_src
#+begin_src c :tangle trexio_text.c
trexio_exit_code trexio_text_unlock(trexio_t* file) {
if (file == NULL) return TREXIO_INVALID_ARG_1;
trexio_text_t* f = (trexio_text_t*) file;
struct flock fl;
fl.l_type = F_WRLCK;