This commit is contained in:
Enrico Fucile 2016-11-01 14:38:01 +00:00
parent 04b3d2ece9
commit 3b354b205f
6 changed files with 108 additions and 24 deletions

View File

@ -779,7 +779,7 @@ grib_accessors_list* grib_accessors_list_create(grib_context* c)
return (grib_accessors_list*)grib_context_malloc_clear(c,sizeof(grib_accessors_list));
}
void grib_accessors_list_push(grib_accessors_list* al,grib_accessor* a)
void grib_accessors_list_push(grib_accessors_list* al,grib_accessor* a,int rank)
{
grib_accessors_list* last;
grib_context* c=a->context;
@ -789,9 +789,11 @@ void grib_accessors_list_push(grib_accessors_list* al,grib_accessor* a)
last->next=(grib_accessors_list*)grib_context_malloc_clear(c,sizeof(grib_accessors_list));
last->next->accessor=a;
last->next->prev=last;
last->next->rank=rank;
al->last=last->next;
} else {
al->accessor=a;
al->rank=rank;
al->last=al;
}
}

View File

@ -62,6 +62,8 @@
MEMBERS = int nInputShortReplications
MEMBERS = int iInputShortReplications
MEMBERS = grib_iarray* iss_list
MEMBERS = grib_trie* dataAccessorsTrie
MEMBERS = grib_trie* dataAccessorsRank
END_CLASS_DEF
@ -133,6 +135,8 @@ typedef struct grib_accessor_bufr_data_array {
int nInputShortReplications;
int iInputShortReplications;
grib_iarray* iss_list;
grib_trie* dataAccessorsTrie;
grib_trie* dataAccessorsRank;
} grib_accessor_bufr_data_array;
extern grib_accessor_class* grib_accessor_class_gen;
@ -287,6 +291,8 @@ static void init(grib_accessor* a,const long v, grib_arguments* params)
cancel_bitmap(self);
self->expanded=0;
self->expandedAccessor=0;
self->dataAccessorsTrie=0;
self->dataAccessorsRank=0;
a->length=0;
self->bitsToEndData=get_length(a)*8;
@ -386,6 +392,12 @@ grib_accessors_list* accessor_bufr_data_array_get_dataAccessors(grib_accessor* a
return self->dataAccessors;
}
grib_trie* accessor_bufr_data_array_get_dataAccessorsTrie(grib_accessor* a)
{
grib_accessor_bufr_data_array *self =(grib_accessor_bufr_data_array*)a;
return self->dataAccessorsTrie;
}
void accessor_bufr_data_array_set_unpackMode(grib_accessor* a,int unpackMode)
{
grib_accessor_bufr_data_array *self =(grib_accessor_bufr_data_array*)a;
@ -1780,10 +1792,29 @@ static grib_accessor* accessor_or_attribute_with_same_name(grib_accessor* a,cons
}
}
static int get_key_rank(grib_trie* accessorsRank,grib_accessor* a) {
int* r=(int*)grib_trie_get(accessorsRank,a->name);
if (r) (*r)++;
else {
r=grib_context_malloc(a->context,sizeof(int));
*r=1;
grib_trie_insert(accessorsRank,a->name,(void*)r);
}
return *r;
}
static void grib_data_accessors_trie_push(grib_trie* accessorsTrie,grib_accessor* a,int r) {
char* name=grib_context_malloc_clear(a->context,strlen(a->name)+20);
sprintf(name,"#%d#%s",r,a->name);
grib_trie_insert(accessorsTrie,name,a);
}
static int create_keys(grib_accessor* a,long onlySubset,long startSubset,long endSubset)
{
grib_accessor_bufr_data_array *self =(grib_accessor_bufr_data_array*)a;
int err=0;
int rank;
grib_accessor* elementAccessor=0;
grib_accessor* associatedFieldAccessor=0;
grib_accessor* associatedFieldSignificanceAccessor=0;
@ -1827,11 +1858,19 @@ static int create_keys(grib_accessor* a,long onlySubset,long startSubset,long en
if (self->dataAccessors) {
grib_accessors_list_delete(c,self->dataAccessors);
/* grib_empty_section ( c,self->dataKeys ); */
/* grib_context_free ( c,self->dataKeys->block ); */
}
self->dataAccessors=grib_accessors_list_create(c);
if (self->dataAccessorsTrie) {
grib_trie_delete(self->dataAccessorsTrie);
}
self->dataAccessorsTrie=grib_trie_new(c);
if (self->dataAccessorsRank) {
grib_trie_delete(self->dataAccessorsRank);
}
self->dataAccessorsRank=grib_trie_new(c);
end= self->compressedData ? 1 : self->numberOfSubsets;
groupNumber=1;
@ -1981,7 +2020,9 @@ static int create_keys(grib_accessor* a,long onlySubset,long startSubset,long en
grib_pack_long(asn,&subsetNumber,&len);
grib_push_accessor(asn,section->block);
grib_accessors_list_push(self->dataAccessors,asn);
rank=get_key_rank(self->dataAccessorsRank,asn);
grib_accessors_list_push(self->dataAccessors,asn,rank);
grib_data_accessors_trie_push(self->dataAccessorsTrie,asn,rank);
}
count++;
elementAccessor=create_accessor_from_descriptor(a,associatedFieldAccessor,section,ide,iss,dump,count);
@ -1996,7 +2037,9 @@ static int create_keys(grib_accessor* a,long onlySubset,long startSubset,long en
newAccessor->parent=groupSection;
newAccessor->name=grib_context_strdup(c,elementFromBitmap->name);
grib_push_accessor(newAccessor,groupSection->block);
grib_accessors_list_push(self->dataAccessors,newAccessor);
rank=get_key_rank(self->dataAccessorsRank,newAccessor);
grib_accessors_list_push(self->dataAccessors,newAccessor,rank);
grib_data_accessors_trie_push(self->dataAccessorsTrie,newAccessor,rank);
}
err=grib_accessor_add_attribute(accessor_or_attribute_with_same_name(elementFromBitmap,elementAccessor->name),elementAccessor,1);
@ -2022,8 +2065,10 @@ static int create_keys(grib_accessor* a,long onlySubset,long startSubset,long en
break;
default:
grib_push_accessor(elementAccessor,section->block);
grib_accessors_list_push(self->dataAccessors,elementAccessor);
rank=get_key_rank(self->dataAccessorsRank,elementAccessor);
grib_accessors_list_push(self->dataAccessors,elementAccessor,rank);
lastAccessorInList=grib_accessors_list_last(self->dataAccessors);
grib_data_accessors_trie_push(self->dataAccessorsTrie,elementAccessor,rank);
}
}
}
@ -2563,4 +2608,6 @@ static void destroy(grib_context* c,grib_accessor* a)
grib_accessor_bufr_data_array *self =(grib_accessor_bufr_data_array*)a;
self_clear(c,self);
if (self->dataAccessors) grib_accessors_list_delete(c,self->dataAccessors);
if (self->dataAccessorsTrie) grib_trie_delete_container(self->dataAccessorsTrie);
if (self->dataAccessorsRank) grib_trie_delete(self->dataAccessorsRank);
}

View File

@ -426,6 +426,7 @@ typedef struct grib_accessors_list grib_accessors_list;
struct grib_accessors_list {
grib_accessor* accessor;
int rank;
grib_accessors_list* next;
grib_accessors_list* prev;
grib_accessors_list* last;

View File

@ -153,7 +153,7 @@ grib_accessor *_grib_accessor_get_attribute(grib_accessor *a, const char *name,
int grib_accessor_has_attributes(grib_accessor *a);
grib_accessor *grib_accessor_get_attribute(grib_accessor *a, const char *name);
grib_accessors_list *grib_accessors_list_create(grib_context *c);
void grib_accessors_list_push(grib_accessors_list *al, grib_accessor *a);
void grib_accessors_list_push(grib_accessors_list *al, grib_accessor *a,int rank);
grib_accessors_list *grib_accessors_list_last(grib_accessors_list *al);
grib_accessors_list *grib_accessors_list_find(grib_accessors_list *al, grib_accessor *a);
void grib_accessors_list_delete(grib_context *c, grib_accessors_list *al);
@ -280,6 +280,7 @@ size_t grib_viarray_used_size(grib_viarray *v);
int accessor_bufr_data_array_create_keys(grib_accessor *a, long onlySubset, long startSubset, long endSubset);
int accessor_bufr_data_array_process_elements(grib_accessor *a, int flag, long onlySubset, long startSubset, long endSubset);
grib_vsarray *accessor_bufr_data_array_get_stringValues(grib_accessor *a);
grib_trie* accessor_bufr_data_array_get_dataAccessorsTrie(grib_accessor* a);
grib_accessors_list *accessor_bufr_data_array_get_dataAccessors(grib_accessor *a);
void accessor_bufr_data_array_set_unpackMode(grib_accessor *a, int unpackMode);
@ -1112,6 +1113,7 @@ int grib_count_in_file(grib_context *c, FILE *f, int *n);
/* grib_trie.c */
grib_trie *grib_trie_new(grib_context *c);
void grib_trie_delete(grib_trie *t);
void grib_trie_delete_container(grib_trie *t);
void grib_trie_clear(grib_trie *t);
void *grib_trie_insert(grib_trie *t, const char *key, void *data);
void *grib_trie_insert_no_replace(grib_trie *t, const char *key, void *data);
@ -1169,8 +1171,6 @@ grib_action *grib_parse_file(grib_context *gc, const char *filename);
int grib_type_to_int(char id);
/* grib_query.c */
char *get_rank(const char *name, long *rank);
char *get_condition(const char *name, codes_condition *condition);
grib_accessors_list *grib_find_accessors_list(grib_handle *h, const char *name);
char *grib_split_name_attribute(grib_context *c, const char *name, char *attribute_name);
grib_accessor *grib_find_accessor(grib_handle *h, const char *name);

View File

@ -133,7 +133,7 @@ static grib_accessor* _search_and_cache(grib_handle* h, const char* name,const c
}
}
char* get_rank(const char* name,long *rank) {
static char* get_rank(const char* name,int *rank) {
char* p=(char*)name;
char* end=p;
char* ret=NULL;
@ -153,7 +153,7 @@ char* get_rank(const char* name,long *rank) {
return ret;
}
char* get_condition(const char* name,codes_condition* condition)
static char* get_condition(const char* name,codes_condition* condition)
{
char* equal=(char*)name;
char* endCondition=NULL;
@ -201,6 +201,16 @@ char* get_condition(const char* name,codes_condition* condition)
return str;
}
static grib_accessor* _search_by_rank(grib_accessor* a,const char* name) {
long r=1;
grib_accessor* ret=NULL;
grib_trie* t=accessor_bufr_data_array_get_dataAccessorsTrie(a);
ret=(grib_accessor*)grib_trie_get(t,name);
return ret;
}
/*
static grib_accessor* _search_by_rank(grib_accessor* a,const char* name,long rank) {
long r=1;
grib_accessors_list* al=accessor_bufr_data_array_get_dataAccessors(a);
@ -215,14 +225,19 @@ static grib_accessor* _search_by_rank(grib_accessor* a,const char* name,long ran
return NULL;
}
*/
static grib_accessor* search_by_rank(grib_handle* h, const char* name,const char *the_namespace,long rank)
static grib_accessor* search_by_rank(grib_handle* h, const char* name,const char *the_namespace)
{
grib_accessor* data=search_and_cache(h,"dataAccessors",the_namespace);
if (data) {
return _search_by_rank(data,name,rank);
return _search_by_rank(data,name);
} else {
return _search_and_cache(h,name,the_namespace);
grib_accessor* ret=NULL;
int rank;
char* str=get_rank(name,&rank);
ret=_search_and_cache(h,str,the_namespace);
grib_context_free(h->context,str);
}
}
@ -265,7 +280,7 @@ static void search_from_accessors_list(grib_accessors_list* al,grib_accessors_li
accessor_result=al->accessor;
}
if (accessor_result) {
grib_accessors_list_push(result,accessor_result);
grib_accessors_list_push(result,accessor_result,al->rank);
}
}
al=al->next;
@ -278,7 +293,7 @@ static void search_from_accessors_list(grib_accessors_list* al,grib_accessors_li
accessor_result=al->accessor;
}
if (accessor_result) {
grib_accessors_list_push(result,accessor_result);
grib_accessors_list_push(result,accessor_result,al->rank);
}
}
}
@ -329,7 +344,7 @@ static void grib_find_same_and_push(grib_accessors_list* al,grib_accessor* a)
{
if (a) {
grib_find_same_and_push(al,a->same);
grib_accessors_list_push(al,a);
grib_accessors_list_push(al,a,al->rank);
}
}
@ -353,8 +368,12 @@ grib_accessors_list* grib_find_accessors_list(grib_handle* h,const char* name)
} else if (name[0]=='#') {
a=grib_find_accessor(h, name);
if (a) {
char* str;
int r;
al=(grib_accessors_list*)grib_context_malloc_clear(h->context,sizeof(grib_accessors_list));
grib_accessors_list_push(al,a);
str=get_rank(name,&r);
grib_accessors_list_push(al,a,r);
grib_context_free(h->context,str);
}
} else {
a=grib_find_accessor(h, name);
@ -369,14 +388,11 @@ grib_accessors_list* grib_find_accessors_list(grib_handle* h,const char* name)
static grib_accessor* search_and_cache(grib_handle* h, const char* name,const char *the_namespace)
{
char* str=0;
grib_accessor* a=NULL;
long rank;
if (name[0]=='#') {
str=get_rank(name,&rank);
a=search_by_rank(h,str,the_namespace,rank);
grib_context_free(h->context,str);
a=search_by_rank(h,name,the_namespace);
} else {
a=_search_and_cache(h,name,the_namespace);
}

View File

@ -47,7 +47,7 @@ static int mapping[] = {
0, /* 20 */
0, /* 21 */
0, /* 22 */
0, /* 23 */
38, /* # */
0, /* 24 */
0, /* 25 */
0, /* 26 */
@ -270,7 +270,7 @@ static int mapping[] = {
0, /* ff */
};
#define SIZE 38
#define SIZE 39
#if GRIB_PTHREADS
static pthread_once_t once = PTHREAD_ONCE_INIT;
@ -320,6 +320,24 @@ grib_trie *grib_trie_new(grib_context* c) {
return t;
}
void grib_trie_delete_container(grib_trie *t) {
GRIB_MUTEX_INIT_ONCE(&once,&init)
GRIB_MUTEX_LOCK(&mutex)
if(t) {
int i;
for(i = t->first; i <= t->last; i++)
if (t->next[i]) {
grib_trie_delete_container(t->next[i]);
}
#ifdef RECYCLE_TRIE
grib_context_free_persistent(t->context,t);
#else
grib_context_free(t->context,t);
#endif
}
GRIB_MUTEX_UNLOCK(&mutex)
}
void grib_trie_delete(grib_trie *t) {
GRIB_MUTEX_INIT_ONCE(&once,&init)
GRIB_MUTEX_LOCK(&mutex)