LCOV - code coverage report
Current view: top level - sql/storage - objectset.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 563 602 93.5 %
Date: 2024-04-25 20:03:45 Functions: 42 44 95.5 %

          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             : #include "gdk_atoms.h"
      18             : 
      19             : struct versionhead ;
      20             : 
      21             : #define active                                                  (0)
      22             : #define under_destruction                               (1<<1)
      23             : #define block_destruction                               (1<<2)
      24             : #define deleted                                                 (1<<3)
      25             : #define rollbacked                                              (1<<4)
      26             : 
      27             : /* This objectversion owns its associated versionhead.
      28             :  * When this objectversion gets destroyed,
      29             :  * the cleanup procedure should also destroy the associated (name|id) based versionhead.*/
      30             : #define name_based_versionhead_owner    (1<<5)
      31             : #define id_based_versionhead_owner              (1<<6)
      32             : 
      33             : typedef struct objectversion {
      34             :         ulng ts;
      35             :         ATOMIC_TYPE state;
      36             :         sql_base *b; // base of underlying sql object
      37             :         struct objectset* os;
      38             :         struct objectversion    *name_based_older;
      39             :         struct objectversion    *name_based_newer;
      40             :         struct versionhead              *name_based_head;
      41             : 
      42             :         struct objectversion    *id_based_older;
      43             :         struct objectversion    *id_based_newer;
      44             :         struct versionhead              *id_based_head;
      45             : } objectversion;
      46             : 
      47             : typedef struct versionhead  {
      48             :     struct versionhead * prev;
      49             :     struct versionhead * next;
      50             :     objectversion* ov;
      51             : } versionhead ;
      52             : 
      53             : typedef struct objectset {
      54             :         ATOMIC_TYPE refcnt;
      55             :         sql_allocator *sa;
      56             :         destroy_fptr destroy;
      57             :         MT_RWLock rw_lock;      /*readers-writer lock to protect the links (chains) in the objectversion chain.*/
      58             :         versionhead  *name_based_h;
      59             :         versionhead  *name_based_t;
      60             :         versionhead  *id_based_h;
      61             :         versionhead  *id_based_t;
      62             :         int name_based_cnt;
      63             :         int id_based_cnt;
      64             :         struct sql_hash *name_map;
      65             :         struct sql_hash *id_map;
      66             :         bool
      67             :                 temporary:1,
      68             :                 unique:1, /* names are unique */
      69             :                 concurrent:1,   /* concurrent inserts are allowed */
      70             :             nested:1;
      71             :         sql_store store;
      72             : } objectset;
      73             : 
      74             : static int
      75      324232 : os_id_key(versionhead  *n)
      76             : {
      77      250575 :         return (int) BATatoms[TYPE_int].atomHash(&n->ov->b->id);
      78             : }
      79             : 
      80             : static inline void
      81    23420112 : lock_reader(objectset* os)
      82             : {
      83    23420112 :         MT_rwlock_rdlock(&os->rw_lock);
      84     4222200 : }
      85             : 
      86             : static inline void
      87    23417335 : unlock_reader(objectset* os)
      88             : {
      89    23417335 :         MT_rwlock_rdunlock(&os->rw_lock);
      90     4252280 : }
      91             : 
      92             : static inline void
      93      592510 : lock_writer(objectset* os)
      94             : {
      95      592510 :         MT_rwlock_wrlock(&os->rw_lock);
      96             : }
      97             : 
      98             : static inline void
      99      592510 : unlock_writer(objectset* os)
     100             : {
     101      592510 :         MT_rwlock_wrunlock(&os->rw_lock);
     102          82 : }
     103             : 
     104    19528971 : static bte os_atmc_get_state(objectversion *ov) {
     105    19528971 :         bte state = (bte) ATOMIC_GET(&ov->state);
     106    19528971 :         return state;
     107             : }
     108             : 
     109       63411 : static void os_atmc_set_state(objectversion *ov, bte state) {
     110       63411 :         ATOMIC_SET(&ov->state, state);
     111           0 : }
     112             : 
     113             : static versionhead  *
     114      430228 : find_id(objectset *os, sqlid id)
     115             : {
     116      430228 :         if (os) {
     117      430228 :                 lock_reader(os);
     118      430228 :                 if (os->id_map) {
     119      397984 :                         int key = (int) BATatoms[TYPE_int].atomHash(&id);
     120      397984 :                         sql_hash_e *he = os->id_map->buckets[key&(os->id_map->size-1)];
     121             : 
     122     1398479 :                         for (; he; he = he->chain) {
     123     1165871 :                                 versionhead  *n = he->value;
     124             : 
     125     1165871 :                                 if (n && n->ov->b->id == id) {
     126      165376 :                                         unlock_reader(os);
     127      165376 :                                         return n;
     128             :                                 }
     129             :                         }
     130      232608 :                         unlock_reader(os);
     131      232608 :                         return NULL;
     132             :                 }
     133             : 
     134       97980 :                 for (versionhead  *n = os->id_based_h; n; n = n->next) {
     135       68241 :                         objectversion *ov = n->ov;
     136             : 
     137             :                         /* check if ids match */
     138       68241 :                         if (id == ov->b->id) {
     139        2505 :                                 unlock_reader(os);
     140        2505 :                                 return n;
     141             :                         }
     142             :                 }
     143       29739 :                 unlock_reader(os);
     144             :         }
     145             : 
     146             :         return NULL;
     147             : }
     148             : 
     149             : // TODO copy of static function from sql_list.c. Needs to be made external
     150             : static void
     151       28842 : hash_delete(sql_hash *h, void *data)
     152             : {
     153       28842 :         int key = h->key(data);
     154       28842 :         sql_hash_e *e, *p = h->buckets[key&(h->size-1)];
     155             : 
     156       28842 :         e = p;
     157       42016 :         for (;  p && p->value != data ; p = p->chain)
     158       13174 :                 e = p;
     159       28842 :         if (p && p->value == data) {
     160       28842 :                 if (p == e)
     161       24764 :                         h->buckets[key&(h->size-1)] = p->chain;
     162             :                 else
     163        4078 :                         e->chain = p->chain;
     164       28842 :                 if (!h->sa)
     165       28842 :                         _DELETE(p);
     166             :         }
     167       28842 :         h->entries--;
     168       28842 : }
     169             : 
     170             : static void
     171      522638 : node_destroy(objectset *os, sqlstore *store, versionhead  *n)
     172             : {
     173      522638 :         if (!os->sa)
     174      522638 :                 _DELETE(n);
     175      522638 :         (void)store;
     176      522638 : }
     177             : 
     178             : static versionhead  *
     179       15182 : os_remove_name_based_chain(objectset *os, objectversion* ov)
     180             : {
     181       15182 :         lock_writer(os);
     182       15182 :         versionhead  *n = ov->name_based_head;
     183       15182 :         versionhead  *p = os->name_based_h;
     184       15182 :         if (p != n)
     185     1136996 :                 while (p && p->next != n)
     186             :                         p = p->next;
     187       15182 :         assert(p==n||(p && p->next == n));
     188       15182 :         if (p == n) {
     189        1335 :                 os->name_based_h = n->next;
     190        1335 :                 if (os->name_based_h) // i.e. non-empty os
     191         810 :                         os->name_based_h->prev = NULL;
     192             :                 p = NULL;
     193       13847 :         } else if ( p != NULL)  {
     194       13847 :                 p->next = n->next;
     195       13847 :                 if (p->next) // node in the middle
     196        5186 :                         p->next->prev = p;
     197             :         }
     198       15182 :         if (n == os->name_based_t)
     199        9186 :                 os->name_based_t = p;
     200             : 
     201       15182 :         if (os->name_map && n)
     202       14140 :                 hash_delete(os->name_map, n);
     203             : 
     204       15182 :         os->name_based_cnt--;
     205       15182 :         unlock_writer(os);
     206             : 
     207       15182 :         bte state = os_atmc_get_state(ov);
     208       15182 :         state |= name_based_versionhead_owner;
     209       15182 :         os_atmc_set_state(ov, state);
     210       15182 :         return p;
     211             : }
     212             : 
     213             : static versionhead  *
     214       15248 : os_remove_id_based_chain(objectset *os, objectversion* ov)
     215             : {
     216       15248 :         lock_writer(os);
     217       15248 :         versionhead  *n = ov->id_based_head;
     218       15248 :         versionhead  *p = os->id_based_h;
     219             : 
     220       15248 :         if (p != n)
     221     1144605 :                 while (p && p->next != n)
     222             :                         p = p->next;
     223       15248 :         assert(p==n||(p && p->next == n));
     224       15248 :         if (p == n) {
     225        1335 :                 os->id_based_h = n->next;
     226        1335 :                 if (os->id_based_h) // i.e. non-empty os
     227         810 :                         os->id_based_h->prev = NULL;
     228             :                 p = NULL;
     229       13913 :         } else if ( p != NULL)  {
     230       13913 :                 p->next = n->next;
     231       13913 :                 if (p->next) // node in the middle
     232        5253 :                         p->next->prev = p;
     233             :         }
     234       15248 :         if (n == os->id_based_t)
     235        9185 :                 os->id_based_t = p;
     236             : 
     237       15248 :         if (os->id_map && n)
     238       14702 :                 hash_delete(os->id_map, n);
     239             : 
     240       15248 :         os->name_based_cnt--;
     241       15248 :         unlock_writer(os);
     242             : 
     243       15248 :         bte state = os_atmc_get_state(ov);
     244       15248 :         state |= id_based_versionhead_owner;
     245       15248 :         os_atmc_set_state(ov, state);
     246       15248 :         return p;
     247             : }
     248             : 
     249             : static versionhead  *
     250      523748 : node_create(sql_allocator *sa, objectversion *ov)
     251             : {
     252      523748 :         versionhead  *n = SA_NEW(sa, versionhead );
     253             : 
     254      523748 :         if (n == NULL)
     255             :                 return NULL;
     256      523748 :         *n = (versionhead ) {
     257             :                 .ov = ov,
     258             :         };
     259      523748 :         return n;
     260             : }
     261             : 
     262             : static inline int
     263      315752 : os_name_key(versionhead  *n)
     264             : {
     265      315752 :         return hash_key(n->ov->b->name);
     266             : }
     267             : 
     268             : static objectset *
     269      261841 : os_append_node_name(objectset *os, versionhead  *n)
     270             : {
     271      261841 :         lock_writer(os);
     272      261841 :         if ((!os->name_map || os->name_map->size*16 < os->name_based_cnt) && os->name_based_cnt > HASH_MIN_SIZE) {
     273        3882 :                 hash_destroy(os->name_map);
     274        3882 :                 os->name_map = hash_new(os->sa, os->name_based_cnt, (fkeyvalue)& os_name_key);
     275        3882 :                 if (os->name_map == NULL) {
     276           0 :                         unlock_writer(os);
     277           0 :                         return NULL;
     278             :                 }
     279             : 
     280       70178 :                 for (versionhead  *n = os->name_based_h; n; n = n->next ) {
     281       66296 :                         int key = os_name_key(n);
     282             : 
     283       66296 :                         if (hash_add(os->name_map, key, n) == NULL) {
     284           0 :                                 unlock_writer(os);
     285           0 :                                 return NULL;
     286             :                         }
     287             :                 }
     288             :         }
     289             : 
     290      261841 :         if (os->name_map) {
     291      235316 :                 int key = os->name_map->key(n);
     292             : 
     293      235316 :                 if (hash_add(os->name_map, key, n) == NULL) {
     294           0 :                         unlock_writer(os);
     295           0 :                         return NULL;
     296             :                 }
     297             :         }
     298             : 
     299      261841 :         if (os->name_based_t) {
     300      253606 :                 os->name_based_t->next = n;
     301             :         } else {
     302        8235 :                 os->name_based_h = n;
     303             :         }
     304      261841 :         n->prev = os->name_based_t; // aka the double linked list.
     305      261841 :         os->name_based_t = n;
     306      261841 :         os->name_based_cnt++;
     307      261841 :         unlock_writer(os);
     308      261841 :         return os;
     309             : }
     310             : 
     311             : static objectset *
     312      261841 : os_append_name(objectset *os, objectversion *ov)
     313             : {
     314      261841 :         versionhead  *n = node_create(os->sa, ov);
     315             : 
     316      261841 :         if (n == NULL)
     317             :                 return NULL;
     318             : 
     319      261841 :         ov->name_based_head = n;
     320      261841 :         if (!(os = os_append_node_name(os, n))){
     321           0 :                 _DELETE(n);
     322           0 :                 return NULL;
     323             :         }
     324             : 
     325             :         return os;
     326             : }
     327             : 
     328             : static void
     329        3973 : os_append_id_map(objectset *os)
     330             : {
     331        3973 :         if (os->id_map)
     332         442 :                 hash_destroy(os->id_map);
     333        3973 :         os->id_map = hash_new(os->sa, os->id_based_cnt, (fkeyvalue)&os_id_key);
     334        3973 :         if (os->id_map == NULL)
     335             :                 return ;
     336       77630 :         for (versionhead  *n = os->id_based_h; n; n = n->next ) {
     337       73657 :                 int key = os_id_key(n);
     338             : 
     339       73657 :                 if (hash_add(os->id_map, key, n) == NULL) {
     340           0 :                         hash_destroy(os->id_map);
     341           0 :                         os->id_map = NULL;
     342           0 :                         return ;
     343             :                 }
     344             :         }
     345             : }
     346             : 
     347             : static objectset *
     348      261907 : os_append_node_id(objectset *os, versionhead  *n)
     349             : {
     350      261907 :         lock_writer(os);
     351      261907 :         if ((!os->id_map || os->id_map->size*16 < os->id_based_cnt) && os->id_based_cnt > HASH_MIN_SIZE)
     352        3973 :                 os_append_id_map(os); /* on failure just fall back to slow method */
     353             : 
     354      261907 :         if (os->id_map) {
     355      235873 :                 int key = os->id_map->key(n);
     356      235873 :                 if (hash_add(os->id_map, key, n) == NULL) {
     357           0 :                         hash_destroy(os->id_map);
     358           0 :                         os->id_map = NULL;
     359             :                         /* fall back to slow search */
     360             :                 }
     361             :         }
     362             : 
     363      261907 :         if (os->id_based_t) {
     364      253672 :                 os->id_based_t->next = n;
     365             :         } else {
     366        8235 :                 os->id_based_h = n;
     367             :         }
     368      261907 :         n->prev = os->id_based_t; // aka the double linked list.
     369      261907 :         os->id_based_t = n;
     370      261907 :         os->id_based_cnt++;
     371      261907 :         unlock_writer(os);
     372      261907 :         return os;
     373             : }
     374             : 
     375             : static objectset *
     376      261907 : os_append_id(objectset *os, objectversion *ov)
     377             : {
     378      261907 :         versionhead  *n = node_create(os->sa, ov);
     379             : 
     380      261907 :         if (n == NULL)
     381             :                 return NULL;
     382      261907 :         ov->id_based_head = n;
     383      261907 :         if (!(os = os_append_node_id(os, n))){
     384           0 :                 _DELETE(n);
     385           0 :                 return NULL;
     386             :         }
     387             : 
     388             :         return os;
     389             : }
     390             : 
     391             : static versionhead * find_name(objectset *os, const char *name);
     392             : 
     393             : static void
     394      280447 : objectversion_destroy(sqlstore *store, objectset* os, objectversion *ov)
     395             : {
     396             : 
     397      280447 :         bte state = os_atmc_get_state(ov);
     398             : 
     399      280447 :         if (state & name_based_versionhead_owner) {
     400       15182 :                 node_destroy(ov->os, store, ov->name_based_head);
     401             :         }
     402             : 
     403      280447 :         if (state & id_based_versionhead_owner) {
     404       15248 :                 node_destroy(ov->os, store, ov->id_based_head);
     405             :         }
     406             : 
     407      280447 :         if (os->destroy && ov->b)
     408      256420 :                 os->destroy(store, ov->b);
     409             : 
     410      280447 :         if (os->temporary && (state & deleted || state & under_destruction || state & rollbacked))
     411         195 :                 os_destroy(os, store); // TODO transaction_layer_revamp: embed into refcounting subproject : reference is already dropped by os_cleanup
     412      280447 :         _DELETE(ov);
     413      280447 : }
     414             : 
     415             : static void
     416        6098 : _os_rollback(objectversion *ov, sqlstore *store)
     417             : {
     418        6098 :         assert(ov->ts >= TRANSACTION_ID_BASE);
     419             : 
     420        6098 :         bte state = os_atmc_get_state(ov);
     421        6098 :         if (state & rollbacked) {
     422             :                 return;
     423             :         }
     424             : 
     425        5596 :         state |= rollbacked;
     426        5596 :         os_atmc_set_state(ov, state);
     427             : 
     428        5596 :         bte state_older;
     429             : 
     430             :         /*
     431             :          * We have to use the readers-writer lock here,
     432             :          * since the pointer containing the adress of the older objectversion might be concurrently overwritten if the older itself hass just been put in the under_destruction state .
     433             :          */
     434        5596 :         lock_reader(ov->os);
     435        5596 :         objectversion* name_based_older = ov->name_based_older;
     436        5596 :         unlock_reader(ov->os);
     437             : 
     438        5596 :         if (name_based_older && !((state_older= os_atmc_get_state(name_based_older)) & rollbacked)) {
     439         745 :                 if (ov->ts != name_based_older->ts) {
     440             :                         // older is last committed state or belongs to parent transaction.
     441             :                         // In any case, we restore versionhead pointer to that.
     442             : 
     443         248 :                         ATOMIC_BASE_TYPE expected_deleted = deleted;
     444         248 :                         if (state_older == active || (state_older == deleted && ATOMIC_CAS(&name_based_older->state, &expected_deleted, block_destruction))) {
     445         248 :                                 ov->name_based_head->ov = name_based_older;
     446         248 :                                 name_based_older->name_based_newer=NULL;
     447         248 :                                 if (state_older != active && expected_deleted == deleted)
     448           0 :                                         os_atmc_set_state(name_based_older, deleted); //Restore the deleted older back to its deleted state.
     449             :                         }
     450             :                 }
     451             :                 else {
     452         497 :                         _os_rollback(name_based_older, store);
     453             :                 }
     454             :         }
     455        4851 :         else if (!name_based_older) {
     456             :                 // this is a terminal node. i.e. this objectversion does not have name based committed history
     457        4851 :                 if (ov->name_based_head) // The oposite can happen during an early conflict in os_add or os_del.
     458        4845 :                         os_remove_name_based_chain(ov->os, ov);
     459             :         }
     460             : 
     461             :         /*
     462             :          * We have to use the readers-writer lock here,
     463             :          * since the pointer containing the adress of the older objectversion might be concurrently overwritten if the older itself hass just been put in the under_destruction state .
     464             :          */
     465        5596 :         lock_reader(ov->os);
     466        5596 :         objectversion* id_based_older = ov->id_based_older;
     467        5596 :         unlock_reader(ov->os);
     468        5596 :         if (id_based_older && !((state_older= os_atmc_get_state(id_based_older)) & rollbacked)) {
     469         253 :                 if (ov->ts != id_based_older->ts) {
     470             :                         // older is last committed state or belongs to parent transaction.
     471             :                         // In any case, we restore versionhead pointer to that.
     472             : 
     473         248 :                         ATOMIC_BASE_TYPE expected_deleted = deleted;
     474         248 :                         if (state_older == active || (state_older == deleted && ATOMIC_CAS(&id_based_older->state, &expected_deleted, block_destruction))) {
     475         248 :                                 ov->id_based_head->ov = id_based_older;
     476         248 :                                 id_based_older->id_based_newer=NULL;
     477         248 :                                 if (state_older != active && expected_deleted == deleted)
     478           0 :                                         os_atmc_set_state(id_based_older, deleted); //Restore the deleted older back to its deleted state.
     479             :                         }
     480             :                 }
     481           5 :                 else if (id_based_older != name_based_older)
     482           5 :                         _os_rollback(id_based_older, store);
     483             :         }
     484        4889 :         else if (!id_based_older) {
     485             :                 // this is a terminal node. i.e. this objectversion does not have id based committed history
     486        4889 :                 os_remove_id_based_chain(ov->os, ov);
     487             :         }
     488             : 
     489        5596 :         if (ov->name_based_newer && !(os_atmc_get_state(ov->name_based_newer) & rollbacked)) {
     490           0 :                 _os_rollback(ov->name_based_newer, store);
     491             :         }
     492             : 
     493        5596 :         if (ov->id_based_newer && ov->id_based_newer != ov->name_based_newer && !(os_atmc_get_state(ov->id_based_newer) & rollbacked)) {
     494           0 :                 _os_rollback(ov->id_based_newer, store);
     495             :         }
     496             : }
     497             : 
     498             : static int
     499        5596 : os_rollback(objectversion *ov, sqlstore *store)
     500             : {
     501        5596 :         _os_rollback(ov, store);
     502             : 
     503        5596 :         return LOG_OK;
     504             : }
     505             : 
     506             : static void
     507       24027 : ov_destroy_obj_recursive(sqlstore* store, objectversion *ov)
     508             : {
     509       24027 :         if (ov->id_based_older && ov->id_based_older == ov->name_based_older) {
     510       13805 :                 ov_destroy_obj_recursive(store, ov->id_based_older);
     511             :         }
     512       24027 :         if (ov->os->destroy && ov->b) {
     513       24027 :                 ov->os->destroy(store, ov->b);
     514       24027 :                 ov->b = NULL;
     515             :         }
     516       24027 : }
     517             : 
     518             : static inline void
     519       10389 : try_to_mark_deleted_for_destruction(sqlstore* store, objectversion *ov)
     520             : {
     521       10389 :         ATOMIC_BASE_TYPE expected_deleted = deleted;
     522       10389 :         if (ATOMIC_CAS(&ov->state, &expected_deleted, under_destruction)) {
     523             : 
     524       10389 :                 if (!ov->name_based_newer || (os_atmc_get_state(ov->name_based_newer) & rollbacked)) {
     525       10337 :                         os_remove_name_based_chain(ov->os, ov);
     526             :                 }
     527             :                 else {
     528          52 :                         lock_writer(ov->os);
     529          52 :                         ov->name_based_newer->name_based_older = NULL;
     530          52 :                         unlock_writer(ov->os);
     531             :                 }
     532             : 
     533       10389 :                 if (!ov->id_based_newer || (os_atmc_get_state(ov->id_based_newer) & rollbacked)) {
     534       10359 :                         os_remove_id_based_chain(ov->os, ov);
     535             :                 }
     536             :                 else {
     537          30 :                         lock_writer(ov->os);
     538          30 :                         ov->id_based_newer->id_based_older = NULL;
     539          30 :                         unlock_writer(ov->os);
     540             :                 }
     541             : 
     542       10389 :                 ov->ts = store_get_timestamp(store)+1;
     543       10389 :                 if (!ov->os->nested)
     544       10222 :                         ov_destroy_obj_recursive(store, ov);
     545             :         }
     546       10389 : }
     547             : 
     548             : static void
     549       28196 : objectversion_destroy_recursive(sqlstore* store, objectversion *ov)
     550             : {
     551       28196 :         if (ov->id_based_older && ov->id_based_older == ov->name_based_older) {
     552       13972 :                 objectversion_destroy_recursive(store, ov->id_based_older);
     553             :         }
     554       28196 :         objectversion_destroy(store, ov->os, ov);
     555       28196 : }
     556             : 
     557             : static int
     558      307873 : os_cleanup(sqlstore* store, objectversion *ov, ulng oldest)
     559             : {
     560      307873 :         if (os_atmc_get_state(ov) & under_destruction) {
     561       14722 :                 if (ov->ts < oldest) {
     562             :                         // This one is ready to be freed
     563       10389 :                         objectversion_destroy_recursive(store, ov);
     564       10389 :                         return LOG_ERR;
     565             :                 }
     566             : 
     567             :                 // not yet old enough to be safely removed. Try later.
     568             :                 return LOG_OK;
     569             :         }
     570             : 
     571      293151 :         if (os_atmc_get_state(ov) & rollbacked) {
     572       17656 :                 if (ov->ts < oldest) {
     573             :                         // This one is ready to be freed
     574        5596 :                         if (ov->name_based_older && ov->name_based_older->name_based_newer == ov)
     575         497 :                                 ov->name_based_older->name_based_newer=NULL;
     576        5596 :                         if (ov->id_based_older && ov->id_based_older->id_based_newer == ov)
     577         459 :                                 ov->id_based_older->id_based_newer=NULL;
     578        5596 :                         objectversion_destroy(store, ov->os, ov);
     579        5596 :                         return LOG_ERR;
     580             :                 }
     581             : 
     582       12060 :                 if (ov->ts > TRANSACTION_ID_BASE) {
     583             :                         /* We mark it with the latest possible starttime and reinsert it into the cleanup list.
     584             :                          * This will cause a safe eventual destruction of this rollbacked ov.
     585             :                          */
     586        5596 :                         ov->ts = store_get_timestamp(store)+1;
     587             :                 }
     588             : 
     589             :                 // not yet old enough to be safely removed. Try later.
     590       12060 :                 return LOG_OK;
     591             :         }
     592             : 
     593      275495 :         if (os_atmc_get_state(ov) == deleted) {
     594       10478 :                 if (ov->ts <= oldest) {
     595             :                         // the oldest relevant state is deleted so lets try to mark it as destroyed
     596       10389 :                         try_to_mark_deleted_for_destruction(store, ov);
     597       10389 :                         return LOG_OK+1;
     598             :                 }
     599             : 
     600             :                 // Keep it inplace on the cleanup list, either because it is now marked for destruction or
     601             :                 // we want to retry marking it for destruction later.
     602             :                 return LOG_OK;
     603             :         }
     604             : 
     605      265017 :         assert(os_atmc_get_state(ov) != deleted && os_atmc_get_state(ov) != under_destruction && os_atmc_get_state(ov) != rollbacked);
     606      265017 :         if (ov->os->temporary) os_destroy(ov->os, store); // TODO transaction_layer_revamp: embed into refcounting subproject: (old) live versions should drop their reference to the os
     607             : 
     608      272986 :         while (ov->id_based_older && ov->id_based_older == ov->name_based_older && ov->ts >= oldest) {
     609             :                 ov = ov->id_based_older;
     610             :         }
     611             : 
     612      265017 :         if (ov->id_based_older && ov->id_based_older == ov->name_based_older) {
     613             :                 // Destroy everything older then the oldest possibly relevant objectversion.
     614        3835 :                 objectversion_destroy_recursive(store, ov->id_based_older);
     615        3835 :                 ov->id_based_older = NULL;
     616             :         }
     617             : 
     618             :         return LOG_ERR;
     619             : }
     620             : 
     621             : static int
     622      307880 : tc_gc_objectversion(sql_store store, sql_change *change, ulng oldest)
     623             : {
     624             : //      assert(!change->handled);
     625      307880 :         objectversion *ov = (objectversion*)change->data;
     626             : 
     627      307880 :         if (oldest >= TRANSACTION_ID_BASE)
     628             :                 return 0;
     629      307873 :         int res = os_cleanup( (sqlstore*) store, ov, oldest);
     630      307873 :         change->handled = (res)?true:false;
     631      307873 :         return res>=0?LOG_OK:LOG_ERR;
     632             : }
     633             : 
     634             : static int
     635      281009 : tc_commit_objectversion(sql_trans *tr, sql_change *change, ulng commit_ts, ulng oldest)
     636             : {
     637      281009 :         objectversion *ov = (objectversion*)change->data;
     638      281009 :         if (commit_ts) {
     639      275413 :                 assert(ov->ts == tr->tid);
     640      275413 :                 ov->ts = commit_ts;
     641      275413 :                 change->committed = commit_ts < TRANSACTION_ID_BASE ? true: false;
     642      275413 :                 (void)oldest;
     643      275413 :                 if (!tr->parent)
     644      275406 :                         change->obj->new = 0;
     645             :         }
     646             :         else {
     647        5596 :                 os_rollback(ov, tr->store);
     648             :         }
     649             : 
     650      281009 :         return LOG_OK;
     651             : }
     652             : 
     653             : objectset *
     654       60235 : os_new(sql_allocator *sa, destroy_fptr destroy, bool temporary, bool unique, bool concurrent, bool nested, sql_store store)
     655             : {
     656       60235 :         assert(!sa);
     657       60235 :         objectset *os = SA_NEW(sa, objectset);
     658       60235 :         if (os) {
     659       60235 :                 *os = (objectset) {
     660             :                         .refcnt = ATOMIC_VAR_INIT(1),
     661             :                         .sa = sa,
     662             :                         .destroy = destroy,
     663             :                         .temporary = temporary,
     664             :                         .unique = unique,
     665             :                         .concurrent = concurrent,
     666             :                         .nested = nested,
     667             :                         .store = store
     668             :                 };
     669       60235 :                 os->destroy = destroy;
     670       60235 :                 MT_rwlock_init(&os->rw_lock, "sa_readers_lock");
     671             :         }
     672             : 
     673       60235 :         return os;
     674             : }
     675             : 
     676             : objectset *
     677        4563 : os_dup(objectset *os)
     678             : {
     679        4563 :         ATOMIC_INC(&os->refcnt);
     680        4563 :         return os;
     681             : }
     682             : 
     683             : void
     684       64740 : os_destroy(objectset *os, sql_store store)
     685             : {
     686       64740 :         if (ATOMIC_DEC(&os->refcnt) > 0)
     687             :                 return;
     688       60177 :         MT_rwlock_destroy(&os->rw_lock);
     689       60177 :         ATOMIC_DESTROY(&os->refcnt);
     690       60177 :         versionhead* n=os->id_based_h;
     691      306281 :         while(n) {
     692      246104 :                 objectversion *ov = n->ov;
     693      492759 :                 while(ov) {
     694      246655 :                         objectversion *older = ov->id_based_older;
     695      246655 :                         objectversion_destroy(store, os, ov);
     696      246655 :                         ov = older;
     697             :                 }
     698      246104 :                 versionhead* hn =n->next;
     699      246104 :                 node_destroy(os, store, n);
     700      246104 :                 n = hn;
     701             :         }
     702             : 
     703       60177 :         n=os->name_based_h;
     704      306281 :         while(n) {
     705      246104 :                 versionhead* hn =n->next;
     706      246104 :                 node_destroy(os, store, n);
     707      246104 :                 n = hn;
     708             :         }
     709             : 
     710       60177 :         if (os->id_map)
     711        3521 :                 hash_destroy(os->id_map);
     712             : 
     713       60177 :         if (os->name_map)
     714        3495 :                 hash_destroy(os->name_map);
     715             : 
     716       60177 :         if (!os->sa)
     717       60177 :                 _DELETE(os);
     718             : }
     719             : 
     720             : static versionhead  *
     721    16144063 : find_name(objectset *os, const char *name)
     722             : {
     723    16144063 :         lock_reader(os);
     724    16141409 :         if (os->name_map) {
     725    12562002 :                 int key = hash_key(name);
     726    12562002 :                 sql_hash_e *he = os->name_map->buckets[key&(os->name_map->size-1)];
     727             : 
     728    69326920 :                 for (; he; he = he->chain) {
     729    68964789 :                         versionhead  *n = he->value;
     730             : 
     731    68964789 :                         if (n && n->ov->b->name && strcmp(n->ov->b->name, name) == 0) {
     732    12199871 :                                 unlock_reader(os);
     733    12199871 :                                 return n;
     734             :                         }
     735             :                 }
     736      362131 :                 unlock_reader(os);
     737      362131 :                 return NULL;
     738             :         }
     739             : 
     740     4261916 :         for (versionhead  *n = os->name_based_h; n; n = n->next) {
     741     3967544 :                 objectversion *ov = n->ov;
     742             : 
     743             :                 /* check if names match */
     744     3967544 :                 if (name[0] == ov->b->name[0] && strcmp(name, ov->b->name) == 0) {
     745     3285035 :                         unlock_reader(os);
     746     3285035 :                         return n;
     747             :                 }
     748             :         }
     749             : 
     750      294372 :         unlock_reader(os);
     751      294372 :         return NULL;
     752             : }
     753             : 
     754             : static objectversion*
     755    16805808 : get_valid_object_name(sql_trans *tr, objectversion *ov)
     756             : {
     757    16805882 :         while(ov) {
     758    16805851 :                 if (ov->ts == tr->tid || (tr->parent && tr_version_of_parent(tr, ov->ts)) || ov->ts < tr->ts)
     759    16805550 :                         return ov;
     760             :                 else {
     761         300 :                         lock_reader(ov->os);
     762         166 :                         objectversion* name_based_older = ov->name_based_older;
     763         166 :                         unlock_reader(ov->os);
     764         166 :                         ov = name_based_older;
     765             :                 }
     766             :         }
     767             :         return ov;
     768             : }
     769             : 
     770             : static objectversion*
     771      560234 : get_valid_object_id(sql_trans *tr, objectversion *ov)
     772             : {
     773      560407 :         while(ov) {
     774      560287 :                 if (ov->ts == tr->tid || (tr->parent && tr_version_of_parent(tr, ov->ts))  || ov->ts < tr->ts)
     775      560114 :                         return ov;
     776             :                 else {
     777         173 :                         lock_reader(ov->os);
     778         173 :                         objectversion* id_based_older = ov->id_based_older;
     779         173 :                         unlock_reader(ov->os);
     780         173 :                         ov = id_based_older;
     781             :                 }
     782             :         }
     783             :         return ov;
     784             : }
     785             : 
     786             : static int
     787      270135 : os_add_name_based(objectset *os, struct sql_trans *tr, const char *name, objectversion *ov) {
     788      270135 :         versionhead  *name_based_node = NULL;
     789      270135 :         if (ov->id_based_older && strcmp(ov->id_based_older->b->name, name) == 0)
     790        8205 :                 name_based_node = ov->id_based_older->name_based_head;
     791      261930 :         else if (os->unique) // Previous name based objectversion is of a different id, so now we do have to perform an extensive look up
     792       81851 :                 name_based_node = find_name(os, name);
     793             :         // else names are not unique and each id based version head maps to its own name based version head.
     794             : 
     795       90056 :         if (name_based_node) {
     796        8294 :                 objectversion *co = name_based_node->ov;
     797        8294 :                 objectversion *oo = get_valid_object_name(tr, co);
     798        8294 :                 if (co != oo) { /* conflict ? */
     799           6 :                         TRC_WARNING(SQL_STORE, "%s" "if (co != oo) { /* conflict ? */", __func__);
     800           6 :                         return -3;
     801             :                 }
     802             : 
     803        8288 :                 assert(ov != oo); // Time loops are not allowed
     804             : 
     805        8288 :                 bte state = os_atmc_get_state(oo);
     806        8288 :                 if (state != active) {
     807             :                         // This can only happen if the parent oo was a comitted deleted at some point.
     808          96 :                         assert(state == deleted || state == under_destruction || state == block_destruction);
     809             :                         /* Since our parent oo is comitted deleted objectversion, we might have a conflict with
     810             :                         * another transaction that tries to clean up oo or also wants to add a new objectversion.
     811             :                         */
     812          96 :                         ATOMIC_BASE_TYPE expected_deleted = deleted;
     813          96 :                         if (!ATOMIC_CAS(&oo->state, &expected_deleted, block_destruction)) {
     814           0 :                                 TRC_WARNING(SQL_STORE, "%s: " "if (!ATOMIC_CAS(&oo->state, &expected_deleted, block_destruction)) { /*conflict with cleaner or write-write conflict*/ ", __func__);
     815           0 :                                 return -3; /*conflict with cleaner or write-write conflict*/
     816             :                         }
     817             :                 }
     818             : 
     819             :                 /* new object with same name within transaction, should have a delete in between */
     820        8192 :                 assert(!(state == active && oo->ts == ov->ts && !(os_atmc_get_state(ov) & deleted)));
     821             : 
     822        8288 :                 lock_writer(os);
     823        8288 :                 ov->name_based_head = oo->name_based_head;
     824        8288 :                 ov->name_based_older = oo;
     825             : 
     826        8288 :                 name_based_node->ov = ov;
     827        8288 :                 if (oo) {
     828        8288 :                         oo->name_based_newer = ov;
     829             :                         // if the parent was originally deleted, we restore it to that state.
     830        8288 :                         os_atmc_set_state(oo, state);
     831             :                 }
     832        8288 :                 unlock_writer(os);
     833        8288 :                 return 0;
     834             :         } else { /* new */
     835      261841 :                 if (os_append_name(os, ov) == NULL)
     836             :                         return -1; // MALLOC_FAIL
     837             :                 return 0;
     838             :         }
     839             : }
     840             : 
     841             : static int
     842      270145 : os_add_id_based(objectset *os, struct sql_trans *tr, sqlid id, objectversion *ov) {
     843      270145 :         versionhead  *id_based_node;
     844             : 
     845      270145 :         id_based_node = find_id(os, id);
     846             : 
     847      270145 :         if (id_based_node) {
     848        8238 :                 objectversion *co = id_based_node->ov;
     849        8238 :                 objectversion *oo = get_valid_object_id(tr, co);
     850        8238 :                 if (co != oo) { /* conflict ? */
     851          10 :                         TRC_WARNING(SQL_STORE, "%s" "if (co != oo) { /* conflict ? */", __func__);
     852          10 :                         return -3;
     853             :                 }
     854             : 
     855        8228 :                 assert(ov != oo); // Time loops are not allowed
     856             : 
     857        8228 :                 bte state = os_atmc_get_state(oo);
     858        8228 :                 if (state != active) {
     859             :                         // This can only happen if the parent oo was a comitted deleted at some point.
     860          36 :                         assert(state == deleted || state == under_destruction || state == block_destruction);
     861             :                         /* Since our parent oo is comitted deleted objectversion, we might have a conflict with
     862             :                         * another transaction that tries to clean up oo or also wants to add a new objectversion.
     863             :                         */
     864          36 :                         ATOMIC_BASE_TYPE expected_deleted = deleted;
     865          36 :                         if (!ATOMIC_CAS(&oo->state, &expected_deleted, block_destruction)) {
     866           0 :                                 TRC_WARNING(SQL_STORE, "%s" "if (!ATOMIC_CAS(&oo->state, &expected_deleted, block_destruction)) { /*conflict with cleaner or write-write conflict*/", __func__);
     867           0 :                                 return -3; /*conflict with cleaner or write-write conflict*/
     868             :                         }
     869             :                 }
     870             : 
     871        8228 :                 lock_writer(os);
     872        8228 :                 ov->id_based_head = oo->id_based_head;
     873        8228 :                 ov->id_based_older = oo;
     874             : 
     875        8228 :                 id_based_node->ov = ov;
     876        8228 :                 if (oo) {
     877        8228 :                         oo->id_based_newer = ov;
     878             :                         // if the parent was originally deleted, we restore it to that state.
     879        8228 :                         os_atmc_set_state(oo, state);
     880             :                 }
     881        8228 :                 unlock_writer(os);
     882        8228 :                 return 0;
     883             :         } else { /* new */
     884      261907 :                 if (os_append_id(os, ov) == NULL)
     885             :                         return -1; // MALLOC_FAIL
     886             : 
     887             :                 return 0;
     888             :         }
     889             : }
     890             : 
     891             : static int /*ok, error (name existed) and conflict (added before) */
     892      270146 : os_add_(objectset *os, struct sql_trans *tr, const char *name, sql_base *b)
     893             : {
     894      270146 :         int res = 0;
     895      270146 :         objectversion *ov = SA_NEW(os->sa, objectversion);
     896             : 
     897      270146 :         *ov = (objectversion) {
     898      270146 :                 .ts = tr->tid,
     899             :                 .b = b,
     900             :                 .os = os,
     901             :         };
     902             : 
     903      270146 :         if (!os->concurrent && os_has_changes(os, tr)) { /* for object sets without concurrent support, conflict if concurrent changes are there */
     904           1 :                 if (os->destroy)
     905           1 :                         os->destroy(os->store, ov->b);
     906           1 :                 _DELETE(ov);
     907           1 :                 TRC_WARNING(SQL_STORE, "%s" "if (!os->concurrent && os_has_changes(os, tr)) { /* for object sets without concurrent support, conflict if concurrent changes are there */", __func__);
     908           1 :                 return -3; /* conflict */
     909             :         }
     910             : 
     911      270145 :         if ((res = os_add_id_based(os, tr, b->id, ov))) {
     912          10 :                 if (os->destroy)
     913          10 :                         os->destroy(os->store, ov->b);
     914          10 :                 _DELETE(ov);
     915          10 :                 return res;
     916             :         }
     917             : 
     918      270135 :         if ((res = os_add_name_based(os, tr, name, ov))) {
     919           6 :                 trans_add(tr, b, ov, &tc_gc_objectversion, &tc_commit_objectversion, NULL);
     920           6 :                 return res;
     921             :         }
     922             : 
     923      270129 :         if (os->temporary) (void) os_dup(os); // TODO transaction_layer_revamp: embed into refcounting subproject
     924      270129 :         trans_add(tr, b, ov, &tc_gc_objectversion, &tc_commit_objectversion, NULL);
     925      270129 :         return res;
     926             : }
     927             : 
     928             : int
     929      270146 : os_add(objectset *os, struct sql_trans *tr, const char *name, sql_base *b)
     930             : {
     931      270146 :         store_lock(tr->store);
     932      270146 :         int res = os_add_(os, tr, name, b);
     933      270146 :         store_unlock(tr->store);
     934      270146 :         return res;
     935             : }
     936             : 
     937             : static int
     938       10867 : os_del_name_based(objectset *os, struct sql_trans *tr, const char *name, objectversion *ov) {
     939       10867 :         versionhead  *name_based_node = NULL;
     940       10867 :         if (ov->id_based_older && strcmp(ov->id_based_older->b->name, name) == 0)
     941       10867 :                 name_based_node = ov->id_based_older->name_based_head;
     942           0 :         else if (os->unique) // Previous name based objectversion is of a different id, so now we do have to perform an extensive look up
     943           0 :                 name_based_node = find_name(os, name);
     944             : 
     945       10867 :         if (name_based_node) {
     946       10867 :                 objectversion *co = name_based_node->ov;
     947       10867 :                 objectversion *oo = get_valid_object_name(tr, co);
     948       10867 :                 ov->name_based_head = oo->name_based_head;
     949       10867 :                 if (co != oo) { /* conflict ? */
     950           0 :                         TRC_WARNING(SQL_STORE, "%s: " "if (co != oo) { /* conflict ? */", __func__);
     951           0 :                         return -3;
     952             :                 }
     953       10867 :                 ov->name_based_older = oo;
     954             : 
     955       10867 :                 lock_writer(os);
     956       10867 :                 if (oo) {
     957       10867 :                         oo->name_based_newer = ov;
     958       10867 :                         assert(os_atmc_get_state(oo) == active);
     959             :                 }
     960       10867 :                 name_based_node->ov = ov;
     961       10867 :                 unlock_writer(os);
     962       10867 :                 return 0;
     963             :         } else {
     964             :                 /* missing */
     965           0 :                 return -1;
     966             :         }
     967             : }
     968             : 
     969             : static int
     970       10869 : os_del_id_based(objectset *os, struct sql_trans *tr, sqlid id, objectversion *ov) {
     971       10869 :         versionhead  *id_based_node;
     972       10869 :         if (ov->name_based_older && ov->name_based_older->b->id == id)
     973           0 :                 id_based_node = ov->name_based_older->id_based_head;
     974             :         else // Previous id based objectversion is of a different name, so now we do have to perform an extensive look up
     975       10869 :                 id_based_node = find_id(os, id);
     976             : 
     977       10869 :         if (id_based_node) {
     978       10869 :                 objectversion *co = id_based_node->ov;
     979       10869 :                 objectversion *oo = get_valid_object_id(tr, co);
     980       10869 :                 ov->id_based_head = oo->id_based_head;
     981       10869 :                 if (co != oo) { /* conflict ? */
     982           2 :                         TRC_WARNING(SQL_STORE, "%s" "if (co != oo) { /* conflict ? */", __func__);
     983           2 :                         return -3;
     984             :                 }
     985       10867 :                 ov->id_based_older = oo;
     986             : 
     987       10867 :                 lock_writer(os);
     988       10867 :                 if (oo) {
     989       10867 :                         oo->id_based_newer = ov;
     990       10867 :                         assert(os_atmc_get_state(oo) == active);
     991             :                 }
     992       10867 :                 id_based_node->ov = ov;
     993       10867 :                 unlock_writer(os);
     994       10867 :                 return 0;
     995             :         } else {
     996             :                 /* missing */
     997             :                 return -1;
     998             :         }
     999             : }
    1000             : 
    1001             : static int
    1002       10869 : os_del_(objectset *os, struct sql_trans *tr, const char *name, sql_base *b)
    1003             : {
    1004       10869 :         int res = 0;
    1005       10869 :         objectversion *ov = SA_NEW(os->sa, objectversion);
    1006             : 
    1007       10869 :         *ov = (objectversion) {
    1008       10869 :                 .ts = tr->tid,
    1009             :                 .b = b,
    1010             :                 .os = os,
    1011             :         };
    1012       10869 :         os_atmc_set_state(ov, deleted);
    1013             : 
    1014       10869 :         if ((res = os_del_id_based(os, tr, b->id, ov))) {
    1015           2 :                 if (os->destroy)
    1016           2 :                         os->destroy(os->store, ov->b);
    1017           2 :                 _DELETE(ov);
    1018           2 :                 return res;
    1019             :         }
    1020             : 
    1021       10867 :         if ((res = os_del_name_based(os, tr, name, ov))) {
    1022           0 :                 trans_add(tr, b, ov, &tc_gc_objectversion, &tc_commit_objectversion, NULL);
    1023           0 :                 return res;
    1024             :         }
    1025             : 
    1026       10867 :         if (os->temporary) (void) os_dup(os); // TODO transaction_layer_revamp: embed into refcounting subproject
    1027       10867 :         trans_add(tr, b, ov, &tc_gc_objectversion, &tc_commit_objectversion, NULL);
    1028       10867 :         return res;
    1029             : }
    1030             : 
    1031             : int
    1032       10869 : os_del(objectset *os, struct sql_trans *tr, const char *name, sql_base *b)
    1033             : {
    1034       10869 :         store_lock(tr->store);
    1035       10869 :         int res = os_del_(os, tr, name, b);
    1036       10869 :         store_unlock(tr->store);
    1037       10869 :         return res;
    1038             : }
    1039             : 
    1040             : int
    1041         364 : os_size(objectset *os, struct sql_trans *tr)
    1042             : {
    1043         364 :         int cnt = 0;
    1044         364 :         if (os) {
    1045         364 :                 lock_reader(os);
    1046         374 :                 for(versionhead  *n = os->name_based_h; n; n=n->next) {
    1047          10 :                         objectversion *ov = n->ov;
    1048          10 :                         if ((ov=get_valid_object_name(tr, ov)) && os_atmc_get_state(ov) == active)
    1049           7 :                                 cnt++;
    1050             :                 }
    1051         364 :                 unlock_reader(os);
    1052             :         }
    1053         364 :         return cnt;
    1054             : }
    1055             : 
    1056             : int
    1057           0 : os_empty(objectset *os, struct sql_trans *tr)
    1058             : {
    1059           0 :         return os_size(os, tr)==0;
    1060             : }
    1061             : 
    1062             : int
    1063           0 : os_remove(objectset *os, sql_trans *tr, const char *name)
    1064             : {
    1065           0 :         (void) os;
    1066           0 :         (void) tr;
    1067           0 :         (void) name;
    1068             :         // TODO remove entire versionhead  corresponding to this name.
    1069             : 
    1070             :         // TODO assert os->unique?s
    1071           0 :         return LOG_OK;
    1072             : }
    1073             : 
    1074             : sql_base *
    1075    16065029 : os_find_name(objectset *os, struct sql_trans *tr, const char *name)
    1076             : {
    1077    16065029 :         if (!os)
    1078             :                 return NULL;
    1079             : 
    1080    16065027 :         assert(os->unique);
    1081    16065027 :         versionhead  *n = find_name(os, name);
    1082             : 
    1083    16061463 :         if (n) {
    1084    15486723 :                  objectversion *ov = get_valid_object_name(tr, n->ov);
    1085    15484312 :                  if (ov && os_atmc_get_state(ov) == active)
    1086    15484157 :                          return ov->b;
    1087             :         }
    1088             :         return NULL;
    1089             : }
    1090             : 
    1091             : sql_base *
    1092      145680 : os_find_id(objectset *os, struct sql_trans *tr, sqlid id)
    1093             : {
    1094      145680 :         if (!os)
    1095             :                 return NULL;
    1096      145680 :         versionhead  *n = find_id(os, id);
    1097             : 
    1098      145680 :         if (n) {
    1099      145240 :                  objectversion *ov = get_valid_object_id(tr, n->ov);
    1100      145240 :                  if (ov && os_atmc_get_state(ov) == active)
    1101      145236 :                          return ov->b;
    1102             :         }
    1103             :         return NULL;
    1104             : }
    1105             : 
    1106             : void
    1107     2611596 : os_iterator(struct os_iter *oi, struct objectset *os, struct sql_trans *tr, const char *name /*optional*/)
    1108             : {
    1109     2611596 :         *oi = (struct os_iter) {
    1110             :                 .os = os,
    1111             :                 .tr = tr,
    1112             :                 .name = name,
    1113             :         };
    1114             : 
    1115     2611596 :         lock_reader(os);
    1116     2611606 :         oi->n =      os->name_based_h;
    1117     2611606 :         unlock_reader(os);
    1118     2611603 : }
    1119             : 
    1120             : sql_base *
    1121     4222196 : oi_next(struct os_iter *oi)
    1122             : {
    1123     4222196 :         sql_base *b = NULL;
    1124             : 
    1125     4222196 :         if (oi->name) {
    1126     3045900 :                 versionhead  *n = oi->n;
    1127             : 
    1128     3045900 :                 lock_reader(oi->os); /* intentionally outside of while loop */
    1129   479736086 :                 while (n && !b) {
    1130             : 
    1131   473644286 :                         if (n->ov->b->name && strcmp(n->ov->b->name, oi->name) == 0) {
    1132     1300771 :                                 objectversion *ov = n->ov;
    1133             : 
    1134     1300771 :                                 n = oi->n = n->next;
    1135     1300771 :                                 ov = get_valid_object_name(oi->tr, ov);
    1136     1300771 :                                 if (ov && os_atmc_get_state(ov) == active)
    1137     1300198 :                                         b = ov->b;
    1138             :                         } else {
    1139   472343515 :                                 n = oi->n = n->next;
    1140             :                         }
    1141             :                 }
    1142     3045900 :                 unlock_reader(oi->os);
    1143             :         } else {
    1144     1176296 :                 versionhead  *n = oi->n;
    1145             : 
    1146     1176296 :                 lock_reader(oi->os); /* intentionally outside of while loop */
    1147     2748483 :                 while (n && !b) {
    1148      395890 :                         objectversion *ov = n->ov;
    1149      395890 :                         n = oi->n = n->next;
    1150             : 
    1151      395890 :                         ov = get_valid_object_id(oi->tr, ov);
    1152      395887 :                         if (ov && os_atmc_get_state(ov) == active)
    1153      371478 :                                 b = ov->b;
    1154             :                 }
    1155     1176297 :                 unlock_reader(oi->os);
    1156             :         }
    1157     4222177 :         return b;
    1158             : }
    1159             : 
    1160             : bool
    1161        3534 : os_obj_intransaction(objectset *os, struct sql_trans *tr, sql_base *b)
    1162             : {
    1163        3534 :         versionhead  *n = find_id(os, b->id);
    1164             : 
    1165        3534 :         if (n) {
    1166        3534 :                 objectversion *ov = n->ov;
    1167             : 
    1168        3534 :                 if (ov && os_atmc_get_state(ov) == active && ov->ts == tr->tid)
    1169             :                         return true;
    1170             :         }
    1171             :         return false;
    1172             : }
    1173             : 
    1174             : /* return true if this object set has changes pending for an other transaction */
    1175             : bool
    1176      174846 : os_has_changes(objectset *os, struct sql_trans *tr)
    1177             : {
    1178      174846 :         versionhead  *n = os->id_based_t;
    1179             : 
    1180      174846 :         if (n) {
    1181      170531 :                 objectversion *ov = n->ov;
    1182             : 
    1183      170531 :                 if (ov && os_atmc_get_state(ov) == active && ov->ts != tr->tid && ov->ts > TRANSACTION_ID_BASE)
    1184             :                         return true;
    1185             :         }
    1186             :         return false;
    1187             : }

Generated by: LCOV version 1.14