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 : #include "monetdb_config.h"
14 : #include "sql_catalog.h"
15 : #include "sql_storage.h"
16 :
17 : #include "gdk_atoms.h"
18 :
19 : static inline int
20 1172 : node_key( node *n )
21 : {
22 1172 : sql_base *b = n->data;
23 0 : return hash_key(b->name);
24 : }
25 :
26 : objlist *
27 377732 : ol_new(sql_allocator *sa, destroy_fptr destroy, sql_store store)
28 : {
29 377732 : objlist *ol = SA_NEW(sa, objlist);
30 755464 : *ol = (objlist) {
31 377732 : .l = list_new(sa, (fdestroy)destroy),
32 377732 : .h = hash_new(sa, 16, (fkeyvalue)&node_key),
33 : .store = store
34 : };
35 377732 : return ol;
36 : }
37 :
38 : void
39 243064 : ol_destroy(objlist *ol, sql_store store)
40 : {
41 243064 : if (!ol->l->sa) {
42 243064 : hash_destroy(ol->h);
43 243064 : list_destroy2(ol->l, store);
44 243064 : _DELETE(ol);
45 : }
46 243064 : }
47 :
48 : int
49 782493 : ol_add(objlist *ol, sql_base *data)
50 : {
51 782493 : list *l = list_append(ol->l, data);
52 782492 : if (!l) {
53 0 : if (ol->l->destroy)
54 0 : ol->l->destroy(ol->store, data);
55 0 : return -1;
56 : }
57 782492 : node *n = l->t;
58 782492 : assert(n->data == data);
59 782492 : int sz = list_length(ol->l);
60 782492 : if (ol->h->size <= sz) {
61 6651 : hash_destroy(ol->h);
62 6651 : ol->h = hash_new(ol->l->sa, 4*sz, (fkeyvalue)&node_key);
63 6651 : if (ol->h == NULL)
64 : return -1;
65 184491 : for (node *n = ol->l->h; n; n = n->next) {
66 177840 : if (hash_add(ol->h, base_key(n->data), n) == NULL)
67 : /* No need to clean, ie expect a full transaction rollback */
68 : return -1;
69 : }
70 : } else {
71 775841 : if (hash_add(ol->h, base_key(data), n) == NULL)
72 : /* No need to clean, ie expect a full transaction rollback */
73 : return -1;
74 : }
75 : return 0;
76 : }
77 :
78 : void
79 1159 : ol_del(objlist *ol, sql_store store, node *n)
80 : {
81 2318 : hash_del(ol->h, node_key(n), n);
82 1159 : list_remove_node(ol->l, store, n);
83 1159 : }
84 :
85 : node *
86 13 : ol_rehash(objlist *ol, const char *oldname, node *n)
87 : {
88 26 : hash_del(ol->h, hash_key(oldname), n);
89 26 : if (hash_add(ol->h, node_key(n), n) == NULL)
90 0 : return NULL;
91 : return n;
92 : }
93 :
94 : node *
95 14239527 : ol_find_name(objlist *ol, const char *name)
96 : {
97 14239527 : int key = hash_key(name);
98 14239527 : sql_hash_e *he = ol->h->buckets[key&(ol->h->size-1)];
99 :
100 16605930 : for (; he; he = he->chain) {
101 13089020 : node *n = he->value;
102 13089020 : sql_base *b = n->data;
103 :
104 13089020 : if (b->name && strcmp(b->name, name) == 0)
105 10722617 : return n;
106 : }
107 : return NULL;
108 : }
109 :
110 36886 : node *ol_find_id(objlist *ol, sqlid id)
111 : {
112 : /* if needed we could add hash on id's as well */
113 1370948 : for (node *n = ol->l->h; n; n = n->next) {
114 1370850 : sql_base *b = n->data;
115 1370850 : if (b->id == id)
116 36788 : return n;
117 : }
118 : return NULL;
119 : }
|