netcdf-c/libnczarr/zmap_nz4.c
Dennis Heimbigner 59e04ae071 This PR adds EXPERIMENTAL support for accessing data in the
cloud using a variant of the Zarr protocol and storage
format. This enhancement is generically referred to as "NCZarr".

The data model supported by NCZarr is netcdf-4 minus the user-defined
types and the String type. In this sense it is similar to the CDF-5
data model.

More detailed information about enabling and using NCZarr is
described in the document NUG/nczarr.md and in a
[Unidata Developer's blog entry](https://www.unidata.ucar.edu/blogs/developer/en/entry/overview-of-zarr-support-in).

WARNING: this code has had limited testing, so do use this version
for production work. Also, performance improvements are ongoing.
Note especially the following platform matrix of successful tests:

Platform | Build System | S3 support
------------------------------------
Linux+gcc      | Automake     | yes
Linux+gcc      | CMake        | yes
Visual Studio  | CMake        | no

Additionally, and as a consequence of the addition of NCZarr,
major changes have been made to the Filter API. NOTE: NCZarr
does not yet support filters, but these changes are enablers for
that support in the future.  Note that it is possible
(probable?) that there will be some accidental reversions if the
changes here did not correctly mimic the existing filter testing.

In any case, previously filter ids and parameters were of type
unsigned int. In order to support the more general zarr filter
model, this was all converted to char*.  The old HDF5-specific,
unsigned int operations are still supported but they are
wrappers around the new, char* based nc_filterx_XXX functions.
This entailed at least the following changes:
1. Added the files libdispatch/dfilterx.c and include/ncfilter.h
2. Some filterx utilities have been moved to libdispatch/daux.c
3. A new entry, "filter_actions" was added to the NCDispatch table
   and the version bumped.
4. An overly complex set of structs was created to support funnelling
   all of the filterx operations thru a single dispatch
   "filter_actions" entry.
5. Move common code to from libhdf5 to libsrc4 so that it is accessible
   to nczarr.

Changes directly related to Zarr:
1. Modified CMakeList.txt and configure.ac to support both C and C++
   -- this is in support of S3 support via the awd-sdk libraries.
2. Define a size64_t type to support nczarr.
3. More reworking of libdispatch/dinfermodel.c to
   support zarr and to regularize the structure of the fragments
   section of a URL.

Changes not directly related to Zarr:
1. Make client-side filter registration be conditional, with default off.
2. Hack include/nc4internal.h to make some flags added by Ed be unique:
   e.g. NC_CREAT, NC_INDEF, etc.
3. cleanup include/nchttp.h and libdispatch/dhttp.c.
4. Misc. changes to support compiling under Visual Studio including:
   * Better testing under windows for dirent.h and opendir and closedir.
5. Misc. changes to the oc2 code to support various libcurl CURLOPT flags
   and to centralize error reporting.
6. By default, suppress the vlen tests that have unfixed memory leaks; add option to enable them.
7. Make part of the nc_test/test_byterange.sh test be contingent on remotetest.unidata.ucar.edu being accessible.

Changes Left TO-DO:
1. fix provenance code, it is too HDF5 specific.
2020-06-28 18:02:47 -06:00

641 lines
16 KiB
C

/*
* Copyright 2018, University Corporation for Atmospheric Research
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
*/
#include "zincludes.h"
#include "fbits.h"
#include "ncwinpath.h"
/*
Do a simple mapping of our simplified map model
to a netcdf-4 file.
For the object API, the mapping is as follows:
1. Every object (e.g. group or array) is mapped to a netcdf-4 group.
2. Object content is kept as a ubyte typed variable with one
unlimited dimension
*/
#undef DEBUG
#define NCZM_NC4_V1 1
/* What to replace ZDOT with */
#define ZDOTNC4 '_'
/* define the attr/var name containing an objects content */
#define ZCONTENT "data"
/* Define the dimension for the ZCONTENT variable */
/* Avoid creating a coordinate variable */
#define ZCONTENTDIM "data_dim"
/* Define the "subclass" of NCZMAP */
typedef struct Z4MAP {
NCZMAP map;
char* path;
int ncid;
} Z4MAP;
/* Forward */
static NCZMAP_API zapi;
static int znc4close(NCZMAP* map, int delete);
static int zlookupgroup(Z4MAP*, NClist* segments, int nskip, int* grpidp);
static int zlookupobj(Z4MAP*, NClist* segments, int* objidp);
static int zcreategroup(Z4MAP* z4map, NClist* segments, int nskip, int* grpidp);
static int zcreateobj(Z4MAP*, NClist* segments, int* objidp);
static int zcreatedim(Z4MAP*, int, int* dimidp);
static int parseurl(const char* path0, NCURI** urip);
static void nc4ify(const char* zname, char* nc4name);
static void zify(const char* nc4name, char* zname);
static int z4getcwd(char** cwdp);
static int testcontentbearing(int grpid);
/* Define the Dataset level API */
static int
znc4create(const char *path, int mode, size64_t flags, void* parameters, NCZMAP** mapp)
{
int stat = NC_NOERR;
char* truepath = NULL;
Z4MAP* z4map = NULL;
int ncid;
NCURI* url = NULL;
char* z4cwd = NULL;
if((stat=parseurl(path,&url)))
goto done;
/* Fix up mode */
mode = (NC_NETCDF4 | NC_WRITE | mode);
if(flags & FLAG_BYTERANGE)
mode &= ~(NC_CLOBBER | NC_WRITE);
if(!(mode & NC_WRITE))
{stat = NC_EPERM; goto done;}
/* Get cwd so we can use absolute paths */
if((stat = z4getcwd(&z4cwd))) goto done;
if(flags & FLAG_BYTERANGE)
truepath = ncuribuild(url,NULL,NULL,NCURIALL);
else {
/* Make the root path be absolute */
if(!nczm_isabsolutepath(url->path)) {
if((stat = nczm_concat(z4cwd,url->path,&truepath))) goto done;
} else
truepath = strdup(url->path);
}
/* Build the z4 state */
if((z4map = calloc(1,sizeof(Z4MAP))) == NULL)
{stat = NC_ENOMEM; goto done;}
z4map->map.format = NCZM_NC4;
z4map->map.url = ncuribuild(url,NULL,NULL,NCURIALL);
z4map->map.mode = mode;
z4map->map.flags = flags;
z4map->map.api = &zapi;
z4map->path = truepath;
truepath = NULL;
if((stat=nc_create(z4map->path,mode,&ncid)))
{stat = NC_EEMPTY; goto done;} /* could not open */
z4map->ncid = ncid;
if(mapp) *mapp = (NCZMAP*)z4map;
done:
ncurifree(url);
nullfree(truepath);
nullfree(z4cwd);
if(stat) znc4close((NCZMAP*)z4map,1);
return (stat);
}
static int
znc4open(const char *path, int mode, size64_t flags, void* parameters, NCZMAP** mapp)
{
int stat = NC_NOERR;
char* truepath = NULL;
Z4MAP* z4map = NULL;
int ncid;
NCURI* url = NULL;
char* z4cwd = NULL;
/* Fixup mode */
mode = NC_NETCDF4 | mode;
if(flags & FLAG_BYTERANGE)
mode &= ~(NC_CLOBBER | NC_WRITE);
if((stat=parseurl(path,&url)))
goto done;
/* Get cwd so we can use absolute paths */
if((stat = z4getcwd(&z4cwd))) goto done;
if(flags & FLAG_BYTERANGE)
truepath = ncuribuild(url,NULL,NULL,NCURIALL);
else {
/* Make the root path be absolute */
if(!nczm_isabsolutepath(url->path)) {
if((stat = nczm_concat(z4cwd,url->path,&truepath))) goto done;
} else
truepath = strdup(url->path);
}
/* Build the z4 state */
if((z4map = calloc(1,sizeof(Z4MAP))) == NULL)
{stat = NC_ENOMEM; goto done;}
z4map->map.format = NCZM_NC4;
z4map->map.url = ncuribuild(url,NULL,NULL,NCURIALL);
z4map->map.mode = mode;
z4map->map.flags = flags;
z4map->map.api = (NCZMAP_API*)&zapi;
z4map->path = truepath;
truepath = NULL;
if((stat=nc_open(z4map->path,mode,&ncid)))
{stat = NC_EEMPTY; goto done;} /* could not open */
z4map->ncid = ncid;
if(mapp) *mapp = (NCZMAP*)z4map;
done:
nullfree(truepath);
nullfree(z4cwd);
ncurifree(url);
if(stat) znc4close((NCZMAP*)z4map,0);
return (stat);
}
/**************************************************/
/* Object API */
static int
znc4close(NCZMAP* map, int delete)
{
int stat = NC_NOERR;
Z4MAP* z4map = (Z4MAP*)map;
char* path = NULL;
if(map == NULL) return NC_NOERR;
path = z4map->path;
if((stat = nc_close(z4map->ncid)))
goto done;
if(delete) {
if((stat = nc_delete(path)))
goto done;
}
done:
nullfree(z4map->path);
nczm_clear(map);
free(z4map);
return (stat);
}
static int
znc4exists(NCZMAP* map, const char* key)
{
int stat = NC_NOERR;
Z4MAP* z4map = (Z4MAP*)map;
NClist* segments = nclistnew();
int grpid;
if((stat=nczm_split(key,segments)))
goto done;
switch(stat=zlookupobj(z4map,segments,&grpid)) {
case NC_NOERR: break;
case NC_ENOTFOUND: stat = NC_EEMPTY; /* Does not exist */
case NC_EEMPTY: /* Not an object */
default: break; /* other error */
}
done:
nclistfreeall(segments);
return (stat);
}
static int
znc4len(NCZMAP* map, const char* key, size64_t* lenp)
{
int stat = NC_NOERR;
Z4MAP* z4map = (Z4MAP*)map;
NClist* segments = nclistnew();
int grpid, vid;
size_t dimlen;
int dimids[1];
if((stat=nczm_split(key,segments)))
goto done;
switch(stat=zlookupobj(z4map,segments,&grpid)) {
case NC_NOERR:
/* Look for the data variable */
if((stat = nc_inq_varid(grpid,ZCONTENT,&vid))) goto done;
/* Get size for this variable */
if((stat = nc_inq_vardimid(grpid,vid,dimids))) goto done;
/* Get size of the one and only dim */
if((stat = nc_inq_dimlen(z4map->ncid,dimids[0],&dimlen))) goto done;
if(lenp) *lenp = (size64_t)dimlen;
break;
case NC_ENOTFOUND: stat = NC_EEMPTY; /* does not exist */
case NC_EEMPTY: /* Not an object */
if(lenp) *lenp = 0;
break;
default: break;
}
done:
nclistfreeall(segments);
return (stat);
}
static int
znc4defineobj(NCZMAP* map, const char* key)
{
int stat = NC_NOERR;
int grpid;
Z4MAP* z4map = (Z4MAP*)map; /* cast to true type */
NClist* segments = nclistnew();
if((stat=nczm_split(key,segments)))
goto done;
switch (stat = zlookupobj(z4map,segments,&grpid)) {
case NC_NOERR: break; /* already exists */
case NC_ENOTFOUND: stat = NC_EEMPTY;
case NC_EEMPTY:
if((stat = zcreateobj(z4map,segments,&grpid))) goto done;
break;
default: break; /* other error */
}
done:
nclistfreeall(segments);
return (stat);
}
static int
znc4read(NCZMAP* map, const char* key, size64_t start, size64_t count, void* content)
{
int stat = NC_NOERR;
int grpid,vid;
Z4MAP* z4map = (Z4MAP*)map; /* cast to true type */
size_t vstart[1];
size_t vcount[1];
NClist* segments = nclistnew();
if((stat=nczm_split(key,segments)))
goto done;
switch (stat = zlookupobj(z4map,segments,&grpid)) {
case NC_NOERR: /* exists */
/* Look for the data variable */
if((stat = nc_inq_varid(grpid,ZCONTENT,&vid))) goto done;
vstart[0] = (size_t)start;
vcount[0] = (size_t)count;
if((stat = nc_get_vara(grpid,vid,vstart,vcount,content))) goto done;
break;
case NC_ENOTFOUND: stat = NC_EEMPTY;
case NC_EEMPTY: break; /* no data */
default: break; /* other error */
}
done:
nclistfreeall(segments);
return (stat);
}
static int
znc4write(NCZMAP* map, const char* key, size64_t start, size64_t count, const void* content)
{
int stat = NC_NOERR;
int grpid,vid;
Z4MAP* z4map = (Z4MAP*)map; /* cast to true type */
size_t vstart[1];
size_t vcount[1];
NClist* segments = nclistnew();
if((stat=nczm_split(key,segments)))
goto done;
switch (stat = zlookupobj(z4map,segments,&grpid)) {
case NC_NOERR: /* exists */
/* Look for the data variable */
if((stat = nc_inq_varid(grpid,ZCONTENT,&vid))) goto done;
vstart[0] = (size_t)start;
vcount[0] = (size_t)count;
if((stat = nc_put_vara(grpid,vid,vstart,vcount,content))) goto done;
break;
case NC_ENOTFOUND: stat = NC_EEMPTY;
case NC_EEMPTY: break; /* no data */
default: break; /* other error */
}
done:
nclistfreeall(segments);
return (stat);
}
/*
Return a list of keys immediately "below" a specified prefix.
In theory, the returned list should be sorted in lexical order,
but it is not.
*/
int
znc4search(NCZMAP* map, const char* prefix, NClist* matches)
{
int stat = NC_NOERR;
Z4MAP* z4map = (Z4MAP*)map;
NClist* segments = nclistnew();
int grpid, ngrps;
int* subgrps = NULL;
int* vars = NULL;
int i;
NCbytes* key = ncbytesnew();
int trailing = 0;
if((stat=nczm_split(prefix,segments)))
goto done;
if(nclistlength(segments) > 0) {
/* Fix the last name */
size_t pos = nclistlength(segments)-1;
char* name = nclistget(segments,pos);
char zname[NC_MAX_NAME];
zify(name,zname);
nclistset(segments,pos,strdup(zname));
nullfree(name);
}
#ifdef DEBUG
{
int i;
fprintf(stderr,"segments: %d: ",nclistlength(segments));
for(i=0;i<nclistlength(segments);i++)
fprintf(stderr," |%s|",(char*)nclistget(segments,i));
}
fprintf(stderr,"\n");
#endif
/* Get grpid of the group for the prefix */
if((stat = zlookupgroup(z4map,segments,0,&grpid)))
goto done;
/* get subgroup ids */
if((stat = nc_inq_grps(grpid,&ngrps,NULL)))
goto done;
if((subgrps = calloc(1,sizeof(int)*ngrps)) == NULL)
{stat = NC_ENOMEM; goto done;}
if((stat = nc_inq_grps(grpid,&ngrps,subgrps)))
goto done;
/* Add the subgroup keys to the list of matches (zified) */
trailing = (prefix[strlen(prefix)-1] == '/'); /* does prefix end with '/' */
for(i=0;i<ngrps;i++) {
char gname[NC_MAX_NAME];
char zname[NC_MAX_NAME];
/* See if this group is content-bearing */
if((stat = nc_inq_grpname(subgrps[i],gname))) goto done;
zify(gname,zname);
ncbytescat(key,prefix);
if(!trailing) ncbytescat(key,"/");
ncbytescat(key,zname);
nclistpush(matches,ncbytesextract(key));
}
done:
ncbytesfree(key);
nullfree(vars);
nullfree(subgrps);
nclistfreeall(segments);
return stat;
}
/**************************************************/
/* Utilities */
static int
testcontentbearing(int grpid)
{
int stat = NC_NOERR;
int varid;
/* See if there is a content variable */
switch (stat = nc_inq_varid(grpid,ZCONTENT,&varid)) {
default: goto done; /* true error */
case NC_NOERR: /* This is a data bearing object */
return NC_NOERR;
case NC_ENOTVAR:
return NC_EEMPTY;
}
done:
return stat;
}
/* Lookup a group by parsed path (segments)*/
/* Return NC_ENOTFUND if not found */
static int
zlookupgroup(Z4MAP* z4map, NClist* segments, int nskip, int* grpidp)
{
int stat = NC_NOERR;
int i, len, grpid;
len = nclistlength(segments);
len += nskip; /* leave off last nskip segments */
grpid = z4map->ncid;
for(i=0;i<len;i++) {
int grpid2;
const char* seg = nclistget(segments,i);
char nc4name[NC_MAX_NAME];
nc4ify(seg,nc4name);
if((stat=nc_inq_grp_ncid(grpid,nc4name,&grpid2)))
{stat = NC_ENOTFOUND; goto done;}
grpid = grpid2;
}
/* ok, so grpid should be it */
if(grpidp) *grpidp = grpid;
done:
return (stat);
}
/* Lookup an object.
@return NC_NOERR if found and is a content-bearing object
@return NC_EEMPTY if exists but is not-content-bearing
@return NC_ENOTFOUND if not found
*/
static int
zlookupobj(Z4MAP* z4map, NClist* segments, int* grpidp)
{
int stat = NC_NOERR;
int grpid;
/* Lookup thru the final object group */
if((stat = zlookupgroup(z4map,segments,0,&grpid))) {
goto done;
}
/* See if this is content-bearing */
if((stat = testcontentbearing(grpid)))
goto done;
if(grpidp) *grpidp = grpid;
done:
return (stat);
}
/* Create a group; assume all intermediate groups exist
(do nothing if it already exists) */
static int
zcreategroup(Z4MAP* z4map, NClist* segments, int nskip, int* grpidp)
{
int stat = NC_NOERR;
int i, len, grpid, grpid2;
const char* gname = NULL;
char nc4name[NC_MAX_NAME];
len = nclistlength(segments);
len -= nskip; /* leave off last nskip segments (assume nskip > 0) */
gname = nclistget(segments,len-1);
grpid = z4map->ncid;
/* Do all but last group */
for(i=0;i<(len-1);i++) {
const char* seg = nclistget(segments,i);
nc4ify(seg,nc4name);
/* Does this group exist? */
if((stat=nc_inq_grp_ncid(grpid,nc4name,&grpid2)) == NC_ENOGRP) {
{stat = NC_ENOTFOUND; goto done;} /* missing intermediate */
}
grpid = grpid2;
}
/* Check status of last group */
nc4ify(gname,nc4name);
if((stat = nc_inq_grp_ncid(grpid,nc4name,&grpid2))) {
if(stat != NC_ENOGRP) goto done;
if((stat = nc_def_grp(grpid,nc4name,&grpid2)))
goto done;
grpid = grpid2;
}
if(grpidp) *grpidp = grpid;
done:
return (stat);
}
static int
zcreatedim(Z4MAP* z4map, int grpid, int* dimidp)
{
int stat = NC_NOERR;
int dimid;
NC_UNUSED(z4map);
if((stat=nc_inq_dimid(grpid,ZCONTENTDIM,&dimid))) {
/* create it */
if((stat=nc_def_dim(grpid,ZCONTENTDIM,NC_UNLIMITED,&dimid)))
goto done;
}
if(dimidp) *dimidp = dimid;
done:
return (stat);
}
/* Create an object group corresponding to a key; create any
necessary intermediates.
*/
static int
zcreateobj(Z4MAP* z4map, NClist* segments, int* grpidp)
{
int skip,stat = NC_NOERR;
int grpid, varid;
int dimid[1];
/* Create the whole path */
skip = nclistlength(segments);
for(skip--;skip >= 0; skip--) {
if((stat = zcreategroup(z4map,segments,skip,&grpid)))
goto done;
}
/* Last grpid should be one we want */
if(grpidp) *grpidp = grpid;
/* Create the content-bearer */
/* Create the corresponding dimension */
if((stat = zcreatedim(z4map,grpid,&dimid[0])))
goto done;
/* Create the variable */
if((stat=nc_def_var(grpid, ZCONTENT, NC_UBYTE, 1, dimid, &varid)))
goto done;
done:
return (stat);
}
static int
parseurl(const char* path0, NCURI** urip)
{
int stat = NC_NOERR;
NCURI* uri = NULL;
ncuriparse(path0,&uri);
if(uri == NULL)
{stat = NC_EURL; goto done;}
if(urip) {*urip = uri; uri = NULL;}
done:
ncurifree(uri);
return stat;
}
/* Convert _z... name to .z... name */
static void
zify(const char* nc4name, char* zname)
{
zname[0] = '\0';
strlcat(zname,nc4name,NC_MAX_NAME);
if(zname[0] == ZDOTNC4) zname[0] = NCZM_DOT;
}
/* Convert .z... name to _z... name */
static void
nc4ify(const char* zname, char* nc4name)
{
nc4name[0] = '\0';
strlcat(nc4name,zname,NC_MAX_NAME);
if(nc4name[0] == NCZM_DOT) nc4name[0] = ZDOTNC4;
}
static int
z4getcwd(char** cwdp)
{
char buf[4096];
char* cwd = NULL;
cwd = NCcwd(buf,sizeof(buf));
if(cwd == NULL) return errno;
if(cwdp) *cwdp = strdup(buf);
return NC_NOERR;
}
/**************************************************/
/* External API objects */
NCZMAP_DS_API zmap_nz4 = {
NCZM_NC4_V1,
znc4create,
znc4open,
};
static NCZMAP_API zapi = {
NCZM_NC4_V1,
znc4close,
znc4exists,
znc4len,
znc4defineobj,
znc4read,
znc4write,
znc4search,
};