Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace linked list with array for var storage in netcdf-4 format #328

Merged
merged 7 commits into from
Nov 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions include/nc4internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ typedef struct NC_ATT_INFO
/* This is a struct to handle the var metadata. */
typedef struct NC_VAR_INFO
{
NC_LIST_NODE_T l; /* Use generic doubly-linked list (must be first) */
char *name;
char *hdf5_name; /* used if different from name */
int ndims;
Expand Down Expand Up @@ -262,6 +261,12 @@ typedef struct NC_TYPE_INFO
} u; /* Union of structs, for each type/class */
} NC_TYPE_INFO_T;

typedef struct NC_VAR_ARRAY_T {
size_t nalloc; /* number allocated >= nelems */
size_t nelems; /* length of the array */
NC_VAR_INFO_T **value;
} NC_VAR_ARRAY_T;

/* This holds information for one group. Groups reproduce with
* parthenogenesis. */
typedef struct NC_GRP_INFO
Expand All @@ -273,7 +278,7 @@ typedef struct NC_GRP_INFO
struct NC_HDF5_FILE_INFO *nc4_info;
struct NC_GRP_INFO *parent;
struct NC_GRP_INFO *children;
NC_VAR_INFO_T *var;
NC_VAR_ARRAY_T vars;
NC_DIM_INFO_T *dim;
NC_ATT_INFO_T *att;
NC_TYPE_INFO_T *type;
Expand Down Expand Up @@ -385,8 +390,8 @@ int nc4_type_free(NC_TYPE_INFO_T *type);

/* These list functions add and delete vars, atts. */
int nc4_nc4f_list_add(NC *nc, const char *path, int mode);
int nc4_var_list_add(NC_VAR_INFO_T **list, NC_VAR_INFO_T **var);
int nc4_var_list_del(NC_VAR_INFO_T **list, NC_VAR_INFO_T *var);
int nc4_var_add(NC_VAR_INFO_T **var);
int nc4_var_del(NC_VAR_INFO_T *var);
int nc4_dim_list_add(NC_DIM_INFO_T **list, NC_DIM_INFO_T **dim);
int nc4_dim_list_del(NC_DIM_INFO_T **list, NC_DIM_INFO_T *dim);
int nc4_att_list_add(NC_ATT_INFO_T **list, NC_ATT_INFO_T **att);
Expand Down
46 changes: 18 additions & 28 deletions libsrc4/nc4attr.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ nc4_get_att(int ncid, NC *nc, int varid, const char *name,
char norm_name[NC_MAX_NAME + 1];
int i;
int retval = NC_NOERR;
const char** sp;

if (attnum) {
my_attnum = *attnum;
Expand Down Expand Up @@ -261,14 +260,12 @@ nc4_put_att(int ncid, NC *nc, int varid, const char *name,
attlist = &grp->att;
else
{
for (var = grp->var; var; var = var->l.next)
if (var->varid == varid)
{
attlist = &var->att;
break;
}
if (!var)
return NC_ENOTVAR;
if (varid < 0 || varid >= grp->vars.nelems)
return NC_ENOTVAR;
var = grp->vars.value[varid];
if (!var) return NC_ENOTVAR;
attlist = &var->att;
assert(var->varid == varid);
}

for (att = *attlist; att; att = att->l.next)
Expand Down Expand Up @@ -364,7 +361,6 @@ nc4_put_att(int ncid, NC *nc, int varid, const char *name,
* attribute). */
if (!strcmp(att->name, _FillValue) && varid != NC_GLOBAL)
{
NC_ATT_INFO_T *varatt;
int size;

/* Fill value must be same type and have exactly one value */
Expand Down Expand Up @@ -666,14 +662,12 @@ NC4_rename_att(int ncid, int varid, const char *name,
}
else
{
for (var = grp->var; var; var = var->l.next)
if (var->varid == varid)
{
list = var->att;
break;
}
if (!var)
return NC_ENOTVAR;
if (varid < 0 || varid >= grp->vars.nelems)
return NC_ENOTVAR;
var = grp->vars.value[varid];
if (!var) return NC_ENOTVAR;
assert(var->varid == varid);
list = var->att;
}
for (att = list; att; att = att->l.next)
if (!strncmp(att->name, norm_newname, NC_MAX_NAME))
Expand Down Expand Up @@ -777,16 +771,12 @@ NC4_del_att(int ncid, int varid, const char *name)
}
else
{
for(var = grp->var; var; var = var->l.next)
{
if (var->varid == varid)
{
attlist = &var->att;
break;
}
}
if (!var)
return NC_ENOTVAR;
if (varid < 0 || varid >= grp->vars.nelems)
return NC_ENOTVAR;
var = grp->vars.value[varid];
if (!var) return NC_ENOTVAR;
attlist = &var->att;
assert(var->varid == varid);
if (var->created)
locid = var->hdf_datasetid;
}
Expand Down
25 changes: 17 additions & 8 deletions libsrc4/nc4file.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ COPYRIGHT file for copying and redistribution conditions.
#include "nc4internal.h"
#include "nc4dispatch.h"

extern int nc4_vararray_add(NC_GRP_INFO_T *grp,
NC_VAR_INFO_T *var);

/* must be after nc4internal.h */
#include <H5DSpublic.h>
#include <H5Fpublic.h>
Expand Down Expand Up @@ -506,7 +509,6 @@ NC4_create(const char* path, int cmode, size_t initialsz, int basepe,
MPI_Comm comm = MPI_COMM_WORLD;
MPI_Info info = MPI_INFO_NULL;
int res;
NC* nc;

assert(nc_file && path);

Expand Down Expand Up @@ -1534,7 +1536,7 @@ read_var(NC_GRP_INFO_T *grp, hid_t datasetid, const char *obj_name,
LOG((4, "%s: obj_name %s", __func__, obj_name));

/* Add a variable to the end of the group's var list. */
if ((retval = nc4_var_list_add(&grp->var, &var)))
if ((retval = nc4_var_add(&var)))
BAIL(retval);

/* Fill in what we already know. */
Expand Down Expand Up @@ -1810,6 +1812,8 @@ read_var(NC_GRP_INFO_T *grp, hid_t datasetid, const char *obj_name,
} /* endif not HDF5 att */
} /* next attribute */

nc4_vararray_add(grp, var);

/* Is this a deflated variable with a chunksize greater than the
* current cache size? */
if ((retval = nc4_adjust_var_cache(grp, var)))
Expand All @@ -1820,7 +1824,7 @@ read_var(NC_GRP_INFO_T *grp, hid_t datasetid, const char *obj_name,
{
if (incr_id_rc && H5Idec_ref(datasetid) < 0)
BAIL2(NC_EHDFERR);
if (var && nc4_var_list_del(&grp->var, var))
if (var && nc4_var_del(var))
BAIL2(NC_EHDFERR);
}
if (access_pid && H5Pclose(access_pid) < 0)
Expand Down Expand Up @@ -2590,14 +2594,16 @@ nc4_open_hdf4_file(const char *path, int mode, NC *nc)
size_t var_type_size;
int a;

/* Add a variable to the end of the group's var list. */
if ((retval = nc4_var_list_add(&grp->var, &var)))
/* Add a variable. */
if ((retval = nc4_var_add(&var)))
return retval;

var->varid = grp->nvars++;
var->created = NC_TRUE;
var->written_to = NC_TRUE;

nc4_vararray_add(grp, var);

/* Open this dataset in HDF4 file. */
if ((var->sdsid = SDselect(h5->sdid, v)) == FAIL)
return NC_EVARMETA;
Expand Down Expand Up @@ -3205,7 +3211,6 @@ NC4_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
NC_GRP_INFO_T *grp;
NC_DIM_INFO_T *dim;
NC_ATT_INFO_T *att;
NC_VAR_INFO_T *var;
int retval;

LOG((2, "%s: ncid 0x%x", __func__, ncid));
Expand All @@ -3225,9 +3230,13 @@ NC4_inq(int ncid, int *ndimsp, int *nvarsp, int *nattsp, int *unlimdimidp)
}
if (nvarsp)
{
int i;
*nvarsp = 0;
for (var = grp->var; var; var= var->l.next)
(*nvarsp)++;
for (i=0; i < grp->vars.nelems; i++)
{
if (grp->vars.value[i])
(*nvarsp)++;
}
}
if (nattsp)
{
Expand Down
14 changes: 7 additions & 7 deletions libsrc4/nc4grp.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ NC4_inq_varids(int ncid, int *nvars, int *varids)
NC_VAR_INFO_T *var;
int v, num_vars = 0;
int retval;
int i;

LOG((2, "nc_inq_varids: ncid 0x%x", ncid));

Expand All @@ -413,14 +414,13 @@ NC4_inq_varids(int ncid, int *nvars, int *varids)
{
/* This is a netCDF-4 group. Round up them doggies and count
* 'em. The list is in correct (i.e. creation) order. */
if (grp->var)
for (i=0; i < grp->vars.nelems; i++)
{
for (var = grp->var; var; var = var->l.next)
{
if (varids)
varids[num_vars] = var->varid;
num_vars++;
}
var = grp->vars.value[i];
if (!var) continue;
if (varids)
varids[num_vars] = var->varid;
num_vars++;
}
}

Expand Down
Loading