Line data Source code
1 : /*
2 : * SPDX-License-Identifier: MPL-2.0
3 : *
4 : * This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 : *
8 : * Copyright 2024 MonetDB Foundation;
9 : * Copyright August 2008 - 2023 MonetDB B.V.;
10 : * Copyright 1997 - July 2008 CWI.
11 : */
12 :
13 : /*
14 : * @a M. L. Kersten, P. Boncz, N. Nes
15 : * @* BAT Module
16 : * In this Chapter we describe the BAT implementation in more detail.
17 : * The routines mentioned are primarily meant to simplify the library
18 : * implementation.
19 : *
20 : * @+ BAT Construction
21 : * BATs are implemented in several blocks of memory, prepared for disk
22 : * storage and easy shipment over a network.
23 : *
24 : * The BAT starts with a descriptor, which indicates the required BAT
25 : * library version and the BAT administration details. In particular,
26 : * it describes the binary relationship maintained and the location of
27 : * fields required for storage.
28 : *
29 : * The general layout of the BAT in this implementation is as follows.
30 : * Each BAT comes with a heap for the loc-size buns and, optionally,
31 : * with heaps to manage the variable-sized data items of both
32 : * dimensions. The buns are assumed to be stored as loc-size objects.
33 : * This is essentially an array of structs to store the associations.
34 : * The size is determined at BAT creation time using an upper bound on
35 : * the number of elements to be accommodated. In case of overflow,
36 : * its storage space is extended automatically.
37 : *
38 : * The capacity of a BAT places an upper limit on the number of BUNs
39 : * to be stored initially. The actual space set aside may be quite
40 : * large. Moreover, the size is aligned to int boundaries to speedup
41 : * access and avoid some machine limitations.
42 : *
43 : * Initialization of the variable parts rely on type specific routines
44 : * called atomHeap.
45 : */
46 : #include "monetdb_config.h"
47 : #include "gdk.h"
48 : #include "gdk_private.h"
49 : #include "mutils.h"
50 :
51 : #ifdef ALIGN
52 : #undef ALIGN
53 : #endif
54 : #define ALIGN(n,b) ((b)?(b)*(1+(((n)-1)/(b))):n)
55 :
56 : #define ATOMneedheap(tpe) (BATatoms[tpe].atomHeap != NULL)
57 :
58 : BAT *
59 19495171 : BATcreatedesc(oid hseq, int tt, bool heapnames, role_t role, uint16_t width)
60 : {
61 19495171 : bat bid;
62 19495171 : BAT *bn;
63 19495171 : Heap *h = NULL, *vh = NULL;
64 :
65 : /*
66 : * Alloc space for the BAT and its dependent records.
67 : */
68 19495171 : assert(tt >= 0);
69 :
70 19495171 : if (heapnames) {
71 9506186 : if ((h = GDKmalloc(sizeof(Heap))) == NULL) {
72 : return NULL;
73 : }
74 19009257 : *h = (Heap) {
75 9505042 : .farmid = BBPselectfarm(role, tt, offheap),
76 : .dirty = true,
77 : .refs = ATOMIC_VAR_INIT(1),
78 : };
79 :
80 9504215 : if (ATOMneedheap(tt)) {
81 747595 : if ((vh = GDKmalloc(sizeof(Heap))) == NULL) {
82 0 : GDKfree(h);
83 0 : return NULL;
84 : }
85 747502 : *vh = (Heap) {
86 747579 : .farmid = BBPselectfarm(role, tt, varheap),
87 : .dirty = true,
88 : .refs = ATOMIC_VAR_INIT(1),
89 : };
90 : }
91 : }
92 :
93 19493107 : bid = BBPallocbat(tt);
94 19487677 : if (bid == 0) {
95 0 : GDKfree(h);
96 0 : GDKfree(vh);
97 0 : return NULL;
98 : }
99 19487677 : bn = BBP_desc(bid);
100 :
101 : /*
102 : * Fill in basic column info
103 : */
104 38984096 : *bn = (BAT) {
105 : .batCacheid = bid,
106 : .hseqbase = hseq,
107 :
108 : .ttype = tt,
109 : .tkey = true,
110 : .tnonil = true,
111 : .tnil = false,
112 19496419 : .tsorted = ATOMlinear(tt),
113 : .trevsorted = ATOMlinear(tt),
114 19496419 : .tascii = tt == TYPE_str,
115 : .tseqbase = oid_nil,
116 : .tminpos = BUN_NONE,
117 : .tmaxpos = BUN_NONE,
118 : .tunique_est = 0.0,
119 :
120 : .batRole = role,
121 : .batTransient = true,
122 : .batRestricted = BAT_WRITE,
123 : .theap = h,
124 : .tvheap = vh,
125 19487677 : .creator_tid = MT_getpid(),
126 : };
127 :
128 19496419 : if (bn->theap) {
129 9506365 : bn->theap->parentid = bn->batCacheid;
130 9506365 : const char *nme = BBP_physical(bn->batCacheid);
131 9506365 : settailname(bn->theap, nme, tt, width);
132 :
133 9503604 : if (bn->tvheap) {
134 747484 : bn->tvheap->parentid = bn->batCacheid;
135 747484 : strconcat_len(bn->tvheap->filename,
136 : sizeof(bn->tvheap->filename),
137 : nme, ".theap", NULL);
138 : }
139 : }
140 19493529 : char name[MT_NAME_LEN];
141 19493529 : snprintf(name, sizeof(name), "heaplock%d", bn->batCacheid); /* fits */
142 19493529 : MT_lock_init(&bn->theaplock, name);
143 19491207 : snprintf(name, sizeof(name), "BATlock%d", bn->batCacheid); /* fits */
144 19491207 : MT_lock_init(&bn->batIdxLock, name);
145 19494472 : snprintf(name, sizeof(name), "hashlock%d", bn->batCacheid); /* fits */
146 19494472 : MT_rwlock_init(&bn->thashlock, name);
147 19496293 : return bn;
148 : }
149 :
150 : uint8_t
151 19043529 : ATOMelmshift(int sz)
152 : {
153 19043529 : uint8_t sh;
154 19043529 : int i = sz >> 1;
155 :
156 36121503 : for (sh = 0; i != 0; sh++) {
157 17077974 : i >>= 1;
158 : }
159 19043529 : return sh;
160 : }
161 :
162 :
163 : void
164 9510948 : BATsetdims(BAT *b, uint16_t width)
165 : {
166 9510948 : b->twidth = b->ttype == TYPE_str ? width > 0 ? width : 1 : ATOMsize(b->ttype);
167 9510948 : b->tshift = ATOMelmshift(b->twidth);
168 9510948 : assert_shift_width(b->tshift, b->twidth);
169 9510948 : }
170 :
171 : const char *
172 1260 : BATtailname(const BAT *b)
173 : {
174 1260 : if (b->ttype == TYPE_str) {
175 340 : switch (b->twidth) {
176 250 : case 1:
177 250 : return "tail1";
178 75 : case 2:
179 75 : return "tail2";
180 15 : case 4:
181 : #if SIZEOF_VAR_T == 8
182 15 : return "tail4";
183 : case 8:
184 : #endif
185 : break;
186 : default:
187 0 : MT_UNREACHABLE();
188 : }
189 : }
190 : return "tail";
191 : }
192 :
193 : void
194 9570168 : settailname(Heap *restrict tail, const char *restrict physnme, int tt, int width)
195 : {
196 9570168 : if (tt == TYPE_str) {
197 779098 : switch (width) {
198 725910 : case 0:
199 : case 1:
200 725910 : strconcat_len(tail->filename,
201 : sizeof(tail->filename), physnme,
202 : ".tail1", NULL);
203 725910 : return;
204 48845 : case 2:
205 48845 : strconcat_len(tail->filename,
206 : sizeof(tail->filename), physnme,
207 : ".tail2", NULL);
208 48845 : return;
209 4343 : case 4:
210 : #if SIZEOF_VAR_T == 8
211 4343 : strconcat_len(tail->filename,
212 : sizeof(tail->filename), physnme,
213 : ".tail4", NULL);
214 4343 : return;
215 : case 8:
216 : #endif
217 : break;
218 : default:
219 0 : MT_UNREACHABLE();
220 : }
221 : }
222 8791070 : strconcat_len(tail->filename, sizeof(tail->filename), physnme,
223 : ".tail", NULL);
224 : }
225 :
226 : /*
227 : * @- BAT allocation
228 : * Allocate BUN heap and variable-size atomheaps (see e.g. strHeap).
229 : * We now initialize new BATs with their heapname such that the
230 : * modified HEAPalloc/HEAPextend primitives can possibly use memory
231 : * mapped files as temporary heap storage.
232 : *
233 : * In case of huge bats, we want HEAPalloc to write a file to disk,
234 : * and memory map it. To make this possible, we must provide it with
235 : * filenames.
236 : */
237 : BAT *
238 9505704 : COLnew2(oid hseq, int tt, BUN cap, role_t role, uint16_t width)
239 : {
240 9505704 : BAT *bn;
241 :
242 9505704 : assert(cap <= BUN_MAX);
243 9505704 : assert(hseq <= oid_nil);
244 9505704 : ERRORcheck((tt < 0) || (tt > GDKatomcnt), "tt error\n", NULL);
245 :
246 : /* round up to multiple of BATTINY */
247 9505704 : if (cap < BUN_MAX - BATTINY)
248 9505691 : cap = (cap + BATTINY - 1) & ~(BATTINY - 1);
249 9505704 : if (ATOMstorage(tt) == TYPE_msk) {
250 150712 : if (cap < 8*BATTINY)
251 : cap = 8*BATTINY;
252 : else
253 53530 : cap = (cap + 31) & ~(BUN)31;
254 9354992 : } else if (cap < BATTINY)
255 : cap = BATTINY;
256 : /* limit the size */
257 3292927 : if (cap > BUN_MAX)
258 : cap = BUN_MAX;
259 :
260 9505704 : bn = BATcreatedesc(hseq, tt, true, role, width);
261 9505172 : if (bn == NULL)
262 : return NULL;
263 :
264 9505172 : BATsetdims(bn, width);
265 9504129 : bn->batCapacity = cap;
266 :
267 9504129 : if (ATOMstorage(tt) == TYPE_msk)
268 150712 : cap /= 8; /* 8 values per byte */
269 :
270 : /* alloc the main heaps */
271 9504129 : if (tt && HEAPalloc(bn->theap, cap, bn->twidth) != GDK_SUCCEED) {
272 0 : goto bailout;
273 : }
274 :
275 9504491 : if (bn->tvheap && width == 0 && ATOMheap(tt, bn->tvheap, cap) != GDK_SUCCEED) {
276 0 : HEAPfree(bn->theap, true);
277 0 : goto bailout;
278 : }
279 9504490 : DELTAinit(bn);
280 9504394 : if (BBPcacheit(bn, true) != GDK_SUCCEED) {
281 : /* cannot happen, function always returns success */
282 0 : goto bailout;
283 : }
284 9505400 : TRC_DEBUG(ALGO, "-> " ALGOBATFMT "\n", ALGOBATPAR(bn));
285 : return bn;
286 0 : bailout:
287 0 : BBPclear(bn->batCacheid);
288 0 : return NULL;
289 : }
290 :
291 : BAT *
292 9257701 : COLnew(oid hseq, int tt, BUN cap, role_t role)
293 : {
294 9257701 : return COLnew2(hseq, tt, cap, role, 0);
295 : }
296 :
297 : BAT *
298 4415309 : BATdense(oid hseq, oid tseq, BUN cnt)
299 : {
300 4415309 : BAT *bn;
301 :
302 4415309 : bn = COLnew(hseq, TYPE_void, 0, TRANSIENT);
303 4415176 : if (bn != NULL) {
304 4415176 : BATtseqbase(bn, tseq);
305 4415087 : BATsetcount(bn, cnt);
306 4414818 : TRC_DEBUG(ALGO, OIDFMT "," OIDFMT "," BUNFMT
307 : "-> " ALGOBATFMT "\n", hseq, tseq, cnt,
308 : ALGOBATPAR(bn));
309 : }
310 4414818 : return bn;
311 : }
312 :
313 : BAT *
314 0 : BATattach(int tt, const char *heapfile, role_t role)
315 : {
316 0 : BAT *bn;
317 0 : char *p;
318 0 : size_t m;
319 0 : FILE *f;
320 :
321 0 : ERRORcheck(tt <= 0 , "bad tail type (<=0)\n", NULL);
322 0 : ERRORcheck(ATOMvarsized(tt) && ATOMstorage(tt) != TYPE_str, "bad tail type (varsized and not str)\n", NULL);
323 0 : ERRORcheck(heapfile == NULL, "bad heapfile name\n", NULL);
324 :
325 0 : if ((f = MT_fopen(heapfile, "rb")) == NULL) {
326 0 : GDKsyserror("BATattach: cannot open %s\n", heapfile);
327 0 : return NULL;
328 : }
329 0 : if (ATOMstorage(tt) == TYPE_str) {
330 0 : size_t n;
331 0 : char *s;
332 0 : int c, u;
333 :
334 0 : if ((bn = COLnew(0, tt, 0, role)) == NULL) {
335 0 : fclose(f);
336 0 : return NULL;
337 : }
338 0 : m = 4096;
339 0 : n = 0;
340 0 : u = 0;
341 0 : s = p = GDKmalloc(m);
342 0 : if (p == NULL) {
343 0 : fclose(f);
344 0 : BBPreclaim(bn);
345 0 : return NULL;
346 : }
347 0 : while ((c = getc(f)) != EOF) {
348 0 : if (n == m) {
349 0 : m += 4096;
350 0 : s = GDKrealloc(p, m);
351 0 : if (s == NULL) {
352 0 : GDKfree(p);
353 0 : BBPreclaim(bn);
354 0 : fclose(f);
355 0 : return NULL;
356 : }
357 0 : p = s;
358 0 : s = p + n;
359 : }
360 0 : if (c == '\n' && n > 0 && s[-1] == '\r') {
361 : /* deal with CR-LF sequence */
362 0 : s[-1] = c;
363 : } else {
364 0 : *s++ = c;
365 0 : n++;
366 : }
367 0 : if (u) {
368 0 : if ((c & 0xC0) == 0x80)
369 0 : u--;
370 : else
371 0 : goto notutf8;
372 0 : } else if ((c & 0xF8) == 0xF0)
373 : u = 3;
374 0 : else if ((c & 0xF0) == 0xE0)
375 : u = 2;
376 0 : else if ((c & 0xE0) == 0xC0)
377 : u = 1;
378 0 : else if ((c & 0x80) == 0x80)
379 0 : goto notutf8;
380 0 : else if (c == 0) {
381 0 : if (BUNappend(bn, p, false) != GDK_SUCCEED) {
382 0 : BBPreclaim(bn);
383 0 : fclose(f);
384 0 : GDKfree(p);
385 0 : return NULL;
386 : }
387 : s = p;
388 : n = 0;
389 : }
390 : }
391 0 : fclose(f);
392 0 : GDKfree(p);
393 0 : if (n > 0) {
394 0 : BBPreclaim(bn);
395 0 : GDKerror("last string is not null-terminated\n");
396 0 : return NULL;
397 : }
398 : } else {
399 0 : struct stat st;
400 0 : int atomsize;
401 0 : BUN cap;
402 0 : lng n;
403 :
404 0 : if (fstat(fileno(f), &st) < 0) {
405 0 : GDKsyserror("BATattach: cannot stat %s\n", heapfile);
406 0 : fclose(f);
407 0 : return NULL;
408 : }
409 0 : atomsize = ATOMsize(tt);
410 0 : if (st.st_size % atomsize != 0) {
411 0 : fclose(f);
412 0 : GDKerror("heapfile size not integral number of atoms\n");
413 0 : return NULL;
414 : }
415 0 : if (ATOMstorage(tt) == TYPE_msk ?
416 : (st.st_size > (off_t) (BUN_MAX / 8)) :
417 0 : ((size_t) (st.st_size / atomsize) > (size_t) BUN_MAX)) {
418 0 : fclose(f);
419 0 : GDKerror("heapfile too large\n");
420 0 : return NULL;
421 : }
422 0 : cap = (BUN) (ATOMstorage(tt) == TYPE_msk ?
423 0 : st.st_size * 8 :
424 0 : st.st_size / atomsize);
425 0 : bn = COLnew(0, tt, cap, role);
426 0 : if (bn == NULL) {
427 0 : fclose(f);
428 0 : return NULL;
429 : }
430 0 : p = Tloc(bn, 0);
431 0 : n = (lng) st.st_size;
432 0 : while (n > 0 && (m = fread(p, 1, (size_t) MIN(1024*1024, n), f)) > 0) {
433 0 : p += m;
434 0 : n -= m;
435 : }
436 0 : fclose(f);
437 0 : if (n > 0) {
438 0 : GDKerror("couldn't read the complete file\n");
439 0 : BBPreclaim(bn);
440 0 : return NULL;
441 : }
442 0 : BATsetcount(bn, cap);
443 0 : bn->tnonil = cap == 0;
444 0 : bn->tnil = false;
445 0 : bn->tseqbase = oid_nil;
446 0 : if (cap > 1) {
447 0 : bn->tsorted = false;
448 0 : bn->trevsorted = false;
449 0 : bn->tkey = false;
450 : } else {
451 0 : bn->tsorted = ATOMlinear(tt);
452 0 : bn->trevsorted = ATOMlinear(tt);
453 0 : bn->tkey = true;
454 : }
455 : }
456 : return bn;
457 :
458 0 : notutf8:
459 0 : fclose(f);
460 0 : BBPreclaim(bn);
461 0 : GDKfree(p);
462 0 : GDKerror("input is not UTF-8\n");
463 0 : return NULL;
464 : }
465 :
466 : /*
467 : * If the BAT runs out of storage for BUNS it will reallocate space.
468 : * For memory mapped BATs we simple extend the administration after
469 : * having an assurance that the BAT still can be safely stored away.
470 : */
471 : BUN
472 22037 : BATgrows(BAT *b)
473 : {
474 22037 : BUN oldcap, newcap;
475 :
476 22037 : BATcheck(b, 0);
477 :
478 22037 : newcap = oldcap = BATcapacity(b);
479 22037 : if (newcap < BATTINY)
480 : newcap = 2 * BATTINY;
481 22037 : else if (newcap < 10 * BATTINY)
482 20746 : newcap = 4 * newcap;
483 1291 : else if (newcap < 50 * BATTINY)
484 780 : newcap = 2 * newcap;
485 511 : else if ((double) newcap * BATMARGIN <= (double) BUN_MAX)
486 511 : newcap = (BUN) ((double) newcap * BATMARGIN);
487 : else
488 : newcap = BUN_MAX;
489 22037 : if (newcap == oldcap) {
490 0 : if (newcap <= BUN_MAX - 10)
491 0 : newcap += 10;
492 : else
493 : newcap = BUN_MAX;
494 : }
495 22037 : if (ATOMstorage(b->ttype) == TYPE_msk) /* round up to multiple of 32 */
496 2 : newcap = (newcap + 31) & ~(BUN)31;
497 : return newcap;
498 : }
499 :
500 : /*
501 : * The routine should ensure that the BAT keeps its location in the
502 : * BAT buffer.
503 : *
504 : * Overflow in the other heaps are dealt with in the atom routines.
505 : * Here we merely copy their references into the new administration
506 : * space.
507 : */
508 : gdk_return
509 182248 : BATextend(BAT *b, BUN newcap)
510 : {
511 182248 : size_t theap_size;
512 182248 : gdk_return rc = GDK_SUCCEED;
513 :
514 182248 : assert(newcap <= BUN_MAX);
515 182248 : BATcheck(b, GDK_FAIL);
516 : /*
517 : * The main issue is to properly predict the new BAT size.
518 : * storage overflow. The assumption taken is that capacity
519 : * overflow is rare. It is changed only when the position of
520 : * the next available BUN surpasses the free area marker. Be
521 : * aware that the newcap should be greater than the old value,
522 : * otherwise you may easily corrupt the administration of
523 : * malloc.
524 : */
525 182248 : if (newcap <= BATcapacity(b)) {
526 : return GDK_SUCCEED;
527 : }
528 :
529 31695 : if (ATOMstorage(b->ttype) == TYPE_msk) {
530 1099 : newcap = (newcap + 31) & ~(BUN)31; /* round up to multiple of 32 */
531 1099 : theap_size = (size_t) (newcap / 8); /* in bytes */
532 : } else {
533 30596 : theap_size = (size_t) newcap << b->tshift;
534 : }
535 :
536 31695 : MT_lock_set(&b->theaplock);
537 31680 : if (b->theap->base) {
538 31680 : TRC_DEBUG(HEAP, "HEAPgrow in BATextend %s %zu %zu\n",
539 : b->theap->filename, b->theap->size, theap_size);
540 31680 : rc = HEAPgrow(&b->theap, theap_size, b->batRestricted == BAT_READ);
541 31696 : if (rc == GDK_SUCCEED)
542 31696 : b->batCapacity = newcap;
543 : } else {
544 0 : b->batCapacity = newcap;
545 : }
546 31696 : MT_lock_unset(&b->theaplock);
547 :
548 31696 : return rc;
549 : }
550 :
551 :
552 :
553 : /*
554 : * @+ BAT destruction
555 : * BATclear quickly removes all elements from a BAT. It must respect
556 : * the transaction rules; so stable elements must be moved to the
557 : * "deleted" section of the BAT (they cannot be fully deleted
558 : * yet). For the elements that really disappear, we must free
559 : * heapspace. As an optimization, in the case of no stable elements, we quickly empty
560 : * the heaps by copying a standard small empty image over them.
561 : */
562 : gdk_return
563 427 : BATclear(BAT *b, bool force)
564 : {
565 427 : BUN p, q;
566 :
567 427 : BATcheck(b, GDK_FAIL);
568 :
569 427 : if (!force && b->batInserted > 0) {
570 0 : GDKerror("cannot clear committed BAT\n");
571 0 : return GDK_FAIL;
572 : }
573 :
574 427 : TRC_DEBUG(ALGO, ALGOBATFMT "\n", ALGOBATPAR(b));
575 :
576 : /* kill all search accelerators */
577 427 : HASHdestroy(b);
578 427 : IMPSdestroy(b);
579 427 : OIDXdestroy(b);
580 427 : STRMPdestroy(b);
581 427 : RTREEdestroy(b);
582 427 : PROPdestroy(b);
583 :
584 427 : bat tvp = 0;
585 :
586 : /* we must dispose of all inserted atoms */
587 427 : MT_lock_set(&b->theaplock);
588 427 : if (force && BATatoms[b->ttype].atomDel == NULL) {
589 420 : assert(b->tvheap == NULL || b->tvheap->parentid == b->batCacheid);
590 : /* no stable elements: we do a quick heap clean */
591 : /* need to clean heap which keeps data even though the
592 : BUNs got removed. This means reinitialize when
593 : free > 0
594 : */
595 420 : if (b->tvheap && b->tvheap->free > 0) {
596 21 : Heap *th = GDKmalloc(sizeof(Heap));
597 :
598 21 : if (th == NULL) {
599 0 : MT_lock_unset(&b->theaplock);
600 0 : return GDK_FAIL;
601 : }
602 21 : *th = (Heap) {
603 21 : .farmid = b->tvheap->farmid,
604 21 : .parentid = b->tvheap->parentid,
605 : .dirty = true,
606 21 : .hasfile = b->tvheap->hasfile,
607 : .refs = ATOMIC_VAR_INIT(1),
608 : };
609 21 : strcpy_len(th->filename, b->tvheap->filename, sizeof(th->filename));
610 21 : if (ATOMheap(b->ttype, th, 0) != GDK_SUCCEED) {
611 0 : MT_lock_unset(&b->theaplock);
612 0 : return GDK_FAIL;
613 : }
614 21 : tvp = b->tvheap->parentid;
615 21 : HEAPdecref(b->tvheap, false);
616 21 : b->tvheap = th;
617 : }
618 : } else {
619 : /* do heap-delete of all inserted atoms */
620 7 : void (*tatmdel)(Heap*,var_t*) = BATatoms[b->ttype].atomDel;
621 :
622 : /* TYPE_str has no del method, so we shouldn't get here */
623 7 : assert(tatmdel == NULL || b->twidth == sizeof(var_t));
624 0 : if (tatmdel) {
625 0 : BATiter bi = bat_iterator_nolock(b);
626 :
627 0 : for (p = b->batInserted, q = BATcount(b); p < q; p++)
628 0 : (*tatmdel)(b->tvheap, (var_t*) BUNtloc(bi,p));
629 0 : b->tvheap->dirty = true;
630 : }
631 : }
632 :
633 427 : b->batInserted = 0;
634 427 : b->batCount = 0;
635 427 : if (b->ttype == TYPE_void)
636 0 : b->batCapacity = 0;
637 427 : b->theap->free = 0;
638 427 : BAThseqbase(b, 0);
639 427 : BATtseqbase(b, ATOMtype(b->ttype) == TYPE_oid ? 0 : oid_nil);
640 427 : b->theap->dirty = true;
641 427 : b->tnonil = true;
642 427 : b->tnil = false;
643 427 : b->tsorted = b->trevsorted = ATOMlinear(b->ttype);
644 427 : b->tnosorted = b->tnorevsorted = 0;
645 427 : b->tkey = true;
646 427 : b->tnokey[0] = b->tnokey[1] = 0;
647 427 : b->tminpos = b->tmaxpos = BUN_NONE;
648 427 : b->tunique_est = 0;
649 427 : MT_lock_unset(&b->theaplock);
650 427 : if (tvp != 0 && tvp != b->batCacheid)
651 0 : BBPrelease(tvp);
652 : return GDK_SUCCEED;
653 : }
654 :
655 : /* free a cached BAT; leave the bat descriptor cached */
656 : void
657 357046 : BATfree(BAT *b)
658 : {
659 357046 : if (b == NULL)
660 : return;
661 :
662 : /* deallocate all memory for a bat */
663 357046 : MT_rwlock_rdlock(&b->thashlock);
664 357046 : BUN nunique = BUN_NONE;
665 357046 : if (b->thash && b->thash != (Hash *) 1) {
666 2035 : nunique = b->thash->nunique;
667 : }
668 357046 : MT_rwlock_rdunlock(&b->thashlock);
669 357046 : HASHfree(b);
670 357046 : IMPSfree(b);
671 357046 : OIDXfree(b);
672 357046 : STRMPfree(b);
673 357046 : RTREEfree(b);
674 357046 : MT_lock_set(&b->theaplock);
675 357046 : if (nunique != BUN_NONE) {
676 2035 : b->tunique_est = (double) nunique;
677 : }
678 : /* wait until there are no other references to the heap; a
679 : * reference is possible in e.g. BBPsync that uses a
680 : * bat_iterator directly on the BBP_desc, i.e. without fix */
681 357046 : while (b->theap && (ATOMIC_GET(&b->theap->refs) & HEAPREFS) > 1) {
682 0 : MT_lock_unset(&b->theaplock);
683 0 : MT_sleep_ms(1);
684 357046 : MT_lock_set(&b->theaplock);
685 : }
686 357046 : if (b->theap) {
687 357046 : assert((ATOMIC_GET(&b->theap->refs) & HEAPREFS) == 1);
688 357046 : assert(b->theap->parentid == b->batCacheid);
689 357046 : HEAPfree(b->theap, false);
690 : }
691 : /* wait until there are no other references to the heap; a
692 : * reference is possible in e.g. BBPsync that uses a
693 : * bat_iterator directly on the BBP_desc, i.e. without fix */
694 31555 : while (b->tvheap && (ATOMIC_GET(&b->tvheap->refs) & HEAPREFS) > 1) {
695 0 : MT_lock_unset(&b->theaplock);
696 0 : MT_sleep_ms(1);
697 357046 : MT_lock_set(&b->theaplock);
698 : }
699 357046 : if (b->tvheap) {
700 31555 : assert((ATOMIC_GET(&b->tvheap->refs) & HEAPREFS) == 1);
701 31555 : assert(b->tvheap->parentid == b->batCacheid);
702 31555 : HEAPfree(b->tvheap, false);
703 : }
704 357046 : MT_lock_unset(&b->theaplock);
705 : }
706 :
707 : /* free a cached BAT descriptor */
708 : void
709 19523100 : BATdestroy(BAT *b)
710 : {
711 19523100 : if (b->tvheap) {
712 29646 : GDKfree(b->tvheap);
713 : }
714 19523100 : PROPdestroy_nolock(b);
715 19521758 : MT_lock_destroy(&b->theaplock);
716 19521351 : MT_lock_destroy(&b->batIdxLock);
717 19520819 : MT_rwlock_destroy(&b->thashlock);
718 19520189 : if (b->theap) {
719 350718 : GDKfree(b->theap);
720 : }
721 19520090 : if (b->oldtail) {
722 0 : ATOMIC_AND(&b->oldtail->refs, ~DELAYEDREMOVE);
723 : /* the bat has not been committed, so we cannot remove
724 : * the old tail file */
725 0 : HEAPdecref(b->oldtail, false);
726 0 : b->oldtail = NULL;
727 : }
728 19520090 : *b = (BAT) {
729 : .batCacheid = 0,
730 : };
731 19520090 : }
732 :
733 : /*
734 : * @+ BAT copying
735 : *
736 : * BAT copying is an often used operation. So it deserves attention.
737 : * When making a copy of a BAT, the following aspects are of
738 : * importance:
739 : *
740 : * - the requested head and tail types. The purpose of the copy may be
741 : * to slightly change these types (e.g. void <-> oid). We may also
742 : * remap between types as long as they share the same
743 : * ATOMstorage(type), i.e. the types have the same physical
744 : * implementation. We may even want to allow 'dirty' trick such as
745 : * viewing a flt-column suddenly as int.
746 : *
747 : * To allow such changes, the desired column-types is a
748 : * parameter of COLcopy.
749 : *
750 : * - access mode. If we want a read-only copy of a read-only BAT, a
751 : * VIEW may do (in this case, the user may be after just an
752 : * independent BAT header and id). This is indicated by the
753 : * parameter (writable = FALSE).
754 : *
755 : * In other cases, we really want an independent physical copy
756 : * (writable = TRUE). Changing the mode to BAT_WRITE will be a
757 : * zero-cost operation if the BAT was copied with (writable = TRUE).
758 : *
759 : * In GDK, the result is a BAT that is BAT_WRITE iff (writable ==
760 : * TRUE).
761 : *
762 : * In these cases the copy becomes a logical view on the original,
763 : * which ensures that the original cannot be modified or destroyed
764 : * (which could affect the shared heaps).
765 : */
766 : static bool
767 404 : wrongtype(int t1, int t2)
768 : {
769 : /* check if types are compatible. be extremely forgiving */
770 404 : if (t1 != TYPE_void) {
771 404 : t1 = ATOMtype(ATOMstorage(t1));
772 404 : t2 = ATOMtype(ATOMstorage(t2));
773 404 : if (t1 != t2) {
774 354 : if (ATOMvarsized(t1) ||
775 354 : ATOMvarsized(t2) ||
776 354 : t1 == TYPE_msk || t2 == TYPE_msk ||
777 354 : ATOMsize(t1) != ATOMsize(t2))
778 0 : return true;
779 : }
780 : }
781 : return false;
782 : }
783 :
784 : /*
785 : * There are four main implementation cases:
786 : * (1) we are allowed to return a view (zero effort),
787 : * (2) the result is void,void (zero effort),
788 : * (3) we can copy the heaps (memcopy, or even VM page sharing)
789 : * (4) we must insert BUN-by-BUN into the result (fallback)
790 : * The latter case is still optimized for the case that the result
791 : * is bat[void,T] for a simple fixed-size type T. In that case we
792 : * do inline array[T] inserts.
793 : */
794 : BAT *
795 48587 : COLcopy(BAT *b, int tt, bool writable, role_t role)
796 : {
797 48587 : bool slowcopy = false;
798 48587 : BAT *bn = NULL;
799 48587 : BATiter bi;
800 48587 : char strhash[GDK_STRHASHSIZE];
801 :
802 48587 : BATcheck(b, NULL);
803 :
804 : /* maybe a bit ugly to change the requested bat type?? */
805 48587 : if (b->ttype == TYPE_void && !writable)
806 48587 : tt = TYPE_void;
807 :
808 48587 : if (tt != b->ttype && wrongtype(tt, b->ttype)) {
809 0 : GDKerror("wrong tail-type requested\n");
810 0 : return NULL;
811 : }
812 :
813 : /* in case of a string bat, we save the string heap hash table
814 : * while we have the lock so that we can restore it in the copy;
815 : * this is because during our operation, a parallel thread could
816 : * be adding strings to the vheap which would modify the hash
817 : * table and that would result in buckets containing values
818 : * beyond the original vheap that we're copying */
819 48587 : MT_lock_set(&b->theaplock);
820 48589 : BAT *pb = NULL, *pvb = NULL;
821 48589 : if (b->theap->parentid != b->batCacheid) {
822 10811 : pb = BBP_desc(b->theap->parentid);
823 10811 : MT_lock_set(&pb->theaplock);
824 : }
825 48589 : if (b->tvheap &&
826 16492 : b->tvheap->parentid != b->batCacheid &&
827 11597 : b->tvheap->parentid != b->theap->parentid) {
828 10937 : pvb = BBP_desc(b->tvheap->parentid);
829 10937 : MT_lock_set(&pvb->theaplock);
830 : }
831 48588 : bi = bat_iterator_nolock(b);
832 48588 : if (ATOMstorage(b->ttype) == TYPE_str && b->tvheap->free >= GDK_STRHASHSIZE)
833 12243 : memcpy(strhash, b->tvheap->base, GDK_STRHASHSIZE);
834 :
835 48588 : bat_iterator_incref(&bi);
836 48588 : if (pvb)
837 10936 : MT_lock_unset(&pvb->theaplock);
838 48587 : if (pb)
839 10811 : MT_lock_unset(&pb->theaplock);
840 48587 : MT_lock_unset(&b->theaplock);
841 :
842 : /* first try case (1); create a view, possibly with different
843 : * atom-types */
844 48588 : if (!writable &&
845 48588 : role == TRANSIENT &&
846 20305 : bi.restricted == BAT_READ &&
847 16174 : ATOMstorage(b->ttype) != TYPE_msk && /* no view on TYPE_msk */
848 16174 : (bi.h == NULL ||
849 16174 : bi.h->parentid == b->batCacheid ||
850 3598 : BBP_desc(bi.h->parentid)->batRestricted == BAT_READ)) {
851 16174 : bn = VIEWcreate(b->hseqbase, b, 0, BUN_MAX);
852 16174 : if (bn == NULL) {
853 0 : goto bunins_failed;
854 : }
855 16174 : if (tt != bn->ttype) {
856 47 : bn->ttype = tt;
857 47 : if (bn->tvheap && !ATOMvarsized(tt)) {
858 0 : if (bn->tvheap->parentid != bn->batCacheid)
859 0 : BBPrelease(bn->tvheap->parentid);
860 0 : HEAPdecref(bn->tvheap, false);
861 0 : bn->tvheap = NULL;
862 : }
863 47 : bn->tseqbase = ATOMtype(tt) == TYPE_oid ? bi.tseq : oid_nil;
864 : }
865 16174 : bat_iterator_end(&bi);
866 16174 : return bn;
867 : } else {
868 : /* check whether we need case (4); BUN-by-BUN copy (by
869 : * setting slowcopy to true) */
870 32414 : if (ATOMsize(tt) != ATOMsize(bi.type)) {
871 : /* oops, void materialization */
872 : slowcopy = true;
873 32060 : } else if (bi.h && bi.h->parentid != b->batCacheid &&
874 7213 : BATcapacity(BBP_desc(bi.h->parentid)) > bi.count + bi.count) {
875 : /* reduced slice view: do not copy too much
876 : * garbage */
877 : slowcopy = true;
878 25042 : } else if (bi.vh && bi.vh->parentid != b->batCacheid &&
879 10341 : BATcount(BBP_desc(bi.vh->parentid)) > bi.count + bi.count) {
880 : /* reduced vheap view: do not copy too much
881 : * garbage; this really is a heuristic since the
882 : * vheap could be used completely, even if the
883 : * offset heap is only (less than) half the size
884 : * of the parent's offset heap */
885 9586 : slowcopy = true;
886 : }
887 :
888 32414 : bn = COLnew2(b->hseqbase, tt, bi.count, role, bi.width);
889 32416 : if (bn == NULL) {
890 0 : goto bunins_failed;
891 : }
892 32416 : if (bn->tvheap != NULL && bn->tvheap->base == NULL) {
893 : /* this combination can happen since the last
894 : * argument of COLnew2 not being zero triggers a
895 : * skip in the allocation of the tvheap */
896 12492 : if (ATOMheap(bn->ttype, bn->tvheap, bn->batCapacity) != GDK_SUCCEED) {
897 0 : goto bunins_failed;
898 : }
899 : }
900 :
901 32414 : if (tt == TYPE_void) {
902 : /* case (2): a void,void result => nothing to
903 : * copy! */
904 1117 : bn->theap->free = 0;
905 31297 : } else if (!slowcopy) {
906 : /* case (3): just copy the heaps */
907 21711 : if (bn->tvheap && HEAPextend(bn->tvheap, bi.vhfree, true) != GDK_SUCCEED) {
908 0 : goto bunins_failed;
909 : }
910 21712 : memcpy(bn->theap->base, bi.base, bi.hfree);
911 21712 : bn->theap->free = bi.hfree;
912 21712 : bn->theap->dirty = true;
913 21712 : if (bn->tvheap) {
914 9578 : memcpy(bn->tvheap->base, bi.vh->base, bi.vhfree);
915 9578 : bn->tvheap->free = bi.vhfree;
916 9578 : bn->tvheap->dirty = true;
917 9578 : bn->tascii = bi.ascii;
918 9578 : if (ATOMstorage(b->ttype) == TYPE_str && bi.vhfree >= GDK_STRHASHSIZE)
919 8747 : memcpy(bn->tvheap->base, strhash, GDK_STRHASHSIZE);
920 : }
921 :
922 : /* make sure we use the correct capacity */
923 21712 : if (ATOMstorage(bn->ttype) == TYPE_msk)
924 0 : bn->batCapacity = (BUN) (bn->theap->size * 8);
925 21712 : else if (bn->ttype)
926 21712 : bn->batCapacity = (BUN) (bn->theap->size >> bn->tshift);
927 : else
928 0 : bn->batCapacity = 0;
929 19170 : } else if (tt != TYPE_void || ATOMextern(tt)) {
930 : /* case (4): one-by-one BUN insert (really slow) */
931 9586 : QryCtx *qry_ctx = MT_thread_get_qry_ctx();
932 :
933 8703278 : TIMEOUT_LOOP_IDX_DECL(p, bi.count, qry_ctx) {
934 8683648 : const void *t = BUNtail(bi, p);
935 :
936 8682893 : if (bunfastapp_nocheck(bn, t) != GDK_SUCCEED) {
937 0 : goto bunins_failed;
938 : }
939 : }
940 9583 : TIMEOUT_CHECK(qry_ctx, GOTO_LABEL_TIMEOUT_HANDLER(bunins_failed, qry_ctx));
941 9584 : bn->theap->dirty |= bi.count > 0;
942 : } else if (tt != TYPE_void && bi.type == TYPE_void) {
943 : /* case (4): optimized for unary void
944 : * materialization */
945 : oid cur = bi.tseq, *dst = (oid *) Tloc(bn, 0);
946 : const oid inc = !is_oid_nil(cur);
947 :
948 : bn->theap->free = bi.count * sizeof(oid);
949 : bn->theap->dirty |= bi.count > 0;
950 : for (BUN p = 0; p < bi.count; p++) {
951 : dst[p] = cur;
952 : cur += inc;
953 : }
954 : } else if (ATOMstorage(bi.type) == TYPE_msk) {
955 : /* convert number of bits to number of bytes,
956 : * and round the latter up to a multiple of
957 : * 4 (copy in units of 4 bytes) */
958 : bn->theap->free = ((bi.count + 31) / 32) * 4;
959 : bn->theap->dirty |= bi.count > 0;
960 : memcpy(Tloc(bn, 0), bi.base, bn->theap->free);
961 : } else {
962 : /* case (4): optimized for simple array copy */
963 : bn->theap->free = bi.count << bn->tshift;
964 : bn->theap->dirty |= bi.count > 0;
965 : memcpy(Tloc(bn, 0), bi.base, bn->theap->free);
966 : }
967 : /* copy all properties (size+other) from the source bat */
968 32413 : BATsetcount(bn, bi.count);
969 : }
970 : /* set properties (note that types may have changed in the copy) */
971 64471 : if (ATOMtype(tt) == ATOMtype(bi.type)) {
972 32409 : if (ATOMtype(tt) == TYPE_oid) {
973 8190 : BATtseqbase(bn, bi.tseq);
974 : } else {
975 24219 : BATtseqbase(bn, oid_nil);
976 : }
977 32412 : BATkey(bn, bi.key);
978 32411 : bn->tsorted = bi.sorted;
979 32411 : bn->trevsorted = bi.revsorted;
980 32411 : bn->tnorevsorted = bi.norevsorted;
981 32411 : if (bi.nokey[0] != bi.nokey[1]) {
982 2654 : bn->tnokey[0] = bi.nokey[0];
983 2654 : bn->tnokey[1] = bi.nokey[1];
984 : } else {
985 29757 : bn->tnokey[0] = bn->tnokey[1] = 0;
986 : }
987 32411 : bn->tnosorted = bi.nosorted;
988 32411 : bn->tnonil = bi.nonil;
989 32411 : bn->tnil = bi.nil;
990 32411 : bn->tminpos = bi.minpos;
991 32411 : bn->tmaxpos = bi.maxpos;
992 32411 : bn->tunique_est = bi.unique_est;
993 3 : } else if (ATOMstorage(tt) == ATOMstorage(b->ttype) &&
994 3 : ATOMcompare(tt) == ATOMcompare(b->ttype)) {
995 3 : BUN h = bi.count;
996 3 : bn->tsorted = bi.sorted;
997 3 : bn->trevsorted = bi.revsorted;
998 3 : BATkey(bn, bi.key);
999 3 : bn->tnonil = bi.nonil;
1000 3 : bn->tnil = bi.nil;
1001 3 : if (bi.nosorted > 0 && bi.nosorted < h)
1002 1 : bn->tnosorted = bi.nosorted;
1003 : else
1004 2 : bn->tnosorted = 0;
1005 3 : if (bi.norevsorted > 0 && bi.norevsorted < h)
1006 3 : bn->tnorevsorted = bi.norevsorted;
1007 : else
1008 0 : bn->tnorevsorted = 0;
1009 3 : if (bi.nokey[0] < h &&
1010 3 : bi.nokey[1] < h &&
1011 : bi.nokey[0] != bi.nokey[1]) {
1012 0 : bn->tnokey[0] = bi.nokey[0];
1013 0 : bn->tnokey[1] = bi.nokey[1];
1014 : } else {
1015 3 : bn->tnokey[0] = bn->tnokey[1] = 0;
1016 : }
1017 3 : bn->tminpos = bi.minpos;
1018 3 : bn->tmaxpos = bi.maxpos;
1019 3 : bn->tunique_est = bi.unique_est;
1020 : } else {
1021 0 : bn->tsorted = bn->trevsorted = false; /* set based on count later */
1022 0 : bn->tnonil = bn->tnil = false;
1023 0 : bn->tkey = false;
1024 0 : bn->tnosorted = bn->tnorevsorted = 0;
1025 0 : bn->tnokey[0] = bn->tnokey[1] = 0;
1026 : }
1027 32414 : if (BATcount(bn) <= 1) {
1028 6022 : bn->tsorted = ATOMlinear(b->ttype);
1029 6022 : bn->trevsorted = ATOMlinear(b->ttype);
1030 6022 : bn->tkey = true;
1031 : }
1032 32414 : bat_iterator_end(&bi);
1033 32414 : if (!writable)
1034 4131 : bn->batRestricted = BAT_READ;
1035 32414 : TRC_DEBUG(ALGO, ALGOBATFMT " -> " ALGOBATFMT "\n",
1036 : ALGOBATPAR(b), ALGOBATPAR(bn));
1037 : return bn;
1038 0 : bunins_failed:
1039 0 : bat_iterator_end(&bi);
1040 0 : BBPreclaim(bn);
1041 : return NULL;
1042 : }
1043 :
1044 : /* Append an array of values of length count to the bat. For
1045 : * fixed-sized values, `values' is an array of values, for
1046 : * variable-sized values, `values' is an array of pointers to values.
1047 : * If values equals NULL, count times nil will be appended. */
1048 : gdk_return
1049 55204347 : BUNappendmulti(BAT *b, const void *values, BUN count, bool force)
1050 : {
1051 55204347 : BUN p;
1052 55204347 : BUN nunique = 0;
1053 :
1054 55204347 : BATcheck(b, GDK_FAIL);
1055 :
1056 55204347 : assert(!VIEWtparent(b));
1057 :
1058 55204347 : if (count == 0)
1059 : return GDK_SUCCEED;
1060 :
1061 55203958 : TRC_DEBUG(ALGO, ALGOBATFMT " appending " BUNFMT " values%s\n", ALGOBATPAR(b), count, values ? "" : " (all nil)");
1062 :
1063 55202718 : p = BATcount(b); /* insert at end */
1064 55202718 : if (p == BUN_MAX || BATcount(b) + count >= BUN_MAX) {
1065 0 : GDKerror("bat too large\n");
1066 0 : return GDK_FAIL;
1067 : }
1068 :
1069 55202718 : ALIGNapp(b, force, GDK_FAIL);
1070 : /* load hash so that we can maintain it */
1071 55212238 : (void) BATcheckhash(b);
1072 :
1073 55213566 : if (b->ttype == TYPE_void && BATtdense(b)) {
1074 0 : const oid *ovals = values;
1075 0 : bool dense = b->batCount == 0 || (ovals != NULL && b->tseqbase + 1 == ovals[0]);
1076 0 : if (ovals) {
1077 0 : for (BUN i = 1; dense && i < count; i++) {
1078 0 : dense = ovals[i - 1] + 1 == ovals[i];
1079 : }
1080 : }
1081 0 : if (dense) {
1082 0 : MT_lock_set(&b->theaplock);
1083 0 : if (b->batCount == 0)
1084 0 : b->tseqbase = ovals ? ovals[0] : oid_nil;
1085 0 : BATsetcount(b, BATcount(b) + count);
1086 0 : MT_lock_unset(&b->theaplock);
1087 0 : return GDK_SUCCEED;
1088 : } else {
1089 : /* we need to materialize b; allocate enough capacity */
1090 0 : if (BATmaterialize(b, BATcount(b) + count) != GDK_SUCCEED)
1091 : return GDK_FAIL;
1092 : }
1093 : }
1094 :
1095 55213566 : if (unshare_varsized_heap(b) != GDK_SUCCEED) {
1096 : return GDK_FAIL;
1097 : }
1098 :
1099 55208914 : if (BATcount(b) + count > BATcapacity(b)) {
1100 : /* if needed space exceeds a normal growth extend just
1101 : * with what's needed */
1102 11374 : BUN ncap = BATcount(b) + count;
1103 11374 : BUN grows = BATgrows(b);
1104 :
1105 11374 : if (ncap > grows)
1106 : grows = ncap;
1107 11374 : gdk_return rc = BATextend(b, grows);
1108 11374 : if (rc != GDK_SUCCEED)
1109 : return rc;
1110 : }
1111 :
1112 55208914 : const void *t = b->ttype == TYPE_msk ? &(msk){false} : ATOMnilptr(b->ttype);
1113 55208914 : MT_lock_set(&b->theaplock);
1114 55189539 : BATiter bi = bat_iterator_nolock(b);
1115 55189539 : const ValRecord *prop;
1116 55189539 : ValRecord minprop, maxprop;
1117 55189539 : const void *minbound = NULL, *maxbound = NULL;
1118 55189550 : if ((prop = BATgetprop_nolock(b, GDK_MIN_BOUND)) != NULL &&
1119 11 : VALcopy(&minprop, prop) != NULL)
1120 11 : minbound = VALptr(&minprop);
1121 55221282 : if ((prop = BATgetprop_nolock(b, GDK_MAX_BOUND)) != NULL &&
1122 10 : VALcopy(&maxprop, prop) != NULL)
1123 10 : maxbound = VALptr(&maxprop);
1124 55213769 : const bool notnull = BATgetprop_nolock(b, GDK_NOT_NULL) != NULL;
1125 55208348 : MT_lock_unset(&b->theaplock);
1126 55207966 : MT_rwlock_wrlock(&b->thashlock);
1127 55222308 : if (values && b->ttype) {
1128 55211035 : int (*atomcmp) (const void *, const void *) = ATOMcompare(b->ttype);
1129 55211035 : const void *atomnil = ATOMnilptr(b->ttype);
1130 55211035 : const void *minvalp = NULL, *maxvalp = NULL;
1131 55211035 : if (b->tvheap) {
1132 22007646 : if (bi.minpos != BUN_NONE)
1133 19115638 : minvalp = BUNtvar(bi, bi.minpos);
1134 22007041 : if (bi.maxpos != BUN_NONE)
1135 19115012 : maxvalp = BUNtvar(bi, bi.maxpos);
1136 22006259 : const void *vbase = b->tvheap->base;
1137 44026200 : for (BUN i = 0; i < count; i++) {
1138 22020911 : t = ((void **) values)[i];
1139 22020911 : bool isnil = atomcmp(t, atomnil) == 0;
1140 22019504 : gdk_return rc;
1141 22019504 : if (notnull && isnil) {
1142 0 : assert(0);
1143 : GDKerror("NULL value not within bounds\n");
1144 : rc = GDK_FAIL;
1145 22019504 : } else if (minbound &&
1146 22019504 : !isnil &&
1147 0 : atomcmp(t, minbound) < 0) {
1148 0 : assert(0);
1149 : GDKerror("value not within bounds\n");
1150 : rc = GDK_FAIL;
1151 22019504 : } else if (maxbound &&
1152 0 : !isnil &&
1153 0 : atomcmp(t, maxbound) >= 0) {
1154 0 : assert(0);
1155 : GDKerror("value not within bounds\n");
1156 : rc = GDK_FAIL;
1157 : } else {
1158 22019504 : rc = tfastins_nocheckVAR(b, p, t);
1159 : }
1160 22021368 : if (rc != GDK_SUCCEED) {
1161 0 : MT_rwlock_wrunlock(&b->thashlock);
1162 0 : if (minbound)
1163 0 : VALclear(&minprop);
1164 0 : if (maxbound)
1165 0 : VALclear(&maxprop);
1166 0 : return rc;
1167 : }
1168 22021368 : if (vbase != b->tvheap->base) {
1169 : /* tvheap changed location, so
1170 : * pointers may need to be
1171 : * updated (not if they were
1172 : * initialized from t below, but
1173 : * we don't know) */
1174 3398 : BUN minpos = bi.minpos;
1175 3398 : BUN maxpos = bi.maxpos;
1176 3398 : MT_lock_set(&b->theaplock);
1177 3398 : bi = bat_iterator_nolock(b);
1178 3398 : MT_lock_unset(&b->theaplock);
1179 3398 : bi.minpos = minpos;
1180 3398 : bi.maxpos = maxpos;
1181 3398 : vbase = b->tvheap->base;
1182 3398 : if (bi.minpos != BUN_NONE)
1183 2656 : minvalp = BUNtvar(bi, bi.minpos);
1184 3398 : if (bi.maxpos != BUN_NONE)
1185 2655 : maxvalp = BUNtvar(bi, bi.maxpos);
1186 : }
1187 22021368 : if (!isnil) {
1188 20348031 : if (p == 0) {
1189 215512 : bi.minpos = bi.maxpos = 0;
1190 215512 : minvalp = maxvalp = t;
1191 : } else {
1192 39198605 : if (bi.minpos != BUN_NONE &&
1193 19066891 : atomcmp(minvalp, t) > 0) {
1194 82683 : bi.minpos = p;
1195 82683 : minvalp = t;
1196 : }
1197 39196678 : if (bi.maxpos != BUN_NONE &&
1198 19065586 : atomcmp(maxvalp, t) < 0) {
1199 2247183 : bi.maxpos = p;
1200 2247183 : maxvalp = t;
1201 : }
1202 : }
1203 : } else {
1204 1673337 : b->tnil = true;
1205 1673337 : b->tnonil = false;
1206 : }
1207 22019941 : p++;
1208 : }
1209 22005289 : if (minbound)
1210 0 : VALclear(&minprop);
1211 22005360 : if (maxbound)
1212 0 : VALclear(&maxprop);
1213 22005213 : if (b->thash) {
1214 5883 : p -= count;
1215 11766 : for (BUN i = 0; i < count; i++) {
1216 5883 : t = ((void **) values)[i];
1217 5883 : HASHappend_locked(b, p, t);
1218 5883 : p++;
1219 : }
1220 5883 : nunique = b->thash ? b->thash->nunique : 0;
1221 : }
1222 33203389 : } else if (ATOMstorage(b->ttype) == TYPE_msk) {
1223 7893 : bi.minpos = bi.maxpos = BUN_NONE;
1224 7893 : minvalp = maxvalp = NULL;
1225 7893 : b->tnil = false;
1226 7893 : b->tnonil = true;
1227 15786 : for (BUN i = 0; i < count; i++) {
1228 7893 : t = (void *) ((char *) values + (i << b->tshift));
1229 7893 : mskSetVal(b, p, *(msk *) t);
1230 7893 : p++;
1231 : }
1232 : } else {
1233 33195496 : if (bi.minpos != BUN_NONE)
1234 31647962 : minvalp = BUNtloc(bi, bi.minpos);
1235 33195496 : if (bi.maxpos != BUN_NONE)
1236 31692682 : maxvalp = BUNtloc(bi, bi.maxpos);
1237 66427847 : for (BUN i = 0; i < count; i++) {
1238 33243668 : t = (void *) ((char *) values + (i << b->tshift));
1239 33243668 : gdk_return rc = tfastins_nocheckFIX(b, p, t);
1240 33235408 : if (rc != GDK_SUCCEED) {
1241 0 : MT_rwlock_wrunlock(&b->thashlock);
1242 0 : return rc;
1243 : }
1244 33235408 : if (b->thash) {
1245 481554 : HASHappend_locked(b, p, t);
1246 : }
1247 33235408 : if (atomcmp(t, atomnil) != 0) {
1248 32194815 : if (p == 0) {
1249 378017 : bi.minpos = bi.maxpos = 0;
1250 378017 : minvalp = maxvalp = t;
1251 : } else {
1252 63488992 : if (bi.minpos != BUN_NONE &&
1253 31672234 : atomcmp(minvalp, t) > 0) {
1254 40607 : bi.minpos = p;
1255 40607 : minvalp = t;
1256 : }
1257 63529417 : if (bi.maxpos != BUN_NONE &&
1258 31716514 : atomcmp(maxvalp, t) < 0) {
1259 7814763 : bi.maxpos = p;
1260 7814763 : maxvalp = t;
1261 : }
1262 : }
1263 : } else {
1264 1041431 : b->tnil = true;
1265 1041431 : b->tnonil = false;
1266 : }
1267 33232351 : p++;
1268 : }
1269 33184179 : nunique = b->thash ? b->thash->nunique : 0;
1270 : }
1271 : } else {
1272 : /* inserting nils, unless it's msk */
1273 26722 : for (BUN i = 0; i < count; i++) {
1274 15451 : gdk_return rc = tfastins_nocheck(b, p, t);
1275 15449 : if (rc != GDK_SUCCEED) {
1276 0 : MT_rwlock_wrunlock(&b->thashlock);
1277 0 : return rc;
1278 : }
1279 15449 : if (b->thash) {
1280 0 : HASHappend_locked(b, p, t);
1281 : }
1282 15449 : p++;
1283 : }
1284 11271 : nunique = b->thash ? b->thash->nunique : 0;
1285 11271 : b->tnil = b->ttype != TYPE_msk;
1286 11271 : b->tnonil = false;
1287 : }
1288 55208556 : MT_lock_set(&b->theaplock);
1289 55206678 : b->tminpos = bi.minpos;
1290 55206678 : b->tmaxpos = bi.maxpos;
1291 55206678 : if (count > BATcount(b) / gdk_unique_estimate_keep_fraction)
1292 16986675 : b->tunique_est = 0;
1293 :
1294 55206678 : if (b->ttype == TYPE_oid) {
1295 : /* spend extra effort on oid (possible candidate list) */
1296 548610 : if (values == NULL || is_oid_nil(((oid *) values)[0])) {
1297 158 : b->tsorted = false;
1298 158 : b->trevsorted = false;
1299 158 : b->tkey = false;
1300 158 : b->tseqbase = oid_nil;
1301 : } else {
1302 548452 : if (b->batCount == 0) {
1303 6295 : b->tsorted = true;
1304 6295 : b->trevsorted = true;
1305 6295 : b->tkey = true;
1306 6295 : b->tseqbase = count == 1 ? ((oid *) values)[0] : oid_nil;
1307 : } else {
1308 542157 : if (!is_oid_nil(b->tseqbase) &&
1309 302072 : (count > 1 ||
1310 302072 : b->tseqbase + b->batCount != ((oid *) values)[0]))
1311 2186 : b->tseqbase = oid_nil;
1312 542157 : if (b->tsorted && !is_oid_nil(((oid *) b->theap->base)[b->batCount - 1]) && ((oid *) b->theap->base)[b->batCount - 1] > ((oid *) values)[0]) {
1313 53 : b->tsorted = false;
1314 53 : if (b->tnosorted == 0)
1315 53 : b->tnosorted = b->batCount;
1316 : }
1317 542157 : if (b->trevsorted && !is_oid_nil(((oid *) values)[0]) && ((oid *) b->theap->base)[b->batCount - 1] < ((oid *) values)[0]) {
1318 5491 : b->trevsorted = false;
1319 5491 : if (b->tnorevsorted == 0)
1320 5491 : b->tnorevsorted = b->batCount;
1321 : }
1322 542157 : if (b->tkey) {
1323 539575 : if (((oid *) b->theap->base)[b->batCount - 1] == ((oid *) values)[0]) {
1324 17 : b->tkey = false;
1325 17 : if (b->tnokey[1] == 0) {
1326 17 : b->tnokey[0] = b->batCount - 1;
1327 17 : b->tnokey[1] = b->batCount;
1328 : }
1329 539558 : } else if (!b->tsorted && !b->trevsorted)
1330 42 : b->tkey = false;
1331 : }
1332 : }
1333 548452 : for (BUN i = 1; i < count; i++) {
1334 0 : if (is_oid_nil(((oid *) values)[i])) {
1335 0 : b->tsorted = false;
1336 0 : b->trevsorted = false;
1337 0 : b->tkey = false;
1338 0 : b->tseqbase = oid_nil;
1339 0 : break;
1340 : }
1341 0 : if (((oid *) values)[i - 1] == ((oid *) values)[i]) {
1342 0 : b->tkey = false;
1343 0 : if (b->tnokey[1] == 0) {
1344 0 : b->tnokey[0] = b->batCount + i - 1;
1345 0 : b->tnokey[1] = b->batCount + i;
1346 : }
1347 0 : } else if (((oid *) values)[i - 1] > ((oid *) values)[i]) {
1348 0 : b->tsorted = false;
1349 0 : if (b->tnosorted == 0)
1350 0 : b->tnosorted = b->batCount + i;
1351 0 : if (!b->trevsorted)
1352 0 : b->tkey = false;
1353 : } else {
1354 0 : if (((oid *) values)[i - 1] + 1 != ((oid *) values)[i])
1355 0 : b->tseqbase = oid_nil;
1356 0 : b->trevsorted = false;
1357 0 : if (b->tnorevsorted == 0)
1358 0 : b->tnorevsorted = b->batCount + i;
1359 0 : if (!b->tsorted)
1360 0 : b->tkey = false;
1361 : }
1362 : }
1363 : }
1364 54658068 : } else if (!ATOMlinear(b->ttype)) {
1365 7893 : b->tsorted = b->trevsorted = b->tkey = false;
1366 54650175 : } else if (b->batCount == 0) {
1367 599249 : if (values == NULL) {
1368 0 : b->tsorted = b->trevsorted = true;
1369 0 : b->tkey = count == 1;
1370 0 : b->tunique_est = 1;
1371 : } else {
1372 599249 : int c;
1373 599249 : switch (count) {
1374 598916 : case 1:
1375 598916 : b->tsorted = b->trevsorted = b->tkey = true;
1376 598916 : b->tunique_est = 1;
1377 598916 : break;
1378 122 : case 2:
1379 122 : if (b->tvheap)
1380 41 : c = ATOMcmp(b->ttype,
1381 : ((void **) values)[0],
1382 : ((void **) values)[1]);
1383 : else
1384 81 : c = ATOMcmp(b->ttype,
1385 : values,
1386 : (char *) values + b->twidth);
1387 122 : b->tsorted = c <= 0;
1388 122 : b->tnosorted = !b->tsorted;
1389 122 : b->trevsorted = c >= 0;
1390 122 : b->tnorevsorted = !b->trevsorted;
1391 122 : b->tkey = c != 0;
1392 122 : b->tnokey[0] = 0;
1393 122 : b->tnokey[1] = !b->tkey;
1394 122 : b->tunique_est = (double) (1 + b->tkey);
1395 122 : break;
1396 211 : default:
1397 211 : b->tsorted = b->trevsorted = b->tkey = false;
1398 211 : break;
1399 : }
1400 : }
1401 54050926 : } else if (b->batCount == 1 && count == 1) {
1402 432762 : bi = bat_iterator_nolock(b);
1403 432762 : t = b->ttype == TYPE_msk ? &(msk){false} : ATOMnilptr(b->ttype);
1404 432762 : if (values != NULL) {
1405 432751 : if (b->tvheap)
1406 163769 : t = ((void **) values)[0];
1407 : else
1408 : t = values;
1409 : }
1410 432762 : int c = ATOMcmp(b->ttype, BUNtail(bi, 0), t);
1411 432676 : b->tsorted = c <= 0;
1412 432676 : b->tnosorted = !b->tsorted;
1413 432676 : b->trevsorted = c >= 0;
1414 432676 : b->tnorevsorted = !b->trevsorted;
1415 432676 : b->tkey = c != 0;
1416 432676 : b->tnokey[0] = 0;
1417 432676 : b->tnokey[1] = !b->tkey;
1418 432676 : b->tunique_est = (double) (1 + b->tkey);
1419 : } else {
1420 53618164 : b->tsorted = b->trevsorted = b->tkey = false;
1421 : }
1422 55206592 : BATsetcount(b, p);
1423 55198260 : if (nunique != 0)
1424 487437 : b->tunique_est = (double) nunique;
1425 55198260 : MT_lock_unset(&b->theaplock);
1426 55198888 : MT_rwlock_wrunlock(&b->thashlock);
1427 :
1428 55214484 : IMPSdestroy(b); /* no support for inserts in imprints yet */
1429 55221210 : OIDXdestroy(b);
1430 55213418 : STRMPdestroy(b); /* TODO: use STRMPappendBitstring */
1431 55212770 : RTREEdestroy(b);
1432 55212770 : return GDK_SUCCEED;
1433 : }
1434 :
1435 : /* Append a single value to the bat. */
1436 : gdk_return
1437 40383653 : BUNappend(BAT *b, const void *t, bool force)
1438 : {
1439 40383653 : return BUNappendmulti(b, b->ttype && b->tvheap ? (const void *) &t : (const void *) t, 1, force);
1440 : }
1441 :
1442 : gdk_return
1443 4 : BUNdelete(BAT *b, oid o)
1444 : {
1445 4 : BUN p;
1446 4 : BATiter bi = bat_iterator_nolock(b);
1447 4 : const void *val;
1448 4 : bool locked = false;
1449 4 : BUN nunique;
1450 :
1451 4 : assert(!is_oid_nil(b->hseqbase) || BATcount(b) == 0);
1452 4 : if (o < b->hseqbase || o >= b->hseqbase + BATcount(b)) {
1453 : /* value already not there */
1454 : return GDK_SUCCEED;
1455 : }
1456 4 : assert(BATcount(b) > 0); /* follows from "if" above */
1457 4 : p = o - b->hseqbase;
1458 4 : if (p < b->batInserted) {
1459 0 : GDKerror("cannot delete committed value\n");
1460 0 : return GDK_FAIL;
1461 : }
1462 4 : TRC_DEBUG(ALGO, ALGOBATFMT " deleting oid " OIDFMT "\n", ALGOBATPAR(b), o);
1463 : /* load hash so that we can maintain it */
1464 4 : (void) BATcheckhash(b);
1465 :
1466 4 : val = BUNtail(bi, p);
1467 : /* writing the values should be locked, reading could be done
1468 : * unlocked (since we're the only thread that should be changing
1469 : * anything) */
1470 4 : MT_lock_set(&b->theaplock);
1471 4 : if (b->tmaxpos == p)
1472 1 : b->tmaxpos = BUN_NONE;
1473 4 : if (b->tminpos == p)
1474 0 : b->tminpos = BUN_NONE;
1475 4 : MT_lock_unset(&b->theaplock);
1476 4 : nunique = HASHdelete(&bi, p, val);
1477 4 : ATOMdel(b->ttype, b->tvheap, (var_t *) BUNtloc(bi, p));
1478 4 : if (p != BATcount(b) - 1 &&
1479 2 : (b->ttype != TYPE_void || BATtdense(b))) {
1480 : /* replace to-be-delete BUN with last BUN; materialize
1481 : * void column before doing so */
1482 2 : if (b->ttype == TYPE_void &&
1483 0 : BATmaterialize(b, BUN_NONE) != GDK_SUCCEED)
1484 : return GDK_FAIL;
1485 2 : if (ATOMstorage(b->ttype) == TYPE_msk) {
1486 0 : msk mval = mskGetVal(b, BATcount(b) - 1);
1487 0 : assert(b->thash == NULL);
1488 0 : mskSetVal(b, p, mval);
1489 : /* don't leave garbage */
1490 0 : mskClr(b, BATcount(b) - 1);
1491 : } else {
1492 2 : val = Tloc(b, BATcount(b) - 1);
1493 2 : nunique = HASHdelete(&bi, BATcount(b) - 1, val);
1494 2 : memcpy(Tloc(b, p), val, b->twidth);
1495 2 : nunique = HASHinsert(&bi, p, val);
1496 2 : MT_lock_set(&b->theaplock);
1497 2 : locked = true;
1498 2 : if (b->tminpos == BATcount(b) - 1)
1499 0 : b->tminpos = p;
1500 2 : if (b->tmaxpos == BATcount(b) - 1)
1501 1 : b->tmaxpos = p;
1502 : }
1503 : /* no longer sorted */
1504 2 : if (!locked) {
1505 0 : MT_lock_set(&b->theaplock);
1506 0 : locked = true;
1507 : }
1508 2 : b->tsorted = b->trevsorted = false;
1509 2 : b->theap->dirty = true;
1510 : }
1511 4 : if (!locked)
1512 2 : MT_lock_set(&b->theaplock);
1513 4 : if (b->tnosorted >= p)
1514 0 : b->tnosorted = 0;
1515 4 : if (b->tnorevsorted >= p)
1516 1 : b->tnorevsorted = 0;
1517 4 : b->batCount--;
1518 4 : if (nunique != 0)
1519 0 : b->tunique_est = (double) nunique;
1520 4 : else if (BATcount(b) < gdk_unique_estimate_keep_fraction)
1521 4 : b->tunique_est = 0;
1522 4 : if (b->batCount <= 1) {
1523 : /* some trivial properties */
1524 0 : b->tkey = true;
1525 0 : b->tsorted = b->trevsorted = true;
1526 0 : b->tnosorted = b->tnorevsorted = 0;
1527 0 : if (b->batCount == 0) {
1528 0 : b->tnil = false;
1529 0 : b->tnonil = true;
1530 : }
1531 : }
1532 4 : MT_lock_unset(&b->theaplock);
1533 4 : IMPSdestroy(b);
1534 4 : OIDXdestroy(b);
1535 4 : return GDK_SUCCEED;
1536 : }
1537 :
1538 : /* @- BUN replace
1539 : * The last operation in this context is BUN replace. It assumes that
1540 : * the header denotes a key. The old value association is destroyed
1541 : * (if it exists in the first place) and the new value takes its
1542 : * place.
1543 : *
1544 : * In order to make updates on void columns workable; replaces on them
1545 : * are always done in-place. Performing them without bun-movements
1546 : * greatly simplifies the problem. The 'downside' is that when
1547 : * transaction management has to be performed, replaced values should
1548 : * be saved explicitly.
1549 : */
1550 : static gdk_return
1551 1047423 : BUNinplacemulti(BAT *b, const oid *positions, const void *values, BUN count, bool force, bool autoincr)
1552 : {
1553 1047423 : BUN prv, nxt;
1554 1047423 : const void *val;
1555 1047423 : int (*atomcmp) (const void *, const void *) = ATOMcompare(b->ttype);
1556 1047423 : const void *atomnil = ATOMnilptr(b->ttype);
1557 :
1558 1047423 : MT_lock_set(&b->theaplock);
1559 1047531 : BUN last = BATcount(b) - 1;
1560 1047531 : BATiter bi = bat_iterator_nolock(b);
1561 : /* zap alignment info */
1562 1047531 : if (!force && (b->batRestricted != BAT_WRITE ||
1563 551232 : ((ATOMIC_GET(&b->theap->refs) & HEAPREFS) > 1))) {
1564 0 : MT_lock_unset(&b->theaplock);
1565 0 : GDKerror("access denied to %s, aborting.\n",
1566 : BATgetId(b));
1567 0 : assert(0);
1568 : return GDK_FAIL;
1569 : }
1570 1047531 : TRC_DEBUG(ALGO, ALGOBATFMT " replacing " BUNFMT " values\n", ALGOBATPAR(b), count);
1571 1047417 : if (b->ttype == TYPE_void) {
1572 0 : PROPdestroy(b);
1573 0 : b->tminpos = BUN_NONE;
1574 0 : b->tmaxpos = BUN_NONE;
1575 0 : b->tunique_est = 0.0;
1576 1047417 : } else if (count > BATcount(b) / gdk_unique_estimate_keep_fraction) {
1577 526190 : b->tunique_est = 0;
1578 : }
1579 1047417 : const ValRecord *prop;
1580 1047417 : ValRecord minprop, maxprop;
1581 1047417 : const void *minbound = NULL, *maxbound = NULL;
1582 1047417 : if ((prop = BATgetprop_nolock(b, GDK_MIN_BOUND)) != NULL &&
1583 0 : VALcopy(&minprop, prop) != NULL)
1584 0 : minbound = VALptr(&minprop);
1585 1047475 : if ((prop = BATgetprop_nolock(b, GDK_MAX_BOUND)) != NULL &&
1586 0 : VALcopy(&maxprop, prop) != NULL)
1587 0 : maxbound = VALptr(&maxprop);
1588 1047419 : const bool notnull = BATgetprop_nolock(b, GDK_NOT_NULL) != NULL;
1589 1047397 : MT_lock_unset(&b->theaplock);
1590 : /* load hash so that we can maintain it */
1591 1047312 : (void) BATcheckhash(b);
1592 1047384 : MT_rwlock_wrlock(&b->thashlock);
1593 2094690 : for (BUN i = 0; i < count; i++) {
1594 1047479 : BUN p = autoincr ? positions[0] - b->hseqbase + i : positions[i] - b->hseqbase;
1595 1047459 : const void *t = b->ttype && b->tvheap ?
1596 1187532 : ((const void **) values)[i] :
1597 907426 : (const void *) ((const char *) values + (i << b->tshift));
1598 1047479 : bool isnil = atomnil && atomcmp(t, atomnil) == 0;
1599 1047526 : if (notnull && isnil) {
1600 0 : assert(0);
1601 : GDKerror("NULL value not within bounds\n");
1602 : MT_rwlock_wrunlock(&b->thashlock);
1603 : goto bailout;
1604 1047526 : } else if (!isnil &&
1605 0 : ((minbound &&
1606 999472 : atomcmp(t, minbound) < 0) ||
1607 0 : (maxbound &&
1608 0 : atomcmp(t, maxbound) >= 0))) {
1609 0 : assert(0);
1610 : GDKerror("value not within bounds\n");
1611 : MT_rwlock_wrunlock(&b->thashlock);
1612 : goto bailout;
1613 : }
1614 :
1615 : /* retrieve old value, but if this comes from the
1616 : * logger, we need to deal with offsets that point
1617 : * outside of the valid vheap */
1618 1047423 : if (b->ttype == TYPE_void) {
1619 0 : val = BUNtpos(bi, p);
1620 1047423 : } else if (bi.type == TYPE_msk) {
1621 118 : val = BUNtmsk(bi, p);
1622 1047305 : } else if (b->tvheap) {
1623 140049 : size_t off = BUNtvaroff(bi, p);
1624 140042 : if (off < bi.vhfree)
1625 140041 : val = bi.vh->base + off;
1626 : else
1627 : val = NULL; /* bad offset */
1628 : } else {
1629 907256 : val = BUNtloc(bi, p);
1630 : }
1631 :
1632 1047415 : if (val) {
1633 1047415 : if (atomcmp(val, t) == 0)
1634 233952 : continue; /* nothing to do */
1635 813467 : if (!isnil &&
1636 167894 : b->tnil &&
1637 167895 : atomcmp(val, atomnil) == 0) {
1638 : /* if old value is nil and new value
1639 : * isn't, we're not sure anymore about
1640 : * the nil property, so we must clear
1641 : * it */
1642 167735 : MT_lock_set(&b->theaplock);
1643 167733 : b->tnil = false;
1644 167733 : MT_lock_unset(&b->theaplock);
1645 : }
1646 813466 : if (b->ttype != TYPE_void) {
1647 813459 : if (bi.maxpos != BUN_NONE) {
1648 522792 : if (!isnil && atomcmp(BUNtail(bi, bi.maxpos), t) < 0) {
1649 : /* new value is larger
1650 : * than previous
1651 : * largest */
1652 54710 : bi.maxpos = p;
1653 468079 : } else if (bi.maxpos == p && atomcmp(BUNtail(bi, bi.maxpos), t) != 0) {
1654 : /* old value is equal to
1655 : * largest and new value
1656 : * is smaller or nil (see
1657 : * above), so we don't
1658 : * know anymore which is
1659 : * the largest */
1660 500 : bi.maxpos = BUN_NONE;
1661 : }
1662 : }
1663 813456 : if (bi.minpos != BUN_NONE) {
1664 371442 : if (!isnil && atomcmp(BUNtail(bi, bi.minpos), t) > 0) {
1665 : /* new value is smaller
1666 : * than previous
1667 : * smallest */
1668 424 : bi.minpos = p;
1669 371017 : } else if (bi.minpos == p && atomcmp(BUNtail(bi, bi.minpos), t) != 0) {
1670 : /* old value is equal to
1671 : * smallest and new value
1672 : * is larger or nil (see
1673 : * above), so we don't
1674 : * know anymore which is
1675 : * the largest */
1676 619 : bi.minpos = BUN_NONE;
1677 : }
1678 : }
1679 : }
1680 813462 : HASHdelete_locked(&bi, p, val); /* first delete old value from hash */
1681 : } else {
1682 : /* out of range old value, so the properties and
1683 : * hash cannot be trusted */
1684 1 : PROPdestroy(b);
1685 0 : Hash *hs = b->thash;
1686 0 : if (hs) {
1687 0 : b->thash = NULL;
1688 0 : doHASHdestroy(b, hs);
1689 : }
1690 0 : MT_lock_set(&b->theaplock);
1691 0 : bi.minpos = BUN_NONE;
1692 0 : bi.maxpos = BUN_NONE;
1693 0 : b->tunique_est = 0.0;
1694 0 : MT_lock_unset(&b->theaplock);
1695 : }
1696 813475 : OIDXdestroy(b);
1697 813476 : IMPSdestroy(b);
1698 813479 : STRMPdestroy(b);
1699 813481 : RTREEdestroy(b);
1700 :
1701 813494 : if (b->tvheap && b->ttype) {
1702 67073 : var_t _d;
1703 67073 : ptr _ptr;
1704 67073 : _ptr = BUNtloc(bi, p);
1705 67073 : switch (b->twidth) {
1706 8758 : case 1:
1707 8758 : _d = (var_t) * (uint8_t *) _ptr + GDK_VAROFFSET;
1708 8758 : break;
1709 53461 : case 2:
1710 53461 : _d = (var_t) * (uint16_t *) _ptr + GDK_VAROFFSET;
1711 53461 : break;
1712 4854 : case 4:
1713 4854 : _d = (var_t) * (uint32_t *) _ptr;
1714 4854 : break;
1715 : #if SIZEOF_VAR_T == 8
1716 0 : case 8:
1717 0 : _d = (var_t) * (uint64_t *) _ptr;
1718 0 : break;
1719 : #endif
1720 : default:
1721 0 : MT_UNREACHABLE();
1722 : }
1723 67073 : MT_lock_set(&b->theaplock);
1724 67064 : if (ATOMreplaceVAR(b, &_d, t) != GDK_SUCCEED) {
1725 0 : MT_lock_unset(&b->theaplock);
1726 0 : MT_rwlock_wrunlock(&b->thashlock);
1727 0 : goto bailout;
1728 : }
1729 67077 : MT_lock_unset(&b->theaplock);
1730 67074 : if (b->twidth < SIZEOF_VAR_T &&
1731 67076 : (b->twidth <= 2 ? _d - GDK_VAROFFSET : _d) >= ((size_t) 1 << (8 << b->tshift))) {
1732 : /* doesn't fit in current heap, upgrade it */
1733 84 : if (GDKupgradevarheap(b, _d, 0, bi.count) != GDK_SUCCEED) {
1734 0 : MT_rwlock_wrunlock(&b->thashlock);
1735 0 : goto bailout;
1736 : }
1737 : }
1738 : /* reinitialize iterator after possible heap upgrade */
1739 : {
1740 : /* save and restore minpos/maxpos */
1741 67074 : BUN minpos = bi.minpos;
1742 67074 : BUN maxpos = bi.maxpos;
1743 67074 : bi = bat_iterator_nolock(b);
1744 67074 : bi.minpos = minpos;
1745 67074 : bi.maxpos = maxpos;
1746 : }
1747 67074 : _ptr = BUNtloc(bi, p);
1748 67074 : switch (b->twidth) {
1749 8683 : case 1:
1750 8683 : * (uint8_t *) _ptr = (uint8_t) (_d - GDK_VAROFFSET);
1751 8683 : break;
1752 53528 : case 2:
1753 53528 : * (uint16_t *) _ptr = (uint16_t) (_d - GDK_VAROFFSET);
1754 53528 : break;
1755 4863 : case 4:
1756 4863 : * (uint32_t *) _ptr = (uint32_t) _d;
1757 4863 : break;
1758 : #if SIZEOF_VAR_T == 8
1759 0 : case 8:
1760 0 : * (uint64_t *) _ptr = (uint64_t) _d;
1761 0 : break;
1762 : #endif
1763 : default:
1764 67074 : MT_UNREACHABLE();
1765 : }
1766 746421 : } else if (ATOMstorage(b->ttype) == TYPE_msk) {
1767 118 : mskSetVal(b, p, * (msk *) t);
1768 : } else {
1769 746303 : assert(BATatoms[b->ttype].atomPut == NULL);
1770 746303 : switch (ATOMsize(b->ttype)) {
1771 : case 0: /* void */
1772 : break;
1773 13077 : case 1:
1774 13077 : ((bte *) b->theap->base)[p] = * (bte *) t;
1775 13077 : break;
1776 6799 : case 2:
1777 6799 : ((sht *) b->theap->base)[p] = * (sht *) t;
1778 6799 : break;
1779 174454 : case 4:
1780 174454 : ((int *) b->theap->base)[p] = * (int *) t;
1781 174454 : break;
1782 551973 : case 8:
1783 551973 : ((lng *) b->theap->base)[p] = * (lng *) t;
1784 551973 : break;
1785 0 : case 16:
1786 : #ifdef HAVE_HGE
1787 0 : ((hge *) b->theap->base)[p] = * (hge *) t;
1788 : #else
1789 : ((uuid *) b->theap->base)[p] = * (uuid *) t;
1790 : #endif
1791 0 : break;
1792 0 : default:
1793 0 : memcpy(BUNtloc(bi, p), t, ATOMsize(b->ttype));
1794 0 : break;
1795 : }
1796 : }
1797 :
1798 813495 : HASHinsert_locked(&bi, p, t); /* insert new value into hash */
1799 :
1800 813454 : prv = p > 0 ? p - 1 : BUN_NONE;
1801 813454 : nxt = p < last ? p + 1 : BUN_NONE;
1802 :
1803 813454 : MT_lock_set(&b->theaplock);
1804 813422 : if (b->tsorted) {
1805 2887 : if (prv != BUN_NONE &&
1806 1133 : atomcmp(t, BUNtail(bi, prv)) < 0) {
1807 26 : b->tsorted = false;
1808 26 : b->tnosorted = p;
1809 2454 : } else if (nxt != BUN_NONE &&
1810 726 : atomcmp(t, BUNtail(bi, nxt)) > 0) {
1811 652 : b->tsorted = false;
1812 652 : b->tnosorted = nxt;
1813 1076 : } else if (b->ttype != TYPE_void && BATtdense(b)) {
1814 0 : if (prv != BUN_NONE &&
1815 0 : 1 + * (oid *) BUNtloc(bi, prv) != * (oid *) t) {
1816 0 : b->tseqbase = oid_nil;
1817 0 : } else if (nxt != BUN_NONE &&
1818 0 : * (oid *) BUNtloc(bi, nxt) != 1 + * (oid *) t) {
1819 0 : b->tseqbase = oid_nil;
1820 0 : } else if (prv == BUN_NONE &&
1821 0 : nxt == BUN_NONE) {
1822 0 : b->tseqbase = * (oid *) t;
1823 : }
1824 : }
1825 811668 : } else if (b->tnosorted >= p)
1826 3657 : b->tnosorted = 0;
1827 813422 : if (b->trevsorted) {
1828 1369 : if (prv != BUN_NONE &&
1829 402 : atomcmp(t, BUNtail(bi, prv)) > 0) {
1830 52 : b->trevsorted = false;
1831 52 : b->tnorevsorted = p;
1832 1050 : } else if (nxt != BUN_NONE &&
1833 135 : atomcmp(t, BUNtail(bi, nxt)) < 0) {
1834 27 : b->trevsorted = false;
1835 27 : b->tnorevsorted = nxt;
1836 : }
1837 812455 : } else if (b->tnorevsorted >= p)
1838 2037 : b->tnorevsorted = 0;
1839 813422 : if (((b->ttype != TYPE_void) & b->tkey) && b->batCount > 1) {
1840 935 : BATkey(b, false);
1841 812487 : } else if (!b->tkey && (b->tnokey[0] == p || b->tnokey[1] == p))
1842 847 : b->tnokey[0] = b->tnokey[1] = 0;
1843 813422 : if (b->tnonil && ATOMstorage(b->ttype) != TYPE_msk)
1844 582355 : b->tnonil = t && atomcmp(t, atomnil) != 0;
1845 1047370 : MT_lock_unset(&b->theaplock);
1846 : }
1847 1047309 : BUN nunique = b->thash ? b->thash->nunique : 0;
1848 1047309 : MT_rwlock_wrunlock(&b->thashlock);
1849 1047316 : MT_lock_set(&b->theaplock);
1850 1046900 : if (nunique != 0)
1851 21262 : b->tunique_est = (double) nunique;
1852 1046900 : b->tminpos = bi.minpos;
1853 1046900 : b->tmaxpos = bi.maxpos;
1854 1046900 : b->theap->dirty = true;
1855 1046900 : if (b->tvheap)
1856 139925 : b->tvheap->dirty = true;
1857 1046900 : MT_lock_unset(&b->theaplock);
1858 :
1859 1046681 : return GDK_SUCCEED;
1860 :
1861 0 : bailout:
1862 0 : if (minbound)
1863 0 : VALclear(&minprop);
1864 0 : if (maxbound)
1865 0 : VALclear(&maxprop);
1866 : return GDK_FAIL;
1867 : }
1868 :
1869 : /* Replace multiple values given by their positions with the given values. */
1870 : gdk_return
1871 554823 : BUNreplacemulti(BAT *b, const oid *positions, const void *values, BUN count, bool force)
1872 : {
1873 554823 : BATcheck(b, GDK_FAIL);
1874 :
1875 554823 : if (b->ttype == TYPE_void && BATmaterialize(b, BUN_NONE) != GDK_SUCCEED)
1876 : return GDK_FAIL;
1877 :
1878 554823 : return BUNinplacemulti(b, positions, values, count, force, false);
1879 : }
1880 :
1881 : /* Replace multiple values starting from a given position with the given
1882 : * values. */
1883 : gdk_return
1884 489029 : BUNreplacemultiincr(BAT *b, oid position, const void *values, BUN count, bool force)
1885 : {
1886 489029 : BATcheck(b, GDK_FAIL);
1887 :
1888 489029 : if (b->ttype == TYPE_void && BATmaterialize(b, BUN_NONE) != GDK_SUCCEED)
1889 : return GDK_FAIL;
1890 :
1891 489029 : return BUNinplacemulti(b, &position, values, count, force, true);
1892 : }
1893 :
1894 : gdk_return
1895 554823 : BUNreplace(BAT *b, oid id, const void *t, bool force)
1896 : {
1897 554823 : return BUNreplacemulti(b, &id, b->ttype && b->tvheap ? (const void *) &t : t, 1, force);
1898 : }
1899 :
1900 : /* very much like BUNreplace, but this doesn't make any changes if the
1901 : * tail column is void */
1902 : gdk_return
1903 3543 : void_inplace(BAT *b, oid id, const void *val, bool force)
1904 : {
1905 3543 : assert(id >= b->hseqbase && id < b->hseqbase + BATcount(b));
1906 3543 : if (id < b->hseqbase || id >= b->hseqbase + BATcount(b)) {
1907 : GDKerror("id out of range\n");
1908 : return GDK_FAIL;
1909 : }
1910 3543 : if (b->ttype == TYPE_void)
1911 : return GDK_SUCCEED;
1912 3543 : return BUNinplacemulti(b, &id, b->ttype && b->tvheap ? (const void *) &val : (const void *) val, 1, force, false);
1913 : }
1914 :
1915 : /*
1916 : * @- BUN Lookup
1917 : * Location of a BUN using a value should use the available indexes to
1918 : * speed up access. If indexes are lacking then a hash index is
1919 : * constructed under the assumption that 1) multiple access to the BAT
1920 : * can be expected and 2) building the hash is only slightly more
1921 : * expensive than the full linear scan. BUN_NONE is returned if no
1922 : * such element could be found. In those cases where the type is
1923 : * known and a hash index is available, one should use the inline
1924 : * functions to speed-up processing.
1925 : */
1926 : static BUN
1927 0 : slowfnd(BAT *b, const void *v)
1928 : {
1929 0 : BATiter bi = bat_iterator(b);
1930 0 : BUN p, q;
1931 0 : int (*cmp)(const void *, const void *) = ATOMcompare(bi.type);
1932 :
1933 0 : BATloop(b, p, q) {
1934 0 : if ((*cmp)(v, BUNtail(bi, p)) == 0) {
1935 0 : bat_iterator_end(&bi);
1936 0 : return p;
1937 : }
1938 : }
1939 0 : bat_iterator_end(&bi);
1940 0 : return BUN_NONE;
1941 : }
1942 :
1943 : static BUN
1944 0 : mskfnd(BAT *b, msk v)
1945 : {
1946 0 : BUN p, q;
1947 :
1948 0 : if (v) {
1949 : /* find a 1 value */
1950 0 : for (p = 0, q = (BATcount(b) + 31) / 32; p < q; p++) {
1951 0 : if (((uint32_t *) b->theap->base)[p] != 0) {
1952 : /* there's at least one 1 bit */
1953 0 : return p * 32 + candmask_lobit(((uint32_t *) b->theap->base)[p]);
1954 : }
1955 : }
1956 : } else {
1957 : /* find a 0 value */
1958 0 : for (p = 0, q = (BATcount(b) + 31) / 32; p < q; p++) {
1959 0 : if (((uint32_t *) b->theap->base)[p] != ~0U) {
1960 : /* there's at least one 0 bit */
1961 0 : return p * 32 + candmask_lobit(~((uint32_t *) b->theap->base)[p]);
1962 : }
1963 : }
1964 : }
1965 : return BUN_NONE;
1966 : }
1967 :
1968 : BUN
1969 1762886 : BUNfnd(BAT *b, const void *v)
1970 : {
1971 1762886 : BUN r = BUN_NONE;
1972 1762886 : BATiter bi;
1973 :
1974 1762886 : BATcheck(b, BUN_NONE);
1975 1762886 : if (!v || BATcount(b) == 0)
1976 : return r;
1977 1349095 : if (complex_cand(b)) {
1978 0 : struct canditer ci;
1979 0 : canditer_init(&ci, NULL, b);
1980 0 : return canditer_search(&ci, * (const oid *) v, false);
1981 : }
1982 1349095 : if (BATtvoid(b))
1983 709482 : return BUNfndVOID(b, v);
1984 639613 : if (ATOMstorage(b->ttype) == TYPE_msk) {
1985 0 : return mskfnd(b, *(msk*)v);
1986 : }
1987 639613 : if (!BATcheckhash(b)) {
1988 164450 : if (BATordered(b) || BATordered_rev(b))
1989 164110 : return SORTfnd(b, v);
1990 : }
1991 475481 : if (BAThash(b) == GDK_SUCCEED) {
1992 475506 : bi = bat_iterator(b); /* outside of hashlock */
1993 475508 : MT_rwlock_rdlock(&b->thashlock);
1994 475508 : if (b->thash == NULL) {
1995 0 : MT_rwlock_rdunlock(&b->thashlock);
1996 0 : bat_iterator_end(&bi);
1997 0 : goto hashfnd_failed;
1998 : }
1999 950702 : switch (ATOMbasetype(bi.type)) {
2000 5 : case TYPE_bte:
2001 5 : HASHloop_bte(bi, b->thash, r, v)
2002 : break;
2003 : break;
2004 0 : case TYPE_sht:
2005 0 : HASHloop_sht(bi, b->thash, r, v)
2006 : break;
2007 : break;
2008 32 : case TYPE_int:
2009 32 : HASHloop_int(bi, b->thash, r, v)
2010 : break;
2011 : break;
2012 0 : case TYPE_flt:
2013 0 : HASHloop_flt(bi, b->thash, r, v)
2014 : break;
2015 : break;
2016 0 : case TYPE_dbl:
2017 0 : HASHloop_dbl(bi, b->thash, r, v)
2018 : break;
2019 : break;
2020 298 : case TYPE_lng:
2021 298 : HASHloop_lng(bi, b->thash, r, v)
2022 : break;
2023 : break;
2024 : #ifdef HAVE_HGE
2025 0 : case TYPE_hge:
2026 0 : HASHloop_hge(bi, b->thash, r, v)
2027 : break;
2028 : break;
2029 : #endif
2030 0 : case TYPE_uuid:
2031 0 : HASHloop_uuid(bi, b->thash, r, v)
2032 : break;
2033 : break;
2034 475172 : case TYPE_str:
2035 659858 : HASHloop_str(bi, b->thash, r, v)
2036 : break;
2037 : break;
2038 1 : default:
2039 3 : HASHloop(bi, b->thash, r, v)
2040 : break;
2041 : break;
2042 : }
2043 475502 : MT_rwlock_rdunlock(&b->thashlock);
2044 475508 : bat_iterator_end(&bi);
2045 475508 : return r;
2046 : }
2047 0 : hashfnd_failed:
2048 : /* can't build hash table, search the slow way */
2049 0 : GDKclrerr();
2050 0 : return slowfnd(b, v);
2051 : }
2052 :
2053 : /*
2054 : * @+ BAT Property Management
2055 : *
2056 : * The function BATcount returns the number of active elements in a
2057 : * BAT. Counting is type independent. It can be implemented quickly,
2058 : * because the system ensures a dense BUN list.
2059 : */
2060 : void
2061 1893933 : BATsetcapacity(BAT *b, BUN cnt)
2062 : {
2063 1893933 : b->batCapacity = cnt;
2064 1893933 : assert(b->batCount <= cnt);
2065 1893933 : }
2066 :
2067 : /* Set the batCount value for the bat and also set some dependent
2068 : * properties. This function should be called only when it is save from
2069 : * concurrent use (e.g. when theaplock is being held). */
2070 : void
2071 74964733 : BATsetcount(BAT *b, BUN cnt)
2072 : {
2073 : /* head column is always VOID, and some head properties never change */
2074 74964733 : assert(!is_oid_nil(b->hseqbase));
2075 74964733 : assert(cnt <= BUN_MAX);
2076 :
2077 74964733 : b->batCount = cnt;
2078 74964733 : if (b->theap->parentid == b->batCacheid) {
2079 73078744 : b->theap->dirty |= b->ttype != TYPE_void && cnt > 0;
2080 73078744 : b->theap->free = tailsize(b, cnt);
2081 : }
2082 74964733 : if (b->ttype == TYPE_void)
2083 9516903 : b->batCapacity = cnt;
2084 74964733 : if (cnt <= 1) {
2085 13869350 : b->tsorted = b->trevsorted = ATOMlinear(b->ttype);
2086 13869350 : b->tnosorted = b->tnorevsorted = 0;
2087 : }
2088 : /* if the BAT was made smaller, we need to zap some values */
2089 74964733 : if (b->tnosorted >= BATcount(b))
2090 12062884 : b->tnosorted = 0;
2091 74964733 : if (b->tnorevsorted >= BATcount(b))
2092 11988064 : b->tnorevsorted = 0;
2093 74964733 : if (b->tnokey[0] >= BATcount(b) || b->tnokey[1] >= BATcount(b)) {
2094 12033784 : b->tnokey[0] = 0;
2095 12033784 : b->tnokey[1] = 0;
2096 : }
2097 74964733 : if (b->ttype == TYPE_void) {
2098 9517015 : b->tsorted = true;
2099 9517015 : if (is_oid_nil(b->tseqbase)) {
2100 4907596 : b->tkey = cnt <= 1;
2101 4907596 : b->trevsorted = true;
2102 4907596 : b->tnil = true;
2103 4907596 : b->tnonil = false;
2104 : } else {
2105 4609419 : b->tkey = true;
2106 4609419 : b->trevsorted = cnt <= 1;
2107 4609419 : b->tnil = false;
2108 4609419 : b->tnonil = true;
2109 : }
2110 : }
2111 74964733 : assert(b->batCapacity >= cnt);
2112 74964733 : }
2113 :
2114 : /*
2115 : * The key and name properties can be changed at any time. Keyed
2116 : * dimensions are automatically supported by an auxiliary hash-based
2117 : * access structure to speed up searching. Turning off the key
2118 : * integrity property does not cause the index to disappear. It can
2119 : * still be used to speed-up retrieval. The routine BATkey sets the
2120 : * key property of the association head.
2121 : */
2122 : gdk_return
2123 47566 : BATkey(BAT *b, bool flag)
2124 : {
2125 47566 : BATcheck(b, GDK_FAIL);
2126 47566 : if (b->ttype == TYPE_void) {
2127 1123 : if (BATtdense(b) && !flag) {
2128 0 : GDKerror("dense column must be unique.\n");
2129 0 : return GDK_FAIL;
2130 : }
2131 1123 : if (is_oid_nil(b->tseqbase) && flag && b->batCount > 1) {
2132 0 : GDKerror("void column cannot be unique.\n");
2133 0 : return GDK_FAIL;
2134 : }
2135 : }
2136 47566 : b->tkey = flag;
2137 47566 : if (!flag) {
2138 32466 : b->tseqbase = oid_nil;
2139 : } else
2140 15100 : b->tnokey[0] = b->tnokey[1] = 0;
2141 47566 : gdk_return rc = GDK_SUCCEED;
2142 47566 : if (flag && VIEWtparent(b)) {
2143 : /* if a view is key, then so is the parent if the two
2144 : * are aligned */
2145 2 : BAT *bp = BATdescriptor(VIEWtparent(b));
2146 2 : if (bp != NULL) {
2147 2 : MT_lock_set(&bp->theaplock);
2148 4 : if (BATcount(b) == BATcount(bp) &&
2149 2 : ATOMtype(BATttype(b)) == ATOMtype(BATttype(bp)) &&
2150 2 : !BATtkey(bp) &&
2151 0 : ((BATtvoid(b) && BATtvoid(bp) && b->tseqbase == bp->tseqbase) ||
2152 : BATcount(b) == 0))
2153 0 : rc = BATkey(bp, true);
2154 2 : MT_lock_unset(&bp->theaplock);
2155 2 : BBPunfix(bp->batCacheid);
2156 : }
2157 : }
2158 : return rc;
2159 : }
2160 :
2161 : void
2162 1700352 : BAThseqbase(BAT *b, oid o)
2163 : {
2164 1700352 : if (b != NULL) {
2165 1700352 : assert(o <= GDK_oid_max); /* i.e., not oid_nil */
2166 1700352 : assert(o + BATcount(b) <= GDK_oid_max);
2167 1700352 : b->hseqbase = o;
2168 : }
2169 1700352 : }
2170 :
2171 : void
2172 5510491 : BATtseqbase(BAT *b, oid o)
2173 : {
2174 5510491 : assert(o <= oid_nil);
2175 5510491 : if (b == NULL)
2176 : return;
2177 5510491 : assert(is_oid_nil(o) || o + BATcount(b) <= GDK_oid_max);
2178 5510491 : if (ATOMtype(b->ttype) == TYPE_oid) {
2179 4765915 : b->tseqbase = o;
2180 :
2181 : /* adapt keyness */
2182 4765915 : if (BATtvoid(b)) {
2183 4740782 : b->tsorted = true;
2184 4740782 : if (is_oid_nil(o)) {
2185 112 : b->tkey = b->batCount <= 1;
2186 112 : b->tnonil = b->batCount == 0;
2187 112 : b->tnil = b->batCount > 0;
2188 112 : b->trevsorted = true;
2189 112 : b->tnosorted = b->tnorevsorted = 0;
2190 112 : if (!b->tkey) {
2191 0 : b->tnokey[0] = 0;
2192 0 : b->tnokey[1] = 1;
2193 : } else {
2194 112 : b->tnokey[0] = b->tnokey[1] = 0;
2195 : }
2196 : } else {
2197 4740670 : if (!b->tkey) {
2198 15810 : b->tkey = true;
2199 15810 : b->tnokey[0] = b->tnokey[1] = 0;
2200 : }
2201 4740670 : b->tnonil = true;
2202 4740670 : b->tnil = false;
2203 4740670 : b->trevsorted = b->batCount <= 1;
2204 4740670 : if (!b->trevsorted)
2205 16103 : b->tnorevsorted = 1;
2206 : }
2207 : }
2208 : } else {
2209 744576 : assert(o == oid_nil);
2210 744576 : b->tseqbase = oid_nil;
2211 : }
2212 : }
2213 :
2214 : /*
2215 : * @- Change the BAT access permissions (read, append, write)
2216 : * Regrettably, BAT access-permissions, persistent status and memory
2217 : * map modes, interact in ways that makes one's brain sizzle. This
2218 : * makes BATsetaccess and TMcommit (where a change in BAT persistence
2219 : * mode is made permanent) points in which the memory map status of
2220 : * bats needs to be carefully re-assessed and ensured.
2221 : *
2222 : * Another complication is the fact that during commit, concurrent
2223 : * users may access the heaps, such that the simple solution
2224 : * unmap;re-map is out of the question.
2225 : * Even worse, it is not possible to even rename an open mmap file in
2226 : * Windows. For this purpose, we dropped the old .priv scheme, which
2227 : * relied on file moves. Now, the file that is opened with mmap is
2228 : * always the X file, in case of newstorage=STORE_PRIV, we save in a
2229 : * new file X.new
2230 : *
2231 : * we must consider the following dimensions:
2232 : *
2233 : * persistence:
2234 : * not simply the current persistence mode but whether the bat *was*
2235 : * present at the last commit point (BBP status & BBPEXISTING).
2236 : * The crucial issue is namely whether we must guarantee recovery
2237 : * to a previous sane state.
2238 : *
2239 : * access:
2240 : * whether the BAT is BAT_READ or BAT_WRITE. Note that BAT_APPEND
2241 : * is usually the same as BAT_READ (as our concern are only data pages
2242 : * that already existed at the last commit).
2243 : *
2244 : * storage:
2245 : * the current way the heap file X is memory-mapped;
2246 : * STORE_MMAP uses direct mapping (so dirty pages may be flushed
2247 : * at any time to disk), STORE_PRIV uses copy-on-write.
2248 : *
2249 : * newstorage:
2250 : * the current save-regime. STORE_MMAP calls msync() on the heap X,
2251 : * whereas STORE_PRIV writes the *entire* heap in a file: X.new
2252 : * If a BAT is loaded from disk, the field newstorage is used
2253 : * to set storage as well (so before change-access and commit-
2254 : * persistence mayhem, we always have newstorage=storage).
2255 : *
2256 : * change-access:
2257 : * what happens if the bat-access mode is changed from
2258 : * BAT_READ into BAT_WRITE (or vice versa).
2259 : *
2260 : * commit-persistence:
2261 : * what happens during commit if the bat-persistence mode was
2262 : * changed (from TRANSIENT into PERSISTENT, or vice versa).
2263 : *
2264 : * this is the scheme:
2265 : *
2266 : * persistence access newstorage storage change-access commit-persistence
2267 : * =========== ========= ========== ========== ============= ==================
2268 : * 0 transient BAT_READ STORE_MMAP STORE_MMAP =>2 =>4
2269 : * 1 transient BAT_READ STORE_PRIV STORE_PRIV =>3 =>5
2270 : * 2 transient BAT_WRITE STORE_MMAP STORE_MMAP =>0 =>6+
2271 : * 3 transient BAT_WRITE STORE_PRIV STORE_PRIV =>1 =>7
2272 : * 4 persistent BAT_READ STORE_MMAP STORE_MMAP =>6+ =>0
2273 : * 5 persistent BAT_READ STORE_PRIV STORE_PRIV =>7 =>1
2274 : * 6 persistent BAT_WRITE STORE_PRIV STORE_MMAP del X.new=>4+ del X.new;=>2+
2275 : * 7 persistent BAT_WRITE STORE_PRIV STORE_PRIV =>5 =>3
2276 : *
2277 : * exception states:
2278 : * a transient BAT_READ STORE_PRIV STORE_MMAP =>b =>c
2279 : * b transient BAT_WRITE STORE_PRIV STORE_MMAP =>a =>6
2280 : * c persistent BAT_READ STORE_PRIV STORE_MMAP =>6 =>a
2281 : *
2282 : * (+) indicates that we must ensure that the heap gets saved in its new mode
2283 : *
2284 : * Note that we now allow a heap with save-regime STORE_PRIV that was
2285 : * actually mapped STORE_MMAP. In effect, the potential corruption of
2286 : * the X file is compensated by writing out full X.new files that take
2287 : * precedence. When transitioning out of this state towards one with
2288 : * both storage regime and OS as STORE_MMAP we need to move the X.new
2289 : * files into the backup directory. Then msync the X file and (on
2290 : * success) remove the X.new; see backup_new().
2291 : *
2292 : * Exception states are only reachable if the commit fails and those
2293 : * new persistent bats have already been processed (but never become
2294 : * part of a committed state). In that case a transition 2=>6 may end
2295 : * up 2=>b. Exception states a and c are reachable from b.
2296 : *
2297 : * Errors in HEAPchangeaccess() can be handled atomically inside the
2298 : * routine. The work on changing mmap modes HEAPcommitpersistence()
2299 : * is done during the BBPsync() for all bats that are newly persistent
2300 : * (BBPNEW). After the TMcommit(), it is done for those bats that are
2301 : * no longer persistent after the commit (BBPDELETED), only if it
2302 : * succeeds. Such transient bats cannot be processed before the
2303 : * commit, because the commit may fail and then the more unsafe
2304 : * transient mmap modes would be present on a persistent bat.
2305 : *
2306 : * See dirty_bat() in BBPsync() -- gdk_bbp.c and epilogue() in
2307 : * gdk_tm.c.
2308 : *
2309 : * Including the exception states, we have 11 of the 16
2310 : * combinations. As for the 5 avoided states, all four
2311 : * (persistence,access) states with (STORE_MMAP,STORE_PRIV) are
2312 : * omitted (this would amount to an msync() save regime on a
2313 : * copy-on-write heap -- which does not work). The remaining avoided
2314 : * state is the patently unsafe
2315 : * (persistent,BAT_WRITE,STORE_MMAP,STORE_MMAP).
2316 : *
2317 : * Note that after a server restart exception states are gone, as on
2318 : * BAT loads the saved descriptor is inspected again (which will
2319 : * reproduce the state at the last succeeded commit).
2320 : *
2321 : * To avoid exception states, a TMsubcommit protocol would need to be
2322 : * used which is too heavy for BATsetaccess().
2323 : *
2324 : * Note that this code is not about making heaps mmap-ed in the first
2325 : * place. It is just about determining which flavor of mmap should be
2326 : * used. The MAL user is oblivious of such details.
2327 : */
2328 :
2329 : /* rather than deleting X.new, we comply with the commit protocol and
2330 : * move it to backup storage */
2331 : static gdk_return
2332 0 : backup_new(Heap *hp, bool lock)
2333 : {
2334 0 : int batret, bakret, ret = -1;
2335 0 : char *batpath, *bakpath;
2336 0 : struct stat st;
2337 :
2338 0 : char *bak_filename = NULL;
2339 0 : if ((bak_filename = strrchr(hp->filename, DIR_SEP)) != NULL)
2340 0 : bak_filename++;
2341 : else
2342 : bak_filename = hp->filename;
2343 : /* check for an existing X.new in BATDIR, BAKDIR and SUBDIR */
2344 0 : batpath = GDKfilepath(hp->farmid, BATDIR, hp->filename, "new");
2345 0 : bakpath = GDKfilepath(hp->farmid, BAKDIR, bak_filename, "new");
2346 0 : if (batpath != NULL && bakpath != NULL) {
2347 : /* file actions here interact with the global commits */
2348 0 : if (lock)
2349 0 : BBPtmlock();
2350 :
2351 0 : batret = MT_stat(batpath, &st);
2352 0 : bakret = MT_stat(bakpath, &st);
2353 :
2354 0 : if (batret == 0 && bakret) {
2355 : /* no backup yet, so move the existing X.new there out
2356 : * of the way */
2357 0 : if ((ret = MT_rename(batpath, bakpath)) < 0)
2358 0 : GDKsyserror("backup_new: rename %s to %s failed\n",
2359 : batpath, bakpath);
2360 0 : TRC_DEBUG(IO_, "rename(%s,%s) = %d\n", batpath, bakpath, ret);
2361 0 : } else if (batret == 0) {
2362 : /* there is a backup already; just remove the X.new */
2363 0 : if ((ret = MT_remove(batpath)) != 0)
2364 0 : GDKsyserror("backup_new: remove %s failed\n", batpath);
2365 0 : TRC_DEBUG(IO_, "remove(%s) = %d\n", batpath, ret);
2366 : } else {
2367 : ret = 0;
2368 : }
2369 0 : if (lock)
2370 0 : BBPtmunlock();
2371 : }
2372 0 : GDKfree(batpath);
2373 0 : GDKfree(bakpath);
2374 0 : return ret ? GDK_FAIL : GDK_SUCCEED;
2375 : }
2376 :
2377 : #define ACCESSMODE(wr,rd) ((wr)?BAT_WRITE:(rd)?BAT_READ:-1)
2378 :
2379 : /* transition heap from readonly to writable */
2380 : static storage_t
2381 4609298 : HEAPchangeaccess(Heap *hp, int dstmode, bool existing)
2382 : {
2383 4609298 : if (hp->base == NULL || hp->newstorage == STORE_MEM || !existing || dstmode == -1)
2384 4609298 : return hp->newstorage; /* 0<=>2,1<=>3,a<=>b */
2385 :
2386 0 : if (dstmode == BAT_WRITE) {
2387 0 : if (hp->storage != STORE_PRIV)
2388 0 : hp->dirty = true; /* exception c does not make it dirty */
2389 0 : return STORE_PRIV; /* 4=>6,5=>7,c=>6 persistent BAT_WRITE needs STORE_PRIV */
2390 : }
2391 0 : if (hp->storage == STORE_MMAP) { /* 6=>4 */
2392 0 : hp->dirty = true;
2393 0 : return backup_new(hp, true) != GDK_SUCCEED ? STORE_INVALID : STORE_MMAP; /* only called for existing bats */
2394 : }
2395 : return hp->storage; /* 7=>5 */
2396 : }
2397 :
2398 : /* heap changes persistence mode (at commit point) */
2399 : static storage_t
2400 198365 : HEAPcommitpersistence(Heap *hp, bool writable, bool existing)
2401 : {
2402 198365 : if (existing) { /* existing, ie will become transient */
2403 57977 : if (hp->storage == STORE_MMAP && hp->newstorage == STORE_PRIV && writable) { /* 6=>2 */
2404 0 : hp->dirty = true;
2405 0 : return backup_new(hp, false) != GDK_SUCCEED ? STORE_INVALID : STORE_MMAP; /* only called for existing bats */
2406 : }
2407 57977 : return hp->newstorage; /* 4=>0,5=>1,7=>3,c=>a no change */
2408 : }
2409 : /* !existing, ie will become persistent */
2410 140388 : if (hp->newstorage == STORE_MEM)
2411 : return hp->newstorage;
2412 774 : if (hp->newstorage == STORE_MMAP && !writable)
2413 : return STORE_MMAP; /* 0=>4 STORE_MMAP */
2414 :
2415 0 : if (hp->newstorage == STORE_MMAP)
2416 0 : hp->dirty = true; /* 2=>6 */
2417 : return STORE_PRIV; /* 1=>5,2=>6,3=>7,a=>c,b=>6 states */
2418 : }
2419 :
2420 :
2421 : #define ATOMappendpriv(t, h) (ATOMstorage(t) != TYPE_str /*|| GDK_ELIMDOUBLES(h) */)
2422 :
2423 : /* change the heap modes at a commit */
2424 : gdk_return
2425 175024 : BATcheckmodes(BAT *b, bool existing)
2426 : {
2427 175024 : storage_t m1 = STORE_MEM, m3 = STORE_MEM;
2428 175024 : bool dirty = false, wr;
2429 :
2430 175024 : BATcheck(b, GDK_FAIL);
2431 :
2432 175024 : wr = (b->batRestricted == BAT_WRITE);
2433 175024 : if (b->ttype) {
2434 175024 : m1 = HEAPcommitpersistence(b->theap, wr, existing);
2435 175024 : dirty |= (b->theap->newstorage != m1);
2436 : }
2437 :
2438 175024 : if (b->tvheap) {
2439 23341 : bool ta = (b->batRestricted == BAT_APPEND) && ATOMappendpriv(b->ttype, b->tvheap);
2440 23341 : m3 = HEAPcommitpersistence(b->tvheap, wr || ta, existing);
2441 23341 : dirty |= (b->tvheap->newstorage != m3);
2442 : }
2443 175024 : if (m1 == STORE_INVALID || m3 == STORE_INVALID)
2444 : return GDK_FAIL;
2445 :
2446 175024 : if (dirty) {
2447 0 : b->theap->newstorage = m1;
2448 0 : if (b->tvheap)
2449 0 : b->tvheap->newstorage = m3;
2450 : }
2451 : return GDK_SUCCEED;
2452 : }
2453 :
2454 : BAT *
2455 5818309 : BATsetaccess(BAT *b, restrict_t newmode)
2456 : {
2457 5818309 : restrict_t bakmode;
2458 :
2459 5818309 : BATcheck(b, NULL);
2460 5818309 : if (newmode != BAT_READ &&
2461 19070 : (isVIEW(b) || (ATOMIC_GET(&b->theap->refs) & HEAPREFS) > 1)) {
2462 0 : BAT *bn = COLcopy(b, b->ttype, true, b->batRole);
2463 0 : BBPunfix(b->batCacheid);
2464 0 : if (bn == NULL)
2465 : return NULL;
2466 : b = bn;
2467 : }
2468 5818309 : MT_lock_set(&b->theaplock);
2469 5815281 : bakmode = b->batRestricted;
2470 5815281 : if (bakmode != newmode) {
2471 3985224 : bool existing = (BBP_status(b->batCacheid) & BBPEXISTING) != 0;
2472 3985224 : bool wr = (newmode == BAT_WRITE);
2473 3985224 : bool rd = (bakmode == BAT_WRITE);
2474 3985224 : storage_t m1 = STORE_MEM, m3 = STORE_MEM;
2475 3985224 : storage_t b1 = STORE_MEM, b3 = STORE_MEM;
2476 :
2477 3985224 : if (b->theap->parentid == b->batCacheid) {
2478 3983423 : b1 = b->theap->newstorage;
2479 3993666 : m1 = HEAPchangeaccess(b->theap, ACCESSMODE(wr, rd), existing);
2480 : }
2481 3986817 : if (b->tvheap && b->tvheap->parentid == b->batCacheid) {
2482 623206 : bool ta = (newmode == BAT_APPEND && ATOMappendpriv(b->ttype, b->tvheap));
2483 623206 : b3 = b->tvheap->newstorage;
2484 1246375 : m3 = HEAPchangeaccess(b->tvheap, ACCESSMODE(wr && ta, rd && ta), existing);
2485 : }
2486 3986733 : if (m1 == STORE_INVALID || m3 == STORE_INVALID) {
2487 1314155 : MT_lock_unset(&b->theaplock);
2488 1314533 : BBPunfix(b->batCacheid);
2489 1314533 : return NULL;
2490 : }
2491 :
2492 : /* set new access mode and mmap modes */
2493 2672578 : b->batRestricted = newmode;
2494 2672578 : if (b->theap->parentid == b->batCacheid)
2495 2672207 : b->theap->newstorage = m1;
2496 2672578 : if (b->tvheap && b->tvheap->parentid == b->batCacheid)
2497 604553 : b->tvheap->newstorage = m3;
2498 :
2499 2672578 : MT_lock_unset(&b->theaplock);
2500 2672543 : if (existing && !isVIEW(b) && BBPsave(b) != GDK_SUCCEED) {
2501 : /* roll back all changes */
2502 0 : MT_lock_set(&b->theaplock);
2503 0 : b->batRestricted = bakmode;
2504 0 : b->theap->newstorage = b1;
2505 0 : if (b->tvheap)
2506 0 : b->tvheap->newstorage = b3;
2507 0 : MT_lock_unset(&b->theaplock);
2508 0 : BBPunfix(b->batCacheid);
2509 0 : return NULL;
2510 : }
2511 : } else {
2512 1830057 : MT_lock_unset(&b->theaplock);
2513 : }
2514 : return b;
2515 : }
2516 :
2517 : restrict_t
2518 0 : BATgetaccess(BAT *b)
2519 : {
2520 0 : BATcheck(b, BAT_WRITE);
2521 0 : MT_lock_set(&b->theaplock);
2522 0 : restrict_t restricted = b->batRestricted;
2523 0 : MT_lock_unset(&b->theaplock);
2524 0 : return restricted;
2525 : }
2526 :
2527 : /*
2528 : * @- change BAT persistency (persistent,session,transient)
2529 : * In the past, we prevented BATS with certain types from being saved at all:
2530 : * - BATs of BATs, as having recursive bats creates cascading
2531 : * complexities in commits/aborts.
2532 : * - any atom with refcounts, as the BBP has no overview of such
2533 : * user-defined refcounts.
2534 : * - pointer types, as the values they point to are bound to be transient.
2535 : *
2536 : * However, nowadays we do allow such saves, as the BBP swapping
2537 : * mechanism was altered to be able to save transient bats temporarily
2538 : * to disk in order to make room. Thus, we must be able to save any
2539 : * transient BAT to disk.
2540 : *
2541 : * What we don't allow is to make such bats persistent.
2542 : *
2543 : * Although the persistent state does influence the allowed mmap
2544 : * modes, this only goes for the *real* committed persistent
2545 : * state. Making the bat persistent with BATmode does not matter for
2546 : * the heap modes until the commit point is reached. So we do not need
2547 : * to do anything with heap modes yet at this point.
2548 : */
2549 : gdk_return
2550 430443 : BATmode(BAT *b, bool transient)
2551 : {
2552 430443 : BATcheck(b, GDK_FAIL);
2553 :
2554 : /* can only make a bat PERSISTENT if its role is already
2555 : * PERSISTENT */
2556 430443 : assert(transient || b->batRole == PERSISTENT);
2557 : /* cannot make a view PERSISTENT */
2558 245438 : assert(transient || !isVIEW(b));
2559 :
2560 430443 : if (b->batRole == TRANSIENT && !transient) {
2561 0 : GDKerror("cannot change mode of BAT in TRANSIENT farm.\n");
2562 0 : return GDK_FAIL;
2563 : }
2564 :
2565 430443 : BATiter bi = bat_iterator(b);
2566 430443 : bool mustrelease = false;
2567 430443 : bool mustretain = false;
2568 430443 : bat bid = b->batCacheid;
2569 :
2570 430443 : if (transient != bi.transient) {
2571 430443 : if (!transient) {
2572 245438 : if (ATOMisdescendant(b->ttype, TYPE_ptr)) {
2573 0 : GDKerror("%s type implies that %s[%s] "
2574 : "cannot be made persistent.\n",
2575 : ATOMname(b->ttype), BATgetId(b),
2576 : ATOMname(b->ttype));
2577 0 : bat_iterator_end(&bi);
2578 0 : return GDK_FAIL;
2579 : }
2580 : }
2581 :
2582 : /* we need to delay the calls to BBPretain and
2583 : * BBPrelease until after we have released our reference
2584 : * to the heaps (i.e. until after bat_iterator_end),
2585 : * because in either case, BBPfree can be called (either
2586 : * directly here or in BBPtrim) which waits for the heap
2587 : * reference to come down. BBPretain calls incref which
2588 : * waits until the trim that is waiting for us is done,
2589 : * so that causes deadlock, and BBPrelease can call
2590 : * BBPfree which causes deadlock with a single thread */
2591 185005 : if (!transient) {
2592 : /* persistent BATs get a logical reference */
2593 : mustretain = true;
2594 185005 : } else if (!bi.transient) {
2595 : /* transient BATs loose their logical reference */
2596 185005 : mustrelease = true;
2597 : }
2598 430443 : MT_lock_set(&GDKswapLock(bid));
2599 430443 : if (!transient) {
2600 245438 : if (BBP_status(bid) & BBPDELETED) {
2601 0 : BBP_status_on(bid, BBPEXISTING);
2602 0 : BBP_status_off(bid, BBPDELETED);
2603 : } else
2604 245438 : BBP_status_on(bid, BBPNEW);
2605 185005 : } else if (!bi.transient) {
2606 185005 : if (!(BBP_status(bid) & BBPNEW))
2607 60169 : BBP_status_on(bid, BBPDELETED);
2608 185005 : BBP_status_off(bid, BBPPERSISTENT);
2609 : }
2610 : /* session bats or persistent bats that did not
2611 : * witness a commit yet may have been saved */
2612 430443 : MT_lock_set(&b->theaplock);
2613 430443 : if (b->batCopiedtodisk) {
2614 49693 : if (!transient) {
2615 67 : BBP_status_off(bid, BBPTMP);
2616 : } else {
2617 : /* TMcommit must remove it to
2618 : * guarantee free space */
2619 49626 : BBP_status_on(bid, BBPTMP);
2620 : }
2621 : }
2622 430443 : b->batTransient = transient;
2623 430443 : MT_lock_unset(&b->theaplock);
2624 430443 : MT_lock_unset(&GDKswapLock(bid));
2625 : }
2626 430443 : bat_iterator_end(&bi);
2627 : /* retain/release after bat_iterator_end because of refs to heaps */
2628 430443 : if (mustretain)
2629 245438 : BBPretain(bid);
2630 185005 : else if (mustrelease)
2631 185005 : BBPrelease(bid);
2632 : return GDK_SUCCEED;
2633 : }
2634 :
2635 : /* BATassertProps checks whether properties are set correctly. Under
2636 : * no circumstances will it change any properties. Note that the
2637 : * "nil" property is not actually used anywhere, but it is checked. */
2638 :
2639 : #ifdef NDEBUG
2640 : /* assertions are disabled, turn failing tests into a message */
2641 : #undef assert
2642 : #define assert(test) ((void) ((test) || (TRC_CRITICAL_ENDIF(CHECK_, "Assertion `%s' failed\n", #test), 0)))
2643 : #endif
2644 :
2645 : static void
2646 250980060 : assert_ascii(const char *s)
2647 : {
2648 501960120 : if (!strNil(s)) {
2649 3920982804 : while (*s) {
2650 3723111163 : assert((*s & 0x80) == 0);
2651 3723111163 : s++;
2652 : }
2653 : }
2654 250980060 : }
2655 :
2656 : /* Assert that properties are set correctly.
2657 : *
2658 : * A BAT can have a bunch of properties set. Mostly, the property
2659 : * bits are set if we *know* the property holds, and not set if we
2660 : * don't know whether the property holds (or if we know it doesn't
2661 : * hold). All properties are per column.
2662 : *
2663 : * The properties currently maintained are:
2664 : *
2665 : * seqbase Only valid for TYPE_oid and TYPE_void columns: each
2666 : * value in the column is exactly one more than the
2667 : * previous value, starting at position 0 with the value
2668 : * stored in this property.
2669 : * This implies sorted, key, nonil (which therefore need
2670 : * to be set).
2671 : * nil There is at least one NIL value in the column.
2672 : * nonil There are no NIL values in the column.
2673 : * key All values in the column are distinct.
2674 : * sorted The column is sorted (ascending). If also revsorted,
2675 : * then all values are equal.
2676 : * revsorted The column is reversely sorted (descending). If
2677 : * also sorted, then all values are equal.
2678 : * nosorted BUN position which proofs not sorted (given position
2679 : * and one before are not ordered correctly).
2680 : * norevsorted BUN position which proofs not revsorted (given position
2681 : * and one before are not ordered correctly).
2682 : * nokey Pair of BUN positions that proof not all values are
2683 : * distinct (i.e. values at given locations are equal).
2684 : * ascii Only valid for TYPE_str columns: all strings in the column
2685 : * are ASCII, i.e. the UTF-8 encoding for all characters is a
2686 : * single byte.
2687 : *
2688 : * Note that the functions BATtseqbase and BATkey also set more
2689 : * properties than you might suspect. When setting properties on a
2690 : * newly created and filled BAT, you may want to first make sure the
2691 : * batCount is set correctly (e.g. by calling BATsetcount), then use
2692 : * BATtseqbase and BATkey, and finally set the other properties.
2693 : *
2694 : * For a view, we cannot check all properties, since it is possible with
2695 : * the way the SQL layer works, that a parent BAT gets changed, changing
2696 : * the properties, while there is a view. The view is supposed to look
2697 : * at only the non-changing part of the BAT (through candidate lists),
2698 : * but this means that the properties of the view might not be correct.
2699 : * For this reason, for views, we skip all property checking that looks
2700 : * at the BAT content.
2701 : */
2702 :
2703 : void
2704 12840470 : BATassertProps(BAT *b)
2705 : {
2706 12840470 : unsigned bbpstatus;
2707 12840470 : BUN p, q;
2708 12840470 : int (*cmpf)(const void *, const void *);
2709 12840470 : int cmp;
2710 12840470 : const void *prev = NULL, *valp, *nilp;
2711 12840470 : char filename[sizeof(b->theap->filename)];
2712 12840470 : bool isview1, isview2;
2713 :
2714 : /* do the complete check within a lock */
2715 12840470 : MT_lock_set(&b->theaplock);
2716 :
2717 : /* general BAT sanity */
2718 12802753 : assert(b != NULL);
2719 12802753 : assert(b->batCacheid > 0);
2720 12802753 : assert(b->batCacheid < getBBPsize());
2721 12832400 : assert(b == BBP_desc(b->batCacheid));
2722 12832400 : assert(b->batCount >= b->batInserted);
2723 :
2724 : /* headless */
2725 12832400 : assert(b->hseqbase <= GDK_oid_max); /* non-nil seqbase */
2726 12832400 : assert(b->hseqbase + BATcount(b) <= GDK_oid_max);
2727 :
2728 12832400 : isview1 = b->theap->parentid != b->batCacheid;
2729 12832400 : isview2 = b->tvheap && b->tvheap->parentid != b->batCacheid;
2730 :
2731 12832400 : bbpstatus = BBP_status(b->batCacheid);
2732 : /* only at most one of BBPDELETED, BBPEXISTING, BBPNEW may be set */
2733 12832400 : assert(((bbpstatus & BBPDELETED) != 0) +
2734 : ((bbpstatus & BBPEXISTING) != 0) +
2735 : ((bbpstatus & BBPNEW) != 0) <= 1);
2736 :
2737 12832400 : assert(b->ttype >= TYPE_void);
2738 12832400 : assert(b->ttype < GDKatomcnt);
2739 12832400 : assert(isview1 ||
2740 : b->ttype == TYPE_void ||
2741 : BBPfarms[b->theap->farmid].roles & (1 << b->batRole));
2742 12832400 : assert(isview2 ||
2743 : b->tvheap == NULL ||
2744 : (BBPfarms[b->tvheap->farmid].roles & (1 << b->batRole)));
2745 :
2746 12832400 : cmpf = ATOMcompare(b->ttype);
2747 12832400 : nilp = ATOMnilptr(b->ttype);
2748 :
2749 12832400 : assert(isview1 || b->theap->free >= tailsize(b, BATcount(b)));
2750 12832400 : if (b->ttype != TYPE_void) {
2751 9921739 : assert(b->batCount <= b->batCapacity);
2752 9921739 : assert(isview1 || b->theap->size >= b->theap->free);
2753 9921739 : if (ATOMstorage(b->ttype) == TYPE_msk) {
2754 : /* 32 values per 4-byte word (that's not the
2755 : * same as 8 values per byte...) */
2756 4123 : assert(isview1 || b->theap->size >= 4 * ((b->batCapacity + 31) / 32));
2757 : } else
2758 9917616 : assert(isview1 || b->theap->size >> b->tshift >= b->batCapacity);
2759 : }
2760 12832400 : if (!isview1) {
2761 9654175 : strconcat_len(filename, sizeof(filename),
2762 9654175 : BBP_physical(b->theap->parentid),
2763 1728656 : b->ttype == TYPE_str ? b->twidth == 1 ? ".tail1" : b->twidth == 2 ? ".tail2" :
2764 : #if SIZEOF_VAR_T == 8
2765 : b->twidth == 4 ? ".tail4" :
2766 : #endif
2767 : ".tail" : ".tail",
2768 : NULL);
2769 9656885 : assert(strcmp(b->theap->filename, filename) == 0);
2770 : }
2771 12835110 : if (!isview2 && b->tvheap) {
2772 1623954 : strconcat_len(filename, sizeof(filename),
2773 1623954 : BBP_physical(b->tvheap->parentid),
2774 : ".theap",
2775 : NULL);
2776 1624517 : assert(strcmp(b->tvheap->filename, filename) == 0);
2777 : }
2778 :
2779 : /* void, str and blob imply varsized */
2780 12835673 : if (ATOMstorage(b->ttype) == TYPE_str ||
2781 : ATOMstorage(b->ttype) == TYPE_blob)
2782 2325000 : assert(b->tvheap != NULL);
2783 : /* other "known" types are not varsized */
2784 12835673 : if (ATOMstorage(b->ttype) > TYPE_void &&
2785 : ATOMstorage(b->ttype) < TYPE_str)
2786 7607428 : assert(b->tvheap == NULL);
2787 : /* shift and width have a particular relationship */
2788 12835673 : if (ATOMstorage(b->ttype) == TYPE_str)
2789 2322868 : assert(b->twidth >= 1 && b->twidth <= ATOMsize(b->ttype));
2790 : else
2791 10512805 : assert(b->twidth == ATOMsize(b->ttype));
2792 12835673 : assert(b->tseqbase <= oid_nil);
2793 : /* only oid/void columns can be dense */
2794 12835673 : assert(is_oid_nil(b->tseqbase) || b->ttype == TYPE_oid || b->ttype == TYPE_void);
2795 : /* a column cannot both have and not have NILs */
2796 12835673 : assert(!b->tnil || !b->tnonil);
2797 : /* only string columns can be ASCII */
2798 12835673 : assert(!b->tascii || ATOMstorage(b->ttype) == TYPE_str);
2799 12835673 : if (b->ttype == TYPE_void) {
2800 2896670 : assert(b->tshift == 0);
2801 2896670 : assert(b->twidth == 0);
2802 2896670 : assert(b->tsorted);
2803 2896670 : if (is_oid_nil(b->tseqbase)) {
2804 257 : assert(b->tvheap == NULL);
2805 257 : assert(BATcount(b) == 0 || !b->tnonil);
2806 257 : assert(BATcount(b) <= 1 || !b->tkey);
2807 257 : assert(b->trevsorted);
2808 : } else {
2809 2896413 : if (b->tvheap != NULL) {
2810 : /* candidate list with exceptions */
2811 36100 : assert(b->batRole == TRANSIENT || b->batRole == SYSTRANS);
2812 36100 : assert(b->tvheap->free <= b->tvheap->size);
2813 36100 : assert(b->tvheap->free >= sizeof(ccand_t));
2814 36100 : assert((negoid_cand(b) && ccand_free(b) % SIZEOF_OID == 0) || mask_cand(b));
2815 36100 : if (negoid_cand(b) && ccand_free(b) > 0) {
2816 370 : const oid *oids = (const oid *) ccand_first(b);
2817 370 : q = ccand_free(b) / SIZEOF_OID;
2818 370 : assert(oids != NULL);
2819 370 : assert(b->tseqbase + BATcount(b) + q <= GDK_oid_max);
2820 : /* exceptions within range */
2821 370 : assert(oids[0] >= b->tseqbase);
2822 370 : assert(oids[q - 1] < b->tseqbase + BATcount(b) + q);
2823 : /* exceptions sorted */
2824 2839170 : for (p = 1; p < q; p++)
2825 2838800 : assert(oids[p - 1] < oids[p]);
2826 : }
2827 : }
2828 2896413 : assert(b->tseqbase + b->batCount <= GDK_oid_max);
2829 2896413 : assert(BATcount(b) == 0 || !b->tnil);
2830 2896413 : assert(BATcount(b) <= 1 || !b->trevsorted);
2831 2896413 : assert(b->tkey);
2832 2896413 : assert(b->tnonil);
2833 : }
2834 2896670 : MT_lock_unset(&b->theaplock);
2835 7466049 : return;
2836 : }
2837 :
2838 9939003 : BATiter bi = bat_iterator_nolock(b);
2839 :
2840 9939003 : if (BATtdense(b)) {
2841 191058 : assert(b->tseqbase + b->batCount <= GDK_oid_max);
2842 191058 : assert(b->ttype == TYPE_oid);
2843 191058 : assert(b->tsorted);
2844 191058 : assert(b->tkey);
2845 191058 : assert(b->tnonil);
2846 191058 : if ((q = b->batCount) != 0) {
2847 163129 : const oid *o = (const oid *) Tloc(b, 0);
2848 163129 : assert(*o == b->tseqbase);
2849 220025028 : for (p = 1; p < q; p++)
2850 219861899 : assert(o[p - 1] + 1 == o[p]);
2851 : }
2852 191058 : MT_lock_unset(&b->theaplock);
2853 191043 : return;
2854 : }
2855 9747945 : assert(1 << b->tshift == b->twidth);
2856 : /* only linear atoms can be sorted */
2857 9747945 : assert(!b->tsorted || ATOMlinear(b->ttype));
2858 9747945 : assert(!b->trevsorted || ATOMlinear(b->ttype));
2859 9747945 : if (ATOMlinear(b->ttype)) {
2860 9744720 : assert(b->tnosorted == 0 ||
2861 : (b->tnosorted > 0 &&
2862 : b->tnosorted < b->batCount));
2863 9744720 : assert(!b->tsorted || b->tnosorted == 0);
2864 9744720 : if (!isview1 &&
2865 9744720 : !isview2 &&
2866 1649900 : !b->tsorted &&
2867 115981 : b->tnosorted > 0 &&
2868 115981 : b->tnosorted < b->batCount)
2869 116002 : assert(cmpf(BUNtail(bi, b->tnosorted - 1),
2870 : BUNtail(bi, b->tnosorted)) > 0);
2871 9744674 : assert(b->tnorevsorted == 0 ||
2872 : (b->tnorevsorted > 0 &&
2873 : b->tnorevsorted < b->batCount));
2874 9744674 : assert(!b->trevsorted || b->tnorevsorted == 0);
2875 9744674 : if (!isview1 &&
2876 6443436 : !isview2 &&
2877 2213293 : !b->trevsorted &&
2878 474617 : b->tnorevsorted > 0 &&
2879 474617 : b->tnorevsorted < b->batCount)
2880 474617 : assert(cmpf(BUNtail(bi, b->tnorevsorted - 1),
2881 : BUNtail(bi, b->tnorevsorted)) < 0);
2882 : }
2883 : /* if tkey property set, both tnokey values must be 0 */
2884 9747781 : assert(!b->tkey || (b->tnokey[0] == 0 && b->tnokey[1] == 0));
2885 9747781 : if (!isview1 &&
2886 9747781 : !isview2 &&
2887 1961120 : !b->tkey &&
2888 1961120 : (b->tnokey[0] != 0 || b->tnokey[1] != 0)) {
2889 : /* if tkey not set and tnokey indicates a proof of
2890 : * non-key-ness, make sure the tnokey values are in
2891 : * range and indeed provide a proof */
2892 524264 : assert(b->tnokey[0] != b->tnokey[1]);
2893 524264 : assert(b->tnokey[0] < b->batCount);
2894 524264 : assert(b->tnokey[1] < b->batCount);
2895 524264 : assert(cmpf(BUNtail(bi, b->tnokey[0]),
2896 : BUNtail(bi, b->tnokey[1])) == 0);
2897 : }
2898 : /* var heaps must have sane sizes */
2899 9747477 : assert(b->tvheap == NULL || b->tvheap->free <= b->tvheap->size);
2900 :
2901 9747477 : if (!b->tkey && !b->tsorted && !b->trevsorted &&
2902 3627174 : !b->tnonil && !b->tnil) {
2903 : /* nothing more to prove */
2904 1480064 : MT_lock_unset(&b->theaplock);
2905 1479124 : return;
2906 : }
2907 :
2908 : /* only do a scan if the bat is not a view */
2909 8267413 : if (!isview1 && !isview2) {
2910 6004081 : const ValRecord *prop;
2911 6004081 : const void *maxval = NULL;
2912 6004081 : const void *minval = NULL;
2913 6004081 : const void *maxbound = NULL;
2914 6004081 : const void *minbound = NULL;
2915 6004081 : const bool notnull = BATgetprop_nolock(b, GDK_NOT_NULL) != NULL;
2916 6000178 : bool seenmax = false, seenmin = false;
2917 6000178 : bool seennil = false;
2918 :
2919 6000178 : if ((prop = BATgetprop_nolock(b, GDK_MAX_BOUND)) != NULL)
2920 12 : maxbound = VALptr(prop);
2921 5998165 : if ((prop = BATgetprop_nolock(b, GDK_MIN_BOUND)) != NULL)
2922 3262 : minbound = VALptr(prop);
2923 6000701 : if (b->tmaxpos != BUN_NONE) {
2924 1057060 : assert(b->tmaxpos < BATcount(b));
2925 1057060 : maxval = BUNtail(bi, b->tmaxpos);
2926 1056697 : assert(cmpf(maxval, nilp) != 0);
2927 : }
2928 5998335 : if (b->tminpos != BUN_NONE) {
2929 1007680 : assert(b->tminpos < BATcount(b));
2930 1007680 : minval = BUNtail(bi, b->tminpos);
2931 1007541 : assert(cmpf(minval, nilp) != 0);
2932 : }
2933 5996168 : if (ATOMstorage(b->ttype) == TYPE_msk) {
2934 : /* for now, don't do extra checks for bit mask */
2935 : ;
2936 5992105 : } else if (b->tsorted || b->trevsorted || !b->tkey) {
2937 : /* if sorted (either way), or we don't have to
2938 : * prove uniqueness, we can do a simple
2939 : * scan */
2940 : /* only call compare function if we have to */
2941 5972542 : bool cmpprv = b->tsorted | b->trevsorted | b->tkey;
2942 :
2943 6281915520 : BATloop(b, p, q) {
2944 6276106146 : valp = BUNtail(bi, p);
2945 6276101857 : bool isnil = cmpf(valp, nilp) == 0;
2946 6097714490 : assert(!isnil || !notnull);
2947 6097714490 : assert(!b->tnonil || !isnil);
2948 6097714490 : assert(b->ttype != TYPE_flt || !isinf(*(flt*)valp));
2949 6097714490 : assert(b->ttype != TYPE_dbl || !isinf(*(dbl*)valp));
2950 6097714490 : if (b->tascii)
2951 248859303 : assert_ascii(valp);
2952 6097715413 : if (minbound && !isnil) {
2953 30 : cmp = cmpf(minbound, valp);
2954 30 : assert(cmp <= 0);
2955 : }
2956 6097715413 : if (maxbound && !isnil) {
2957 30 : cmp = cmpf(maxbound, valp);
2958 30 : assert(cmp > 0);
2959 : }
2960 6097715413 : if (maxval && !isnil) {
2961 858477326 : cmp = cmpf(maxval, valp);
2962 858486941 : assert(cmp >= 0);
2963 858486941 : seenmax |= cmp == 0;
2964 : }
2965 6097725028 : if (minval && !isnil) {
2966 50846395 : cmp = cmpf(minval, valp);
2967 50845533 : assert(cmp <= 0);
2968 50845533 : seenmin |= cmp == 0;
2969 : }
2970 6097724166 : if (prev && cmpprv) {
2971 2943427953 : cmp = cmpf(prev, valp);
2972 3121806354 : assert(!b->tsorted || cmp <= 0);
2973 3121806354 : assert(!b->trevsorted || cmp >= 0);
2974 3121806354 : assert(!b->tkey || cmp != 0);
2975 : }
2976 6276102567 : seennil |= isnil;
2977 6276102567 : if (seennil && !cmpprv &&
2978 4614231 : maxval == NULL && minval == NULL &&
2979 159581 : minbound == NULL && maxbound == NULL) {
2980 : /* we've done all the checking
2981 : * we can do */
2982 : break;
2983 : }
2984 6275942978 : prev = valp;
2985 : }
2986 : } else { /* b->tkey && !b->tsorted && !b->trevsorted */
2987 : /* we need to check for uniqueness the hard
2988 : * way (i.e. using a hash table) */
2989 19563 : const char *nme = BBP_physical(b->batCacheid);
2990 19563 : Hash *hs = NULL;
2991 19563 : BUN mask;
2992 :
2993 19563 : if ((hs = GDKzalloc(sizeof(Hash))) == NULL) {
2994 0 : TRC_WARNING(BAT_, "Cannot allocate hash table\n");
2995 0 : goto abort_check;
2996 : }
2997 19564 : if (snprintf(hs->heaplink.filename, sizeof(hs->heaplink.filename), "%s.thshprpl%x", nme, (unsigned) MT_getpid()) >= (int) sizeof(hs->heaplink.filename) ||
2998 19561 : snprintf(hs->heapbckt.filename, sizeof(hs->heapbckt.filename), "%s.thshprpb%x", nme, (unsigned) MT_getpid()) >= (int) sizeof(hs->heapbckt.filename)) {
2999 : /* cannot happen, see comment in gdk.h
3000 : * about sizes near definition of
3001 : * BBPINIT */
3002 0 : GDKfree(hs);
3003 0 : TRC_CRITICAL(BAT_, "Heap filename is too large\n");
3004 0 : goto abort_check;
3005 : }
3006 19564 : if (ATOMsize(b->ttype) == 1)
3007 : mask = (BUN) 1 << 8;
3008 19558 : else if (ATOMsize(b->ttype) == 2)
3009 : mask = (BUN) 1 << 16;
3010 : else
3011 19558 : mask = HASHmask(b->batCount);
3012 19564 : hs->heapbckt.parentid = b->batCacheid;
3013 19564 : hs->heaplink.parentid = b->batCacheid;
3014 19564 : if ((hs->heaplink.farmid = BBPselectfarm(
3015 19564 : TRANSIENT, b->ttype, hashheap)) < 0 ||
3016 19564 : (hs->heapbckt.farmid = BBPselectfarm(
3017 39128 : TRANSIENT, b->ttype, hashheap)) < 0 ||
3018 19564 : HASHnew(hs, b->ttype, BATcount(b),
3019 : mask, BUN_NONE, false) != GDK_SUCCEED) {
3020 0 : GDKfree(hs);
3021 0 : TRC_WARNING(BAT_, "Cannot allocate hash table\n");
3022 0 : goto abort_check;
3023 : }
3024 136905356 : BATloop(b, p, q) {
3025 136885796 : BUN hb;
3026 136885796 : BUN prb;
3027 136885796 : valp = BUNtail(bi, p);
3028 136885796 : bool isnil = cmpf(valp, nilp) == 0;
3029 136886279 : assert(!isnil || !notnull);
3030 136886279 : assert(b->ttype != TYPE_flt || !isinf(*(flt*)valp));
3031 136886279 : assert(b->ttype != TYPE_dbl || !isinf(*(dbl*)valp));
3032 136886279 : if (b->tascii)
3033 10181 : assert_ascii(valp);
3034 136886279 : if (minbound && !isnil) {
3035 0 : cmp = cmpf(minbound, valp);
3036 0 : assert(cmp <= 0);
3037 : }
3038 136886279 : if (maxbound && !isnil) {
3039 0 : cmp = cmpf(maxbound, valp);
3040 0 : assert(cmp > 0);
3041 : }
3042 136886279 : if (maxval && !isnil) {
3043 6391 : cmp = cmpf(maxval, valp);
3044 6391 : assert(cmp >= 0);
3045 6391 : seenmax |= cmp == 0;
3046 : }
3047 136886279 : if (minval && !isnil) {
3048 6391 : cmp = cmpf(minval, valp);
3049 6391 : assert(cmp <= 0);
3050 6391 : seenmin |= cmp == 0;
3051 : }
3052 136886279 : prb = HASHprobe(hs, valp);
3053 136886864 : for (hb = HASHget(hs, prb);
3054 138998138 : hb != BUN_NONE;
3055 2111274 : hb = HASHgetlink(hs, hb))
3056 2125488 : if (cmpf(valp, BUNtail(bi, hb)) == 0)
3057 0 : assert(!b->tkey);
3058 136872650 : HASHputlink(hs, p, HASHget(hs, prb));
3059 136883453 : HASHput(hs, prb, p);
3060 136885792 : assert(!b->tnonil || !isnil);
3061 136885792 : seennil |= isnil;
3062 : }
3063 19560 : HEAPfree(&hs->heaplink, true);
3064 19564 : HEAPfree(&hs->heapbckt, true);
3065 19564 : GDKfree(hs);
3066 : }
3067 5992590 : abort_check:
3068 5992590 : GDKclrerr();
3069 5992591 : assert(maxval == NULL || seenmax);
3070 5992591 : assert(minval == NULL || seenmin);
3071 5992591 : assert(!b->tnil || seennil);
3072 : }
3073 8255923 : MT_lock_unset(&b->theaplock);
3074 : }
|