LCOV - code coverage report
Current view: top level - sql/common - sql_keyword.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 50 50 100.0 %
Date: 2024-04-25 20:03:45 Functions: 6 6 100.0 %

          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_mem.h"
      15             : #include "sql_string.h"
      16             : #include "sql_keyword.h"
      17             : 
      18             : #define HASH_SIZE 32768
      19             : #define HASH_MASK (HASH_SIZE-1)
      20             : 
      21             : static int keywords_init_done = 0;
      22             : static keyword *keywords[HASH_SIZE];
      23             : 
      24             : static int
      25    13315921 : keyword_key(char *k, int *l)
      26             : {
      27    13315921 :         char *s = k;
      28    13315921 :         unsigned int h = 1;
      29             : 
      30    79913763 :         while (*k) {
      31    66597842 :                 h <<= 5;
      32    66597842 :                 h += (*k - 'a');
      33    66597842 :                 k++;
      34             :         }
      35    13315921 :         *l = (int) (k - s);
      36    13315921 :         h <<= 4;
      37    13315921 :         h += *l;
      38    13315921 :         return (int) ((h & 0x80000000) ? ~h + 1 : h);
      39             : }
      40             : 
      41             : int
      42      148144 : keywords_insert(char *k, int token)
      43             : {
      44      148144 :         keyword *kw = MNEW(keyword);
      45      148144 :         if(kw) {
      46      148144 :                 int len = 0;
      47      148144 :                 int bucket = keyword_key(k = toLower(k), &len) & HASH_MASK;
      48             : #ifndef NDEBUG
      49             :                 /* no duplicate keywords */
      50      148144 :                 keyword *kw2;
      51      182080 :                 for (kw2 = keywords[bucket]; kw2; kw2 = kw2->next)
      52       33936 :                         assert(strcmp(kw2->keyword, k) != 0);
      53             : #endif
      54             : 
      55      148144 :                 *kw = (keyword) {
      56             :                         .keyword = k,
      57             :                         .len = len,
      58             :                         .token = token,
      59             :                         .next = keywords[bucket],
      60             :                 };
      61      148144 :                 keywords[bucket] = kw;
      62      148144 :                 return 0;
      63             :         } else {
      64             :                 return -1;
      65             :         }
      66             : }
      67             : 
      68             : keyword *
      69    13167776 : find_keyword(char *text)
      70             : {
      71    13167776 :         int len = 0;
      72    13167776 :         int bucket = keyword_key(mkLower(text), &len) & HASH_MASK;
      73    13167766 :         keyword *k = keywords[bucket];
      74             : 
      75    15934668 :         while (k) {
      76    10848142 :                 if (len == k->len && strcmp(k->keyword, text) == 0)
      77     8081240 :                         return k;
      78             : 
      79     2766902 :                 k = k->next;
      80             :         }
      81             :         return NULL;
      82             : }
      83             : 
      84             : int
      85       11019 : keyword_exists(char *text)
      86             : {
      87       11019 :         if (find_keyword(text)) {
      88        7358 :                 return 1;
      89             :         }
      90             :         return 0;
      91             : }
      92             : 
      93             : void
      94         336 : keyword_init(void)
      95             : {
      96         336 :         int i;
      97             : 
      98         336 :         if (keywords_init_done)
      99             :                 return;
     100         336 :         keywords_init_done = 1;
     101             : 
     102    11010384 :         for (i = 0; i < HASH_SIZE; i++)
     103    11010048 :                 keywords[i] = NULL;
     104             : }
     105             : 
     106             : void
     107         335 : keyword_exit(void)
     108             : {
     109         335 :         int i;
     110             : 
     111         335 :         if (keywords_init_done == 0)
     112             :                 return;
     113         335 :         keywords_init_done = 0;
     114             : 
     115    10977615 :         for (i = 0; i < HASH_SIZE; i++) {
     116    10977280 :                 keyword *k = keywords[i];
     117             : 
     118    10977280 :                 while (k) {
     119      147705 :                         keyword *l = k;
     120             : 
     121      147705 :                         k = k->next;
     122      147705 :                         _DELETE(l->keyword);
     123             : 
     124    11124985 :                         _DELETE(l);
     125             :                 }
     126             :         }
     127             : }

Generated by: LCOV version 1.14