📄 gdk_bat.c
字号:
#line 65 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"#include "monetdb_config.h"#include "gdk.h"#ifdef ALIGN#undef ALIGN#endif#define ALIGN(n,b) ((b)?(b)*(1+(((n)-1)/(b))):n)#define ATOMneedheap(tpe) (BATatoms[tpe].atomHeap != NULL)char *BATstring_h = "h";char *BATstring_t = "t";int default_ident( char *s){ return ((s) == BATstring_h || (s) == BATstring_t);}voidBATinit_idents(BAT * bn) { bn->hident = (char*)BATstring_h; bn->tident = (char*)BATstring_t;}BAT *BATcreatedesc(int ht, int tt, int heapnames){ BAT *bn;#line 99 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx" BATstore *bs = (BATstore *) GDKzalloc(sizeof(BATstore));#line 105 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx" assert(ht >= 0 && tt >= 0); bn = &bs->B; bn->H = &bs->H; bn->T = &bs->T; bn->P = &bs->P; bn->U = &bs->U;#line 115 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx" bn->htype = ht; bn->ttype = tt; bn->hkey = FALSE; bn->tkey = FALSE; bn->hsorted = ((bit) ATOMlinear(ht) ? GDK_SORTED : FALSE); bn->tsorted = ((bit) ATOMlinear(tt) ? GDK_SORTED : FALSE); bn->GDKversion = GDKLIBRARY; bn->batBuns = &bn->U->buns; bn->hident = (char*)BATstring_h; bn->tident = (char*)BATstring_t; bn->halign = OIDnew(2); bn->talign = bn->halign + 1; bn->hseqbase = (ht == TYPE_void) ? oid_nil : 0; bn->tseqbase = (tt == TYPE_void) ? oid_nil : 0; bn->batPersistence = TRANSIENT; bn->H->props = bn->T->props = NULL; bn->void_tid = -1; bn->void_cnt = 0; bn->void_seq1 = oid_nil; bn->void_seq2 = oid_nil;#line 140 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx" BBPinsert(bn);#line 144 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx" bn->batBuns->filename = NULL; bn->batMapbuns = STORE_UNSET; if (heapnames) { str nme = BBP_physical(bn->batCacheid); bn->batBuns->filename = (str) GDKmalloc(strlen(nme) + 12); GDKfilepath(bn->batBuns->filename, NULL, nme, "buns"); if (ATOMneedheap(ht)) { bn->hheap = (Heap*)GDKzalloc(sizeof(Heap)); bn->hheap->filename = (str) GDKmalloc(strlen(nme) + 12); GDKfilepath(bn->hheap->filename, NULL, nme, "hheap"); bn->batMaphheap = STORE_UNSET; } if (ATOMneedheap(tt)) { bn->theap = (Heap*)GDKzalloc(sizeof(Heap)); bn->theap->filename = (str) GDKmalloc(strlen(nme) + 12); GDKfilepath(bn->theap->filename, NULL, nme, "theap"); bn->batMaptheap = STORE_UNSET; } } bn->batDirty = TRUE; return bn;}intBATelmshift(BAT *b){ int sh, i = BUNsize(b) >> 1; for (sh = 0; i != 0; sh++) { i >>= 1; } if (BUNsize(b) != ((size_t) 1 << sh)) { return -1; } return sh;}voidBATsetdims(BAT *b){ if (ATOMalign(b->htype) >= ATOMalign(b->ttype)) { b->tloc = ALIGN(ATOMsize(b->htype), ATOMalign(b->htype)); b->hloc = 0; b->dims.bunwidth = b->tloc + ATOMsize(b->ttype); if (b->ttype == TYPE_void) b->tloc = 0; } else { b->hloc = ALIGN(ATOMsize(b->ttype), ATOMalign(b->htype)); b->tloc = 0; b->dims.bunwidth = b->hloc + ATOMsize(b->htype); if (b->htype == TYPE_void) b->hloc = 0; } if (b->dims.bunwidth == 0) b->dims.bunwidth = 1; b->dims.bunwidth = ALIGN(b->dims.bunwidth, ATOMalign(b->htype)); b->dims.bunwidth = ALIGN(b->dims.bunwidth, ATOMalign(b->ttype)); b->dims.bunshift = BATelmshift(b); b->hvarsized = BATatoms[b->htype].varsized; b->tvarsized = BATatoms[b->ttype].varsized;}#line 219 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"BAT *BATnewstorage(int ht, int tt, size_t cap){ BAT *bn, *recycled; int vv = (!ht && !tt); bn = recycled = BBPrecycle(ht,tt,cap); if (!bn) bn = BATcreatedesc(ht, tt, (ht||tt)); if (bn == NULL) return NULL; if (!recycled) { BATsetdims(bn); /* alloc the main heaps */ if ((ht||tt) && HEAPalloc(bn->batBuns, cap, bn->dims.bunwidth) < 0) { return NULL; } if (ATOMheap(ht, bn->hheap, cap) < 0) { HEAPfree(bn->batBuns); GDKfree(bn->hheap); if (bn->theap) GDKfree(bn->theap); return NULL; } if (ATOMheap(tt, bn->theap, cap) < 0) { HEAPfree(bn->batBuns); if (bn->hheap) { HEAPfree(bn->hheap); GDKfree(bn->hheap); } GDKfree(bn->theap); return NULL; } DELTAinit(bn); BBPcacheit(bn); } if (vv) { bn->batBuns->base = (char*)1; DELTAinit(bn); } return bn;}BAT *BATnew(int ht, int tt, size_t cap){ ERRORcheck((ht < 0) || (ht > GDKatomcnt), "BATnew:ht error\n"); ERRORcheck((tt < 0) || (tt > GDKatomcnt), "BATnew:tt error\n"); /* determine the minimum size. BATTINY bats will be cached */ cap = MAX(BATTINY, cap); if (ht == TYPE_void && tt == TYPE_void) cap = 1; return BATnewstorage(ht, tt, cap);}#line 281 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"BAT *BATclone(BAT *b, size_t cap){ BAT *c = BATnew(b->htype, b->ttype, cap); if (c->htype == TYPE_void && b->hseqbase != oid_nil) BATseqbase(c, b->hseqbase); if (c->ttype == TYPE_void && b->tseqbase != oid_nil) BATseqbase(BATmirror(c), b->tseqbase); return c;}#line 306 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"size_tBATguess(BAT *b){ size_t newcap; BATcheck(b, "BATguess"); newcap = BATcount(b); if (newcap < 10 * BATTINY) return newcap; if (newcap < 50 * BATTINY) return newcap / 2; if (newcap < 100 * BATTINY) return newcap / 10; return newcap / 100;}size_tBATgrows(BAT *b){ size_t oldcap, newcap; BATcheck(b, "BATgrows"); newcap = oldcap = BATcapacity(b); if (newcap < BATTINY) newcap = 2 * BATTINY; else if (newcap < 10 * BATTINY) newcap = 4 * newcap; else if (newcap < 50 * BATTINY) newcap = 2 * newcap; else newcap = (size_t) ((double) newcap * BATMARGIN); if (newcap == oldcap) newcap += 10; /* if we can extend in reserved space, do not miss the opportunity */ if (b->batBuns->size + BUNsize(b) <= b->batBuns->maxsize) { size_t rescap = oldcap + (b->batBuns->maxsize - b->batBuns->size) / BUNsize(b); newcap = MIN(newcap, rescap); } return newcap;}#line 358 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"BAT *BATextend(BAT *b, size_t newcap){ size_t heap_size; int ret; assert(b->htype || b->ttype); BATcheck(b, "BATextend");#line 375 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx" if (newcap <= BATbuncount(b)) { return b; } heap_size = BUNsize(b) * newcap; DELTAsave(b); ret = HEAPextend(b->batBuns, heap_size); DELTAload(b); if (ret < 0) return NULL; HASHdestroy(b); return b;}#line 392 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"#line 404 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"#line 405 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"BAT *BATclear(BAT *b){ int bs; BUN p, q; int voidbat; BAT *bm; BATcheck(b, "BATclear"); bs = BUNsize(b); voidbat = 0; bm = BATmirror(b); if (BAThdense(b) && b->htype == TYPE_void) { voidbat = 1; } if (BATtdense(b) && b->ttype == TYPE_void) { voidbat = 1; } /* small BAT: delete all elements by hand */ if (!voidbat && BATcount(b) < 20) { BATloopDEL(b, p, q, bs) { p = BUNdelete(b, p, FALSE); } return b; } /* kill all search accelerators */ if (b->hhash) { HASHremove(b); } if (b->thash) { HASHremove(bm); } /* we must dispose of all inserted atoms */ if (b->batDeleted == b->batInserted && BATatoms[b->htype].atomDel == NULL && BATatoms[b->ttype].atomDel == NULL) { Heap hh, th; /* no stable elements: we do a quick heap clean */ /* need to clean heap which keep data even though the BUNs got removed. This means reinitialize when free > 0 */ size_t cap = 0; hh.filename = th.filename = NULL; if (b->hheap && b->hheap->free > 0) { if (ATOMheap(b->htype, &hh, cap) < 0) return NULL; } if (b->theap && b->theap->free > 0 && ATOMheap(b->ttype, &th, cap) < 0) { if (b->hheap && b->hheap->free > 0) HEAPfree(&hh); return NULL; } if (b->hheap && b->hheap->free > 0) { HEAPfree(b->hheap); *b->hheap = hh; } if (b->theap && b->theap->free > 0) { HEAPfree(b->theap); *b->theap = th; } } else { /* do heap-delete of all inserted atoms */ void (*hatmdel)(Heap*,var_t*) = BATatoms[b->htype].atomDel; void (*tatmdel)(Heap*,var_t*) = BATatoms[b->ttype].atomDel; if (hatmdel || tatmdel) { for(p = b->batInserted, q = BUNlast(b); p < q; p += bs) { if (hatmdel) (*hatmdel)(b->hheap, (var_t*) BUNhloc(b,p)); if (tatmdel) (*tatmdel)(b->theap, (var_t*) BUNtloc(b,p)); } } } b->batFirst = b->batInserted; b->batBuns->free = b->batInserted - b->batBuns->base; BATsetcount(b,0); b->batDirty = TRUE; return b; }/* free a cached BAT; leave the bat descriptor cached */intBATfree(BAT *b){ BATcheck(b, "BATfree"); /* deallocate all memory for a bat */ if (b->batCacheid < 0) b = BBP_cache(-(b->batCacheid)); if (b->hident && !default_ident(b->hident)) GDKfree(b->hident); b->hident = BATstring_h; if (b->tident && !default_ident(b->tident)) GDKfree(b->tident); b->tident = BATstring_t; if (b->H->props) PROPdestroy(b->H->props); b->H->props = NULL; if (b->T->props) PROPdestroy(b->T->props); b->T->props = NULL; HASHdestroy(b); DELTAsave(b); /* convert to disk format */ if (b->htype != b->ttype || b->htype != TYPE_void) HEAPfree(b->batBuns); if (b->hheap) HEAPfree(b->hheap); if (b->theap) HEAPfree(b->theap); b = BBP_cache(-b->batCacheid); if (b) { BBP_cache(b->batCacheid) = NULL; GDKfree(b); } return 0;}/* free a cached BAT descriptor */voidBATdestroy( BAT *b ){ if (b->hident && !default_ident(b->hident)) GDKfree(b->hident); b->hident = BATstring_h; if (b->tident && !default_ident(b->tident)) GDKfree(b->tident); b->tident = BATstring_t; if (b->hheap) GDKfree(b->hheap); if (b->theap) GDKfree(b->theap); if (b->H->props) PROPdestroy(b->H->props); if (b->T->props) PROPdestroy(b->T->props); GDKfree(b);}#line 555 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"#line 594 "/export/scratch0/monet/monet.GNU.64.64.d.14791/MonetDB/src/gdk/gdk_bat.mx"static int heapcopy(Heap* dst, Heap* src, int *remap) { if (*remap && src->storage == STORE_MMAP) { /* use copy-on-write mmap for isolatable copy */ *dst = *src; dst->base = (char*) -1; dst->filename = GDKstrdup(src->filename); if (dst->filename) { char path[PATHLENGTH]; GDKfilepath(path, BATDIR, dst->filename, NULL); dst->base = GDKmmap(path, MMAP_READ|MMAP_SEQUENTIAL|MMAP_COPY, 0, dst->maxsize); if (dst->base != (char*) -1) { dst->copied = 1; dst->storage = STORE_PRIV; return 0; } GDKfree(dst->filename); dst->filename = NULL; } } *remap = 0; return HEAPcopy(dst, src);}static void heapfree(Heap* src, Heap *dst) { if (dst->filename == NULL) { dst->filename = src->filename; src->filename = NULL; } HEAPfree(src); *src = *dst;}static int wrongtype(int t1, int t2) { /* check if types are compatible. be extremely forgiving */ if (t1) { t1 = ATOMtype(ATOMstorage(t1)); t2 = ATOMtype(ATOMstorage(t2)); if (t1 != t2) { if (ATOMvarsized(t1) || ATOMvarsized(t2) || ATOMsize(t1) != ATOMsize(t2) ||
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -