GRIB-432: Problem with pooled file cleanup

This commit is contained in:
Shahram Najm 2013-09-20 16:27:50 +01:00
parent 341bfc14b4
commit b06096e8fe
2 changed files with 1198 additions and 1166 deletions

View File

@ -33,7 +33,8 @@ static void init() {
static short next_id=0;
GRIB_INLINE static int grib_inline_strcmp(const char* a,const char* b) {
GRIB_INLINE static int grib_inline_strcmp(const char* a,const char* b)
{
if (*a != *b) return 1;
while((*a!=0 && *b!=0) && *(a) == *(b) ) {a++;b++;}
return (*a==0 && *b==0) ? 0 : 1;
@ -369,4 +370,3 @@ void grib_file_delete(grib_file* file)
file=NULL;
GRIB_MUTEX_UNLOCK(&mutex1);
}

View File

@ -10,7 +10,7 @@
/*
*
* Description: index
* Description: grib index
*
*/
@ -51,7 +51,8 @@ static const char* mars_keys =
static int grib_filesid=0;
static int index_count;
static char* get_key(char** keys,int *type) {
static char* get_key(char** keys,int *type)
{
char* key=NULL;
char* p=NULL;
@ -74,7 +75,8 @@ static char* get_key(char** keys,int *type) {
return key;
}
static int compare_long(const void* a,const void* b) {
static int compare_long(const void* a,const void* b)
{
long* arg1 = (long*) a;
long* arg2 = (long*) b;
if( *arg1 == *arg2 ) return 0;
@ -82,17 +84,17 @@ static int compare_long(const void* a,const void* b) {
return *arg1 < *arg2 ? -1 : 1;
}
static int compare_double(const void* a,const void* b) {
static int compare_double(const void* a,const void* b)
{
double* arg1 = (double*) a;
double* arg2 = (double*) b;
if( *arg1 == *arg2 ) return 0;
return *arg1 < *arg2 ? -1 : 1;
}
static int compare_string(const void* a,const void* b) {
static int compare_string(const void* a,const void* b)
{
char* arg1 = *(char* const*) a;
char* arg2 = *(char* const*) b;
@ -103,8 +105,8 @@ static int compare_string(const void* a,const void* b) {
return *arg1 < *arg2 ? -1 : 1;
}
static int grib_index_keys_compress(grib_context* c,grib_index* index,int* compress) {
static int grib_index_keys_compress(grib_context* c,grib_index* index,int* compress)
{
grib_index_key *keys=index->keys->next;
grib_index_key *prev=index->keys;
int level=0;
@ -140,8 +142,8 @@ static int grib_index_keys_compress(grib_context* c,grib_index* index,int* compr
}
static int grib_index_fields_compress(grib_context* c,
grib_field_tree* fields,grib_field_tree* prev,int level,int* compress) {
grib_field_tree* fields,grib_field_tree* prev,int level,int* compress)
{
if (!fields) return 0;
if (!prev) {
@ -173,7 +175,8 @@ static int grib_index_fields_compress(grib_context* c,
return 0;
}
int grib_index_compress(grib_index* index) {
int grib_index_compress(grib_index* index)
{
int err=0;
grib_context* c=index->context;
int compress[200]={0,};
@ -195,7 +198,8 @@ int grib_index_compress(grib_index* index) {
}
static grib_index_key* grib_index_new_key(grib_context* c,grib_index_key* keys,
const char* key,int type,int *err) {
const char* key,int type,int *err)
{
grib_index_key *next=NULL,*current=NULL;
grib_string_list* values=NULL;
@ -233,7 +237,8 @@ static grib_index_key* grib_index_new_key(grib_context* c,grib_index_key* keys,
return keys;
}
int grib_read_uchar( FILE* fh,unsigned char *val) {
int grib_read_uchar( FILE* fh,unsigned char *val)
{
if (fread(val,sizeof(unsigned char),1,fh) <1) {
if (feof(fh)) return GRIB_END_OF_FILE;
else return GRIB_IO_PROBLEM;
@ -241,7 +246,8 @@ int grib_read_uchar( FILE* fh,unsigned char *val) {
return GRIB_SUCCESS;
}
int grib_read_short( FILE* fh,short *val) {
int grib_read_short( FILE* fh,short *val)
{
if (fread(val,sizeof(short),1,fh) <1) {
if (feof(fh)) return GRIB_END_OF_FILE;
else return GRIB_IO_PROBLEM;
@ -249,7 +255,8 @@ int grib_read_short( FILE* fh,short *val) {
return GRIB_SUCCESS;
}
int grib_read_long( FILE* fh,long *val) {
int grib_read_long( FILE* fh,long *val)
{
if (fread(val,sizeof(long),1,fh) <1) {
if (feof(fh)) return GRIB_END_OF_FILE;
else return GRIB_IO_PROBLEM;
@ -257,7 +264,8 @@ int grib_read_long( FILE* fh,long *val) {
return GRIB_SUCCESS;
}
int grib_read_unsigned_long( FILE* fh,unsigned long *val) {
int grib_read_unsigned_long( FILE* fh,unsigned long *val)
{
if (fread(val,sizeof(long),1,fh) <1) {
if (feof(fh)) return GRIB_END_OF_FILE;
else return GRIB_IO_PROBLEM;
@ -265,25 +273,29 @@ int grib_read_unsigned_long( FILE* fh,unsigned long *val) {
return GRIB_SUCCESS;
}
int grib_write_uchar(FILE* fh,unsigned char val) {
int grib_write_uchar(FILE* fh,unsigned char val)
{
if (fwrite(&val,sizeof(unsigned char),1,fh)<1)
return GRIB_IO_PROBLEM;
return GRIB_SUCCESS;
}
int grib_write_short(FILE* fh,short val) {
int grib_write_short(FILE* fh,short val)
{
if (fwrite(&val,sizeof(short),1,fh)<1)
return GRIB_IO_PROBLEM;
return GRIB_SUCCESS;
}
int grib_write_long(FILE* fh,long val) {
int grib_write_long(FILE* fh,long val)
{
if (fwrite(&val,sizeof(long),1,fh)<1)
return GRIB_IO_PROBLEM;
return GRIB_SUCCESS;
}
int grib_write_unsigned_long(FILE* fh,unsigned long val) {
int grib_write_unsigned_long(FILE* fh,unsigned long val)
{
if (fwrite(&val,sizeof(long),1,fh)<1)
return GRIB_IO_PROBLEM;
return GRIB_SUCCESS;
@ -298,20 +310,23 @@ int grib_write_string(FILE* fh,const char* s)
return GRIB_SUCCESS;
}
int grib_write_identifier(FILE* fh) {
int grib_write_identifier(FILE* fh)
{
return grib_write_string(fh,"GRBIDX1");
}
int grib_write_null_marker(FILE* fh) {
int grib_write_null_marker(FILE* fh)
{
return grib_write_uchar(fh,NULL_MARKER);
}
int grib_write_not_null_marker(FILE* fh) {
int grib_write_not_null_marker(FILE* fh)
{
return grib_write_uchar(fh,NOT_NULL_MARKER);
}
char *grib_read_string(grib_context* c,FILE* fh,int *err) {
char *grib_read_string(grib_context* c,FILE* fh,int *err)
{
unsigned char len=0;
char* s=NULL;
*err = grib_read_uchar(fh,&len);
@ -329,7 +344,8 @@ char *grib_read_string(grib_context* c,FILE* fh,int *err) {
return s;
}
static int grib_write_field(FILE* fh,grib_field* field) {
static int grib_write_field(FILE* fh,grib_field* field)
{
int err;
if (!field)
return grib_write_null_marker(fh);
@ -352,8 +368,8 @@ static int grib_write_field(FILE* fh,grib_field* field) {
return GRIB_SUCCESS;
}
static grib_field* grib_read_field(grib_context* c,FILE* fh,grib_file** files,int *err) {
static grib_field* grib_read_field(grib_context* c, FILE* fh, grib_file** files, int *err)
{
grib_field* field=NULL;
short file_id;
unsigned char marker ;
@ -384,8 +400,8 @@ static grib_field* grib_read_field(grib_context* c,FILE* fh,grib_file** files,in
return field;
}
static int grib_write_field_tree(FILE* fh,grib_field_tree* tree) {
static int grib_write_field_tree(FILE* fh,grib_field_tree* tree)
{
int err=0;
if(!tree)
@ -408,9 +424,8 @@ static int grib_write_field_tree(FILE* fh,grib_field_tree* tree) {
return GRIB_SUCCESS;
}
grib_field_tree* grib_read_field_tree(grib_context* c, FILE* fh,
grib_file** files,int *err) {
grib_field_tree* grib_read_field_tree(grib_context* c, FILE* fh, grib_file** files, int *err)
{
grib_field_tree* tree=NULL;
unsigned char marker=0;
*err = grib_read_uchar(fh,&marker);
@ -434,8 +449,8 @@ grib_field_tree* grib_read_field_tree(grib_context* c, FILE* fh,
return tree;
}
grib_index* grib_index_new(grib_context* c,const char* key,int *err) {
grib_index* grib_index_new(grib_context* c,const char* key,int *err)
{
grib_index* index;
grib_index_key* keys=NULL;
char* q;
@ -472,7 +487,8 @@ grib_index* grib_index_new(grib_context* c,const char* key,int *err) {
return index;
}
static void grib_index_values_delete(grib_context* c,grib_string_list* values) {
static void grib_index_values_delete(grib_context* c,grib_string_list* values)
{
if (!values) return;
@ -483,7 +499,8 @@ static void grib_index_values_delete(grib_context* c,grib_string_list* values) {
return ;
}
static void grib_index_key_delete(grib_context* c,grib_index_key* keys) {
static void grib_index_key_delete(grib_context* c,grib_index_key* keys)
{
if (!keys) return;
grib_index_key_delete(c,keys->next);
@ -496,9 +513,9 @@ static void grib_index_key_delete(grib_context* c,grib_index_key* keys) {
return;
}
static long values_count=0;
static grib_string_list* grib_read_key_values(grib_context* c,FILE* fh,int *err) {
static grib_string_list* grib_read_key_values(grib_context* c,FILE* fh,int *err)
{
grib_string_list* values;
unsigned char marker=0;
@ -518,8 +535,8 @@ static grib_string_list* grib_read_key_values(grib_context* c,FILE* fh,int *err)
return values;
}
static int grib_write_key_values(FILE* fh,grib_string_list* values) {
static int grib_write_key_values(FILE* fh,grib_string_list* values)
{
int err=0;
if (!values)
@ -536,8 +553,8 @@ static int grib_write_key_values(FILE* fh,grib_string_list* values) {
return GRIB_SUCCESS;
}
static grib_index_key* grib_read_index_keys(grib_context* c,FILE* fh,int *err) {
static grib_index_key* grib_read_index_keys(grib_context* c,FILE* fh,int *err)
{
grib_index_key* keys=NULL;
unsigned char marker=0;
unsigned char type=0;
@ -568,10 +585,10 @@ static grib_index_key* grib_read_index_keys(grib_context* c,FILE* fh,int *err) {
if (*err) return NULL;
return keys;
}
static int grib_write_index_keys(FILE* fh,grib_index_key* keys) {
static int grib_write_index_keys(FILE* fh,grib_index_key* keys)
{
int err=0;
if (!keys)
@ -594,7 +611,8 @@ static int grib_write_index_keys(FILE* fh,grib_index_key* keys) {
return GRIB_SUCCESS;
}
static void grib_field_delete(grib_context* c,grib_field* field) {
static void grib_field_delete(grib_context* c,grib_field* field)
{
int err=0;
if (!field) return;
@ -610,8 +628,8 @@ static void grib_field_delete(grib_context* c,grib_field* field) {
}
static void grib_field_tree_delete(grib_context* c,grib_field_tree* tree) {
static void grib_field_tree_delete(grib_context* c,grib_field_tree* tree)
{
if(!tree) return;
grib_field_delete(c,tree->field);
@ -626,7 +644,8 @@ static void grib_field_tree_delete(grib_context* c,grib_field_tree* tree) {
return;
}
void grib_index_delete(grib_index* index) {
void grib_index_delete(grib_index* index)
{
grib_file* file=index->files;
grib_index_key_delete(index->context,index->keys);
grib_field_tree_delete(index->context,index->fields);
@ -638,7 +657,8 @@ void grib_index_delete(grib_index* index) {
grib_context_free(index->context,index);
}
static int grib_write_files(FILE* fh,grib_file* files) {
static int grib_write_files(FILE* fh,grib_file* files)
{
int err;
if (!files)
return grib_write_null_marker(fh);
@ -655,7 +675,8 @@ static int grib_write_files(FILE* fh,grib_file* files) {
return grib_write_files(fh,files->next);
}
static grib_file* grib_read_files(grib_context *c,FILE* fh,int *err) {
static grib_file* grib_read_files(grib_context *c, FILE* fh, int *err)
{
unsigned char marker=0;
short id=0;
grib_file* file;
@ -677,8 +698,8 @@ static grib_file* grib_read_files(grib_context *c,FILE* fh,int *err) {
return file;
}
int grib_index_write(grib_index* index,const char* filename) {
int grib_index_write(grib_index* index,const char* filename)
{
int err=0;
FILE* fh;
grib_file* files;
@ -740,7 +761,8 @@ int grib_index_write(grib_index* index,const char* filename) {
return err;
}
grib_index* grib_index_read(grib_context* c,const char* filename,int *err) {
grib_index* grib_index_read(grib_context* c, const char* filename, int *err)
{
grib_file *file,*f;
grib_file** files;
grib_index* index=NULL;
@ -811,7 +833,8 @@ grib_index* grib_index_read(grib_context* c,const char* filename,int *err) {
return index;
}
int grib_index_search_same(grib_index* index,grib_handle* h) {
int grib_index_search_same(grib_index* index, grib_handle* h)
{
int err=0;
char buf[1024]={0,};
size_t buflen=1024;
@ -863,7 +886,8 @@ int grib_index_search_same(grib_index* index,grib_handle* h) {
return 0;
}
int grib_index_add_file(grib_index* index,const char* filename) {
int grib_index_add_file(grib_index* index, const char* filename)
{
double dval;
size_t svallen;
long length,lval;
@ -903,7 +927,7 @@ int grib_index_add_file(grib_index* index,const char* filename) {
grib_filesid++;
newfile=grib_context_malloc_clear(c,sizeof(grib_file));
newfile->id=grib_filesid;
newfile->name=file->name;
newfile->name=strdup(file->name);
indfile->next=newfile;
}
@ -1020,11 +1044,10 @@ int grib_index_add_file(grib_index* index,const char* filename) {
if (err) return err;
index->rewind=1;
return GRIB_SUCCESS;
}
grib_index* grib_index_new_from_file(grib_context* c,
char* filename,const char* keys,int *err) {
grib_index* grib_index_new_from_file(grib_context* c, char* filename, const char* keys, int *err)
{
grib_index* index=NULL;
if(!c) c=grib_context_get_default();
@ -1040,7 +1063,8 @@ grib_index* grib_index_new_from_file(grib_context* c,
return index;
}
int grib_index_get_size(grib_index* index,const char* key,size_t* size) {
int grib_index_get_size(grib_index* index,const char* key,size_t* size)
{
grib_index_key* k=index->keys;
while (k && strcmp(k->name,key)) k=k->next;
if (!k) return GRIB_NOT_FOUND;
@ -1048,8 +1072,8 @@ int grib_index_get_size(grib_index* index,const char* key,size_t* size) {
return 0;
}
int grib_index_get_string(grib_index* index,const char* key,
char** values,size_t *size) {
int grib_index_get_string(grib_index* index, const char* key, char** values, size_t *size)
{
grib_index_key* k=index->keys;
grib_string_list* kv;
int i=0;
@ -1067,8 +1091,8 @@ int grib_index_get_string(grib_index* index,const char* key,
return GRIB_SUCCESS;
}
int grib_index_get_long(grib_index* index,const char* key,
long* values,size_t *size) {
int grib_index_get_long(grib_index* index, const char* key, long* values, size_t *size)
{
grib_index_key* k=index->keys;
grib_string_list* kv;
int i=0;
@ -1094,8 +1118,8 @@ int grib_index_get_long(grib_index* index,const char* key,
return GRIB_SUCCESS;
}
int grib_index_get_double(grib_index* index,const char* key,
double* values,size_t *size) {
int grib_index_get_double(grib_index* index,const char* key, double* values,size_t *size)
{
grib_index_key* k=index->keys;
grib_string_list* kv;
int i=0;
@ -1122,7 +1146,8 @@ int grib_index_get_double(grib_index* index,const char* key,
return GRIB_SUCCESS;
}
int grib_index_select_long(grib_index* index,const char* skey,long value) {
int grib_index_select_long(grib_index* index,const char* skey,long value)
{
grib_index_key* key=NULL;
int err=GRIB_NOT_FOUND;
@ -1153,7 +1178,8 @@ int grib_index_select_long(grib_index* index,const char* skey,long value) {
return 0;
}
int grib_index_select_double(grib_index* index,const char* skey,double value) {
int grib_index_select_double(grib_index* index,const char* skey,double value)
{
grib_index_key* key=NULL;
int err=GRIB_NOT_FOUND;
@ -1184,7 +1210,8 @@ int grib_index_select_double(grib_index* index,const char* skey,double value) {
return 0;
}
int grib_index_select_string(grib_index* index,const char* skey,char* value) {
int grib_index_select_string(grib_index* index,const char* skey,char* value)
{
grib_index_key* key=NULL;
int err=GRIB_NOT_FOUND;
@ -1215,7 +1242,8 @@ int grib_index_select_string(grib_index* index,const char* skey,char* value) {
return 0;
}
grib_handle* grib_index_get_handle(grib_field* field,int *err) {
grib_handle* grib_index_get_handle(grib_field* field,int *err)
{
grib_handle* h=NULL;
grib_file_open(field->file->name,"r",err);
@ -1229,7 +1257,8 @@ grib_handle* grib_index_get_handle(grib_field* field,int *err) {
return h;
}
static int grib_index_execute(grib_index* index) {
static int grib_index_execute(grib_index* index)
{
grib_index_key* keys=index->keys;
grib_field_tree* fields;
@ -1266,11 +1295,13 @@ static int grib_index_execute(grib_index* index) {
return 0;
}
void grib_index_dump(grib_index* index) {
void grib_index_dump(grib_index* index)
{
}
char* grib_get_field_file(grib_index* index,off_t *offset) {
char* grib_get_field_file(grib_index* index,off_t *offset)
{
char* file=NULL;
if (index && index->current && index->current->field) {
file=index->current->field->file->name;
@ -1279,7 +1310,8 @@ char* grib_get_field_file(grib_index* index,off_t *offset) {
return file;
}
grib_handle* grib_handle_new_from_index(grib_index* index,int *err) {
grib_handle* grib_handle_new_from_index(grib_index* index,int *err)
{
grib_index_key* keys;
grib_field_list *fieldset,*next;
grib_handle* h=NULL;
@ -1337,20 +1369,21 @@ grib_handle* grib_handle_new_from_index(grib_index* index,int *err) {
h=grib_index_get_handle(index->current->field,err);
}
return h;
}
void grib_index_rewind(grib_index* index) {
void grib_index_rewind(grib_index* index)
{
index->rewind=1;
}
static grib_index_key* search_key(grib_index_key* keys,grib_index_key* to_search) {
static grib_index_key* search_key(grib_index_key* keys,grib_index_key* to_search)
{
if (!keys || !strcmp(keys->name,to_search->name)) return keys;
return search_key(keys->next,to_search);
}
int grib_index_search(grib_index* index,grib_index_key* keys) {
int grib_index_search(grib_index* index,grib_index_key* keys)
{
grib_index_key* ki=index->keys;
grib_index_key* ks=keys;
@ -1367,4 +1400,3 @@ int grib_index_search(grib_index* index,grib_index_key* keys) {
grib_index_rewind(index);
return 0;
}