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 : static inline int
18 1173 : node_key( node *n )
19 : {
20 1173 : sql_base *b = n->data;
21 0 : return hash_key(b->name);
22 : }
23 :
24 : objlist *
25 396320 : ol_new(allocator *sa, destroy_fptr destroy, sql_store store)
26 : {
27 396320 : objlist *ol = SA_NEW(sa, objlist);
28 792640 : *ol = (objlist) {
29 396320 : .l = list_new(sa, (fdestroy)destroy),
30 396319 : .h = hash_new(sa, 16, (fkeyvalue)&node_key),
31 : .store = store
32 : };
33 396320 : return ol;
34 : }
35 :
36 : void
37 257452 : ol_destroy(objlist *ol, sql_store store)
38 : {
39 257452 : if (!ol->l->sa) {
40 257452 : hash_destroy(ol->h);
41 257452 : list_destroy2(ol->l, store);
42 257452 : _DELETE(ol);
43 : }
44 257452 : }
45 :
46 : int
47 820648 : ol_add(objlist *ol, sql_base *data)
48 : {
49 820648 : list *l = list_append(ol->l, data);
50 820648 : if (!l) {
51 0 : if (ol->l->destroy)
52 0 : ol->l->destroy(ol->store, data);
53 0 : return -1;
54 : }
55 820648 : node *n = l->t;
56 820648 : assert(n->data == data);
57 820648 : int sz = list_length(ol->l);
58 820648 : if (ol->h->size <= sz) {
59 7452 : hash_destroy(ol->h);
60 7452 : ol->h = hash_new(ol->l->sa, 4*sz, (fkeyvalue)&node_key);
61 7452 : if (ol->h == NULL)
62 : return -1;
63 198636 : for (node *n = ol->l->h; n; n = n->next) {
64 191184 : if (hash_add(ol->h, base_key(n->data), n) == NULL)
65 : /* No need to clean, ie expect a full transaction rollback */
66 : return -1;
67 : }
68 : } else {
69 813196 : if (hash_add(ol->h, base_key(data), n) == NULL)
70 : /* No need to clean, ie expect a full transaction rollback */
71 : return -1;
72 : }
73 : return 0;
74 : }
75 :
76 : void
77 1160 : ol_del(objlist *ol, sql_store store, node *n)
78 : {
79 2320 : hash_del(ol->h, node_key(n), n);
80 1160 : list_remove_node(ol->l, store, n);
81 1160 : }
82 :
83 : node *
84 13 : ol_rehash(objlist *ol, const char *oldname, node *n)
85 : {
86 26 : hash_del(ol->h, hash_key(oldname), n);
87 26 : if (hash_add(ol->h, node_key(n), n) == NULL)
88 0 : return NULL;
89 : return n;
90 : }
91 :
92 : node *
93 15656290 : ol_find_name(objlist *ol, const char *name)
94 : {
95 15656290 : int key = hash_key(name);
96 15656290 : sql_hash_e *he = ol->h->buckets[key&(ol->h->size-1)];
97 :
98 18216213 : for (; he; he = he->chain) {
99 14730440 : node *n = he->value;
100 14730440 : sql_base *b = n->data;
101 :
102 14730440 : if (b->name && strcmp(b->name, name) == 0)
103 12170517 : return n;
104 : }
105 : return NULL;
106 : }
107 :
108 36970 : node *ol_find_id(objlist *ol, sqlid id)
109 : {
110 : /* if needed we could add hash on id's as well */
111 1371082 : for (node *n = ol->l->h; n; n = n->next) {
112 1370984 : sql_base *b = n->data;
113 1370984 : if (b->id == id)
114 36872 : return n;
115 : }
116 : return NULL;
117 : }
|