str MATpackValues(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p) { int i,*ret, type, first = 1; BAT *bn; (void) cntxt; type = getArgType(mb,p,first); bn = BATnew(TYPE_void, type, p->argc, TRANSIENT); if( bn == NULL) throw(MAL, "mat.pack", MAL_MALLOC_FAIL); if (ATOMvarsized(type)) { for(i = first; i < p->argc; i++) BUNappend(bn, stk->stk[getArg(p,i)].val.sval, TRUE); } else { for(i = first; i < p->argc; i++) BUNappend(bn, getArgReference(stk, p, i), TRUE); } BATsettrivprop(bn); BATderiveProps(bn,FALSE); ret= (int*) getArgReference(stk,p,0); BBPkeepref(*ret = bn->batCacheid); return MAL_SUCCEED; }
static void infoHeap(BAT *bk, BAT*bv, Heap *hp, str nme) { char buf[1024], *p = buf; if (!hp) return; while (*nme) *p++ = *nme++; strcpy(p, "free"); BUNappend(bk, buf, FALSE); BUNappend(bv, local_utoa(hp->free),FALSE); strcpy(p, "size"); BUNappend(bk, buf, FALSE); BUNappend(bv, local_utoa(hp->size),FALSE); strcpy(p, "storage"); BUNappend(bk, buf, FALSE); BUNappend(bv, (hp->base == NULL || hp->base == (char*)1) ? "absent" : (hp->storage == STORE_MMAP) ? (hp->filename ? "memory mapped" : "anonymous vm") : (hp->storage == STORE_PRIV) ? "private map" : "malloced",FALSE); strcpy(p, "newstorage"); BUNappend(bk, buf, FALSE); BUNappend(bv, (hp->newstorage == STORE_MEM) ? "malloced" : (hp->newstorage == STORE_PRIV) ? "private map" : "memory mapped",FALSE); strcpy(p, "filename"); BUNappend(bk, buf, FALSE); BUNappend(bv, hp->filename ? hp->filename : "no file",FALSE); }
str CMDbbpNames(bat *ret) { BAT *b; int i; b = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); if (b == 0) throw(MAL, "catalog.bbpNames", MAL_MALLOC_FAIL); BATseqbase(b,0); BBPlock("CMDbbpNames"); for (i = 1; i < getBBPsize(); i++) if (i != b->batCacheid) { if (BBP_logical(i) && (BBP_refs(i) || BBP_lrefs(i)) ) { BUNappend(b, BBP_logical(i), FALSE); if (BBP_logical(-i) && (BBP_refs(-i) || BBP_lrefs(-i)) && !BBPtmpcheck(BBP_logical(-i))) BUNappend(b, BBP_logical(-i), FALSE); } } BBPunlock("CMDbbpNames"); if (!(b->batDirty&2)) BATsetaccess(b, BAT_READ); pseudo(ret,b,"bbp","name"); return MAL_SUCCEED; }
str MATpackValues(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p) { int i, type, first = 1; bat *ret; BAT *bn; (void) cntxt; type = getArgType(mb,p,first); bn = BATnew(TYPE_void, type, p->argc, TRANSIENT); if( bn == NULL) throw(MAL, "mat.pack", MAL_MALLOC_FAIL); if (ATOMextern(type)) { for(i = first; i < p->argc; i++) BUNappend(bn, stk->stk[getArg(p,i)].val.pval, TRUE); } else { for(i = first; i < p->argc; i++) BUNappend(bn, getArgReference(stk, p, i), TRUE); } BATseqbase(bn, 0); ret= getArgReference_bat(stk,p,0); BBPkeepref(*ret = bn->batCacheid); return MAL_SUCCEED; }
void GDKsetenv(str name, str value) { BUNappend(GDKkey, name, FALSE); BUNappend(GDKval, value, FALSE); BATfakeCommit(GDKkey); BATfakeCommit(GDKval); }
/* * The BAT status is redundantly stored in CMDbat_info. */ str CMDbbpLocation(bat *ret) { BAT *b; int i; char buf[MAXPATHLEN]; char cwd[MAXPATHLEN]; if (getcwd(cwd, MAXPATHLEN) == NULL) throw(MAL, "catalog.bbpLocation", RUNTIME_DIR_ERROR); b = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); if (b == 0) throw(MAL, "catalog.bbpLocation", MAL_MALLOC_FAIL); BATseqbase(b,0); BBPlock("CMDbbpLocation"); for (i = 1; i < getBBPsize(); i++) if (i != b->batCacheid) { if (BBP_logical(i) && (BBP_refs(i) || BBP_lrefs(i))) { snprintf(buf,MAXPATHLEN,"%s/bat/%s",cwd,BBP_physical(i)); BUNappend(b, buf, FALSE); } } BBPunlock("CMDbbpLocation"); if (!(b->batDirty&2)) BATsetaccess(b, BAT_READ); pseudo(ret,b,"bbp","location"); return MAL_SUCCEED; }
str db_password_wrap(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { (void) mb; if (stk->stk[pci->argv[0]].vtype == TYPE_bat) { BAT *b = BATdescriptor(*getArgReference_bat(stk, pci, 1)); if (b == NULL) throw(SQL, "sql.password", SQLSTATE(HY002) RUNTIME_OBJECT_MISSING); BAT *bn = COLnew(b->hseqbase, TYPE_str, BATcount(b), TRANSIENT); if (bn == NULL) { BBPunfix(b->batCacheid); throw(SQL, "sql.password", SQLSTATE(HY001) MAL_MALLOC_FAIL); } BATiter bi = bat_iterator(b); BUN p, q; BATloop(b, p, q) { char *hash, *msg; msg = AUTHgetPasswordHash(&hash, cntxt, BUNtvar(bi, p)); if (msg != MAL_SUCCEED) { BBPunfix(b->batCacheid); BBPreclaim(bn); return msg; } if (BUNappend(bn, hash, false) != GDK_SUCCEED) { BBPunfix(b->batCacheid); BBPreclaim(bn); throw(SQL, "sql.password", SQLSTATE(HY001) MAL_MALLOC_FAIL); } GDKfree(hash); }
str batstr_2time_timestamptz(bat *res, const bat *bid, const int *digits, int *tz) { BAT *b, *dst; BATiter bi; BUN p, q; char *msg = NULL; if ((b = BATdescriptor(*bid)) == NULL) { throw(SQL, "batcalc.str_2time_timestamp", "Cannot access descriptor"); } bi = bat_iterator(b); dst = BATnew(TYPE_void, TYPE_timestamp, BATcount(b), TRANSIENT); if (dst == NULL) { BBPunfix(b->batCacheid); throw(SQL, "sql.timestamp", MAL_MALLOC_FAIL); } BATseqbase(dst, b->hseqbase); BATloop(b, p, q) { char *v = (char *) BUNtail(bi, p); union { lng l; timestamp r; } u; msg = str_2time_timestamptz(&u.r, &v, digits, tz); if (msg) { BBPunfix(dst->batCacheid); BBPunfix(b->batCacheid); return msg; } BUNappend(dst, &u.r, FALSE); }
static BAT * MATproject_any( BAT *map, BAT **bats, int len ) { BAT *res; int i; BUN j, cnt = BATcount(map); BATiter *bats_i; BUN *batsT; bte *mapT; res = BATnew(TYPE_void, bats[0]->ttype, cnt, TRANSIENT); batsT = (BUN*)GDKmalloc(sizeof(BUN) * len); bats_i = (BATiter*)GDKmalloc(sizeof(BATiter) * len); if (res == NULL || batsT == NULL || bats_i == NULL) { if (res) BBPreclaim(res); if (batsT) GDKfree(batsT); if (bats_i) GDKfree(bats_i); return NULL; } BATseqbase(res, map->hseqbase); mapT = (bte*)Tloc(map, 0); for (i=0; i<len; i++) { batsT[i] = 0; bats_i[i] = bat_iterator(bats[i]); } for (j=0; j<cnt; j++) BUNappend(res, BUNtail(bats_i[mapT[j]], batsT[mapT[j]]++), FALSE); GDKfree(batsT); GDKfree(bats_i); return res; }
str CMDbbpCount(bat *ret) { BAT *b, *bn; int i; lng l; b = BATnew(TYPE_void, TYPE_lng, getBBPsize(), TRANSIENT); if (b == 0) throw(MAL, "catalog.bbpCount", MAL_MALLOC_FAIL); BATseqbase(b,0); for (i = 1; i < getBBPsize(); i++) if (i != b->batCacheid) { if (BBP_logical(i) && (BBP_refs(i) || BBP_lrefs(i))) { bn = BATdescriptor(i); if (bn) { l = BATcount(bn); BUNappend(b, &l, FALSE); BBPunfix(bn->batCacheid); } } } if (!(b->batDirty&2)) BATsetaccess(b, BAT_READ); pseudo(ret,b,"bbp","count"); return MAL_SUCCEED; }
str JSONvalueTable(bat *ret, json *js) { BAT *bn; char *r; int i; JSON *jt; jt = JSONparse(*js, FALSE); // already validated CHECK_JSON(jt); bn = BATnew(TYPE_void, TYPE_json, 64, TRANSIENT); if (bn == NULL) throw(MAL, "json.values", MAL_MALLOC_FAIL); BATseqbase(bn, 0); bn->hsorted = 1; bn->hrevsorted = 0; bn->H->nonil = 1; bn->tsorted = 1; bn->trevsorted = 0; bn->T->nonil = 1; for (i = jt->elm[0].next; i; i = jt->elm[i].next) { if (jt->elm[i].kind == JSON_ELEMENT) r = JSONgetValue(jt, jt->elm[i].child); else r = JSONgetValue(jt, i); BUNappend(bn, r, FALSE); GDKfree(r); } BBPkeepref(*ret = bn->batCacheid); return MAL_SUCCEED; }
static str gsl_bat_chisqprob_cst(bat * retval, bat chi2, dbl datapoints) { BAT *b, *bn; BATiter bi; BUN p,q; dbl r; char *msg = NULL; if (datapoints == dbl_nil) { throw(MAL, "GSLbat_chisqprob_cst", "Parameter datapoints should not be nil"); } if (datapoints < 0) throw(MAL, "gsl.chi2prob", "Wrong value for datapoints"); if ((b = BATdescriptor(chi2)) == NULL) { throw(MAL, "chisqprob", "Cannot access descriptor"); } bi = bat_iterator(b); bn = BATnew(TYPE_void, TYPE_dbl, BATcount(b), TRANSIENT); if (bn == NULL){ BBPunfix(b->batCacheid); throw(MAL, "gsl.chisqprob", MAL_MALLOC_FAIL); } BATseqbase(bn, b->hseqbase); BATloop(b,p,q) { dbl d = *(dbl*)BUNtail(bi,p); if ((d == dbl_nil) || (d < 0)) throw(MAL, "gsl.chi2prob", "Wrong value for chi2"); r = gsl_cdf_chisq_Q(d, datapoints); BUNappend(bn, &r, FALSE); }
str CMDbbpKind(bat *ret) { BAT *b; int i; b = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); if (b == 0) throw(MAL, "catalog.bbpKind", MAL_MALLOC_FAIL); BATseqbase(b,0); BBPlock("CMDbbpKind"); for (i = 1; i < getBBPsize(); i++) if (i != b->batCacheid) if (BBP_logical(i) && (BBP_refs(i) || BBP_lrefs(i))) { char *mode = NULL; if ((BBP_status(i) & BBPDELETED) || !(BBP_status(i) & BBPPERSISTENT)) mode = "transient"; else mode = "persistent"; if (mode) BUNappend(b, mode, FALSE); } BBPunlock("CMDbbpKind"); if (!(b->batDirty&2)) BATsetaccess(b, BAT_READ); pseudo(ret,b,"bbp","kind"); return MAL_SUCCEED; }
static BAT* MATsortloop_( bte *map_res, BAT *i1, bte *map_i1, BUN cnt_i1, BAT *i2, bte map_i2, BUN cnt_i2) { int c; BUN val_i1 = BUNfirst(i1); BUN val_i2 = BUNfirst(i2); BUN end_i1 = val_i1 + cnt_i1; BUN end_i2 = val_i2 + cnt_i2; BATiter bi_i1 = bat_iterator(i1); BATiter bi_i2 = bat_iterator(i2); int (*cmp) (const void *, const void *) = BATatoms[i1->ttype].atomCmp; BAT *res = BATnew(TYPE_void, i1->ttype, cnt_i1 + cnt_i2, TRANSIENT); if (res == NULL) return NULL; BATseqbase(res, 0); if (map_i1 == NULL) { /* map_i1 = 0 */ while ( val_i1 < end_i1 && val_i2 < end_i2) { if ((c = cmp(BUNtail(bi_i1,val_i1),BUNtail(bi_i2,val_i2))) <= 0) { BUNappend(res, BUNtail(bi_i1,val_i1), FALSE); *map_res++ = 0; val_i1++; } else if (c > 0) { BUNappend(res, BUNtail(bi_i2,val_i2), FALSE); *map_res++ = map_i2; val_i2++; } } while ( val_i1 < end_i1 ) { BUNappend(res, BUNtail(bi_i1,val_i1), FALSE); *map_res++ = 0; val_i1++; } } else { while ( val_i1 < end_i1 && val_i2 < end_i2) { if ((c = cmp(BUNtail(bi_i1,val_i1),BUNtail(bi_i2,val_i2))) <= 0) { BUNappend(res, BUNtail(bi_i1,val_i1), FALSE); *map_res++ = *map_i1++; val_i1++; } else if (c > 0) { BUNappend(res, BUNtail(bi_i2,val_i2), FALSE); *map_res++ = map_i2; val_i2++; } } while ( val_i1 < end_i1 ) { BUNappend(res, BUNtail(bi_i1,val_i1), FALSE); *map_res++ = *map_i1++; val_i1++; } } while ( val_i2 < end_i2 ) { BUNappend(res, BUNtail(bi_i2,val_i2), FALSE); *map_res++ = map_i2; val_i2++; } return res; }
static str MDBgetFrame(BAT *b, BAT*bn, Client cntxt, MalBlkPtr mb, MalStkPtr s, int depth) { ValPtr v; int i; char *buf = 0; if (depth > 0) return MDBgetFrame(b,bn, cntxt, mb, s->up, depth - 1); if (s != 0) for (i = 0; i < s->stktop; i++, v++) { v = &s->stk[i]; ATOMformat(v->vtype, VALptr(v), &buf); BUNappend(b, getVarName(mb, i), FALSE); BUNappend(bn, buf, FALSE); GDKfree(buf); buf = NULL; } return MAL_SUCCEED; }
BUN append_inserted(BAT *b, BAT *i ) { BUN nr = 0, r; BATiter ii = bat_iterator(i); for (r = i->batInserted; r < BUNlast(i); r++) { BUNappend(b, BUNtail(ii,r), TRUE); nr++; } return nr; }
static void HASHinfo(BAT *bk, BAT *bv, Hash *h, str s) { BUN i; BUN j; BUN k; BUN cnt[COLLISION + 1]; BUNappend(bk, pre(s, "type"), FALSE); BUNappend(bv, ATOMname(h->type),FALSE); BUNappend(bk, pre(s, "mask"), FALSE); BUNappend(bv, local_utoa(h->lim),FALSE); for (i = 0; i < COLLISION + 1; i++) { cnt[i] = 0; } for (i = 0; i <= h->mask; i++) { j = HASHlist(h, i); for (k = 0; j; k++) j >>= 1; cnt[k]++; } for (i = 0; i < COLLISION + 1; i++) if (cnt[i]) { BUNappend(bk, pre(s, local_utoa(i?(((size_t)1)<<(i-1)):0)), FALSE); BUNappend(bv, local_utoa((size_t) cnt[i]), FALSE); } }
str batnil_2_timestamp(bat *res, const bat *bid) { BAT *b, *dst; BUN p, q; if ((b = BATdescriptor(*bid)) == NULL) { throw(SQL, "batcalc.nil_2_timestamp", "Cannot access descriptor"); } dst = BATnew(TYPE_void, TYPE_timestamp, BATcount(b), TRANSIENT); if (dst == NULL) { BBPunfix(b->batCacheid); throw(SQL, "sql.2_timestamp", MAL_MALLOC_FAIL); } BATseqbase(dst, b->hseqbase); BATloop(b, p, q) { timestamp r = *timestamp_nil; BUNappend(dst, &r, FALSE); }
str batnil_2_timestamp(bat *res, const bat *bid) { BAT *b, *dst; BUN p, q; if ((b = BATdescriptor(*bid)) == NULL) { throw(SQL, "batcalc.nil_2_timestamp", SQLSTATE(HY005) "Cannot access column descriptor"); } dst = COLnew(b->hseqbase, TYPE_timestamp, BATcount(b), TRANSIENT); if (dst == NULL) { BBPunfix(b->batCacheid); throw(SQL, "sql.2_timestamp", SQLSTATE(HY001) MAL_MALLOC_FAIL); } BATloop(b, p, q) { timestamp r = *timestamp_nil; if (BUNappend(dst, &r, FALSE) != GDK_SUCCEED) { BBPunfix(b->batCacheid); BBPreclaim(dst); throw(SQL, "sql.timestamp", SQLSTATE(HY001) MAL_MALLOC_FAIL); } }
str BKCappend_val_force_wrap(bat *r, const bat *bid, const void *u, const bit *force) { BAT *b; if ((b = BATdescriptor(*bid)) == NULL) throw(MAL, "bat.append", RUNTIME_OBJECT_MISSING); if ((b = setaccess(b, BAT_WRITE)) == NULL) throw(MAL, "bat.append", OPERATION_FAILED); if (b->ttype >= TYPE_str && ATOMstorage(b->ttype) >= TYPE_str) { if (u == 0 || *(str*)u == 0) u = (ptr) str_nil; else u = (ptr) *(str *)u; } if (BUNappend(b, u, *force) != GDK_SUCCEED) { BBPunfix(b->batCacheid); throw(MAL, "bat.append", GDK_EXCEPTION); } BBPkeepref(*r = b->batCacheid); return MAL_SUCCEED; }
/* * It is illustrative to dump the code when you * have encountered an error. */ str MDBgetDefinition(Client cntxt, MalBlkPtr m, MalStkPtr stk, InstrPtr p) { int i; bat *ret = getArgReference_bat(stk, p, 0); str ps; BAT *b = BATnew(TYPE_void, TYPE_str, 256, TRANSIENT); (void) cntxt; if (b == 0) throw(MAL, "mdb.getDefinition", MAL_MALLOC_FAIL); BATseqbase(b,0); for (i = 0; i < m->stop; i++) { ps = instruction2str(m,0, getInstrPtr(m, i), 1); BUNappend(b, ps, FALSE); GDKfree(ps); } if (!(b->batDirty&2)) BATsetaccess(b, BAT_READ); pseudo(ret,b,"view","fcn","stmt"); return MAL_SUCCEED; }
str CMDbbpLRefCount(bat *ret) { BAT *b; int i; b = BATnew(TYPE_void, TYPE_int, getBBPsize(), TRANSIENT); if (b == 0) throw(MAL, "catalog.bbpLRefCount", MAL_MALLOC_FAIL); BATseqbase(b,0); BBPlock("CMDbbpLRefCount"); for (i = 1; i < getBBPsize(); i++) if (i != b->batCacheid && BBP_logical(i) && (BBP_refs(i) || BBP_lrefs(i))) { int refs = BBP_lrefs(i); BUNappend(b, &refs, FALSE); } BBPunlock("CMDbbpLRefCount"); if (!(b->batDirty&2)) BATsetaccess(b, BAT_READ); pseudo(ret,b,"bbp","lrefcnt"); return MAL_SUCCEED; }
/* * The BAT dirty status:dirty => (mem != disk); diffs = not-committed */ str CMDbbpDirty(bat *ret) { BAT *b; int i; b = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); if (b == 0) throw(MAL, "catalog.bbpDirty", MAL_MALLOC_FAIL); BATseqbase(b,0); BBPlock("CMDbbpDirty"); for (i = 1; i < getBBPsize(); i++) if (i != b->batCacheid) if (BBP_logical(i) && (BBP_refs(i) || BBP_lrefs(i))) { BAT *bn = BBP_cache(i); BUNappend(b, bn ? BATdirty(bn) ? "dirty" : DELTAdirty(bn) ? "diffs" : "clean" : (BBP_status(i) & BBPSWAPPED) ? "diffs" : "clean", FALSE); } BBPunlock("CMDbbpDirty"); if (!(b->batDirty&2)) BATsetaccess(b, BAT_READ); pseudo(ret,b,"bbp","status"); return MAL_SUCCEED; }
/* * The BAT status is redundantly stored in CMDbat_info. */ str CMDbbpStatus(bat *ret) { BAT *b; int i; b = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); if (b == 0) throw(MAL, "catalog.bbpStatus", MAL_MALLOC_FAIL); BATseqbase(b,0); BBPlock("CMDbbpStatus"); for (i = 1; i < getBBPsize(); i++) if (i != b->batCacheid) if (BBP_logical(i) && (BBP_refs(i) || BBP_lrefs(i))) { char *loc = BBP_cache(i) ? "load" : "disk"; BUNappend(b, loc, FALSE); } BBPunlock("CMDbbpStatus"); if (!(b->batDirty&2)) BATsetaccess(b, BAT_READ); pseudo(ret,b,"bbp","status"); return MAL_SUCCEED; }
static void JSONunfoldContainer(JSON *jt, int idx, BAT *bo, BAT *bk, BAT *bv, oid *o) { int i, last; int cnt = 0; char *r; last = jt->elm[idx].tail; if (jt->elm[idx].kind == JSON_OBJECT) for (i = jt->elm[idx].next; i; i = jt->elm[i].next) { r = JSONgetValue(jt, i); BUNappend(bk, r, FALSE); GDKfree(r); r = JSONgetValue(jt, jt->elm[i].child); BUNappend(bv, r, FALSE); if (bo) BUNappend(bo, o, FALSE); (*o)++; GDKfree(r); if (i == last) break; } else if (jt->elm[idx].kind == JSON_ARRAY) for (i = jt->elm[idx].next; i; i = jt->elm[i].next) { r = GDKstrdup(str_nil); BUNappend(bk, r, FALSE); if (jt->elm[i].kind == JSON_VALUE) r = JSONgetValue(jt, jt->elm[i].child); else r = JSONgetValue(jt, i); BUNappend(bv, r, FALSE); if (bo) BUNappend(bo, o, FALSE); (*o)++; cnt++; GDKfree(r); if (i == last) break; } }
void QOTupdateStatistics(str nme, int actions, lng val) { BATiter bi; BUN p; oid idx; int ival=0, *ip= &ival; lng lval=0, *lp= &lval; QOTstatisticsInit(); MT_lock_set(&qotlock, "QOT statistics"); p = BUNfnd(BATmirror(qotStat[QOTnames]),(ptr)nme); if (p == BUN_NONE) { BUNappend(qotStat[QOTnames], nme, FALSE); BUNappend(qotStat[QOTcalls], &ival, FALSE); BUNappend(qotStat[QOTactions], &ival, FALSE); BUNappend(qotStat[QOTtimings], &lval, FALSE); p = BUNfnd(BATmirror(qotStat[QOTnames]),(ptr)nme); if (p == BUN_NONE){ MT_lock_unset(&qotlock, "QOT statistics"); return; } } bi = bat_iterator(qotStat[QOTnames]); idx = *(oid*) BUNhead(bi,p); p = BUNfnd(qotStat[QOTcalls],&idx); if (p == BUN_NONE) { #ifdef _Q_STATISTICS_DEBUG mnstr_printf(GDKout,"#Could not access 'calls'\n"); #endif MT_lock_unset(&qotlock, "QOT statistics"); return; } bi = bat_iterator(qotStat[QOTcalls]); ip = (int*) BUNtail(bi,p); *ip = *ip+1; bi.b->tsorted = bi.b->trevsorted = 0; bi.b->tkey = 0; p = BUNfnd(qotStat[QOTactions],&idx); if (p == BUN_NONE){ #ifdef _Q_STATISTICS_DEBUG mnstr_printf(GDKout,"#Could not access 'actions'\n"); #endif MT_lock_unset(&qotlock, "QOT statistics"); return; } bi = bat_iterator(qotStat[QOTactions]); ip = (int*) BUNtail(bi,p); *ip = *ip+ actions; bi.b->tsorted = bi.b->trevsorted = 0; bi.b->tkey = 0; p = BUNfnd(qotStat[QOTtimings],&idx); if (p == BUN_NONE){ #ifdef _Q_STATISTICS_DEBUG mnstr_printf(GDKout, "#Could not access 'timings'\n"); #endif MT_lock_unset(&qotlock, "QOT statistics"); return ; } bi = bat_iterator(qotStat[QOTtimings]); lp = (lng*) BUNtail(bi,p); *lp = *lp+ val; bi.b->tsorted = bi.b->trevsorted = 0; bi.b->tkey = 0; MT_lock_unset(&qotlock, "QOT statistics"); }
str RAPIeval(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci, bit grouped) { sql_func * sqlfun = NULL; str exprStr = *getArgReference_str(stk, pci, pci->retc + 1); SEXP x, env, retval; SEXP varname = R_NilValue; SEXP varvalue = R_NilValue; ParseStatus status; int i = 0; char argbuf[64]; char *argnames = NULL; size_t argnameslen; size_t pos; char* rcall = NULL; size_t rcalllen; int ret_cols = 0; /* int because pci->retc is int, too*/ str *args; int evalErr; char *msg = MAL_SUCCEED; BAT *b; node * argnode; int seengrp = FALSE; rapiClient = cntxt; if (!RAPIEnabled()) { throw(MAL, "rapi.eval", "Embedded R has not been enabled. Start server with --set %s=true", rapi_enableflag); } if (!rapiInitialized) { throw(MAL, "rapi.eval", "Embedded R initialization has failed"); } if (!grouped) { sql_subfunc *sqlmorefun = (*(sql_subfunc**) getArgReference(stk, pci, pci->retc)); if (sqlmorefun) sqlfun = (*(sql_subfunc**) getArgReference(stk, pci, pci->retc))->func; } else { sqlfun = *(sql_func**) getArgReference(stk, pci, pci->retc); } args = (str*) GDKzalloc(sizeof(str) * pci->argc); if (args == NULL) { throw(MAL, "rapi.eval", SQLSTATE(HY001) MAL_MALLOC_FAIL); } // get the lock even before initialization of the R interpreter, as this can take a second and must be done only once. MT_lock_set(&rapiLock); env = PROTECT(eval(lang1(install("new.env")), R_GlobalEnv)); assert(env != NULL); // first argument after the return contains the pointer to the sql_func structure // NEW macro temporarily renamed to MNEW to allow including sql_catalog.h if (sqlfun != NULL && sqlfun->ops->cnt > 0) { int carg = pci->retc + 2; argnode = sqlfun->ops->h; while (argnode) { char* argname = ((sql_arg*) argnode->data)->name; args[carg] = GDKstrdup(argname); carg++; argnode = argnode->next; } } // the first unknown argument is the group, we don't really care for the rest. argnameslen = 2; for (i = pci->retc + 2; i < pci->argc; i++) { if (args[i] == NULL) { if (!seengrp && grouped) { args[i] = GDKstrdup("aggr_group"); seengrp = TRUE; } else { snprintf(argbuf, sizeof(argbuf), "arg%i", i - pci->retc - 1); args[i] = GDKstrdup(argbuf); } } argnameslen += strlen(args[i]) + 2; /* extra for ", " */ } // install the MAL variables into the R environment // we can basically map values to int ("INTEGER") or double ("REAL") for (i = pci->retc + 2; i < pci->argc; i++) { int bat_type = getBatType(getArgType(mb,pci,i)); // check for BAT or scalar first, keep code left if (!isaBatType(getArgType(mb,pci,i))) { b = COLnew(0, getArgType(mb, pci, i), 0, TRANSIENT); if (b == NULL) { msg = createException(MAL, "rapi.eval", SQLSTATE(HY001) MAL_MALLOC_FAIL); goto wrapup; } if ( getArgType(mb,pci,i) == TYPE_str) { if (BUNappend(b, *getArgReference_str(stk, pci, i), false) != GDK_SUCCEED) { BBPreclaim(b); b = NULL; msg = createException(MAL, "rapi.eval", SQLSTATE(HY001) MAL_MALLOC_FAIL); goto wrapup; } } else { if (BUNappend(b, getArgReference(stk, pci, i), false) != GDK_SUCCEED) { BBPreclaim(b); b = NULL; msg = createException(MAL, "rapi.eval", SQLSTATE(HY001) MAL_MALLOC_FAIL); goto wrapup; } } } else { b = BATdescriptor(*getArgReference_bat(stk, pci, i)); if (b == NULL) { msg = createException(MAL, "rapi.eval", SQLSTATE(HY001) MAL_MALLOC_FAIL); goto wrapup; } } // check the BAT count, if it is bigger than RAPI_MAX_TUPLES, fail if (BATcount(b) > RAPI_MAX_TUPLES) { msg = createException(MAL, "rapi.eval", "Got "BUNFMT" rows, but can only handle "LLFMT". Sorry.", BATcount(b), (lng) RAPI_MAX_TUPLES); BBPunfix(b->batCacheid); goto wrapup; } varname = PROTECT(Rf_install(args[i])); varvalue = bat_to_sexp(b, bat_type); if (varvalue == NULL) { msg = createException(MAL, "rapi.eval", "unknown argument type "); goto wrapup; } BBPunfix(b->batCacheid); // install vector into R environment Rf_defineVar(varname, varvalue, env); UNPROTECT(2); } /* we are going to evaluate the user function within an anonymous function call: * ret <- (function(arg1){return(arg1*2)})(42) * the user code is put inside the {}, this keeps our environment clean (TM) and gives * a clear path for return values, namely using the builtin return() function * this is also compatible with PL/R */ pos = 0; argnames = malloc(argnameslen); if (argnames == NULL) { msg = createException(MAL, "rapi.eval", SQLSTATE(HY001) MAL_MALLOC_FAIL); goto wrapup; } argnames[0] = '\0'; for (i = pci->retc + 2; i < pci->argc; i++) { pos += snprintf(argnames + pos, argnameslen - pos, "%s%s", args[i], i < pci->argc - 1 ? ", " : ""); } rcalllen = 2 * pos + strlen(exprStr) + 100; rcall = malloc(rcalllen); if (rcall == NULL) { msg = createException(MAL, "rapi.eval", SQLSTATE(HY001) MAL_MALLOC_FAIL); goto wrapup; } snprintf(rcall, rcalllen, "ret <- as.data.frame((function(%s){%s})(%s), nm=NA, stringsAsFactors=F)\n", argnames, exprStr, argnames); free(argnames); argnames = NULL; #ifdef _RAPI_DEBUG_ printf("# R call %s\n",rcall); #endif x = R_ParseVector(mkString(rcall), 1, &status, R_NilValue); if (LENGTH(x) != 1 || status != PARSE_OK) { msg = createException(MAL, "rapi.eval", "Error parsing R expression '%s'. ", exprStr); goto wrapup; } retval = R_tryEval(VECTOR_ELT(x, 0), env, &evalErr); if (evalErr != FALSE) { char* errormsg = strdup(R_curErrorBuf()); size_t c; if (errormsg == NULL) { msg = createException(MAL, "rapi.eval", "Error running R expression."); goto wrapup; } // remove newlines from error message so it fits into a MAPI error (lol) for (c = 0; c < strlen(errormsg); c++) { if (errormsg[c] == '\r' || errormsg[c] == '\n') { errormsg[c] = ' '; } } msg = createException(MAL, "rapi.eval", "Error running R expression: %s", errormsg); free(errormsg); goto wrapup; } // ret should be a data frame with exactly as many columns as we need from retc ret_cols = LENGTH(retval); if (ret_cols != pci->retc) { msg = createException(MAL, "rapi.eval", "Expected result of %d columns, got %d", pci->retc, ret_cols); goto wrapup; } // collect the return values for (i = 0; i < pci->retc; i++) { SEXP ret_col = VECTOR_ELT(retval, i); int bat_type = getBatType(getArgType(mb,pci,i)); if (bat_type == TYPE_any || bat_type == TYPE_void) { getArgType(mb,pci,i) = bat_type; msg = createException(MAL, "rapi.eval", "Unknown return value, possibly projecting with no parameters."); goto wrapup; } // hand over the vector into a BAT b = sexp_to_bat(ret_col, bat_type); if (b == NULL) { msg = createException(MAL, "rapi.eval", "Failed to convert column %i", i); goto wrapup; } // bat return if (isaBatType(getArgType(mb,pci,i))) { *getArgReference_bat(stk, pci, i) = b->batCacheid; } else { // single value return, only for non-grouped aggregations BATiter li = bat_iterator(b); if (VALinit(&stk->stk[pci->argv[i]], bat_type, BUNtail(li, 0)) == NULL) { // TODO BUNtail here msg = createException(MAL, "rapi.eval", SQLSTATE(HY001) MAL_MALLOC_FAIL); goto wrapup; } } msg = MAL_SUCCEED; } /* unprotect environment, so it will be eaten by the GC. */ UNPROTECT(1); wrapup: MT_lock_unset(&rapiLock); if (argnames) free(argnames); if (rcall) free(rcall); for (i = 0; i < pci->argc; i++) GDKfree(args[i]); GDKfree(args); return msg; }
str FITSloadTable(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr pci) { mvc *m = NULL; sql_schema *sch; sql_table *fits_fl, *fits_tbl, *tbl = NULL; sql_column *col; sql_subtype tpe; fitsfile *fptr; str tname = *(str*)getArgReference(stk, pci, 1); str fname; str msg = MAL_SUCCEED; oid rid = oid_nil, frid = oid_nil; int status = 0, cnum = 0, fid, hdu, hdutype, i, j, anynull = 0, mtype; int *tpcode = NULL; long *rep = NULL, *wid = NULL, rows; char keywrd[80], **cname, nm[FLEN_VALUE]; ptr nilptr; msg = getSQLContext(cntxt, mb, &m, NULL); if (msg) return msg; sch = mvc_bind_schema(m, "sys"); fits_tbl = mvc_bind_table(m, sch, "fits_tables"); if (fits_tbl == NULL) { msg = createException(MAL, "fits.loadtable", "FITS catalog is missing.\n"); return msg; } tbl = mvc_bind_table(m, sch, tname); if (tbl) { msg = createException(MAL, "fits.loadtable", "Table %s is already created.\n", tname); return msg; } col = mvc_bind_column(m, fits_tbl, "name"); rid = table_funcs.column_find_row(m->session->tr, col, tname, NULL); if (rid == oid_nil) { msg = createException(MAL, "fits.loadtable", "Table %s is unknown in FITS catalog. Attach first the containing file\n", tname); return msg; } /* Open FITS file and move to the table HDU */ col = mvc_bind_column(m, fits_tbl, "file_id"); fid = *(int*)table_funcs.column_find_value(m->session->tr, col, rid); fits_fl = mvc_bind_table(m, sch, "fits_files"); col = mvc_bind_column(m, fits_fl, "id"); frid = table_funcs.column_find_row(m->session->tr, col, (void *)&fid, NULL); col = mvc_bind_column(m, fits_fl, "name"); fname = (char *)table_funcs.column_find_value(m->session->tr, col, frid); if (fits_open_file(&fptr, fname, READONLY, &status)) { msg = createException(MAL, "fits.loadtable", "Missing FITS file %s.\n", fname); return msg; } col = mvc_bind_column(m, fits_tbl, "hdu"); hdu = *(int*)table_funcs.column_find_value(m->session->tr, col, rid); fits_movabs_hdu(fptr, hdu, &hdutype, &status); if (hdutype != ASCII_TBL && hdutype != BINARY_TBL) { msg = createException(MAL, "fits.loadtable", "HDU %d is not a table.\n", hdu); fits_close_file(fptr, &status); return msg; } /* create a SQL table to hold the FITS table */ /* col = mvc_bind_column(m, fits_tbl, "columns"); cnum = *(int*) table_funcs.column_find_value(m->session->tr, col, rid); */ fits_get_num_cols(fptr, &cnum, &status); tbl = mvc_create_table(m, sch, tname, tt_table, 0, SQL_PERSIST, 0, cnum); tpcode = (int *)GDKzalloc(sizeof(int) * cnum); rep = (long *)GDKzalloc(sizeof(long) * cnum); wid = (long *)GDKzalloc(sizeof(long) * cnum); cname = (char **)GDKzalloc(sizeof(char *) * cnum); for (j = 1; j <= cnum; j++) { /* fits_get_acolparms(fptr, j, cname, &tbcol, tunit, tform, &tscal, &tzero, tnull, tdisp, &status); */ snprintf(keywrd, 80, "TTYPE%d", j); fits_read_key(fptr, TSTRING, keywrd, nm, NULL, &status); if (status) { snprintf(nm, FLEN_VALUE, "column_%d", j); status = 0; } cname[j - 1] = GDKstrdup(toLower(nm)); fits_get_coltype(fptr, j, &tpcode[j - 1], &rep[j - 1], &wid[j - 1], &status); fits2subtype(&tpe, tpcode[j - 1], rep[j - 1], wid[j - 1]); /* mnstr_printf(cntxt->fdout,"#%d %ld %ld - M: %s\n", tpcode[j-1], rep[j-1], wid[j-1], tpe.type->sqlname); */ mvc_create_column(m, tbl, cname[j - 1], &tpe); } /* data load */ fits_get_num_rows(fptr, &rows, &status); mnstr_printf(cntxt->fdout,"#Loading %ld rows in table %s\n", rows, tname); for (j = 1; j <= cnum; j++) { BAT *tmp = NULL; int time0 = GDKms(); mtype = fits2mtype(tpcode[j - 1]); nilptr = ATOMnil(mtype); col = mvc_bind_column(m, tbl, cname[j - 1]); tmp = BATnew(TYPE_void, mtype, rows); if ( tmp == NULL){ GDKfree(tpcode); GDKfree(rep); GDKfree(wid); GDKfree(cname); throw(MAL,"fits.load", MAL_MALLOC_FAIL); } BATseqbase(tmp, 0); if (rows > (long)REMAP_PAGE_MAXSIZE) BATmmap(tmp, STORE_MMAP, STORE_MMAP, STORE_MMAP, STORE_MMAP, 0); if (mtype != TYPE_str) { fits_read_col(fptr, tpcode[j - 1], j, 1, 1, rows, nilptr, (void *)BUNtloc(bat_iterator(tmp), BUNfirst(tmp)), &anynull, &status); BATsetcount(tmp, rows); tmp->tsorted = 0; tmp->trevsorted = 0; } else { /* char *v = GDKzalloc(wid[j-1]);*/ int bsize = 50; int tm0, tloadtm = 0, tattachtm = 0; int batch = bsize, k; char **v = (char **) GDKzalloc(sizeof(char *) * bsize); for(i = 0; i < bsize; i++) v[i] = GDKzalloc(wid[j-1]); for(i = 0; i < rows; i += batch) { batch = rows - i < bsize ? rows - i: bsize; tm0 = GDKms(); fits_read_col(fptr, tpcode[j - 1], j, 1 + i, 1, batch, nilptr, (void *)v, &anynull, &status); tloadtm += GDKms() - tm0; tm0 = GDKms(); for(k = 0; k < batch ; k++) BUNappend(tmp, v[k], TRUE); tattachtm += GDKms() - tm0; } for(i = 0; i < bsize ; i++) GDKfree(v[i]); GDKfree(v); mnstr_printf(cntxt->fdout,"#String column load %d ms, BUNappend %d ms\n", tloadtm, tattachtm); } if (status) { char buf[FLEN_ERRMSG + 1]; fits_read_errmsg(buf); msg = createException(MAL, "fits.loadtable", "Cannot load column %s of %s table: %s.\n", cname[j - 1], tname, buf); break; } mnstr_printf(cntxt->fdout,"#Column %s loaded for %d ms\t", cname[j-1], GDKms() - time0); store_funcs.append_col(m->session->tr, col, tmp, TYPE_bat); mnstr_printf(cntxt->fdout,"#Total %d ms\n", GDKms() - time0); BBPunfix(tmp->batCacheid); } GDKfree(tpcode); GDKfree(rep); GDKfree(wid); GDKfree(cname); fits_close_file(fptr, &status); return msg; }
static str MseedLoadIntern(BAT **bbtime, BAT **bbdata, str targetfile) { str msg = MAL_SUCCEED; MSRecord *msr = 0; BAT *btime, *bdata; int verbose = 1; //int ppackets = 2; int reclen = -1; int dataflag = 1; int retcode; int j; time_t t; struct tm *tm; timestamp ts; char file[BUFSIZ]; date d; daytime dt; tzone tz; int ms,stepsize,minutes=0; snprintf(file,BUFSIZ,"%s%c%s",vaultpath,DIR_SEP,targetfile); if ( access(file,R_OK) ) throw(MAL, "mseed.load", "Cannot access %s\n", file); btime = BATnew(TYPE_void,TYPE_timestamp,0); if ( btime == NULL) throw(MAL,"mseed.load",MAL_MALLOC_FAIL); BATseqbase(btime,0); bdata = BATnew(TYPE_void,TYPE_int,0); if ( bdata == NULL){ BBPreleaseref(btime->batCacheid); throw(MAL,"mseed.load",MAL_MALLOC_FAIL); } BATseqbase(bdata,0); if ( btime == NULL || bdata == NULL ){ if ( btime) BBPreleaseref(btime->batCacheid); if ( bdata) BBPreleaseref(bdata->batCacheid); throw(MAL, "mseed.load", MAL_MALLOC_FAIL); } *bbtime = btime; *bbdata = bdata; MTIMEtzone_create(&tz, &minutes); while ( (retcode = ms_readmsr (&msr, file, reclen, NULL, NULL, 1, dataflag, verbose)) == MS_NOERROR ) { stepsize = 1000000/ msr->samprate; /* collect the statistics */ switch(msr->sampletype){ case 'i': if (msr->datasamples) for ( j=0;j< msr->samplecnt; j++){ t= MS_HPTIME2EPOCH(msr->starttime); tm = gmtime(&t); tm->tm_year += (tm->tm_year > 80?1900:2000); ms = (msr->starttime % HPTMODULUS)/1000; tm->tm_mon++; MTIMEdate_create(&d,&tm->tm_year, &tm->tm_mon, &tm->tm_mday); MTIMEdaytime_create(&dt, &tm->tm_hour, &tm->tm_min, &tm->tm_sec, &ms); MTIMEtimestamp_create(&ts, &d, &dt, &tz); BUNappend(btime, (ptr) &ts , FALSE); BUNappend(bdata, (ptr)(((int*) msr->datasamples)+j) , FALSE); msr->starttime += stepsize; } break; case 'a': case 'f': case 'd': default: msg = createException(MAL,"mseed.load","data type not yet implemented"); goto wrapup; } } wrapup: /* Make sure everything is cleaned up */ ms_readmsr (&msr, NULL, 0, NULL, NULL, 0, 0, 0); if ( retcode != MS_ENDOFFILE ) throw(MAL, "mseed.load", "Cannot read %s: %s\n", targetfile, ms_errorstr(retcode)); return msg; }
str CMDbbp(bat *ID, bat *NS, bat *HT, bat *TT, bat *CNT, bat *REFCNT, bat *LREFCNT, bat *LOCATION, bat *HEAT, bat *DIRTY, bat *STATUS, bat *KIND) { BAT *id, *ns, *ht, *tt, *cnt, *refcnt, *lrefcnt, *location, *heat, *dirty, *status, *kind, *bn; int i; char buf[MAXPATHLEN]; id = BATnew(TYPE_void, TYPE_int, getBBPsize(), TRANSIENT); ns = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); ht = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); tt = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); cnt = BATnew(TYPE_void, TYPE_lng, getBBPsize(), TRANSIENT); refcnt = BATnew(TYPE_void, TYPE_int, getBBPsize(), TRANSIENT); lrefcnt = BATnew(TYPE_void, TYPE_int, getBBPsize(), TRANSIENT); location = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); heat = BATnew(TYPE_void, TYPE_int, getBBPsize(), TRANSIENT); dirty = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); status = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); kind = BATnew(TYPE_void, TYPE_str, getBBPsize(), TRANSIENT); if (!id || !ns || !ht || !tt || !cnt || !refcnt || !lrefcnt || !location || !heat || !dirty || !status || !kind) { BBPreclaim(id); BBPreclaim(ns); BBPreclaim(ht); BBPreclaim(tt); BBPreclaim(cnt); BBPreclaim(refcnt); BBPreclaim(lrefcnt); BBPreclaim(location); BBPreclaim(heat); BBPreclaim(dirty); BBPreclaim(status); BBPreclaim(kind); throw(MAL, "catalog.bbp", MAL_MALLOC_FAIL); } BATseqbase(id, 0); BATseqbase(ns, 0); BATseqbase(ht, 0); BATseqbase(tt, 0); BATseqbase(cnt, 0); BATseqbase(refcnt, 0); BATseqbase(lrefcnt, 0); BATseqbase(location, 0); BATseqbase(heat, 0); BATseqbase(dirty, 0); BATseqbase(status, 0); BATseqbase(kind, 0); for (i = 1; i < getBBPsize(); i++) { if (BBP_logical(i) && (BBP_refs(i) || BBP_lrefs(i))) { bn = BATdescriptor(i); if (bn) { lng l = BATcount(bn); int heat_ = BBP_lastused(i); char *loc = BBP_cache(i) ? "load" : "disk"; char *mode = "persistent"; int refs = BBP_refs(i); int lrefs = BBP_lrefs(i); if ((BBP_status(i) & BBPDELETED) || !(BBP_status(i) & BBPPERSISTENT)) mode = "transient"; snprintf(buf, MAXPATHLEN, "%s", BBP_physical(i)); BUNappend(id, &i, FALSE); BUNappend(ns, BBP_logical(i), FALSE); BUNappend(ht, BATatoms[BAThtype(bn)].name, FALSE); BUNappend(tt, BATatoms[BATttype(bn)].name, FALSE); BUNappend(cnt, &l, FALSE); BUNappend(refcnt, &refs, FALSE); BUNappend(lrefcnt, &lrefs, FALSE); BUNappend(location, buf, FALSE); BUNappend(heat, &heat_, FALSE); BUNappend(dirty, bn ? BATdirty(bn) ? "dirty" : DELTAdirty(bn) ? "diffs" : "clean" : (BBP_status(i) & BBPSWAPPED) ? "diffs" : "clean", FALSE); BUNappend(status, loc, FALSE); BUNappend(kind, mode, FALSE); BBPunfix(bn->batCacheid); } } } BBPkeepref(*ID = id->batCacheid); BBPkeepref(*NS = ns->batCacheid); BBPkeepref(*HT = ht->batCacheid); BBPkeepref(*TT = tt->batCacheid); BBPkeepref(*CNT = cnt->batCacheid); BBPkeepref(*REFCNT = refcnt->batCacheid); BBPkeepref(*LREFCNT = lrefcnt->batCacheid); BBPkeepref(*LOCATION = location->batCacheid); BBPkeepref(*HEAT = heat->batCacheid); BBPkeepref(*DIRTY = dirty->batCacheid); BBPkeepref(*STATUS = status->batCacheid); BBPkeepref(*KIND = kind->batCacheid); return MAL_SUCCEED; }