LCOV - code coverage report
Current view: top level - sql/server - sql_qc.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 99 101 98.0 %
Date: 2024-04-25 21:43:30 Functions: 9 10 90.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             : 
      15             : #include "sql_qc.h"
      16             : #include "sql_mvc.h"
      17             : #include "sql_atom.h"
      18             : #include "rel_exp.h"
      19             : #include "gdk_time.h"
      20             : 
      21             : qc *
      22       38612 : qc_create(allocator *sa, int clientid, int seqnr)
      23             : {
      24       38612 :         qc *r = SA_NEW(sa, qc);
      25       38612 :         if (!r)
      26             :                 return NULL;
      27       38612 :         *r = (qc) {
      28             :                 .clientid = clientid,
      29             :                 .id = seqnr,
      30             :         };
      31       38612 :         return r;
      32             : }
      33             : 
      34             : static void
      35         335 : cq_delete(int clientid, cq *q)
      36             : {
      37         335 :         if (q->name)
      38         334 :                 backend_freecode(NULL, clientid, q->name);
      39             :         /* q, params and name are allocated using sa, ie need to be delete last */
      40         335 :         if (q->sa)
      41         335 :                 sa_destroy(q->sa);
      42         335 : }
      43             : 
      44             : static void
      45         165 : cq_restart(int clientid, cq *q)
      46             : {
      47         165 :         if (q->f->imp)
      48         165 :                 backend_freecode(NULL, clientid, q->f->imp);
      49         165 :         q->f->instantiated = false;
      50         165 : }
      51             : 
      52             : void
      53         207 : qc_delete(qc *cache, cq *q)
      54             : {
      55         207 :         cq *n, *p = NULL;
      56             : 
      57         207 :         if (cache) {
      58         208 :                 for (n = cache->q; n; p = n, n = n->next) {
      59         208 :                         if (n == q) {
      60         207 :                                 if (p) {
      61           1 :                                         p->next = q->next;
      62             :                                 } else {
      63         206 :                                         cache->q = q->next;
      64             :                                 }
      65         207 :                                 cq_delete(cache->clientid, q);
      66         207 :                                 cache->nr--;
      67         207 :                                 break;
      68             :                         }
      69             :                 }
      70             :         }
      71         207 : }
      72             : 
      73             : void
      74       16348 : qc_clean(qc *cache)
      75             : {
      76       16348 :         cq *n, *p = NULL;
      77             : 
      78       16348 :         if (cache) {
      79       16444 :                 for (n = cache->q; n; ) {
      80          96 :                         p = n->next;
      81          96 :                         cq_delete(cache->clientid, n);
      82          96 :                         cache->nr--;
      83          96 :                         n = p;
      84             :                 }
      85       16348 :                 cache->q = NULL;
      86             :         }
      87       16348 : }
      88             : 
      89             : void
      90       20396 : qc_restart(qc *cache)
      91             : {
      92       20396 :         if (cache) {
      93       20561 :                 for (cq *q = cache->q; q; q = q->next)
      94         165 :                         cq_restart(cache->clientid, q);
      95             :         }
      96       20396 : }
      97             : 
      98             : 
      99             : void
     100       38612 : qc_destroy(qc *cache)
     101             : {
     102       38612 :         cq *q, *n;
     103             : 
     104       38612 :         if (cache) {
     105       38644 :                 for (q = cache->q; q; q = n) {
     106          32 :                         n = q->next;
     107             : 
     108          32 :                         cq_delete(cache->clientid, q);
     109          32 :                         cache->nr--;
     110             :                 }
     111             :         }
     112       38612 : }
     113             : 
     114             : cq *
     115        6118 : qc_find(qc *cache, int id)
     116             : {
     117        6118 :         cq *q;
     118             : 
     119        6118 :         if (cache) {
     120        6119 :                 for (q = cache->q; q; q = q->next) {
     121        6108 :                         if (q->id == id) {
     122        6107 :                                 q->count++;
     123        6107 :                                 return q;
     124             :                         }
     125             :                 }
     126             :         }
     127             :         return NULL;
     128             : }
     129             : 
     130             : cq *
     131         335 : qc_insert(qc *cache, allocator *sa, sql_rel *r, symbol *s, list *params, mapi_query_t type, char *cmd, int no_mitosis)
     132             : {
     133         335 :         int namelen;
     134         335 :         sql_func *f = SA_NEW(sa, sql_func);
     135         335 :         cq *n = SA_ZNEW(sa, cq);
     136         335 :         list *res = NULL;
     137             : 
     138         335 :         if (!n || !f || !cache)
     139             :                 return NULL;
     140         335 :         n->id = cache->id++;
     141         335 :         cache->nr++;
     142             : 
     143         335 :         n->sa = sa;
     144         335 :         n->rel = r;
     145         335 :         n->s = s;
     146             : 
     147         335 :         n->next = cache->q;
     148         335 :         n->type = type;
     149         335 :         n->count = 1;
     150         335 :         namelen = 5 + ((n->id+7)>>3) + ((cache->clientid+7)>>3);
     151         335 :         char *name = sa_alloc(sa, namelen);
     152         335 :         n->no_mitosis = no_mitosis;
     153         335 :         n->created = timestamp_current();
     154         335 :         if (!name)
     155             :                 return NULL;
     156         335 :         (void) snprintf(name, namelen, "p%d_%d", n->id, cache->clientid);
     157         335 :         n->name = name;
     158         335 :         cache->q = n;
     159             : 
     160         335 :         if (r && is_project(r->op) && !list_empty(r->exps)) {
     161         286 :                 sql_arg *a;
     162         286 :                 node *m;
     163             : 
     164         286 :                 res = sa_list(sa);
     165        1800 :                 for(m = r->exps->h; m; m = m->next) {
     166        1514 :                         sql_exp *e = m->data;
     167        1514 :                         sql_subtype *t = exp_subtype(e);
     168             : 
     169        1514 :                         a = NULL;
     170        1514 :                         if (t)
     171        1509 :                                 a = sql_create_arg(sa, NULL, t, ARG_OUT);
     172        1514 :                         append(res, a);
     173             :                 }
     174             :         }
     175             : 
     176         335 :         *f = (sql_func) {
     177             :                 .mod = sql_private_module_name,
     178             :                 .type = F_PROC,
     179             :                 .lang = FUNC_LANG_SQL,
     180             :                 .query = cmd,
     181             :                 .ops = params,
     182             :                 .res = res,
     183             :         };
     184         335 :         base_init(sa, &f->base, 0, true, NULL);
     185         335 :         f->base.new = 1;
     186         335 :         f->base.id = n->id;
     187         335 :         f->base.name = f->imp = name;
     188         335 :         f->instantiated = true;
     189         335 :         n->f = f;
     190         335 :         return n;
     191             : }
     192             : 
     193             : int
     194           0 : qc_size(qc *cache)
     195             : {
     196           0 :         return cache ? cache->nr : 0;
     197             : }

Generated by: LCOV version 1.14