1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 *
6 * Copyright 1997 - July 2008 CWI, August 2008 - 2019 MonetDB B.V.
7 */
8
9/*
10 * @a M. L. Kersten, P. Boncz, N. Nes
11 * @* Delta management
12 * The basis for transaction management is to keep track of elements
13 * inserted, deleted, and replaced. This information is stored within
14 * the BAT structure using three delta markers. Inserted denotes the
15 * first added BUN since the last commit. Deleted points to the BUNs
16 * removed. The deletion list is terminated at @%first@, where space
17 * is reserved for swapping BUNs upon deletion. Initialization of the
18 * BAT is extended as follows:
19 */
20
21/*
22 * Impact on hashing and indexing. The hash structure is maintained
23 * for all elements to be deleted ?.
24 */
25#include "monetdb_config.h"
26#include "gdk.h"
27#include "gdk_private.h"
28
29/*
30 * batcommit really forgets the atoms guarded for an undo; we just
31 * need to free their heap space (only if necessary).
32 */
33void
34BATcommit(BAT *b)
35{
36 if (b == NULL)
37 return;
38 DELTADEBUG fprintf(stderr, "#BATcommit1 %s free %zu ins " BUNFMT " base %p\n",
39 BATgetId(b),
40 b->theap.free,
41 b->batInserted,
42 b->theap.base);
43 if (!BATdirty(b)) {
44 b->batDirtyflushed = false;
45 }
46 if (DELTAdirty(b)) {
47 b->batDirtydesc = true;
48 }
49 b->batInserted = BUNlast(b);
50 DELTADEBUG fprintf(stderr, "#BATcommit2 %s free %zu ins " BUNFMT " base %p\n",
51 BATgetId(b),
52 b->theap.free,
53 b->batInserted,
54 b->theap.base);
55}
56
57/*
58 * BATfakeCommit() flushed the delta info, but leaves the BAT marked
59 * clean.
60 */
61void
62BATfakeCommit(BAT *b)
63{
64 if (b) {
65 BATcommit(b);
66 b->batDirtydesc = b->theap.dirty = false;
67 if (b->tvheap)
68 b->tvheap->dirty = false;
69 }
70}
71
72/*
73 * The routine @%BATundo@ restores the BAT to the previous commit
74 * point. The inserted elements are removed from the accelerators,
75 * deleted from the heap. The guarded elements from uncommitted
76 * deletes are inserted into the accelerators.
77 */
78void
79BATundo(BAT *b)
80{
81 BATiter bi = bat_iterator(b);
82 BUN p, bunlast, bunfirst;
83
84 if (b == NULL)
85 return;
86 DELTADEBUG fprintf(stderr, "#BATundo %s \n", BATgetId(b));
87 if (b->batDirtyflushed) {
88 b->batDirtydesc = b->theap.dirty = true;
89 } else {
90 b->batDirtydesc = b->theap.dirty = false;
91 if (b->tvheap)
92 b->tvheap->dirty = false;
93 }
94 bunfirst = b->batInserted;
95 bunlast = BUNlast(b) - 1;
96 if (bunlast >= b->batInserted) {
97 BUN i = bunfirst;
98 int (*tunfix) (const void *) = BATatoms[b->ttype].atomUnfix;
99 void (*tatmdel) (Heap *, var_t *) = BATatoms[b->ttype].atomDel;
100
101 if (b->thash)
102 HASHdestroy(b);
103 if (tunfix || tatmdel) {
104 for (p = bunfirst; p <= bunlast; p++, i++) {
105 if (tunfix)
106 (*tunfix) (BUNtail(bi, p));
107 if (tatmdel)
108 (*tatmdel) (b->tvheap, (var_t *) BUNtloc(bi, p));
109 }
110 }
111 }
112 b->theap.free = tailsize(b, b->batInserted);
113
114 BATsetcount(b, b->batInserted);
115}
116