netcdf-c/libdap2/constraints.c

872 lines
25 KiB
C
Raw Normal View History

2011-04-18 02:56:10 +08:00
/*********************************************************************
* Copyright 1993, UCAR/Unidata
* See netcdf/COPYRIGHT filey for copying and redistribution conditions.
2011-04-18 02:56:10 +08:00
*********************************************************************/
#include "ncdap.h"
2013-02-26 12:31:06 +08:00
#include "dapdump.h"
#include "dceparselex.h"
2011-04-18 02:56:10 +08:00
static void completesegments(NClist* fullpath, NClist* segments);
static NCerror qualifyprojectionnames(DCEprojection* proj);
static NCerror qualifyprojectionsizes(DCEprojection* proj);
static NCerror qualifyprojectionnames(DCEprojection* proj);
static NCerror matchpartialname(NClist* nodes, NClist* segments, CDFnode** nodep);
static int matchsuffix(NClist* matchpath, NClist* segments);
2011-11-14 12:20:19 +08:00
static int iscontainer(CDFnode* node);
static DCEprojection* projectify(CDFnode* field, DCEprojection* container);
static int slicematch(NClist* seglist1, NClist* seglist2);
2011-04-18 02:56:10 +08:00
/* Parse incoming url constraints, if any,
2015-01-21 02:00:03 +08:00
to check for syntactic correctness */
2011-04-18 02:56:10 +08:00
NCerror
dapparsedapconstraints(NCDAPCOMMON* dapcomm, char* constraints,
2011-04-18 02:56:10 +08:00
DCEconstraint* dceconstraint)
{
NCerror ncstat = NC_NOERR;
2015-01-21 02:00:03 +08:00
char* errmsg = NULL;
2011-04-18 02:56:10 +08:00
ASSERT(dceconstraint != NULL);
nclistclear(dceconstraint->projections);
nclistclear(dceconstraint->selections);
ncstat = dapceparse(constraints,dceconstraint,&errmsg);
if(ncstat) {
2015-01-21 02:00:03 +08:00
nclog(NCLOGWARN,"DCE constraint parse failure: %s",errmsg);
nclistclear(dceconstraint->projections);
nclistclear(dceconstraint->selections);
2011-04-18 02:56:10 +08:00
}
2015-01-21 02:00:03 +08:00
/* errmsg is freed whether ncstat or not. */
nullfree(errmsg);
2011-04-18 02:56:10 +08:00
return ncstat;
}
2011-11-14 12:20:19 +08:00
/* Map constraint paths to CDFnode paths in specified tree and fill
in the declsizes.
2012-08-16 01:55:25 +08:00
Two things to watch out for:
1. suffix paths are legal (i.e. incomplete paths)
2. nc_virtual nodes (via restruct)
2011-04-18 02:56:10 +08:00
*/
NCerror
dapmapconstraints(DCEconstraint* constraint,
2011-09-16 00:57:16 +08:00
CDFnode* root)
2011-04-18 02:56:10 +08:00
{
int i;
NCerror ncstat = NC_NOERR;
NClist* nodes = root->tree->nodes;
NClist* dceprojections = constraint->projections;
2011-04-18 02:56:10 +08:00
/* Convert the projection paths to leaves in the dds tree */
for(i=0;i<nclistlength(dceprojections);i++) {
CDFnode* cdfmatch = NULL;
2011-04-18 02:56:10 +08:00
DCEprojection* proj = (DCEprojection*)nclistget(dceprojections,i);
if(proj->discrim != CES_VAR) continue; /* ignore functions */
ncstat = matchpartialname(nodes,proj->var->segments,&cdfmatch);
2011-04-18 02:56:10 +08:00
if(ncstat) goto done;
/* Cross links */
assert(cdfmatch != NULL);
proj->var->annotation = (void*)cdfmatch;
2011-04-18 02:56:10 +08:00
}
done:
return THROW(ncstat);
}
2011-11-14 12:20:19 +08:00
2011-04-18 02:56:10 +08:00
/* Fill in:
1. projection segments
2. projection segment slices declsize
3. selection path
*/
2011-11-14 12:20:19 +08:00
NCerror
dapqualifyconstraints(DCEconstraint* constraint)
2011-04-18 02:56:10 +08:00
{
NCerror ncstat = NC_NOERR;
int i;
2011-11-14 12:20:19 +08:00
#ifdef DEBUG
fprintf(stderr,"ncqualifyconstraints.before: %s\n",
2011-11-14 12:20:19 +08:00
dumpconstraint(constraint));
#endif
2011-04-18 02:56:10 +08:00
if(constraint != NULL) {
2015-01-21 02:00:03 +08:00
for(i=0;i<nclistlength(constraint->projections);i++) {
2011-04-18 02:56:10 +08:00
DCEprojection* p = (DCEprojection*)nclistget(constraint->projections,i);
ncstat = qualifyprojectionnames(p);
ncstat = qualifyprojectionsizes(p);
2011-04-18 02:56:10 +08:00
}
}
2011-11-14 12:20:19 +08:00
#ifdef DEBUG
fprintf(stderr,"ncqualifyconstraints.after: %s\n",
2011-11-14 12:20:19 +08:00
dumpconstraint(constraint));
#endif
2011-04-18 02:56:10 +08:00
return ncstat;
}
/* convert all names in projections in paths to be fully qualified
2011-11-14 12:20:19 +08:00
by adding prefix segment objects.
2011-04-18 02:56:10 +08:00
*/
static NCerror
qualifyprojectionnames(DCEprojection* proj)
2011-04-18 02:56:10 +08:00
{
NCerror ncstat = NC_NOERR;
NClist* fullpath = nclistnew();
ASSERT((proj->discrim == CES_VAR
&& proj->var->annotation != NULL
&& ((CDFnode*)proj->var->annotation)->ocnode != NULL));
collectnodepath((CDFnode*)proj->var->annotation,fullpath,!WITHDATASET);
2011-04-18 02:56:10 +08:00
#ifdef DEBUG
fprintf(stderr,"qualify: %s -> ",
dumpprojection(proj));
#endif
/* Now add path nodes to create full path */
completesegments(fullpath,proj->var->segments);
2011-04-18 02:56:10 +08:00
#ifdef DEBUG
fprintf(stderr,"%s\n",
dumpprojection(proj));
#endif
nclistfree(fullpath);
return ncstat;
}
/* Make sure that the slice declsizes are all defined for this projection */
static NCerror
qualifyprojectionsizes(DCEprojection* proj)
2011-04-18 02:56:10 +08:00
{
int i,j;
ASSERT(proj->discrim == CES_VAR);
2011-11-14 12:20:19 +08:00
#ifdef DEBUG
fprintf(stderr,"qualifyprojectionsizes.before: %s\n",
dumpprojection(proj));
#endif
2011-04-18 02:56:10 +08:00
for(i=0;i<nclistlength(proj->var->segments);i++) {
DCEsegment* seg = (DCEsegment*)nclistget(proj->var->segments,i);
NClist* dimset = NULL;
CDFnode* cdfnode = (CDFnode*)seg->annotation;
ASSERT(cdfnode != NULL);
dimset = cdfnode->array.dimsetplus;
2011-11-14 12:20:19 +08:00
seg->rank = nclistlength(dimset);
/* For this, we do not want any string dimensions */
if(cdfnode->array.stringdim != NULL) seg->rank--;
2011-11-14 12:20:19 +08:00
for(j=0;j<seg->rank;j++) {
2011-04-18 02:56:10 +08:00
CDFnode* dim = (CDFnode*)nclistget(dimset,j);
2011-11-14 12:20:19 +08:00
if(dim->dim.basedim != NULL) dim = dim->dim.basedim;
ASSERT(dim != null);
if(seg->slicesdefined)
seg->slices[j].declsize = dim->dim.declsize;
else
dcemakewholeslice(seg->slices+j,dim->dim.declsize);
2011-04-18 02:56:10 +08:00
}
2011-11-14 12:20:19 +08:00
seg->slicesdefined = 1;
seg->slicesdeclized = 1;
2011-04-18 02:56:10 +08:00
}
2011-11-14 12:20:19 +08:00
#ifdef DEBUG
fprintf(stderr,"qualifyprojectionsizes.after: %s\n",
dumpprojection(proj));
#endif
2011-04-18 02:56:10 +08:00
return NC_NOERR;
}
2011-11-14 12:20:19 +08:00
static void
completesegments(NClist* fullpath, NClist* segments)
2011-11-14 12:20:19 +08:00
{
int i,delta;
/* add path nodes to segments to create full path */
delta = (nclistlength(fullpath) - nclistlength(segments));
ASSERT((delta >= 0));
for(i=0;i<delta;i++) {
DCEsegment* seg = (DCEsegment*)dcecreate(CES_SEGMENT);
CDFnode* node = (CDFnode*)nclistget(fullpath,i);
seg->name = nulldup(node->ocname);
seg->annotation = (void*)node;
2011-11-14 12:20:19 +08:00
seg->rank = nclistlength(node->array.dimset0);
nclistinsert(segments,i,(void*)seg);
2011-11-14 12:20:19 +08:00
}
/* Now modify the segments to point to the appropriate node
and fill in the slices.
*/
for(i=delta;i<nclistlength(segments);i++) {
DCEsegment* seg = (DCEsegment*)nclistget(segments,i);
CDFnode* node = (CDFnode*)nclistget(fullpath,i);
seg->annotation = (void*)node;
2011-11-14 12:20:19 +08:00
}
}
2011-04-18 02:56:10 +08:00
/*
We are given a set of segments (in path)
2011-11-14 12:20:19 +08:00
representing a partial path for a CDFnode variable.
2011-04-18 02:56:10 +08:00
Our goal is to locate all matching
2011-11-14 12:20:19 +08:00
variables for which the path of that
2011-04-18 02:56:10 +08:00
variable has a suffix matching
the given partial path.
If one node matches exactly, then use that one;
otherwise there had better be exactly one
match else ambiguous.
Additional constraints (4/12/2010):
1. if a segment is dimensioned, then use that info
to distinguish e.g a grid node from a possible
grid array within it of the same name.
Treat sequences as of rank 1.
2. if there are two matches, and one is the grid
and the other is the grid array within that grid,
then choose the grid array.
3. If there are multiple matches choose the one with the
shortest path
4. otherwise complain about ambiguity
*/
2011-11-14 12:20:19 +08:00
/**
* Given a path as segments,
* try to locate the CDFnode
* instance (from a given set)
* that corresponds to the path.
* The key difficulty is that the
* path may only be a suffix of the
* complete path.
*/
2011-04-18 02:56:10 +08:00
static NCerror
matchpartialname(NClist* nodes, NClist* segments, CDFnode** nodep)
2011-04-18 02:56:10 +08:00
{
2011-11-14 12:20:19 +08:00
int i,nsegs;
2011-04-18 02:56:10 +08:00
NCerror ncstat = NC_NOERR;
DCEsegment* lastseg = NULL;
NClist* namematches = nclistnew();
NClist* matches = nclistnew();
NClist* matchpath = nclistnew();
/* Locate all nodes with the same name
2011-11-14 12:20:19 +08:00
as the last element in the segment path
2011-04-18 02:56:10 +08:00
*/
nsegs = nclistlength(segments);
lastseg = (DCEsegment*)nclistget(segments,nsegs-1);
for(i=0;i<nclistlength(nodes);i++) {
CDFnode* node = (CDFnode*)nclistget(nodes,i);
2011-11-14 12:20:19 +08:00
if(node->ocname == null)
continue;
/* Path names come from oc space */
if(strcmp(node->ocname,lastseg->name) != 0)
2011-09-16 00:57:16 +08:00
continue;
2011-11-14 12:20:19 +08:00
/* Only look at selected kinds of nodes */
if(node->nctype != NC_Sequence
2011-04-18 02:56:10 +08:00
&& node->nctype != NC_Structure
&& node->nctype != NC_Grid
&& node->nctype != NC_Atomic
2011-11-14 12:20:19 +08:00
)
continue;
nclistpush(namematches,(void*)node);
2015-01-21 02:00:03 +08:00
}
2011-04-18 02:56:10 +08:00
if(nclistlength(namematches)==0) {
nclog(NCLOGERR,"No match for projection name: %s",lastseg->name);
ncstat = NC_EDDS;
goto done;
}
/* Now, collect and compare paths of the matching nodes */
for(i=0;i<nclistlength(namematches);i++) {
CDFnode* matchnode = (CDFnode*)nclistget(namematches,i);
nclistclear(matchpath);
collectnodepath(matchnode,matchpath,0);
2011-04-18 02:56:10 +08:00
/* Do a suffix match */
if(matchsuffix(matchpath,segments)) {
nclistpush(matches,(void*)matchnode);
2011-11-14 12:20:19 +08:00
#ifdef DEBUG
fprintf(stderr,"matchpartialname: pathmatch: %s :: %s\n",
matchnode->ncfullname,dumpsegments(segments));
#endif
2011-04-18 02:56:10 +08:00
}
}
/* |matches|==0 => no match; |matches|>1 => ambiguity */
switch (nclistlength(matches)) {
case 0:
nclog(NCLOGERR,"No match for projection name: %s",lastseg->name);
ncstat = NC_EDDS;
break;
case 1:
if(nodep)
*nodep = (CDFnode*)nclistget(matches,0);
break;
default: {
CDFnode* minnode = NULL;
int minpath = 0;
int nmin = 0; /* to catch multiple ones with same short path */
/* ok, see if one of the matches has a path that is shorter
then all the others */
for(i=0;i<nclistlength(matches);i++) {
CDFnode* candidate = (CDFnode*)nclistget(matches,i);
nclistclear(matchpath);
collectnodepath(candidate,matchpath,0);
2011-04-18 02:56:10 +08:00
if(minpath == 0) {
minpath = nclistlength(matchpath);
minnode = candidate;
} else if(nclistlength(matchpath) == minpath) {
2015-01-21 02:00:03 +08:00
nmin++;
2011-04-18 02:56:10 +08:00
} else if(nclistlength(matchpath) < minpath) {
minpath = nclistlength(matchpath);
minnode = candidate;
nmin = 1;
}
} /*for*/
2015-01-21 02:00:03 +08:00
if(minnode == NULL || nmin > 1) {
2011-04-18 02:56:10 +08:00
nclog(NCLOGERR,"Ambiguous match for projection name: %s",
lastseg->name);
ncstat = NC_EDDS;
} else if(nodep)
*nodep = minnode;
} break;
}
#ifdef DEBUG
2011-11-14 12:20:19 +08:00
fprintf(stderr,"matchpartialname: choice: %s %s for %s\n",
(nclistlength(matches) > 1?"":"forced"),
(*nodep)->ncfullname,dumpsegments(segments));
2011-04-18 02:56:10 +08:00
#endif
done:
2014-04-05 11:04:28 +08:00
nclistfree(namematches);
nclistfree(matches);
nclistfree(matchpath);
2011-11-14 12:20:19 +08:00
return THROW(ncstat);
2011-04-18 02:56:10 +08:00
}
static int
matchsuffix(NClist* matchpath, NClist* segments)
2011-04-18 02:56:10 +08:00
{
2012-08-16 01:55:25 +08:00
int i,pathstart;
2011-11-14 12:20:19 +08:00
int nsegs = nclistlength(segments);
int pathlen = nclistlength(matchpath);
2012-08-16 01:55:25 +08:00
int segmatch;
/* try to match the segment list as a suffix of the path list */
/* Find the maximal point in the path s.t. |suffix of path|
== |segments|
*/
pathstart = (pathlen - nsegs);
if(pathstart < 0)
return 0; /* pathlen <nsegs => no match possible */
/* Walk the suffix of the path and the segments and
matching as we go
*/
for(i=0;i<nsegs;i++) {
CDFnode* node = (CDFnode*)nclistget(matchpath,pathstart+i);
DCEsegment* seg = (DCEsegment*)nclistget(segments,i);
int rank = seg->rank;
segmatch = 1; /* until proven otherwise */
/* Do the names match (in oc name space) */
if(strcmp(seg->name,node->ocname) != 0) {
segmatch = 0;
} else {
2011-11-14 12:20:19 +08:00
/* Do the ranks match (watch out for sequences) */
2012-08-16 01:55:25 +08:00
if(node->nctype == NC_Sequence)
rank--; /* remove sequence pseudo-rank */
if(rank > 0
&& rank != nclistlength(node->array.dimset0))
segmatch = 0; /* rank mismatch */
2011-04-18 02:56:10 +08:00
}
2012-08-16 01:55:25 +08:00
if(!segmatch)
return 0;
2011-11-14 12:20:19 +08:00
}
2012-08-16 01:55:25 +08:00
return 1; /* all segs matched */
2011-04-18 02:56:10 +08:00
}
2011-11-14 12:20:19 +08:00
/* Given the arguments to vara
construct a corresponding projection
with any pseudo dimensions removed
2011-04-18 02:56:10 +08:00
*/
NCerror
dapbuildvaraprojection(CDFnode* var,
2011-04-18 02:56:10 +08:00
const size_t* startp, const size_t* countp, const ptrdiff_t* stridep,
DCEprojection** projectionp)
{
int i,j;
NCerror ncstat = NC_NOERR;
DCEprojection* projection = NULL;
NClist* path = nclistnew();
NClist* segments = NULL;
int dimindex;
/* Build a skeleton projection that has 1 segment for
every cdfnode from root to the variable of interest.
Each segment has the slices from its corresponding node
in the path, including pseudo-dims
*/
2011-11-14 12:20:19 +08:00
ncstat = dapvar2projection(var,&projection);
2011-11-14 12:20:19 +08:00
#ifdef DEBUG
fprintf(stderr,"buildvaraprojection: skeleton: %s\n",dumpprojection(projection));
2011-11-14 12:20:19 +08:00
#endif
2011-04-18 02:56:10 +08:00
/* Now, modify the projection to reflect the corresponding
start/count/stride from the nc_get_vara arguments.
2011-11-14 12:20:19 +08:00
*/
segments = projection->var->segments;
dimindex = 0;
2011-04-18 02:56:10 +08:00
for(i=0;i<nclistlength(segments);i++) {
DCEsegment* segment = (DCEsegment*)nclistget(segments,i);
2011-11-14 12:20:19 +08:00
for(j=0;j<segment->rank;j++) {
2013-04-18 02:58:37 +08:00
size_t count = 0;
2011-04-18 02:56:10 +08:00
DCEslice* slice = &segment->slices[j];
/* make each slice represent the corresponding
start/count/stride */
2011-04-18 02:56:10 +08:00
slice->first = startp[dimindex+j];
slice->stride = stridep[dimindex+j];
2013-04-18 02:58:37 +08:00
count = countp[dimindex+j];
slice->count = count;
slice->length = count * slice->stride;
slice->last = (slice->first + slice->length) - 1;
if(slice->last >= slice->declsize) {
slice->last = slice->declsize - 1;
2012-08-16 01:55:25 +08:00
/* reverse compute the new length */
slice->length = (slice->last - slice->first) + 1;
2012-08-16 01:55:25 +08:00
}
2011-04-18 02:56:10 +08:00
}
2011-11-14 12:20:19 +08:00
dimindex += segment->rank;
2011-04-18 02:56:10 +08:00
}
2011-11-14 12:20:19 +08:00
#ifdef DEBUG
fprintf(stderr,"buildvaraprojection.final: %s\n",dumpprojection(projection));
#endif
2011-04-18 02:56:10 +08:00
#ifdef DEBUG
fprintf(stderr,"buildvaraprojection3: final: projection=%s\n",
dumpprojection(projection));
#endif
2011-04-18 02:56:10 +08:00
if(projectionp) *projectionp = projection;
nclistfree(path);
if(ncstat) dcefree((DCEnode*)projection);
return ncstat;
}
int
dapiswholeslice(DCEslice* slice, CDFnode* dim)
2011-04-18 02:56:10 +08:00
{
if(slice->first != 0 || slice->stride != 1) return 0;
if(dim != NULL) {
if(slice->length != dim->dim.declsize) return 0;
2011-04-18 02:56:10 +08:00
} else if(dim == NULL) {
2013-04-18 02:58:37 +08:00
size_t count = slice->count;
2011-04-18 02:56:10 +08:00
if(slice->declsize == 0
2013-04-18 02:58:37 +08:00
|| count != slice->declsize) return 0;
2011-04-18 02:56:10 +08:00
}
return 1;
}
int
dapiswholesegment(DCEsegment* seg)
2011-04-18 02:56:10 +08:00
{
int i,whole;
NClist* dimset = NULL;
unsigned int rank;
2015-01-21 02:00:03 +08:00
2011-11-14 12:20:19 +08:00
if(seg->rank == 0) return 1;
2011-04-18 02:56:10 +08:00
if(!seg->slicesdefined) return 0;
if(seg->annotation == NULL) return 0;
dimset = ((CDFnode*)seg->annotation)->array.dimset0;
2011-04-18 02:56:10 +08:00
rank = nclistlength(dimset);
whole = 1; /* assume so */
for(i=0;i<rank;i++) {
CDFnode* dim = (CDFnode*)nclistget(dimset,i);
2015-01-21 02:00:03 +08:00
if(!dapiswholeslice(&seg->slices[i],dim)) {whole = 0; break;}
2011-04-18 02:56:10 +08:00
}
return whole;
}
int
dapiswholeprojection(DCEprojection* proj)
2011-04-18 02:56:10 +08:00
{
int i,whole;
2015-01-21 02:00:03 +08:00
2011-04-18 02:56:10 +08:00
ASSERT((proj->discrim == CES_VAR));
whole = 1; /* assume so */
for(i=0;i<nclistlength(proj->var->segments);i++) {
DCEsegment* segment = (DCEsegment*)nclistget(proj->var->segments,i);
2015-01-21 02:00:03 +08:00
if(!dapiswholesegment(segment)) {whole = 0; break;}
2011-04-18 02:56:10 +08:00
}
return whole;
}
int
dapiswholeconstraint(DCEconstraint* con)
2011-04-18 02:56:10 +08:00
{
int i;
if(con == NULL) return 1;
if(con->projections != NULL) {
for(i=0;i<nclistlength(con->projections);i++) {
if(!dapiswholeprojection((DCEprojection*)nclistget(con->projections,i)))
2011-04-18 02:56:10 +08:00
return 0;
}
}
if(con->selections != NULL)
return 0;
return 1;
}
2011-11-14 12:20:19 +08:00
/*
Given a set of projections, we need to produce
an expanded, correct, and equivalent set of projections.
The term "correct" means we must fix the following cases:
1. Multiple occurrences of the same leaf variable
with differing projection slices. Fix is to complain.
2. Occurrences of container and one or more of its fields.
Fix is to suppress the container.
The term "expanded" means
1. Expand all occurrences of only a container by
replacing it with all of its fields.
*/
NCerror
dapfixprojections(NClist* list)
2011-04-18 02:56:10 +08:00
{
2011-11-14 12:20:19 +08:00
int i,j,k;
NCerror ncstat = NC_NOERR;
NClist* tmp = nclistnew(); /* misc. uses */
#ifdef DEBUG
fprintf(stderr,"fixprojection: list = %s\n",dumpprojections(list));
#endif
if(nclistlength(list) == 0) goto done;
/* Step 1: remove duplicates and complain about slice mismatches */
for(i=0;i<nclistlength(list);i++) {
DCEprojection* p1 = (DCEprojection*)nclistget(list,i);
if(p1 == NULL) continue;
if(p1->discrim != CES_VAR) continue; /* dont try to unify functions */
for(j=i;j<nclistlength(list);j++) {
DCEprojection* p2 = (DCEprojection*)nclistget(list,j);
if(p2 == NULL) continue;
if(p1 == p2) continue;
if(p2->discrim != CES_VAR) continue;
if(p1->var->annotation != p2->var->annotation) continue;
2011-11-14 12:20:19 +08:00
/* check for slice mismatches */
if(!slicematch(p1->var->segments,p2->var->segments)) {
/* complain */
nclog(NCLOGWARN,"Malformed projection: same variable with different slicing");
}
/* remove p32 */
2015-01-21 02:00:03 +08:00
nclistset(list,j,(void*)NULL);
dcefree((DCEnode*)p2);
}
2011-11-14 12:20:19 +08:00
}
/* Step 2: remove containers when a field is also present */
for(i=0;i<nclistlength(list);i++) {
DCEprojection* p1 = (DCEprojection*)nclistget(list,i);
if(p1 == NULL) continue;
if(p1->discrim != CES_VAR) continue; /* dont try to unify functions */
if(!iscontainer((CDFnode*)p1->var->annotation))
2011-11-14 12:20:19 +08:00
continue;
for(j=i;j<nclistlength(list);j++) {
DCEprojection* p2 = (DCEprojection*)nclistget(list,j);
if(p2 == NULL) continue;
if(p2->discrim != CES_VAR) continue;
nclistclear(tmp);
collectnodepath((CDFnode*)p2->var->annotation,tmp,WITHDATASET);
2011-11-14 12:20:19 +08:00
for(k=0;k<nclistlength(tmp);k++) {
void* candidate = (void*)nclistget(tmp,k);
if(candidate == p1->var->annotation) {
2015-01-21 02:00:03 +08:00
nclistset(list,i,(void*)NULL);
2011-11-14 12:20:19 +08:00
dcefree((DCEnode*)p1);
goto next;
}
}
}
next: continue;
}
/* Step 3: expand all containers recursively down to the leaf nodes */
for(;;) {
nclistclear(tmp);
for(i=0;i<nclistlength(list);i++) {
DCEprojection* target = (DCEprojection*)nclistget(list,i);
CDFnode* leaf;
if(target == NULL) continue;
if(target->discrim != CES_VAR)
continue; /* dont try to unify functions */
leaf = (CDFnode*)target->var->annotation;
2011-11-14 12:20:19 +08:00
ASSERT(leaf != NULL);
if(iscontainer(leaf)) {/* capture container */
if(!nclistcontains(tmp,(void*)target))
nclistpush(tmp,(void*)target);
nclistset(list,i,(void*)NULL);
2011-11-14 12:20:19 +08:00
}
}
if(nclistlength(tmp) == 0) break; /*done*/
/* Now explode the containers */
for(i=0;i<nclistlength(tmp);i++) {
DCEprojection* container = (DCEprojection*)nclistget(tmp,i);
CDFnode* leaf = (CDFnode*)container->var->annotation;
2011-11-14 12:20:19 +08:00
for(j=0;i<nclistlength(leaf->subnodes);j++) {
CDFnode* field = (CDFnode*)nclistget(leaf->subnodes,j);
/* Convert field node to a proper constraint */
DCEprojection* proj = projectify(field,container);
nclistpush(list,(void*)proj);
2015-01-21 02:00:03 +08:00
}
2011-11-14 12:20:19 +08:00
/* reclaim the container */
dcefree((DCEnode*)container);
}
} /*for(;;)*/
/* remove all NULL elements */
for(i=nclistlength(list)-1;i>=0;i--) {
DCEprojection* target = (DCEprojection*)nclistget(list,i);
if(target == NULL)
2015-01-21 02:00:03 +08:00
nclistremove(list,i);
}
2011-11-14 12:20:19 +08:00
done:
#ifdef DEBUG
fprintf(stderr,"fixprojection: exploded = %s\n",dumpprojections(list));
#endif
nclistfree(tmp);
2011-11-14 12:20:19 +08:00
return ncstat;
}
static int
iscontainer(CDFnode* node)
{
return (node->nctype == NC_Dataset
|| node->nctype == NC_Sequence
|| node->nctype == NC_Structure
|| node->nctype == NC_Grid);
}
static DCEprojection*
projectify(CDFnode* field, DCEprojection* container)
{
DCEprojection* proj = (DCEprojection*)dcecreate(CES_PROJECT);
DCEvar* var = (DCEvar*)dcecreate(CES_VAR);
DCEsegment* seg = (DCEsegment*)dcecreate(CES_SEGMENT);
proj->discrim = CES_VAR;
proj->var = var;
var->annotation = (void*)field;
2011-11-14 12:20:19 +08:00
/* Dup the segment list */
var->segments = dceclonelist(container->var->segments);
seg->rank = 0;
nclistpush(var->segments,(void*)seg);
2011-11-14 12:20:19 +08:00
return proj;
}
static int
slicematch(NClist* seglist1, NClist* seglist2)
{
int i,j;
if((seglist1 == NULL || seglist2 == NULL) && seglist1 != seglist2)
return 0;
if(nclistlength(seglist1) != nclistlength(seglist2))
return 0;
for(i=0;i<nclistlength(seglist1);i++) {
DCEsegment* seg1 = (DCEsegment*)nclistget(seglist1,i);
DCEsegment* seg2 = (DCEsegment*)nclistget(seglist2,i);
if(seg1->rank != seg2->rank)
return 0;
for(j=0;j<seg1->rank;j++) {
2013-04-18 02:58:37 +08:00
DCEslice* slice1 = &seg1->slices[j];
DCEslice* slice2 = &seg2->slices[j];
size_t count1 = slice1->count;
size_t count2 = slice2->count;
if(slice1->first != slice2->first
|| count1 != count2
|| slice1->stride != slice2->stride)
2011-11-14 12:20:19 +08:00
return 0;
}
}
return 1;
}
/* Convert a CDFnode var to a projection; include
pseudodimensions; always whole variable.
*/
int
dapvar2projection(CDFnode* var, DCEprojection** projectionp)
{
int i,j;
int ncstat = NC_NOERR;
NClist* path = nclistnew();
NClist* segments;
DCEprojection* projection = NULL;
int dimindex;
2015-01-21 02:00:03 +08:00
/* Collect the nodes needed to construct the projection segments */
collectnodepath(var,path,!WITHDATASET);
2011-11-14 12:20:19 +08:00
segments = nclistnew();
dimindex = 0; /* point to next subset of slices */
nclistsetalloc(segments,nclistlength(path));
for(i=0;i<nclistlength(path);i++) {
DCEsegment* segment = (DCEsegment*)dcecreate(CES_SEGMENT);
CDFnode* n = (CDFnode*)nclistget(path,i);
int localrank;
NClist* dimset;
segment->annotation = (void*)n;
2011-11-14 12:20:19 +08:00
segment->name = nulldup(n->ocname);
/* We need to assign whole slices to each segment */
localrank = nclistlength(n->array.dimsetplus);
2011-11-14 12:20:19 +08:00
segment->rank = localrank;
dimset = n->array.dimsetplus;
2011-11-14 12:20:19 +08:00
for(j=0;j<localrank;j++) {
DCEslice* slice;
CDFnode* dim;
slice = &segment->slices[j];
dim = (CDFnode*)nclistget(dimset,j);
ASSERT(dim->dim.declsize0 > 0);
dcemakewholeslice(slice,dim->dim.declsize0);
}
segment->slicesdefined = 1;
segment->slicesdeclized = 1;
dimindex += localrank;
nclistpush(segments,(void*)segment);
2011-11-14 12:20:19 +08:00
}
2015-01-21 02:00:03 +08:00
2011-11-14 12:20:19 +08:00
projection = (DCEprojection*)dcecreate(CES_PROJECT);
projection->discrim = CES_VAR;
projection->var = (DCEvar*)dcecreate(CES_VAR);
projection->var->annotation = (void*)var;
2011-11-14 12:20:19 +08:00
projection->var->segments = segments;
#ifdef DEBUG1
fprintf(stderr,"dapvar2projection: projection=%s\n",
dumpprojection(projection));
#endif
nclistfree(path);
if(ncstat) dcefree((DCEnode*)projection);
else if(projectionp) *projectionp = projection;
return ncstat;
2015-01-21 02:00:03 +08:00
}
2011-11-14 12:20:19 +08:00
/*
Given a set of projections and a projection
representing a variable (from, say vara or prefetch)
construct a single projection for fetching that variable
with the proper constraints.
*/
int
daprestrictprojection(NClist* projections, DCEprojection* var, DCEprojection** resultp)
{
int ncstat = NC_NOERR;
2011-04-18 02:56:10 +08:00
int i;
2011-11-14 12:20:19 +08:00
DCEprojection* result = NULL;
#ifdef DEBUG1
fprintf(stderr,"restrictprojection.before: constraints=|%s| vara=|%s|\n",
dumpprojections(projections),
dumpprojection(var));
#endif
2011-04-18 02:56:10 +08:00
2011-11-14 12:20:19 +08:00
ASSERT(var != NULL);
2011-04-18 02:56:10 +08:00
2011-11-14 12:20:19 +08:00
/* the projection list will contain at most 1 match for the var by construction */
for(result=null,i=0;i<nclistlength(projections);i++) {
DCEprojection* p1 = (DCEprojection*)nclistget(projections,i);
if(p1 == NULL || p1->discrim != CES_VAR) continue;
if(p1->var->annotation == var->var->annotation) {
2011-11-14 12:20:19 +08:00
result = p1;
break;
}
}
if(result == NULL) {
result = (DCEprojection*)dceclone((DCEnode*)var); /* use only the var projection */
2015-01-21 02:00:03 +08:00
goto done;
2011-04-18 02:56:10 +08:00
}
2012-02-04 05:31:50 +08:00
result = (DCEprojection*)dceclone((DCEnode*)result); /* so we can modify */
2011-11-14 12:20:19 +08:00
2012-02-04 05:31:50 +08:00
#ifdef DEBUG1
2013-04-18 02:58:37 +08:00
fprintf(stderr,"restrictprojection.choice: base=|%s| add=|%s|\n",
dumpprojection(result),dumpprojection(var));
2012-02-04 05:31:50 +08:00
#endif
2011-11-14 12:20:19 +08:00
/* We need to merge the projection from the projection list
with the var projection
*/
2015-01-21 02:00:03 +08:00
ncstat = dcemergeprojections(result,var); /* result will be modified */
2011-11-14 12:20:19 +08:00
done:
if(resultp) *resultp = result;
#ifdef DEBUG
fprintf(stderr,"restrictprojection.after=|%s|\n",
dumpprojection(result));
#endif
return ncstat;
}
/* Shift the slice so it runs from 0..count by step 1 */
static void
dapshiftslice(DCEslice* slice)
{
size_t first = slice->first;
size_t stride = slice->stride;
if(first == 0 && stride == 1) return; /* no need to do anything */
slice->first = 0;
slice->stride = 1;
slice->length = slice->count;
slice->last = slice->length - 1;
2011-11-14 12:20:19 +08:00
}
int
dapshiftprojection(DCEprojection* projection)
{
int ncstat = NC_NOERR;
int i,j;
NClist* segments;
#ifdef DEBUG1
fprintf(stderr,"dapshiftprojection.before: %s\n",dumpprojection(projection));
2011-11-14 12:20:19 +08:00
#endif
ASSERT(projection->discrim == CES_VAR);
segments = projection->var->segments;
for(i=0;i<nclistlength(segments);i++) {
DCEsegment* seg = (DCEsegment*)nclistget(segments,i);
for(j=0;j<seg->rank;j++) {
DCEslice* slice = seg->slices+j;
dapshiftslice(slice);
}
}
#ifdef DEBUG1
fprintf(stderr,"dapshiftprojection.after: %s\n",dumpprojection(projection));
2011-11-14 12:20:19 +08:00
#endif
return ncstat;
2011-04-18 02:56:10 +08:00
}
/* Compute the set of variables referenced in the projections
of the input constraint.
*/
NCerror
dapcomputeprojectedvars(NCDAPCOMMON* dapcomm, DCEconstraint* constraint)
{
NCerror ncstat = NC_NOERR;
NClist* vars = NULL;
int i;
vars = nclistnew();
if(dapcomm->cdf.projectedvars != NULL)
nclistfree(dapcomm->cdf.projectedvars);
dapcomm->cdf.projectedvars = vars;
if(constraint == NULL || constraint->projections == NULL)
goto done;
for(i=0;i<nclistlength(constraint->projections);i++) {
CDFnode* node;
DCEprojection* proj = (DCEprojection*)nclistget(constraint->projections,i);
if(proj->discrim == CES_FCN) continue; /* ignore these */
2015-01-21 02:00:03 +08:00
node = (CDFnode*)proj->var->annotation;
if(!nclistcontains(vars,(void*)node)) {
nclistpush(vars,(void*)node);
}
2015-01-21 02:00:03 +08:00
}
done:
return ncstat;
}