LCOV - code coverage report
Current view: top level - sql/storage - store_sequence.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 132 158 83.5 %
Date: 2024-04-26 00:35:57 Functions: 13 13 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 "store_sequence.h"
      15             : #include "sql_storage.h"
      16             : 
      17             : void
      18        1804 : sequences_lock(sql_store Store)
      19             : {
      20        1804 :         sqlstore *store = Store;
      21        1804 :         MT_lock_set(&store->column_locks[NR_COLUMN_LOCKS-1]);
      22        1804 : }
      23             : 
      24             : void
      25        1804 : sequences_unlock(sql_store Store)
      26             : {
      27        1804 :         sqlstore *store = Store;
      28        1804 :         MT_lock_unset(&store->column_locks[NR_COLUMN_LOCKS-1]);
      29        1804 : }
      30             : 
      31             : typedef struct store_sequence {
      32             :         sqlid seqid;
      33             :         lng cur;
      34             :         bool intrans;
      35             : } store_sequence;
      36             : 
      37             : void
      38         760 : log_store_sequence(sql_store Store, void *s)
      39             : {
      40         760 :         sqlstore *store = Store;
      41         760 :         store_sequence *seq = s;
      42         760 :         store->logger_api.log_tsequence(store, seq->seqid,  seq->cur);
      43         760 :         seq->intrans = false;
      44         760 : }
      45             : 
      46             : int
      47         172 : seq_hash(void *s)
      48             : {
      49         172 :         store_sequence *seq = s;
      50         172 :         return seq->seqid;
      51             : }
      52             : 
      53             : static void
      54         172 : sequence_destroy( void *dummy, store_sequence *s )
      55             : {
      56         172 :         (void)dummy;
      57         172 :         _DELETE(s);
      58         172 : }
      59             : 
      60             : void
      61         340 : seq_hash_destroy( sql_hash *h )
      62             : {
      63         340 :         if (h == NULL || h->sa)
      64             :                 return ;
      65       11187 :         for (int i = 0; i < h->size; i++) {
      66       10848 :                 sql_hash_e *e = h->buckets[i];
      67             : 
      68       11020 :                 while (e) {
      69         172 :                         sql_hash_e *next = e->chain;
      70             : 
      71         172 :                         sequence_destroy(NULL, e->value);
      72         172 :                         _DELETE(e);
      73         172 :                         e = next;
      74             :                 }
      75             :         }
      76         339 :         _DELETE(h->buckets);
      77         339 :         _DELETE(h);
      78             : }
      79             : 
      80             : static store_sequence *
      81        1179 : sequence_lookup( sql_hash *h, sqlid id)
      82             : {
      83        1179 :         sql_hash_e *e = h->buckets[id & (h->size-1)];
      84        1186 :         while(e) {
      85         976 :                 sql_hash_e *next = e->chain;
      86         976 :                 store_sequence *s = e->value;
      87             : 
      88         976 :                 if (s->seqid == id)
      89         969 :                         return s;
      90             :                 e = next;
      91             :         }
      92             :         return NULL;
      93             : }
      94             : 
      95             : /* lock is held */
      96             : static store_sequence *
      97        1064 : update_sequence(sqlstore *store, store_sequence *s)
      98             : {
      99        1064 :         if (!s->intrans && !list_append(store->seqchanges, s))
     100             :                 return NULL;
     101        1064 :         s->intrans = true;
     102        1064 :         return s;
     103             : }
     104             : 
     105             : /* lock is held */
     106             : static store_sequence *
     107         172 : sequence_create(sqlstore *store, sql_sequence *seq )
     108             : {
     109         172 :         lng val = 0;
     110         172 :         store_sequence *s = NULL;
     111         172 :         s = MNEW(store_sequence);
     112         172 :         if(!s)
     113             :                 return NULL;
     114             : 
     115         172 :         *s = (store_sequence) {
     116         172 :                 .seqid = seq->base.id,
     117         172 :                 .cur = seq->start,
     118             :         };
     119             : 
     120         172 :         if (!isNew(seq) && store->logger_api.get_sequence(store, seq->base.id, &val ))
     121          13 :                 s->cur = val;
     122         172 :         if (!hash_add(store->sequences, seq_hash(s), s)) {
     123           0 :                 _DELETE(s);
     124           0 :                 return NULL;
     125             :         }
     126             :         return s;
     127             : }
     128             : 
     129             : int
     130          47 : seq_restart(sql_store Store, sql_sequence *seq, lng start)
     131             : {
     132          47 :         store_sequence *s;
     133          47 :         sqlstore *store = Store;
     134             : 
     135          47 :         assert(!is_lng_nil(start));
     136          47 :         sequences_lock(store);
     137          47 :         s = sequence_lookup(store->sequences, seq->base.id);
     138             : 
     139          47 :         if (!s) {
     140          38 :                 lng val = 0;
     141             : 
     142          38 :                 if (isNew(seq) || !store->logger_api.get_sequence(store, seq->base.id, &val )) {
     143          38 :                         sequences_unlock(store);
     144          38 :                         return 1;
     145             :                 } else {
     146           0 :                         s = sequence_create(store, seq);
     147           0 :                         if (!s) {
     148           0 :                                 sequences_unlock(store);
     149           0 :                                 return 0;
     150             :                         }
     151             :                 }
     152             :         }
     153           9 :         lng ocur = s->cur;
     154           9 :         s->cur = start;
     155           9 :         if (!update_sequence(store, s)) {
     156           0 :                 s->cur = ocur;
     157           0 :                 sequences_unlock(store);
     158           0 :                 return 0;
     159             :         }
     160           9 :         sequences_unlock(store);
     161           9 :         return 1;
     162             : }
     163             : 
     164             : int
     165        1056 : seqbulk_next_value(sql_store Store, sql_sequence *seq, lng cnt, lng* dest)
     166             : {
     167        1056 :         store_sequence *s;
     168        1056 :         sqlstore *store = Store;
     169             : 
     170             :         // either dest is an array of size cnt or dest is a normal pointer and cnt == 1.
     171             : 
     172        1056 :         assert(dest);
     173             : 
     174        1056 :         sequences_lock(store);
     175        1056 :         s = sequence_lookup(store->sequences, seq->base.id);
     176        1056 :         if (!s) {
     177         133 :                 s = sequence_create(store, seq);
     178         133 :                 if (!s) {
     179           0 :                         sequences_unlock(store);
     180           0 :                         return 0;
     181             :                 }
     182             :         }
     183             : 
     184        1056 :         lng min = seq->minvalue;
     185        1056 :         lng max = seq->maxvalue;
     186        1056 :         lng cur = s->cur;
     187             : 
     188        1056 :         if (!seq->cycle) {
     189        1038 :                 if ((seq->increment > 0 && s->cur > max) ||
     190          16 :                     (seq->increment < 0 && s->cur < min)) {
     191           1 :                         sequences_unlock(store);
     192           1 :                         return 0;
     193             :                 }
     194             :         }
     195        1055 :         bool store_unlocked = false;
     196        1055 :         if (seq->increment > 0) {
     197        1038 :                 lng inc = seq->increment; // new value = old value + inc;
     198             : 
     199        1038 :                 if (0 < cnt && !seq->cycle && !(max > 0 && s->cur < 0)) {
     200        1022 :                         if ((max - s->cur) >= ((cnt-1) * inc)) {
     201        1022 :                                 lng ocur = s->cur;
     202        1022 :                                 s->cur += inc * cnt;
     203             : 
     204        1022 :                                 if (!update_sequence(store, s)) {
     205           0 :                                         s->cur = ocur;
     206           0 :                                         sequences_unlock(store);
     207           0 :                                         return 0;
     208             :                                 }
     209        1022 :                                 sequences_unlock(store);
     210        1022 :                                 store_unlocked = true;
     211             :                         } else {
     212           0 :                                 sequences_unlock(store);
     213           0 :                                 return 0;
     214             :                         }
     215             :                 }
     216     2031421 :                 for(lng i = 0; i < cnt; i++) {
     217     2030383 :                         dest[i] = cur;
     218     2030383 :                         if ((GDK_lng_max - inc < cur) || ((cur += inc) > max)) {
     219             :                                 // overflow
     220           7 :                                 cur = (seq->cycle)?min:lng_nil;
     221             :                         }
     222             :                 }
     223             :         } else { // seq->increment < 0
     224          17 :                 lng inc = -seq->increment; // new value = old value - inc;
     225             : 
     226          17 :                 if (0 < cnt && !seq->cycle && !(min < 0 && s->cur > 0)) {
     227          15 :                         if ((s->cur - min) >= ((cnt-1) * inc)) {
     228          15 :                                 lng ocur = s->cur;
     229          15 :                                 s->cur -= inc * cnt;
     230             : 
     231          15 :                                 if (!update_sequence(store, s)) {
     232           0 :                                         s->cur = ocur;
     233           0 :                                         sequences_unlock(store);
     234           0 :                                         return 0;
     235             :                                 }
     236          15 :                                 sequences_unlock(store);
     237          15 :                                 store_unlocked = true;
     238             :                         } else {
     239           0 :                                 sequences_unlock(store);
     240           0 :                                 return 0;
     241             :                         }
     242             :                 }
     243          70 :                 for(lng i = 0; i < cnt; i++) {
     244          53 :                         dest[i] = cur;
     245          53 :                         if ((-GDK_lng_max + inc > cur) || ((cur -= inc)  < min)) {
     246             :                                 // underflow
     247           3 :                                 cur = (seq->cycle)?max:lng_nil;
     248             :                         }
     249             :                 }
     250             :         }
     251             : 
     252        1055 :         if (!store_unlocked) {
     253          18 :                 lng ocur = s->cur;
     254          18 :                 s->cur = cur;
     255             : 
     256          18 :                 if (!update_sequence(store, s)) {
     257           0 :                         s->cur = ocur;
     258           0 :                         sequences_unlock(store);
     259           0 :                         return 0;
     260             :                 }
     261          18 :                 sequences_unlock(store);
     262             :         }
     263             :         return 1;
     264             : }
     265             : 
     266             : int
     267         968 : seq_next_value(sql_store store, sql_sequence *seq, lng *val)
     268             : {
     269         968 :         return seqbulk_next_value(store, seq, 1, val);
     270             : }
     271             : 
     272             : int
     273          76 : seq_get_value(sql_store Store, sql_sequence *seq, lng *val)
     274             : {
     275          76 :         store_sequence *s;
     276          76 :         sqlstore *store = Store;
     277             : 
     278          76 :         *val = 0;
     279          76 :         sequences_lock(store);
     280          76 :         s = sequence_lookup(store->sequences, seq->base.id);
     281          76 :         if (!s) {
     282          39 :                 s = sequence_create(store, seq);
     283          39 :                 if (!s) {
     284           0 :                         sequences_unlock(store);
     285           0 :                         return 0;
     286             :                 }
     287             :         }
     288          76 :         *val = s->cur;
     289          76 :         sequences_unlock(store);
     290          76 :         return 1;
     291             : }

Generated by: LCOV version 1.14