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#include "monetdb_config.h"
10#include "bat_utils.h"
11#include "mal.h" /* for have_hge */
12
13void
14bat_destroy(BAT *b)
15{
16 if (b)
17 BBPunfix(b->batCacheid);
18}
19
20BAT *
21bat_new(int tt, BUN size, role_t role)
22{
23 return COLnew(0, tt, size, role);
24}
25
26void
27bat_clear(BAT *b)
28{
29 bat_set_access(b,BAT_WRITE);
30 BATclear(b,true);
31 bat_set_access(b,BAT_READ);
32}
33
34BAT *
35temp_descriptor(log_bid b)
36{
37 return BATdescriptor((bat) b);
38}
39
40BAT *
41quick_descriptor(log_bid b)
42{
43 return BBPquickdesc((bat) b, false);
44}
45
46void
47temp_destroy(log_bid b)
48{
49 if (b)
50 BBPrelease(b);
51}
52
53void
54temp_dup(log_bid b)
55{
56 if (b)
57 BBPretain(b);
58}
59
60log_bid
61temp_create(BAT *b)
62{
63 temp_dup(b->batCacheid);
64 return b->batCacheid;
65}
66
67log_bid
68temp_copy(log_bid b, int temp)
69{
70 /* make a copy of b, if temp is set only create a empty bat */
71 BAT *o = temp_descriptor(b);
72 BAT *c;
73 log_bid r;
74
75 if (!o)
76 return BID_NIL;
77 if (!temp) {
78 c = COLcopy(o, o->ttype, true, PERSISTENT);
79 if (!c)
80 return BID_NIL;
81 bat_set_access(c, BAT_READ);
82 BATcommit(c);
83 } else {
84 c = bat_new(o->ttype, COLSIZE, PERSISTENT);
85 if (!c)
86 return BID_NIL;
87 }
88 r = temp_create(c);
89 bat_destroy(c);
90 bat_destroy(o);
91 return r;
92}
93
94BUN
95append_inserted(BAT *b, BAT *i )
96{
97 BUN nr = 0, r;
98 BATiter ii = bat_iterator(i);
99
100 for (r = i->batInserted; r < BUNlast(i); r++) {
101 if (BUNappend(b, BUNtail(ii,r), true) != GDK_SUCCEED)
102 return BUN_NONE;
103 nr++;
104 }
105 return nr;
106}
107
108BAT *ebats[MAXATOMS] = { NULL };
109
110log_bid
111ebat2real(log_bid b, oid ibase)
112{
113 /* make a copy of b */
114 log_bid r = BID_NIL;
115 BAT *o = temp_descriptor(b);
116 if(o) {
117 BAT *c = COLcopy(o, ATOMtype(o->ttype), true, PERSISTENT);
118 if(c) {
119 BAThseqbase(c, ibase );
120 r = temp_create(c);
121 bat_destroy(c);
122 }
123 bat_destroy(o);
124 }
125 return r;
126}
127
128log_bid
129e_bat(int type)
130{
131 if (ebats[type] == NULL &&
132 (ebats[type] = bat_new(type, 0, TRANSIENT)) == NULL)
133 return BID_NIL;
134 return temp_create(ebats[type]);
135}
136
137BAT *
138e_BAT(int type)
139{
140 if (ebats[type] == NULL &&
141 (ebats[type] = bat_new(type, 0, TRANSIENT)) == NULL)
142 return NULL;
143 return temp_descriptor(ebats[type]->batCacheid);
144}
145
146log_bid
147ebat_copy(log_bid b, oid ibase, int temp)
148{
149 /* make a copy of b */
150 BAT *o = temp_descriptor(b);
151 BAT *c;
152 log_bid r;
153
154 if (!o)
155 return BID_NIL;
156 if (!ebats[o->ttype]) {
157 ebats[o->ttype] = bat_new(o->ttype, 0, TRANSIENT);
158 if (!ebats[o->ttype])
159 return BID_NIL;
160 }
161
162 if (!temp && BATcount(o)) {
163 c = COLcopy(o, o->ttype, true, PERSISTENT);
164 if (!c)
165 return BID_NIL;
166 BAThseqbase(c, ibase );
167 BATcommit(c);
168 bat_set_access(c, BAT_READ);
169 r = temp_create(c);
170 bat_destroy(c);
171 } else {
172 c = ebats[o->ttype];
173 if (!c)
174 return BID_NIL;
175 r = temp_create(c);
176 }
177 bat_destroy(o);
178 return r;
179}
180
181int
182bat_utils_init(void)
183{
184 int t;
185 char name[32];
186
187 for (t=1; t<GDKatomcnt; t++) {
188 if (t != TYPE_bat && BATatoms[t].name[0]
189#ifdef HAVE_HGE
190 && (have_hge || t != TYPE_hge)
191#endif
192 ) {
193 ebats[t] = bat_new(t, 0, TRANSIENT);
194 if(ebats[t] == NULL) {
195 for (t = t - 1; t >= 1; t--)
196 bat_destroy(ebats[t]);
197 return -1;
198 }
199 bat_set_access(ebats[t], BAT_READ);
200 /* give it a name for debugging purposes */
201 snprintf(name, sizeof(name), "sql_empty_%s_bat",
202 ATOMname(t));
203 BBPrename(ebats[t]->batCacheid, name);
204 }
205 }
206 return 0;
207}
208
209sql_table *
210tr_find_table( sql_trans *tr, sql_table *t)
211{
212 while (t && t->po && !t->base.allocated && tr) {
213 t = t->po;
214 tr = tr->parent;
215 }
216 if (t->data)
217 return t;
218 return NULL;
219}
220
221sql_column *
222tr_find_column( sql_trans *tr, sql_column *c)
223{
224 while (c && c->po && !c->base.allocated && tr) {
225 c = c->po;
226 tr = tr->parent;
227 }
228 if (c->data)
229 return c;
230 return NULL;
231}
232
233sql_idx *
234tr_find_idx( sql_trans *tr, sql_idx *i)
235{
236 while (i && i->po && !i->base.allocated && tr) {
237 i = i->po;
238 tr = tr->parent;
239 }
240 if (i->data)
241 return i;
242 return NULL;
243}
244
245