LCOV - code coverage report
Current view: top level - sql/storage - objlist.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 46 52 88.5 %
Date: 2021-10-13 02:24:04 Functions: 7 8 87.5 %

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

Generated by: LCOV version 1.14