Esempio n. 1
0
static int
dbfield_isnull( Dbptr db )
{
	char	*fnull = NULL;
	long	fsize;
	char	*stringval = NULL;
	int	isnull = 0;

	dbquery( db, dbFIELD_SIZE, &fsize );
	dbquery( db, dbNULL, &fnull );

	allot( char *, stringval, fsize + 2 );

	dbget( db, stringval );

	strtrim( stringval );

	if( ! strcmp( stringval, fnull ) ) {

		isnull = 1;

	} else {

		isnull = 0;
	}

	free( stringval );

	return isnull;
}
Esempio n. 2
0
static int
ptrmap(daddrt addr, int nind, Blkf f, int isdisk)
{
	int i;
	Memblk *b;
	long tot;

	if(addr == 0)
		return 0;
	if(isdisk)
		b = dbget(DBdata+nind, addr);
	else{
		b = mbget(DBdata+nind, addr, Dontmk);
		if(b == nil)
			return 0;	/* on disk */
	}
	if(catcherror()){
		mbput(b);
		error(nil);
	}
	tot = 0;
	if(f == nil || f(b) == 0){
		tot++;
		if(nind > 0){
			for(i = 0; i < Dptrperblk; i++)
				tot += ptrmap(b->d.ptr[i], nind-1, f, isdisk);
		}
	}
	noerror();
	mbput(b);
	return tot;
}
Esempio n. 3
0
/* for dfblk only */
static Memblk*
getmelted(uint isdir, uint type, daddrt *addrp, int *chg)
{
	Memblk *b, *nb;

	*chg = 0;
	if(*addrp == 0){
		b = dballoc(type);
		*addrp = b->addr;
		*chg = 1;
		return b;
	}

	b = dbget(type, *addrp);
	nb = nil;
	if(!b->frozen)
		return b;
	if(catcherror()){
		mbput(b);
		mbput(nb);
		error(nil);
	}
	nb = dbdup(b);
	assert(type == b->type);
	if(isdir && type == DBdata)
		dupdentries(nb->d.data, Dblkdatasz/Daddrsz);
	USED(&nb);		/* for error() */
	*addrp = nb->addr;
	*chg = 1;
	dbput(b, b->type, b->addr);
	noerror();
	mbput(b);
	return nb;
}
Esempio n. 4
0
/*
 * Walk to a child and return it referenced.
 * If iswr, d must not be frozen and the child is returned melted.
 */
Memblk*
dfwalk(Memblk *d, char *name, int iswr)
{
	Memblk *f;
	Blksl sl;
	daddrt *de;
	uvlong off;
	int i;
	
	if(strcmp(name, "..") == 0)
		fatal("dfwalk: '..'");
	isdir(d);
	if(iswr)
		ismelted(d);

	off = 0;
	f = nil;
	for(;;){
		sl = dfslice(d, Dblkdatasz, off, Rd);
		if(sl.len == 0){
			assert(sl.b == nil);
			break;
		}
		if(sl.b == nil)
			continue;
		if(catcherror()){
			dprint("dfwalk d%#ullx '%s': %r\n", d->addr, name);
			mbput(sl.b);
			error(nil);
		}
		for(i = 0; i < sl.len/Daddrsz; i++){
			de = sl.data;
			de += i;
			if(*de == 0)
				continue;
			f = dbget(DBfile, *de);
			if(strcmp(f->mf->name, name) != 0){
				mbput(f);
				continue;
			}

			/* found  */
			noerror();
			mbput(sl.b);
			if(!iswr || !f->frozen)
				goto done;
			fatal("dfwalk: frozen");
		}
		noerror();
		mbput(sl.b);
		off += sl.len;
	}
	error("file not found");

done:
	dprint("dfwalk d%#ullx '%s' -> d%#ullx\n", d->addr, name, f->addr);
	return f;
}
/* Initializes the scratch records for each table used in this program.  
That is, origin, origerr, and assoc.  This only needs to be done
once.  The procedure loads NULL files into each attribute of the
scratch records for these tables.  This function is based
on a tricky initialization condition using dbget.  Setting
the db record to dbNULL and then calling dbget(db,0) loads the
scratch record with the contents of the NULL record.  This works
because the NULL record is initialized the first time it is
accessed, and passing 0 to dbget is a shortcut that directly 
copies the NULL record to the scratch record.  IMPORTANT:  note
that because this program uses dbputv directly to the scratch
record the assumption is that the SAME set of attributes are
always written to the scratch record of each table.  This is
correct for this program, but you are warned if you try to use
functions from this program elsewhere.  

Arguments:  db - db pointer of open database 

returns 0 on success.  Any errors will always result in
a return of dbINVALID.  The later should, of course, be
trapped but should probably not normally be fatal.
*/
initialize_scratch_records(Dbptr db)
{
	int return_code;
	int error_count=0;

	db = dblookup(db,0,"origin",0,0);
	db.record = dbNULL;
	return_code = dbget(db,0);
	if(return_code) ++error_count;
	db = dblookup(db,0,"origerr",0,0);
	db.record = dbNULL;
	return_code = dbget(db,0);
	if(return_code) ++error_count;
	db = dblookup(db,0,"assoc",0,0);
	db.record = dbNULL;
	return_code = dbget(db,0);
	if(return_code) ++error_count;
	if(error_count)
		return(dbINVALID);
	else
		return(0);
}
Esempio n. 6
0
static Memblk*
xfchild(Memblk *f, int n, int disktoo)
{
	daddrt addr;
	Memblk *b;

	addr = dfdirnth(f, n);
	if(addr == 0)
		return nil;
	b = mbget(DBfile, addr, Dontmk);
	if(b != nil || disktoo == Mem)
		return b;
	return dbget(DBfile, addr);
}
mxArray *
dbfield2mxArray( Dbptr db )
{
    mxArray	*result;
    long	type;
    Dbvalue	value;
    char	warning[STRSZ];

    dbquery( db, dbFIELD_TYPE, &type );
    antelope_mex_clear_register( 1 );

    if( dbget( db, value.s ) < 0 )
    {
        antelope_mex_clear_register( 1 );
        return (mxArray *) NULL;
    }

    switch( type )
    {
    case dbDBPTR:
        result = CreateDbptrStructFromDbptr( value.db );
        break;
    case dbSTRING:
        copystrip( value.s, value.s, strlen( value.s ) );
        result = mxCreateString( value.s );
        break;
    case dbBOOLEAN:
    case dbINTEGER:
    case dbYEARDAY:
        copystrip( value.s, value.s, strlen( value.s ) );
        result = CreateDouble( (double) atol( value.s ) );
        break;
    case dbREAL:
    case dbTIME:
        copystrip( value.s, value.s, strlen( value.s ) );
        result = CreateDouble( (double) atof( value.s ) );
        break;
    default:
        sprintf( warning,
                 "Can't interpret field of type %s",
                 xlatnum( type, Dbxlat, NDbxlat ) );
        mexWarnMsgTxt( warning );
        result = (mxArray *) NULL;
        break;
    }

    return result;
}
Esempio n. 8
0
void mexFunction ( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
	Dbptr	db;
	char	*result = NULL;
	char	*dbscratch_string;
	int	use_dbscratch = 0;
	long	n;
	int	rc;
	double	*doublep;
	int	*intp;

	if( nrhs < 1 || nrhs > 2 )
	{
		antelope_mexUsageMsgTxt ( USAGE );
		return;
	}
        else if( ! get_dbptr( prhs[0], &db ) )
        {
                antelope_mexUsageMsgTxt ( USAGE );
		return;
        }
	else if( nrhs == 2 ) 
        {
		if( ! mtlb_get_string( prhs[1], &dbscratch_string ) )
		{
                	antelope_mexUsageMsgTxt ( USAGE );
			return;
		}
		else if( ! STREQ( dbscratch_string, "dbSCRATCH" ) )
		{
			mxFree( dbscratch_string );
                	antelope_mexUsageMsgTxt ( USAGE );
			return;
		}
		else
		{
			mxFree( dbscratch_string );
			use_dbscratch = 1;
		}
        }

	if( use_dbscratch )
	{
		if( dbget( db, NULL ) < 0 )
		{
			antelope_mex_clear_register( 1 );
			mexErrMsgTxt( "dbget into scratch record failed" );
		}
	}
	else
	{
		rc = dbquery(db, dbSIZE, &n);
		antelope_mex_clear_register( 1 );
		if( rc == dbINVALID )
		{
			mexErrMsgTxt( "dbget: dbquery failed" );
		}

		result = (char *) mxCalloc( n+3, sizeof( char ) );

		if( dbget( db, result ) < 0 )
		{

			antelope_mex_clear_register( 1 );

			mxFree( result );
			mexErrMsgTxt( "dbget failed\n" );

		}
		else
		{
			if ( db.field != dbALL )
			{
				copystrip( result, result, n );
			}
			plhs[0] = mxCreateString( result );
			mxFree( result );
		}
	}

	antelope_mex_clear_register( 1 );
}
Esempio n. 9
0
/*
 * Get a file data block, perhaps allocating it on demand
 * if mkit. The file must be r/wlocked and melted if mkit.
 *
 * Adds disk refs for dir entries copied during melts and
 * considers that /archive is always melted.
 *
 * Read-ahead is not considered here. The file only records
 * the last accessed block number, to help the caller do RA.
 *
 */
static Memblk*
dfblk(Memblk *f, ulong bno, int mkit)
{
	ulong prev, nblks;
	int i, idx, nindir, type, isdir, chg;
	Memblk *b, *pb;
	daddrt *addrp;

	if(mkit)
		ismelted(f);
	isdir = (f->d.mode&DMDIR);

	if(bno != f->mf->lastbno){
		f->mf->sequential = (!mkit && bno == f->mf->lastbno + 1);
		f->mf->lastbno = bno;
	}

	/*
	 * bno: block # relative to the the block we are looking at.
	 * prev: # of blocks before the current one.
	 */
	prev = 0;
	chg = 0;

	/*
	 * Direct block?
	 */
	if(bno < nelem(f->d.dptr)){
		if(mkit)
			b = getmelted(isdir, DBdata, &f->d.dptr[bno], &chg);
		else
			b = dbget(DBdata, f->d.dptr[bno]);
		if(chg)
			changed(f);
		return b;
	}
	bno -= nelem(f->d.dptr);
	prev += nelem(f->d.dptr);

	/*
	 * Indirect block
	 * nblks: # of data blocks addressed by the block we look at.
	 */
	nblks = Dptrperblk;
	for(i = 0; i < nelem(f->d.iptr); i++){
		if(bno < nblks)
			break;
		bno -= nblks;
		prev += nblks;
		nblks *= Dptrperblk;
	}
	if(i == nelem(f->d.iptr))
		error("offset exceeds file capacity");
	ainc(&fs->nindirs[i]);
	type = DBptr0+i;
	dFprint("dfblk: indirect %s nblks %uld (ppb %ud) bno %uld\n",
		tname(type), nblks, Dptrperblk, bno);

	addrp = &f->d.iptr[i];
	if(mkit)
		b = getmelted(isdir, type, addrp, &chg);
	else
		b = dbget(type, *addrp);
	if(chg)
		changed(f);
	pb = b;
	if(catcherror()){
		mbput(pb);
		error(nil);
	}

	/* at the loop header:
	 * 	pb: parent of b
	 * 	b: DBptr block we are looking at.
	 * 	addrp: ptr to b within fb.
	 * 	nblks: # of data blocks addressed by b
	 */
	for(nindir = i+1; nindir >= 0; nindir--){
		chg = 0;
		dFprint("indir %s d%#ullx nblks %uld ptrperblk %d bno %uld\n\n",
			tname(DBdata+nindir), *addrp, nblks, Dptrperblk, bno);
		idx = 0;
		if(nindir > 0){
			nblks /= Dptrperblk;
			idx = bno/nblks;
		}
		if(*addrp == 0 && !mkit){
			/* hole */
			fprint(2, "HOLE\n");
			b = nil;
		}else{
			assert(type >= DBdata);
			if(mkit)
				b = getmelted(isdir, type, addrp, &chg);
			else
				b = dbget(type, *addrp);
			if(chg)
				changed(pb);
			addrp = &b->d.ptr[idx];
		}
		mbput(pb);
		pb = b;
		USED(&b);	/* force to memory in case of error */
		USED(&pb);	/* force to memory in case of error */
		bno -= idx * nblks;
		prev +=  idx * nblks;
		type--;
	}
	noerror();

	return b;
}
Esempio n. 10
0
int
write_trace (Dbptr db, char *sta, char *chan, char *dir, char *dfile, Trace *trace, double tstart, double tend, int overwrite)

{
    int fd;
    char outdir[1024];
    char outbase[1024];
    char fname[1024];
    int sz, size, ret;
    int is, ie, ns;
    Dbvalue dbv;

    is = (tstart-trace->tstart)/trace->dt + 0.0001;
    ie = (tend-trace->tstart)/trace->dt - 0.0001;
    if (is > trace->nsamps-1) return (1);
    if (ie < 0) return (1);
    if (is < 0) is = 0;
    if (ie > trace->nsamps-1) ie = trace->nsamps-1;
    ns = ie - is + 1;
    if (ns < 1) return (1);
    db = dblookup (db, 0, "wfdisc", 0, 0);
    if (dir[0] == '/') {
        sprintf (fname, "%s/%s", dir, dfile);
    } else {
        dbquery (db, dbTABLE_DIRNAME, &dbv);
        strcpy (outbase, dbv.t);
        sprintf (fname, "%s/%s/%s", outbase, dir, dfile);
    }
    if (!overwrite) {
        if (zaccess(fname, F_OK) != -1) {
            fprintf (stderr, "write_trace: wf file conflict for '%s'.\n",
                     fname);
            return (0);
        }
    }
    dirbase (fname, outdir, outbase);
    if (makedir(outdir) == -1) {
        fprintf (stderr, "write_trace: Unable to create %s\n", outdir);
        return (0);
    }
    fd = open (fname, (O_RDWR|O_CREAT|O_TRUNC), 0666);
    if (fd < 0) {
        fprintf (stderr, "write_trace: Open error on '%s'.\n", fname);
        return (0);
    }
    sz = atoi(&trace->rawdata_format[strlen(trace->rawdata_format)-1]);
    size = sz*ns;
    ret = write (fd, &(((char *)trace->raw_data)[is*sz]), size);
    close (fd);
    if (ret < size) {
        fprintf (stderr, "write_trace: Write error on '%s'.\n", fname);
        return (0);
    }
    db.record = dbNULL;
    db.field = dbALL;
    dbget(db, 0);
    db.record = dbSCRATCH;
    dbputv (db, 0,	"sta", sta,
            "chan", chan,
            "time", trace->tstart+is*trace->dt,
            "wfid", dbnextid(db, "wfid"),
            /*			"chanid", chanid,*/
            "jdate", yearday(trace->tstart),
            "endtime", trace->tstart+trace->dt*(trace->nsamps-1),
            "nsamp", (long)ns,
            "samprate", 1.0/trace->dt,
            "calib", trace->calib,
            "calper", trace->calper,
            "instype", "BEAM",
            "segtype", trace->rawdata_type,
            "datatype", trace->rawdata_format,
            /*			"clip", clip,*/
            "dir", dir,
            "dfile", dfile,
            "foff", (long)0,
            NULL);
    dbadd (db, 0);
    return (1);
}
Esempio n. 11
0
int dbpmel_save_results(Dbptr db,
	int nevents,
	long *evid,
	Hypocenter *h,
	Tbl **ta,
	Location_options o,
	Pf *pf)
#endif
{
	Dbptr dbe,dbo,dba,dboe; 
	Dbptr dbes,dbos,dbas;
	Dbptr dbs;  /* subset view created by dblist2subset */
	Tbl *opat,*aspat,*aspat2;
	Tbl *matches=NULL,*match2;  /* output list of matching records */
	/* Dan Q advises it is wise to have a separate hook for each
	table passed through dbmatches */
	Hook *hooke=NULL,*hooko=NULL,*hooka=NULL,*hooka2=NULL;
	long prefor;
	int i,j;
	char *auth;
	Arrival *a;
	long orid;
	int nmatch;
	double **C;
	float emodel[4];
	char *alg=(char *)strdup("dbpmel");
	/* A collection of variables from assoc that have to be 
	copied.  Earlier algorithm using dbget raw on a subset 
	view failed so I have to copy by this mechanism */
	double belief;
	char timedef[2],slodef[2],azdef[2];
	double azres,slores,emares;
	Tbl *utbl;  


	/* This is needed as a stub for the predicted_error function.
	Because we don't support slowness vector data in pmel this 
	has to be an empty (NOT NULL) tbl list */
	utbl=newtbl(0);
	/* All of these are used repeatedly so we do one lookup at the
	top */
	dbe = dblookup(db,0,"event",0,0);
	dbo = dblookup(db,0,"origin",0,0);
	dba = dblookup(db,0,"assoc",0,0);
	dboe = dblookup(db,0,"origerr",0,0);
	dbes = dblookup(db,0,"event",0,0);
	dbos = dblookup(db,0,"origin",0,0);
	dbas = dblookup(db,0,"assoc",0,0);
	dbes.record = dbSCRATCH;
	dbos.record = dbSCRATCH;
	dbas.record = dbSCRATCH;

	opat = strtbl("orid",NULL );
	aspat = strtbl("orid",NULL );
	aspat2 = strtbl("arid","orid","sta",NULL );

	auth = pfget_string(pf,"author");

	C = dmatrix(0,3,0,3);
	/* outer loop over nevents */
	for(i=0;i<nevents;++i)
	{		
    		double conf;
		char *modtype;
		int model;
		double smajax,sminax,strike,sdepth,stime;
		int rc;

		/* Save nothing for events marked no use */
		if(h[i].used==0) continue;  
		/* Start with event table to get prefor */
		dbputv(dbes,0,"evid",evid[i],NULL );
		dbe.record = dbALL;
		if(dbmatches(dbes,dbe,0,0,&hooke,&matches)!=1)
			elog_complain(0,"WARNING:  multiple records in event table have evid=%ld.\nThis is a serious database problem that should be corrected.  Using first one found in table\n",
				evid[i]);
		dbe.record = (long)gettbl(matches,0);
		/* this is excessively paranoid, but better safe than sorry*/
		if(dbe.record<0)
		{
			elog_complain(0,"dbmatches invalid record %ld\nSkip saving evid %ld\n",
				dbe.record,evid[i]);
			continue;
		}
		dbgetv(dbe,0,"prefor",&prefor,NULL );
		freetbl(matches,0);
		matches=NULL;

		dbputv(dbos,0,"orid",prefor,NULL );
		dbo.record = dbALL;
		nmatch=dbmatches(dbos,dbo,&opat,&opat,&hooko,&matches);
		if(nmatch>1)
			elog_complain(0,"WARNING:  multiple records in origin table match orid=%ld.\nThis is a serious database problem that should be corrected.  Using first one found in table\n",
				prefor);
		else if(nmatch<=0)
		{
			elog_complain(0,"Cannot find matching origin\
record for orid %ld prefor of event %ld\n",
				prefor,evid[i]);
		}
		dbo.record = (long)gettbl(matches,0);
		if(dbget(dbo,0)==dbINVALID)
		{
			elog_complain(0,"dbget error on origin table for orid %ld\nData for evid %ld will not be saved\n",
				prefor,evid[i]);
			continue;
		}
		freetbl(matches,0);
		matches=NULL;

		/* origerr code cloned from dbgenloc */
		conf = pfget_double(pf,"confidence");
		modtype = pfget_string(pf,"ellipse_type");
     		if(modtype == NULL)
     		{
        		elog_complain(0,"parameter ellipse_type not defined--default to chi_square");
        		model = CHI_SQUARE;
     		}
     		else if( strcmp( modtype, "chi_square" ) == 0 )
     		{
        		model = CHI_SQUARE;
     		}
     		else if( strcmp( modtype, "F_dist" ) == 0 )
     		{
        		model = F_DIST;
     		}
     		else
     		{
        		elog_complain(0, "parameter ellipse_type %s incorrect (must be F_dist or chi_square)--default to chi_square", modtype );
        		model = CHI_SQUARE;
     		}
		predicted_errors(h[i],ta[i],utbl,o,C,emodel);
    		rc = project_covariance( C, model, &conf,
                             h[i].rms_weighted, h[i].degrees_of_freedom,
                             &smajax, &sminax, &strike, &sdepth, &stime );

    		if( rc != 0 )
    		{
        		elog_complain(0, "project_covariance failed." );
        		smajax = -1;
        		sminax = -1;
        		strike = -1;
        		sdepth = -1;
        		stime = -1;
        		conf = 0.;
    		}
		orid = dbnextid(dbo,"orid");

		if(dbaddv(dboe,0,
                	"orid", orid,
                	"sxx",C[0][0],
                	"syy",C[1][1],
                	"szz",C[2][2],
                	"stt",C[3][3],
                	"sxy",C[0][1],
                	"sxz",C[0][2],
                	"syz",C[1][2],
                	"stx",C[0][3],
                	"sty",C[1][3],
                	"stz",C[2][3],
                	"sdobs", h[i].rms_raw,
                	"smajax", smajax,
                	"sminax", sminax,
                	"strike", strike,
                	"sdepth", sdepth,
                	"stime", stime,
                	"conf", conf,
                		NULL ) < 0 )
		{
			elog_complain(0,"Problem adding origerr record for evid %ld\n",
				evid[i]);
		} 


		/* This edits the scratch record and adds it to the end 
		of the origin table */
		dbputv(dbos,0,"orid",orid,
				"lat",h[i].lat,
				"lon",h[i].lon,
				"depth",h[i].z,
				"time",h[i].time,
				"algorithm",alg,
				"auth",auth,NULL );

/*
		printf("Added to origin table: %8d %9.4lf %9.4lf %9.4lf"
			" %17.5lf %-15s %-15s\n", orid, h[i].lat, 
			h[i].lon, h[i].z, h[i].time, alg, auth);
*/

		if(dbadd(dbo,0)==dbINVALID)
			elog_complain(0,"dbadd error for origin table\
for new orid %ld of evid %ld\n",
				orid,evid[i]);
		/* The assoc tables is much more complex.  Rather than
		do a row for row match against each arrival in ta, I
		use a staged reduction.  Than is I first match the full
		assoc table against orid=prefor only to get a full list
		of records for that event.  We then run matches against
		the smaller table for sta/phase in the arrival list.
		Untested but from previous experience with dbmatches on
		large tables I'm fairly sure this is a necessary 
		complication to keep this from be very slow*/
		dbputv(dbas,0,"orid",prefor,NULL );
		dba.record = dbALL;
		nmatch=dbmatches(dbas,dba,&aspat,&aspat,&hooka,&matches);
		if(nmatch<=0)
		{
			elog_complain(0,"No assoc records for orid %ld\
found\nFail to create new assoc records for orid %ld\n",
				prefor,orid);
			freetbl(matches,0);
			continue;
		}
Esempio n. 12
0
int main(int argc, char **argv)
 { Pf 		*pf;
   char		*orbname,
		*dbname,
		*expr,
		*table,
		*rowtemp,
                verbose;
   int  	orb,
		totalrecords,
		records,
 		record;
   Dbptr  	db,
		dbinput,
		dbscratch;

  elog_init(argc,argv);		

  /* initialize variables */

  verbose = 0;
  expr = NULL;
  loop = 0;
  record = 0;
  totalrecords = 0;

  rowtemp = malloc(ROW_MAX_LENGTH);
  
  if (rowtemp == NULL) die(1,"malloc() error.\n");

  /* read in command line options */

  { int c;
    unsigned char errflg = 0;

    while (( c = getopt( argc, argv, "vl:s:")) != -1)
        switch (c) {
          case 'l': loop = atoi(optarg);
                    sigset(SIGINT,done);
                    break;
          case 's': expr = optarg;
                    break;
          case 'v': verbose = 1;
                    break;
          case '?': errflg++;
        }

    if ( (argc - optind) != 3 ) errflg++;

    if (errflg) 
       { elog_die(0,"usage: %s [-v] [-s subset] [-l delay] db table orb\n",argv[0]); 
       }
    
    dbname = argv[optind++];
    table  = argv[optind++];
    orbname= argv[optind++];
  }
    
  /* start our work */

  if ( dbopen ( dbname,  "r+",  &dbinput ) == dbINVALID )
     { elog_die(1,"Couldn't open input database, \"%s\".\n",dbname); }

  dbinput   = dblookup( dbinput, 0, table, 0, 0);
  dbscratch = dblookup( dbinput, 0, 0, 0, "dbSCRATCH");

  if ( dbinput.table == dbINVALID )
   { elog_die(1,"Couldn't lookup the table \"%s\" in the database \"%s\".\n",
		table,dbname); }
  if ( dbscratch.record == dbINVALID )
   { elog_die(1,"Couldn't lookup the scratch record in the database \"%s\".\n",
		dbname); }
  
  if ( dbquery( dbinput, dbRECORD_COUNT, &records) < 0 )
   { elog_die(1,"dbquery dbRECORD_COUNT failed.\n"); }

  orb      = orbopen( orbname, "w&" );

  if ( orb == -1 )
   { elog_die(1,"Couldn't open the orb, \"%s\".\n",orbname); }

  if (loop == 0) loop = -1;

  while (loop)
   { db = dbinput;
     
     if ( expr != NULL) db = dbsubset( db, expr, 0 );
     
     db.record = record;

     if ( dbquery( db, dbRECORD_COUNT, &records) < 0 )
   	 elog_die(1,"dbquery dbRECORD_COUNT failed.\n");  
     
     for (;db.record<records;db.record++)
       { if (verbose) printf("writing record %d to orb...\n",db.record); 

         /* copy the row from what could be a view into the scratch record of the
            input database, so as to avoid putting a view on the ORB */
         if ( dbget( db,        rowtemp) == dbINVALID ) die(1,"dbget error.\n");
         if ( dbput( dbscratch, rowtemp) == dbINVALID ) die(1,"dbput error.\n");

         if ( db2orbpkt( dbscratch, orb ) < 0 )
            { complain ( 0, "Couldn't write record #%d to %s.\n",
              	   db.record, orbname); } 
         totalrecords++;
       }

     record = db.record; 

     /* if a subset expression was supplied, then we're working with
        a view, which should be freed. */

     if ( expr != NULL) dbfree(db);
     if ( loop == -1 ) loop = 0;
     sleep(loop);
   } 

  printf("posted %d records from database %s to orb %s.\n",totalrecords,dbname,orbname);
  dbclose(dbinput);
  orbclose(orb); 
  free(rowtemp);
 }
Esempio n. 13
0
void mexFunction ( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )
{
	Dbptr	db;
	Dbvalue	*value;	
	int	nfields;
	int	retcode = 0;
	int	rc;
	long	type;
	char	errmsg[STRSZ];
	char	*field_name;
	int	fieldname_index;
	int	fieldval_index;
	int	i;

	if( nrhs < 3 )
	{
		antelope_mexUsageMsgTxt ( USAGE );
		return;
	}
	else if( ( nrhs - 1 ) % 2 != 0 )
	{
		antelope_mexUsageMsgTxt ( USAGE );
		return;
	}
        else if( ! get_dbptr( prhs[0], &db ) )
        {
                antelope_mexUsageMsgTxt ( USAGE );
		return;
        }

	db.record = dbNULL;

	rc = dbget( db, NULL );
	antelope_mex_clear_register( 1 );
	if( rc == dbINVALID )
	{
		mexErrMsgTxt( "dbaddv: failed to get null record" );
	}

	db.record = dbSCRATCH;

	nfields = ( nrhs - 1 ) / 2;

	for( i = 0; i < nfields; i++ )
	{
		fieldname_index = i * 2 + 1;
		if( mxGetClassID( prhs[fieldname_index] ) != mxCHAR_CLASS )
	   	{ 
			antelope_mexUsageMsgTxt ( USAGE );
			return;
		}
	}

	for( i = 0; i < nfields; i++ )
	{
		fieldname_index = i * 2 + 1;
		fieldval_index = fieldname_index + 1;

		get_malloced_string( prhs[fieldname_index], &field_name );

		db = dblookup ( db, 0, 0, field_name, 0 );
		antelope_mex_clear_register( 1 );

		rc = dbquery ( db, dbFIELD_TYPE, &type );
		antelope_mex_clear_register( 1 );
		if( rc == dbINVALID )
		{
			sprintf( errmsg,
				"dbaddv: dbquery failed for field %s",
				field_name );
			mxFree( field_name );
			mexErrMsgTxt( errmsg );
		}

		value = mxArray2dbvalue( prhs[fieldval_index], type );
		if( value == (Dbvalue *) NULL )
		{
			sprintf( errmsg,
				"dbaddv: failed to convert field %s",
				field_name );
			mxFree( field_name );
			mexErrMsgTxt( errmsg );
		}
		
		switch( type )
		{
		case dbDBPTR:
			retcode |= dbputv( db, 0, field_name, value->db, NULL );
			break;
		case dbSTRING:
			retcode |= dbputv( db, 0, field_name, value->s, NULL );
			break;
		case dbBOOLEAN:
		case dbINTEGER:
		case dbYEARDAY:
			retcode |= dbputv( db, 0, field_name, value->i, NULL );
			break;
		case dbREAL:
		case dbTIME:
			retcode |= dbputv( db, 0, field_name, value->d, NULL );
			break;
		default:
			retcode = -1;
			break;
		}
		antelope_mex_clear_register( 1 );

		mxFree( value );
		mxFree( field_name );
	}

	if( retcode != 0 )
	{
		mexErrMsgTxt( "dbaddv failed\n" );
	}

	retcode = dbaddchk ( db, 0 ) ;
	antelope_mex_clear_register( 1 );

	if( retcode < 0 )
	{
		mexErrMsgTxt( "dbaddv failed\n" );
	}
	else
	{
		plhs[0] = CreateDouble( (double) retcode );
		if( plhs[0] == NULL )
		{
			mexErrMsgTxt( "dbaddv: failed to create return value" );
		}
	}
}