LCOV - code coverage report
Current view: top level - sql/storage - objectset.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 578 618 93.5 %
Date: 2024-11-13 19:37:10 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             :         allocator *sa;
      56             :         destroy_fptr destroy;
      57             :         MT_RWLock rw_lock;      /*readers-writer lock to protect the links (chains) in the objectversion chain.*/
      58             :         MT_Lock lock;   /* global objectset lock for os_add/del */
      59             :         versionhead  *name_based_h;
      60             :         versionhead  *name_based_t;
      61             :         versionhead  *id_based_h;
      62             :         versionhead  *id_based_t;
      63             :         int name_based_cnt;
      64             :         int id_based_cnt;
      65             :         struct sql_hash *name_map;
      66             :         struct sql_hash *id_map;
      67             :         bool
      68             :                 temporary:1,
      69             :                 unique:1, /* names are unique */
      70             :                 concurrent:1,   /* concurrent inserts are allowed */
      71             :             nested:1;
      72             :         sql_store store;
      73             : } objectset;
      74             : 
      75             : static int
      76      328458 : os_id_key(versionhead  *n)
      77             : {
      78      254016 :         return (int) BATatoms[TYPE_int].atomHash(&n->ov->b->id);
      79             : }
      80             : 
      81             : static inline void
      82    25422524 : lock_reader(objectset* os)
      83             : {
      84    25422524 :         MT_rwlock_rdlock(&os->rw_lock);
      85     1248131 : }
      86             : 
      87             : static inline void
      88    25503808 : unlock_reader(objectset* os)
      89             : {
      90    25503808 :         MT_rwlock_rdunlock(&os->rw_lock);
      91     4253916 : }
      92             : 
      93             : static inline void
      94      600165 : lock_writer(objectset* os)
      95             : {
      96      600165 :         MT_rwlock_wrlock(&os->rw_lock);
      97             : }
      98             : 
      99             : static inline void
     100      600165 : unlock_writer(objectset* os)
     101             : {
     102      600165 :         MT_rwlock_wrunlock(&os->rw_lock);
     103         251 : }
     104             : 
     105    22259730 : static bte os_atmc_get_state(objectversion *ov) {
     106    22259730 :         bte state = (bte) ATOMIC_GET(&ov->state);
     107    22259730 :         return state;
     108             : }
     109             : 
     110       65284 : static void os_atmc_set_state(objectversion *ov, bte state) {
     111       65284 :         ATOMIC_SET(&ov->state, state);
     112           0 : }
     113             : 
     114             : static versionhead  *
     115      443020 : find_id(objectset *os, sqlid id)
     116             : {
     117      443020 :         if (os) {
     118      443020 :                 lock_reader(os);
     119      443020 :                 if (os->id_map) {
     120      411017 :                         int key = (int) BATatoms[TYPE_int].atomHash(&id);
     121      411017 :                         sql_hash_e *he = os->id_map->buckets[key&(os->id_map->size-1)];
     122             : 
     123     1388723 :                         for (; he; he = he->chain) {
     124     1153168 :                                 versionhead  *n = he->value;
     125             : 
     126     1153168 :                                 if (n && n->ov->b->id == id) {
     127      175462 :                                         unlock_reader(os);
     128      175462 :                                         return n;
     129             :                                 }
     130             :                         }
     131      235555 :                         unlock_reader(os);
     132      235555 :                         return NULL;
     133             :                 }
     134             : 
     135       96853 :                 for (versionhead  *n = os->id_based_h; n; n = n->next) {
     136       67405 :                         objectversion *ov = n->ov;
     137             : 
     138             :                         /* check if ids match */
     139       67405 :                         if (id == ov->b->id) {
     140        2555 :                                 unlock_reader(os);
     141        2555 :                                 return n;
     142             :                         }
     143             :                 }
     144       29448 :                 unlock_reader(os);
     145             :         }
     146             : 
     147             :         return NULL;
     148             : }
     149             : 
     150             : // TODO copy of static function from sql_list.c. Needs to be made external
     151             : static void
     152       29899 : hash_delete(sql_hash *h, void *data)
     153             : {
     154       29899 :         int key = h->key(data);
     155       29899 :         sql_hash_e *e, *p = h->buckets[key&(h->size-1)];
     156             : 
     157       29899 :         e = p;
     158       46769 :         for (;  p && p->value != data ; p = p->chain)
     159       16870 :                 e = p;
     160       29899 :         if (p && p->value == data) {
     161       29899 :                 if (p == e)
     162       25403 :                         h->buckets[key&(h->size-1)] = p->chain;
     163             :                 else
     164        4496 :                         e->chain = p->chain;
     165       29899 :                 if (!h->sa)
     166       29899 :                         _DELETE(p);
     167             :         }
     168       29899 :         h->entries--;
     169       29899 : }
     170             : 
     171             : static void
     172      527622 : node_destroy(objectset *os, sqlstore *store, versionhead  *n)
     173             : {
     174      527622 :         if (!os->sa)
     175      527622 :                 _DELETE(n);
     176      527622 :         (void)store;
     177      527622 : }
     178             : 
     179             : static versionhead  *
     180       15663 : os_remove_name_based_chain(objectset *os, objectversion* ov)
     181             : {
     182       15663 :         lock_writer(os);
     183       15663 :         versionhead  *n = ov->name_based_head;
     184       15663 :         versionhead  *p = os->name_based_h;
     185       15663 :         if (p != n)
     186     1173946 :                 while (p && p->next != n)
     187             :                         p = p->next;
     188       15663 :         assert(p==n||(p && p->next == n));
     189       15663 :         if (p == n) {
     190        1348 :                 os->name_based_h = n->next;
     191        1348 :                 if (os->name_based_h) // i.e. non-empty os
     192         807 :                         os->name_based_h->prev = NULL;
     193             :                 p = NULL;
     194       14315 :         } else if ( p != NULL)  {
     195       14315 :                 p->next = n->next;
     196       14315 :                 if (p->next) // node in the middle
     197        5500 :                         p->next->prev = p;
     198             :         }
     199       15663 :         if (n == os->name_based_t)
     200        9356 :                 os->name_based_t = p;
     201             : 
     202       15663 :         if (os->name_map && n)
     203       14603 :                 hash_delete(os->name_map, n);
     204             : 
     205       15663 :         os->name_based_cnt--;
     206       15663 :         unlock_writer(os);
     207             : 
     208       15663 :         bte state = os_atmc_get_state(ov);
     209       15663 :         state |= name_based_versionhead_owner;
     210       15663 :         os_atmc_set_state(ov, state);
     211       15663 :         return p;
     212             : }
     213             : 
     214             : static versionhead  *
     215       15855 : os_remove_id_based_chain(objectset *os, objectversion* ov)
     216             : {
     217       15855 :         lock_writer(os);
     218       15855 :         versionhead  *n = ov->id_based_head;
     219       15855 :         versionhead  *p = os->id_based_h;
     220             : 
     221       15855 :         if (p != n)
     222     1188452 :                 while (p && p->next != n)
     223             :                         p = p->next;
     224       15855 :         assert(p==n||(p && p->next == n));
     225       15855 :         if (p == n) {
     226        1348 :                 os->id_based_h = n->next;
     227        1348 :                 if (os->id_based_h) // i.e. non-empty os
     228         807 :                         os->id_based_h->prev = NULL;
     229             :                 p = NULL;
     230       14507 :         } else if ( p != NULL)  {
     231       14507 :                 p->next = n->next;
     232       14507 :                 if (p->next) // node in the middle
     233        5693 :                         p->next->prev = p;
     234             :         }
     235       15855 :         if (n == os->id_based_t)
     236        9355 :                 os->id_based_t = p;
     237             : 
     238       15855 :         if (os->id_map && n)
     239       15296 :                 hash_delete(os->id_map, n);
     240             : 
     241       15855 :         os->name_based_cnt--;
     242       15855 :         unlock_writer(os);
     243             : 
     244       15855 :         bte state = os_atmc_get_state(ov);
     245       15855 :         state |= id_based_versionhead_owner;
     246       15855 :         os_atmc_set_state(ov, state);
     247       15855 :         return p;
     248             : }
     249             : 
     250             : static versionhead  *
     251      528772 : node_create(allocator *sa, objectversion *ov)
     252             : {
     253      528772 :         versionhead  *n = SA_NEW(sa, versionhead );
     254             : 
     255      528772 :         if (n == NULL)
     256             :                 return NULL;
     257      528772 :         *n = (versionhead ) {
     258             :                 .ov = ov,
     259             :         };
     260      528772 :         return n;
     261             : }
     262             : 
     263             : static inline int
     264      318468 : os_name_key(versionhead  *n)
     265             : {
     266      318468 :         return hash_key(n->ov->b->name);
     267             : }
     268             : 
     269             : static objectset *
     270      264290 : os_append_node_name(objectset *os, versionhead  *n)
     271             : {
     272      264290 :         lock_writer(os);
     273      264290 :         if ((!os->name_map || os->name_map->size*16 < os->name_based_cnt) && os->name_based_cnt > HASH_MIN_SIZE) {
     274        3814 :                 hash_destroy(os->name_map);
     275        3814 :                 os->name_map = hash_new(os->sa, os->name_based_cnt, (fkeyvalue)& os_name_key);
     276        3814 :                 if (os->name_map == NULL) {
     277           0 :                         unlock_writer(os);
     278           0 :                         return NULL;
     279             :                 }
     280             : 
     281       69642 :                 for (versionhead  *n = os->name_based_h; n; n = n->next ) {
     282       65828 :                         int key = os_name_key(n);
     283             : 
     284       65828 :                         if (hash_add(os->name_map, key, n) == NULL) {
     285           0 :                                 unlock_writer(os);
     286           0 :                                 return NULL;
     287             :                         }
     288             :                 }
     289             :         }
     290             : 
     291      264290 :         if (os->name_map) {
     292      238037 :                 int key = os->name_map->key(n);
     293             : 
     294      238037 :                 if (hash_add(os->name_map, key, n) == NULL) {
     295           0 :                         unlock_writer(os);
     296           0 :                         return NULL;
     297             :                 }
     298             :         }
     299             : 
     300      264290 :         if (os->name_based_t) {
     301      256038 :                 os->name_based_t->next = n;
     302             :         } else {
     303        8252 :                 os->name_based_h = n;
     304             :         }
     305      264290 :         n->prev = os->name_based_t; // aka the double linked list.
     306      264290 :         os->name_based_t = n;
     307      264290 :         os->name_based_cnt++;
     308      264290 :         unlock_writer(os);
     309      264290 :         return os;
     310             : }
     311             : 
     312             : static objectset *
     313      264290 : os_append_name(objectset *os, objectversion *ov)
     314             : {
     315      264290 :         versionhead  *n = node_create(os->sa, ov);
     316             : 
     317      264290 :         if (n == NULL)
     318             :                 return NULL;
     319             : 
     320      264290 :         ov->name_based_head = n;
     321      264290 :         if (!(os = os_append_node_name(os, n))){
     322           0 :                 _DELETE(n);
     323           0 :                 return NULL;
     324             :         }
     325             : 
     326             :         return os;
     327             : }
     328             : 
     329             : static void
     330        3916 : os_append_id_map(objectset *os)
     331             : {
     332        3916 :         if (os->id_map)
     333         452 :                 hash_destroy(os->id_map);
     334        3916 :         os->id_map = hash_new(os->sa, os->id_based_cnt, (fkeyvalue)&os_id_key);
     335        3916 :         if (os->id_map == NULL)
     336             :                 return ;
     337       78358 :         for (versionhead  *n = os->id_based_h; n; n = n->next ) {
     338       74442 :                 int key = os_id_key(n);
     339             : 
     340       74442 :                 if (hash_add(os->id_map, key, n) == NULL) {
     341           0 :                         hash_destroy(os->id_map);
     342           0 :                         os->id_map = NULL;
     343           0 :                         return ;
     344             :                 }
     345             :         }
     346             : }
     347             : 
     348             : static objectset *
     349      264482 : os_append_node_id(objectset *os, versionhead  *n)
     350             : {
     351      264482 :         lock_writer(os);
     352      264482 :         if ((!os->id_map || os->id_map->size*16 < os->id_based_cnt) && os->id_based_cnt > HASH_MIN_SIZE)
     353        3916 :                 os_append_id_map(os); /* on failure just fall back to slow method */
     354             : 
     355      264482 :         if (os->id_map) {
     356      238720 :                 int key = os->id_map->key(n);
     357      238720 :                 if (hash_add(os->id_map, key, n) == NULL) {
     358           0 :                         hash_destroy(os->id_map);
     359           0 :                         os->id_map = NULL;
     360             :                         /* fall back to slow search */
     361             :                 }
     362             :         }
     363             : 
     364      264482 :         if (os->id_based_t) {
     365      256230 :                 os->id_based_t->next = n;
     366             :         } else {
     367        8252 :                 os->id_based_h = n;
     368             :         }
     369      264482 :         n->prev = os->id_based_t; // aka the double linked list.
     370      264482 :         os->id_based_t = n;
     371      264482 :         os->id_based_cnt++;
     372      264482 :         unlock_writer(os);
     373      264482 :         return os;
     374             : }
     375             : 
     376             : static objectset *
     377      264482 : os_append_id(objectset *os, objectversion *ov)
     378             : {
     379      264482 :         versionhead  *n = node_create(os->sa, ov);
     380             : 
     381      264482 :         if (n == NULL)
     382             :                 return NULL;
     383      264482 :         ov->id_based_head = n;
     384      264482 :         if (!(os = os_append_node_id(os, n))){
     385           0 :                 _DELETE(n);
     386           0 :                 return NULL;
     387             :         }
     388             : 
     389             :         return os;
     390             : }
     391             : 
     392             : static versionhead * find_name(objectset *os, const char *name);
     393             : 
     394             : static void
     395      283626 : objectversion_destroy(sqlstore *store, objectset* os, objectversion *ov)
     396             : {
     397             : 
     398      283626 :         bte state = os_atmc_get_state(ov);
     399             : 
     400      283626 :         if (state & name_based_versionhead_owner) {
     401       15663 :                 node_destroy(ov->os, store, ov->name_based_head);
     402             :         }
     403             : 
     404      283626 :         if (state & id_based_versionhead_owner) {
     405       15855 :                 node_destroy(ov->os, store, ov->id_based_head);
     406             :         }
     407             : 
     408      283626 :         if (os->destroy && ov->b)
     409      258376 :                 os->destroy(store, ov->b);
     410             : 
     411      283626 :         if (os->temporary && (state & deleted || state & under_destruction || state & rollbacked))
     412         200 :                 os_destroy(os, store); // TODO transaction_layer_revamp: embed into refcounting subproject : reference is already dropped by os_cleanup
     413      283626 :         _DELETE(ov);
     414      283626 : }
     415             : 
     416             : static void
     417        6121 : _os_rollback(objectversion *ov, sqlstore *store)
     418             : {
     419        6121 :         assert(ov->ts >= TRANSACTION_ID_BASE);
     420             : 
     421        6121 :         bte state = os_atmc_get_state(ov);
     422        6121 :         if (state & rollbacked) {
     423             :                 return;
     424             :         }
     425             : 
     426        5621 :         state |= rollbacked;
     427        5621 :         os_atmc_set_state(ov, state);
     428             : 
     429        5621 :         bte state_older;
     430             : 
     431             :         /*
     432             :          * We have to use the readers-writer lock here,
     433             :          * since the pointer containing the address of the older objectversion might be concurrently overwritten if the older itself has just been put in the under_destruction state .
     434             :          */
     435        5621 :         lock_reader(ov->os);
     436        5621 :         objectversion* name_based_older = ov->name_based_older;
     437        5621 :         unlock_reader(ov->os);
     438             : 
     439        5621 :         if (name_based_older && !((state_older= os_atmc_get_state(name_based_older)) & rollbacked)) {
     440         756 :                 if (ov->ts != name_based_older->ts) {
     441             :                         // older is last committed state or belongs to parent transaction.
     442             :                         // In any case, we restore versionhead pointer to that.
     443             : 
     444         261 :                         ATOMIC_BASE_TYPE expected_deleted = deleted;
     445         261 :                         if (state_older == active || (state_older == deleted && ATOMIC_CAS(&name_based_older->state, &expected_deleted, block_destruction))) {
     446         261 :                                 ov->name_based_head->ov = name_based_older;
     447         261 :                                 name_based_older->name_based_newer=NULL;
     448         261 :                                 if (state_older != active && expected_deleted == deleted)
     449           0 :                                         os_atmc_set_state(name_based_older, deleted); //Restore the deleted older back to its deleted state.
     450             :                         }
     451             :                 }
     452             :                 else {
     453         495 :                         _os_rollback(name_based_older, store);
     454             :                 }
     455             :         }
     456        4865 :         else if (!name_based_older) {
     457             :                 // this is a terminal node. i.e. this objectversion does not have name based committed history
     458        4865 :                 if (ov->name_based_head) // The opposite can happen during an early conflict in os_add or os_del.
     459        4859 :                         os_remove_name_based_chain(ov->os, ov);
     460             :         }
     461             : 
     462             :         /*
     463             :          * We have to use the readers-writer lock here,
     464             :          * since the pointer containing the address of the older objectversion might be concurrently overwritten if the older itself has just been put in the under_destruction state .
     465             :          */
     466        5621 :         lock_reader(ov->os);
     467        5621 :         objectversion* id_based_older = ov->id_based_older;
     468        5621 :         unlock_reader(ov->os);
     469        5621 :         if (id_based_older && !((state_older= os_atmc_get_state(id_based_older)) & rollbacked)) {
     470         266 :                 if (ov->ts != id_based_older->ts) {
     471             :                         // older is last committed state or belongs to parent transaction.
     472             :                         // In any case, we restore versionhead pointer to that.
     473             : 
     474         261 :                         ATOMIC_BASE_TYPE expected_deleted = deleted;
     475         261 :                         if (state_older == active || (state_older == deleted && ATOMIC_CAS(&id_based_older->state, &expected_deleted, block_destruction))) {
     476         261 :                                 ov->id_based_head->ov = id_based_older;
     477         261 :                                 id_based_older->id_based_newer=NULL;
     478         261 :                                 if (state_older != active && expected_deleted == deleted)
     479           0 :                                         os_atmc_set_state(id_based_older, deleted); //Restore the deleted older back to its deleted state.
     480             :                         }
     481             :                 }
     482           5 :                 else if (id_based_older != name_based_older)
     483           5 :                         _os_rollback(id_based_older, store);
     484             :         }
     485        4902 :         else if (!id_based_older) {
     486             :                 // this is a terminal node. i.e. this objectversion does not have id based committed history
     487        4902 :                 os_remove_id_based_chain(ov->os, ov);
     488             :         }
     489             : 
     490        5621 :         if (ov->name_based_newer && !(os_atmc_get_state(ov->name_based_newer) & rollbacked)) {
     491           0 :                 _os_rollback(ov->name_based_newer, store);
     492             :         }
     493             : 
     494        5621 :         if (ov->id_based_newer && ov->id_based_newer != ov->name_based_newer && !(os_atmc_get_state(ov->id_based_newer) & rollbacked)) {
     495           0 :                 _os_rollback(ov->id_based_newer, store);
     496             :         }
     497             : }
     498             : 
     499             : static int
     500        5621 : os_rollback(objectversion *ov, sqlstore *store)
     501             : {
     502        5621 :         _os_rollback(ov, store);
     503             : 
     504        5621 :         return LOG_OK;
     505             : }
     506             : 
     507             : static void
     508       25250 : ov_destroy_obj_recursive(sqlstore* store, objectversion *ov)
     509             : {
     510       25250 :         if (ov->id_based_older && ov->id_based_older == ov->name_based_older) {
     511       14440 :                 ov_destroy_obj_recursive(store, ov->id_based_older);
     512             :         }
     513       25250 :         if (ov->os->destroy && ov->b) {
     514       25250 :                 ov->os->destroy(store, ov->b);
     515       25250 :                 ov->b = NULL;
     516             :         }
     517       25250 : }
     518             : 
     519             : static inline void
     520       11004 : try_to_mark_deleted_for_destruction(sqlstore* store, objectversion *ov)
     521             : {
     522       11004 :         ATOMIC_BASE_TYPE expected_deleted = deleted;
     523       11004 :         if (ATOMIC_CAS(&ov->state, &expected_deleted, under_destruction)) {
     524             : 
     525       11004 :                 if (!ov->name_based_newer || (os_atmc_get_state(ov->name_based_newer) & rollbacked)) {
     526       10804 :                         os_remove_name_based_chain(ov->os, ov);
     527             :                 }
     528             :                 else {
     529         200 :                         lock_writer(ov->os);
     530         200 :                         ov->name_based_newer->name_based_older = NULL;
     531         200 :                         unlock_writer(ov->os);
     532             :                 }
     533             : 
     534       11004 :                 if (!ov->id_based_newer || (os_atmc_get_state(ov->id_based_newer) & rollbacked)) {
     535       10953 :                         os_remove_id_based_chain(ov->os, ov);
     536             :                 }
     537             :                 else {
     538          51 :                         lock_writer(ov->os);
     539          51 :                         ov->id_based_newer->id_based_older = NULL;
     540          51 :                         unlock_writer(ov->os);
     541             :                 }
     542             : 
     543       11004 :                 ov->ts = store_get_timestamp(store)+1;
     544       11004 :                 if (!ov->os->nested)
     545       10810 :                         ov_destroy_obj_recursive(store, ov);
     546             :         }
     547       11004 : }
     548             : 
     549             : static void
     550       29462 : objectversion_destroy_recursive(sqlstore* store, objectversion *ov)
     551             : {
     552       29462 :         if (ov->id_based_older && ov->id_based_older == ov->name_based_older) {
     553       14634 :                 objectversion_destroy_recursive(store, ov->id_based_older);
     554             :         }
     555       29462 :         objectversion_destroy(store, ov->os, ov);
     556       29462 : }
     557             : 
     558             : static int
     559      311636 : os_cleanup(sqlstore* store, objectversion *ov, ulng oldest)
     560             : {
     561      311636 :         if (os_atmc_get_state(ov) & under_destruction) {
     562       15214 :                 if (ov->ts < oldest) {
     563             :                         // This one is ready to be freed
     564       11004 :                         objectversion_destroy_recursive(store, ov);
     565       11004 :                         return LOG_ERR;
     566             :                 }
     567             : 
     568             :                 // not yet old enough to be safely removed. Try later.
     569             :                 return LOG_OK;
     570             :         }
     571             : 
     572      296422 :         if (os_atmc_get_state(ov) & rollbacked) {
     573       17762 :                 if (ov->ts < oldest) {
     574             :                         // This one is ready to be freed
     575        5621 :                         if (ov->name_based_older && ov->name_based_older->name_based_newer == ov)
     576         495 :                                 ov->name_based_older->name_based_newer=NULL;
     577        5621 :                         if (ov->id_based_older && ov->id_based_older->id_based_newer == ov)
     578         458 :                                 ov->id_based_older->id_based_newer=NULL;
     579        5621 :                         objectversion_destroy(store, ov->os, ov);
     580        5621 :                         return LOG_ERR;
     581             :                 }
     582             : 
     583       12141 :                 if (ov->ts > TRANSACTION_ID_BASE) {
     584             :                         /* We mark it with the latest possible starttime and reinsert it into the cleanup list.
     585             :                          * This will cause a safe eventual destruction of this rollbacked ov.
     586             :                          */
     587        5621 :                         ov->ts = store_get_timestamp(store)+1;
     588             :                 }
     589             : 
     590             :                 // not yet old enough to be safely removed. Try later.
     591       12141 :                 return LOG_OK;
     592             :         }
     593             : 
     594      278660 :         if (os_atmc_get_state(ov) == deleted) {
     595       11084 :                 if (ov->ts <= oldest) {
     596             :                         // the oldest relevant state is deleted so lets try to mark it as destroyed
     597       11004 :                         try_to_mark_deleted_for_destruction(store, ov);
     598       11004 :                         return LOG_OK+1;
     599             :                 }
     600             : 
     601             :                 // Keep it inplace on the cleanup list, either because it is now marked for destruction or
     602             :                 // we want to retry marking it for destruction later.
     603             :                 return LOG_OK;
     604             :         }
     605             : 
     606      267576 :         assert(os_atmc_get_state(ov) != deleted && os_atmc_get_state(ov) != under_destruction && os_atmc_get_state(ov) != rollbacked);
     607      267576 :         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
     608             : 
     609      275521 :         while (ov->id_based_older && ov->id_based_older == ov->name_based_older && ov->ts >= oldest) {
     610             :                 ov = ov->id_based_older;
     611             :         }
     612             : 
     613      267576 :         if (ov->id_based_older && ov->id_based_older == ov->name_based_older) {
     614             :                 // Destroy everything older then the oldest possibly relevant objectversion.
     615        3824 :                 objectversion_destroy_recursive(store, ov->id_based_older);
     616        3824 :                 ov->id_based_older = NULL;
     617             :         }
     618             : 
     619             :         return LOG_ERR;
     620             : }
     621             : 
     622             : static int
     623      311643 : tc_gc_objectversion(sql_store store, sql_change *change, ulng oldest)
     624             : {
     625             : //      assert(!change->handled);
     626      311643 :         objectversion *ov = (objectversion*)change->data;
     627             : 
     628      311643 :         if (oldest >= TRANSACTION_ID_BASE)
     629             :                 return 0;
     630      311636 :         int res = os_cleanup( (sqlstore*) store, ov, oldest);
     631      311636 :         change->handled = (res)?true:false;
     632      311636 :         return res>=0?LOG_OK:LOG_ERR;
     633             : }
     634             : 
     635             : static int
     636      284208 : tc_commit_objectversion(sql_trans *tr, sql_change *change, ulng commit_ts, ulng oldest)
     637             : {
     638      284208 :         objectversion *ov = (objectversion*)change->data;
     639      284208 :         if (commit_ts) {
     640      278587 :                 assert(ov->ts == tr->tid);
     641      278587 :                 ov->ts = commit_ts;
     642      278587 :                 change->committed = commit_ts < TRANSACTION_ID_BASE ? true: false;
     643      278587 :                 (void)oldest;
     644      278587 :                 if (!tr->parent)
     645      278580 :                         change->obj->new = 0;
     646      278587 :                 if (!ov->os->temporary)
     647      274128 :                         ATOMIC_INC(&tr->cat->schema_version);
     648             :         } else {
     649        5621 :                 os_rollback(ov, tr->store);
     650             :         }
     651             : 
     652      284208 :         return LOG_OK;
     653             : }
     654             : 
     655             : objectset *
     656       59251 : os_new(allocator *sa, destroy_fptr destroy, bool temporary, bool unique, bool concurrent, bool nested, sql_store store)
     657             : {
     658       59251 :         assert(!sa);
     659       59251 :         objectset *os = SA_NEW(sa, objectset);
     660       59251 :         if (os) {
     661       59251 :                 *os = (objectset) {
     662             :                         .refcnt = ATOMIC_VAR_INIT(1),
     663             :                         .sa = sa,
     664             :                         .destroy = destroy,
     665             :                         .temporary = temporary,
     666             :                         .unique = unique,
     667             :                         .concurrent = concurrent,
     668             :                         .nested = nested,
     669             :                         .store = store
     670             :                 };
     671       59251 :                 os->destroy = destroy;
     672       59251 :                 MT_rwlock_init(&os->rw_lock, "sa_readers_lock");
     673       59251 :                 MT_lock_init(&os->lock, "single_writer_lock");
     674             :         }
     675             : 
     676       59251 :         return os;
     677             : }
     678             : 
     679             : objectset *
     680        4730 : os_dup(objectset *os)
     681             : {
     682        4730 :         ATOMIC_INC(&os->refcnt);
     683        4730 :         return os;
     684             : }
     685             : 
     686             : void
     687       63923 : os_destroy(objectset *os, sql_store store)
     688             : {
     689       63923 :         if (ATOMIC_DEC(&os->refcnt) > 0)
     690             :                 return;
     691       59193 :         MT_lock_destroy(&os->lock);
     692       59193 :         MT_rwlock_destroy(&os->rw_lock);
     693       59193 :         versionhead* n=os->id_based_h;
     694      307245 :         while(n) {
     695      248052 :                 objectversion *ov = n->ov;
     696      496595 :                 while(ov) {
     697      248543 :                         objectversion *older = ov->id_based_older;
     698      248543 :                         objectversion_destroy(store, os, ov);
     699      248543 :                         ov = older;
     700             :                 }
     701      248052 :                 versionhead* hn =n->next;
     702      248052 :                 node_destroy(os, store, n);
     703      248052 :                 n = hn;
     704             :         }
     705             : 
     706       59193 :         n=os->name_based_h;
     707      307245 :         while(n) {
     708      248052 :                 versionhead* hn =n->next;
     709      248052 :                 node_destroy(os, store, n);
     710      248052 :                 n = hn;
     711             :         }
     712             : 
     713       59193 :         if (os->id_map)
     714        3454 :                 hash_destroy(os->id_map);
     715             : 
     716       59193 :         if (os->name_map)
     717        3428 :                 hash_destroy(os->name_map);
     718             : 
     719       59193 :         if (!os->sa)
     720       59193 :                 _DELETE(os);
     721             : }
     722             : 
     723             : static versionhead  *
     724    18455594 : find_name(objectset *os, const char *name)
     725             : {
     726    18455594 :         lock_reader(os);
     727    18518856 :         if (os->name_map) {
     728    15025312 :                 int key = hash_key(name);
     729    15025312 :                 sql_hash_e *he = os->name_map->buckets[key&(os->name_map->size-1)];
     730             : 
     731    81535445 :                 for (; he; he = he->chain) {
     732    81168607 :                         versionhead  *n = he->value;
     733             : 
     734    81168607 :                         if (n && n->ov->b->name && strcmp(n->ov->b->name, name) == 0) {
     735    14658474 :                                 unlock_reader(os);
     736    14658474 :                                 return n;
     737             :                         }
     738             :                 }
     739      366838 :                 unlock_reader(os);
     740      366838 :                 return NULL;
     741             :         }
     742             : 
     743     4179459 :         for (versionhead  *n = os->name_based_h; n; n = n->next) {
     744     3878149 :                 objectversion *ov = n->ov;
     745             : 
     746             :                 /* check if names match */
     747     3878149 :                 if (name[0] == ov->b->name[0] && strcmp(name, ov->b->name) == 0) {
     748     3192234 :                         unlock_reader(os);
     749     3192234 :                         return n;
     750             :                 }
     751             :         }
     752             : 
     753      301310 :         unlock_reader(os);
     754      301310 :         return NULL;
     755             : }
     756             : 
     757             : static objectversion*
     758    19460920 : get_valid_object_name(sql_trans *tr, objectversion *ov)
     759             : {
     760    19483842 :         while(ov) {
     761    19483812 :                 if (ov->ts == tr->tid || (tr->parent && tr_version_of_parent(tr, ov->ts)) || ov->ts < tr->ts)
     762    19500853 :                         return ov;
     763             :                 else {
     764           0 :                         lock_reader(ov->os);
     765         168 :                         objectversion* name_based_older = ov->name_based_older;
     766         168 :                         unlock_reader(ov->os);
     767         168 :                         ov = name_based_older;
     768             :                 }
     769             :         }
     770             :         return ov;
     771             : }
     772             : 
     773             : static objectversion*
     774      579565 : get_valid_object_id(sql_trans *tr, objectversion *ov)
     775             : {
     776      579724 :         while(ov) {
     777      579616 :                 if (ov->ts == tr->tid || (tr->parent && tr_version_of_parent(tr, ov->ts))  || ov->ts < tr->ts)
     778      579457 :                         return ov;
     779             :                 else {
     780         159 :                         lock_reader(ov->os);
     781         159 :                         objectversion* id_based_older = ov->id_based_older;
     782         159 :                         unlock_reader(ov->os);
     783         159 :                         ov = id_based_older;
     784             :                 }
     785             :         }
     786             :         return ov;
     787             : }
     788             : 
     789             : static int
     790      272720 : os_add_name_based(objectset *os, struct sql_trans *tr, const char *name, objectversion *ov) {
     791      272720 :         versionhead  *name_based_node = NULL;
     792      272720 :         if (ov->id_based_older && strcmp(ov->id_based_older->b->name, name) == 0)
     793        8214 :                 name_based_node = ov->id_based_older->name_based_head;
     794      264506 :         else if (os->unique) // Previous name based objectversion is of a different id, so now we do have to perform an extensive look up
     795       81548 :                 name_based_node = find_name(os, name);
     796             :         // else names are not unique and each id based version head maps to its own name based version head.
     797             : 
     798       89762 :         if (name_based_node) {
     799        8430 :                 objectversion *co = name_based_node->ov;
     800        8430 :                 objectversion *oo = get_valid_object_name(tr, co);
     801        8430 :                 if (co != oo) { /* conflict ? */
     802           6 :                         TRC_WARNING(SQL_STORE, "%s" "if (co != oo) { /* conflict ? */", __func__);
     803           6 :                         return -3;
     804             :                 }
     805             : 
     806        8424 :                 assert(ov != oo); // Time loops are not allowed
     807             : 
     808        8424 :                 bte state = os_atmc_get_state(oo);
     809        8424 :                 if (state != active) {
     810             :                         // This can only happen if the parent oo was a committed deleted at some point.
     811         243 :                         assert(state == deleted || state == under_destruction || state == block_destruction);
     812             :                         /* Since our parent oo is committed deleted objectversion, we might have a conflict with
     813             :                         * another transaction that tries to clean up oo or also wants to add a new objectversion.
     814             :                         */
     815         243 :                         ATOMIC_BASE_TYPE expected_deleted = deleted;
     816         243 :                         if (!ATOMIC_CAS(&oo->state, &expected_deleted, block_destruction)) {
     817           0 :                                 TRC_WARNING(SQL_STORE, "%s: " "if (!ATOMIC_CAS(&oo->state, &expected_deleted, block_destruction)) { /*conflict with cleaner or write-write conflict*/ ", __func__);
     818           0 :                                 return -3; /*conflict with cleaner or write-write conflict*/
     819             :                         }
     820             :                 }
     821             : 
     822             :                 /* new object with same name within transaction, should have a delete in between */
     823        8181 :                 assert(!(state == active && oo->ts == ov->ts && !(os_atmc_get_state(ov) & deleted)));
     824             : 
     825        8424 :                 lock_writer(os);
     826        8424 :                 ov->name_based_head = oo->name_based_head;
     827        8424 :                 ov->name_based_older = oo;
     828             : 
     829        8424 :                 name_based_node->ov = ov;
     830        8424 :                 if (oo) {
     831        8424 :                         oo->name_based_newer = ov;
     832             :                         // if the parent was originally deleted, we restore it to that state.
     833        8424 :                         os_atmc_set_state(oo, state);
     834             :                 }
     835        8424 :                 unlock_writer(os);
     836        8424 :                 return 0;
     837             :         } else { /* new */
     838      264290 :                 if (os_append_name(os, ov) == NULL)
     839             :                         return -1; // MALLOC_FAIL
     840             :                 return 0;
     841             :         }
     842             : }
     843             : 
     844             : static int
     845      272730 : os_add_id_based(objectset *os, struct sql_trans *tr, sqlid id, objectversion *ov) {
     846      272730 :         versionhead  *id_based_node;
     847             : 
     848      272730 :         id_based_node = find_id(os, id);
     849             : 
     850      272730 :         if (id_based_node) {
     851        8248 :                 objectversion *co = id_based_node->ov;
     852        8248 :                 objectversion *oo = get_valid_object_id(tr, co);
     853        8248 :                 if (co != oo) { /* conflict ? */
     854          10 :                         TRC_WARNING(SQL_STORE, "%s" "if (co != oo) { /* conflict ? */", __func__);
     855          10 :                         return -3;
     856             :                 }
     857             : 
     858        8238 :                 assert(ov != oo); // Time loops are not allowed
     859             : 
     860        8238 :                 bte state = os_atmc_get_state(oo);
     861        8238 :                 if (state != active) {
     862             :                         // This can only happen if the parent oo was a committed deleted at some point.
     863          57 :                         assert(state == deleted || state == under_destruction || state == block_destruction);
     864             :                         /* Since our parent oo is committed deleted objectversion, we might have a conflict with
     865             :                         * another transaction that tries to clean up oo or also wants to add a new objectversion.
     866             :                         */
     867          57 :                         ATOMIC_BASE_TYPE expected_deleted = deleted;
     868          57 :                         if (!ATOMIC_CAS(&oo->state, &expected_deleted, block_destruction)) {
     869           0 :                                 TRC_WARNING(SQL_STORE, "%s" "if (!ATOMIC_CAS(&oo->state, &expected_deleted, block_destruction)) { /*conflict with cleaner or write-write conflict*/", __func__);
     870           0 :                                 return -3; /*conflict with cleaner or write-write conflict*/
     871             :                         }
     872             :                 }
     873             : 
     874        8238 :                 lock_writer(os);
     875        8238 :                 ov->id_based_head = oo->id_based_head;
     876        8238 :                 ov->id_based_older = oo;
     877             : 
     878        8238 :                 id_based_node->ov = ov;
     879        8238 :                 if (oo) {
     880        8238 :                         oo->id_based_newer = ov;
     881             :                         // if the parent was originally deleted, we restore it to that state.
     882        8238 :                         os_atmc_set_state(oo, state);
     883             :                 }
     884        8238 :                 unlock_writer(os);
     885        8238 :                 return 0;
     886             :         } else { /* new */
     887      264482 :                 if (os_append_id(os, ov) == NULL)
     888             :                         return -1; // MALLOC_FAIL
     889             : 
     890             :                 return 0;
     891             :         }
     892             : }
     893             : 
     894             : static int /*ok, error (name existed) and conflict (added before) */
     895      272731 : os_add_(objectset *os, struct sql_trans *tr, const char *name, sql_base *b)
     896             : {
     897      272731 :         int res = 0;
     898      272731 :         objectversion *ov = SA_NEW(os->sa, objectversion);
     899             : 
     900      272731 :         *ov = (objectversion) {
     901      272731 :                 .ts = tr->tid,
     902             :                 .b = b,
     903             :                 .os = os,
     904             :         };
     905             : 
     906      272731 :         if (!os->concurrent && os_has_changes(os, tr)) { /* for object sets without concurrent support, conflict if concurrent changes are there */
     907           1 :                 if (os->destroy)
     908           1 :                         os->destroy(os->store, ov->b);
     909           1 :                 _DELETE(ov);
     910           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__);
     911           1 :                 return -3; /* conflict */
     912             :         }
     913             : 
     914      272730 :         if ((res = os_add_id_based(os, tr, b->id, ov))) {
     915          10 :                 if (os->destroy)
     916          10 :                         os->destroy(os->store, ov->b);
     917          10 :                 _DELETE(ov);
     918          10 :                 return res;
     919             :         }
     920             : 
     921      272720 :         if ((res = os_add_name_based(os, tr, name, ov))) {
     922           6 :                 trans_add(tr, b, ov, &tc_gc_objectversion, &tc_commit_objectversion, NULL);
     923           6 :                 return res;
     924             :         }
     925             : 
     926      272714 :         if (os->temporary) (void) os_dup(os); // TODO transaction_layer_revamp: embed into refcounting subproject
     927      272714 :         trans_add(tr, b, ov, &tc_gc_objectversion, &tc_commit_objectversion, NULL);
     928      272714 :         return res;
     929             : }
     930             : 
     931             : int
     932      272731 : os_add(objectset *os, struct sql_trans *tr, const char *name, sql_base *b)
     933             : {
     934      272731 :         MT_lock_set(&os->lock);
     935      272731 :         int res = os_add_(os, tr, name, b);
     936      272731 :         MT_lock_unset(&os->lock);
     937      272731 :         return res;
     938             : }
     939             : 
     940             : static int
     941       11481 : os_del_name_based(objectset *os, struct sql_trans *tr, const char *name, objectversion *ov) {
     942       11481 :         versionhead  *name_based_node = NULL;
     943       11481 :         if (ov->id_based_older && strcmp(ov->id_based_older->b->name, name) == 0)
     944       11481 :                 name_based_node = ov->id_based_older->name_based_head;
     945           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
     946           0 :                 name_based_node = find_name(os, name);
     947             : 
     948       11481 :         if (name_based_node) {
     949       11481 :                 objectversion *co = name_based_node->ov;
     950       11481 :                 objectversion *oo = get_valid_object_name(tr, co);
     951       11481 :                 ov->name_based_head = oo->name_based_head;
     952       11481 :                 if (co != oo) { /* conflict ? */
     953           0 :                         TRC_WARNING(SQL_STORE, "%s: " "if (co != oo) { /* conflict ? */", __func__);
     954           0 :                         return -3;
     955             :                 }
     956       11481 :                 ov->name_based_older = oo;
     957             : 
     958       11481 :                 lock_writer(os);
     959       11481 :                 if (oo) {
     960       11481 :                         oo->name_based_newer = ov;
     961       11481 :                         assert(os_atmc_get_state(oo) == active);
     962             :                 }
     963       11481 :                 name_based_node->ov = ov;
     964       11481 :                 unlock_writer(os);
     965       11481 :                 return 0;
     966             :         } else {
     967             :                 /* missing */
     968           0 :                 return -1;
     969             :         }
     970             : }
     971             : 
     972             : static int
     973       11483 : os_del_id_based(objectset *os, struct sql_trans *tr, sqlid id, objectversion *ov) {
     974             : 
     975       11483 :         versionhead  *id_based_node;
     976             : 
     977       11483 :         if (ov->name_based_older && ov->name_based_older->b->id == id)
     978           0 :                 id_based_node = ov->name_based_older->id_based_head;
     979             :         else // Previous id based objectversion is of a different name, so now we do have to perform an extensive look up
     980       11483 :                 id_based_node = find_id(os, id);
     981             : 
     982       11483 :         if (id_based_node) {
     983       11483 :                 objectversion *co = id_based_node->ov;
     984       11483 :                 objectversion *oo = get_valid_object_id(tr, co);
     985       11483 :                 ov->id_based_head = oo->id_based_head;
     986       11483 :                 if (co != oo) { /* conflict ? */
     987           2 :                         TRC_WARNING(SQL_STORE, "%s" "if (co != oo) { /* conflict ? */", __func__);
     988           2 :                         return -3;
     989             :                 }
     990       11481 :                 ov->id_based_older = oo;
     991             : 
     992       11481 :                 lock_writer(os);
     993       11481 :                 if (oo) {
     994       11481 :                         oo->id_based_newer = ov;
     995       11481 :                         assert(os_atmc_get_state(oo) == active);
     996             :                 }
     997       11481 :                 id_based_node->ov = ov;
     998       11481 :                 unlock_writer(os);
     999       11481 :                 return 0;
    1000             :         } else {
    1001             :                 /* missing */
    1002             :                 return -1;
    1003             :         }
    1004             : }
    1005             : 
    1006             : static int
    1007       11483 : os_del_(objectset *os, struct sql_trans *tr, const char *name, sql_base *b)
    1008             : {
    1009       11483 :         int res = 0;
    1010       11483 :         objectversion *ov = SA_NEW(os->sa, objectversion);
    1011             : 
    1012       11483 :         *ov = (objectversion) {
    1013       11483 :                 .ts = tr->tid,
    1014             :                 .b = b,
    1015             :                 .os = os,
    1016             :         };
    1017       11483 :         os_atmc_set_state(ov, deleted);
    1018             : 
    1019       11483 :         if ((res = os_del_id_based(os, tr, b->id, ov))) {
    1020           2 :                 if (os->destroy)
    1021           2 :                         os->destroy(os->store, ov->b);
    1022           2 :                 _DELETE(ov);
    1023           2 :                 return res;
    1024             :         }
    1025             : 
    1026       11481 :         if ((res = os_del_name_based(os, tr, name, ov))) {
    1027           0 :                 trans_add(tr, b, ov, &tc_gc_objectversion, &tc_commit_objectversion, NULL);
    1028           0 :                 return res;
    1029             :         }
    1030             : 
    1031       11481 :         if (os->temporary) (void) os_dup(os); // TODO transaction_layer_revamp: embed into refcounting subproject
    1032       11481 :         trans_add(tr, b, ov, &tc_gc_objectversion, &tc_commit_objectversion, NULL);
    1033       11481 :         return res;
    1034             : }
    1035             : 
    1036             : int
    1037       11483 : os_del(objectset *os, sql_trans *tr, const char *name, sql_base *b)
    1038             : {
    1039       11483 :         MT_lock_set(&os->lock);
    1040       11483 :         int res = os_del_(os, tr, name, b);
    1041       11483 :         MT_lock_unset(&os->lock);
    1042       11483 :         return res;
    1043             : }
    1044             : 
    1045             : int
    1046         315 : os_size(objectset *os, struct sql_trans *tr)
    1047             : {
    1048         315 :         int cnt = 0;
    1049         315 :         if (os) {
    1050         315 :                 lock_reader(os);
    1051         324 :                 for(versionhead  *n = os->name_based_h; n; n=n->next) {
    1052           9 :                         objectversion *ov = n->ov;
    1053           9 :                         if ((ov=get_valid_object_name(tr, ov)) && os_atmc_get_state(ov) == active)
    1054           6 :                                 cnt++;
    1055             :                 }
    1056         315 :                 unlock_reader(os);
    1057             :         }
    1058         315 :         return cnt;
    1059             : }
    1060             : 
    1061             : int
    1062           0 : os_empty(objectset *os, struct sql_trans *tr)
    1063             : {
    1064           0 :         return os_size(os, tr)==0;
    1065             : }
    1066             : 
    1067             : int
    1068           0 : os_remove(objectset *os, sql_trans *tr, const char *name)
    1069             : {
    1070           0 :         (void) os;
    1071           0 :         (void) tr;
    1072           0 :         (void) name;
    1073             :         // TODO remove entire versionhead  corresponding to this name.
    1074             : 
    1075             :         // TODO assert os->unique?s
    1076           0 :         return LOG_OK;
    1077             : }
    1078             : 
    1079             : sql_base *
    1080    18382162 : os_find_name(objectset *os, struct sql_trans *tr, const char *name)
    1081             : {
    1082    18382162 :         if (!os)
    1083             :                 return NULL;
    1084             : 
    1085    18382160 :         assert(os->unique);
    1086    18382160 :         versionhead  *n = find_name(os, name);
    1087             : 
    1088    18483742 :         if (n) {
    1089    17896996 :                  objectversion *ov = get_valid_object_name(tr, n->ov);
    1090    17874047 :                  if (ov && os_atmc_get_state(ov) == active)
    1091    17873702 :                          return ov->b;
    1092             :         }
    1093             :         return NULL;
    1094             : }
    1095             : 
    1096             : sql_base *
    1097      155268 : os_find_id(objectset *os, struct sql_trans *tr, sqlid id)
    1098             : {
    1099      155268 :         if (!os)
    1100             :                 return NULL;
    1101      155268 :         versionhead  *n = find_id(os, id);
    1102             : 
    1103      155268 :         if (n) {
    1104      154747 :                  objectversion *ov = get_valid_object_id(tr, n->ov);
    1105      154747 :                  if (ov && os_atmc_get_state(ov) == active)
    1106      154743 :                          return ov->b;
    1107             :         }
    1108             :         return NULL;
    1109             : }
    1110             : 
    1111             : void
    1112     2305462 : os_iterator(struct os_iter *oi, struct objectset *os, struct sql_trans *tr, const char *name /*optional*/)
    1113             : {
    1114     2305462 :         *oi = (struct os_iter) {
    1115             :                 .os = os,
    1116             :                 .tr = tr,
    1117             :                 .name = name,
    1118             :         };
    1119             : 
    1120     2305462 :         lock_reader(os);
    1121     2306585 :         if (name && os->name_map) {
    1122     1032555 :                 int key = hash_key(name);
    1123     1032555 :                 oi->n = (void*)os->name_map->buckets[key&(os->name_map->size-1)];
    1124             :         } else
    1125     1274030 :                 oi->n =      os->name_based_h;
    1126     2306585 :         unlock_reader(os);
    1127     2306710 : }
    1128             : 
    1129             : sql_base *
    1130     4223773 : oi_next(struct os_iter *oi)
    1131             : {
    1132     4223773 :         sql_base *b = NULL;
    1133             : 
    1134     4223773 :         if (oi->name) {
    1135     2975394 :                 lock_reader(oi->os); /* intentionally outside of while loop */
    1136     2975394 :                 if (oi->os->name_map) {
    1137     2609295 :                         sql_hash_e  *he = (void*)oi->n;
    1138             : 
    1139     6464942 :                         for (; he && !b; he = he->chain) {
    1140     3855647 :                                 versionhead  *n = he->value;
    1141             : 
    1142     3855647 :                                 if (n->ov->b->name && strcmp(n->ov->b->name, oi->name) == 0) {
    1143     1583213 :                                         objectversion *ov = n->ov;
    1144             : 
    1145     1583213 :                                         ov = get_valid_object_name(oi->tr, ov);
    1146     1583213 :                                         if (ov && os_atmc_get_state(ov) == active)
    1147     1581823 :                                                 b = ov->b;
    1148             :                                 }
    1149             :                         }
    1150     2609295 :                         oi->n = (void*)he;
    1151             :                 } else {
    1152      366099 :                         versionhead  *n = oi->n;
    1153             : 
    1154      410961 :                         while (n && !b) {
    1155             : 
    1156       44862 :                                 if (n->ov->b->name && strcmp(n->ov->b->name, oi->name) == 0) {
    1157       13320 :                                         objectversion *ov = n->ov;
    1158             : 
    1159       13320 :                                         n = oi->n = n->next;
    1160       13320 :                                         ov = get_valid_object_name(oi->tr, ov);
    1161       13320 :                                         if (ov && os_atmc_get_state(ov) == active)
    1162       13320 :                                                 b = ov->b;
    1163             :                                 } else {
    1164       31542 :                                         n = oi->n = n->next;
    1165             :                                 }
    1166             :                         }
    1167             :                 }
    1168     2975394 :                 unlock_reader(oi->os);
    1169             :         } else {
    1170     1248379 :                 versionhead  *n = oi->n;
    1171             : 
    1172     1248379 :                 lock_reader(oi->os); /* intentionally outside of while loop */
    1173     2901597 :                 while (n && !b) {
    1174      405149 :                         objectversion *ov = n->ov;
    1175      405149 :                         n = oi->n = n->next;
    1176             : 
    1177      405149 :                         ov = get_valid_object_id(oi->tr, ov);
    1178      405087 :                         if (ov && os_atmc_get_state(ov) == active)
    1179      380662 :                                 b = ov->b;
    1180             :                 }
    1181     1248069 :                 unlock_reader(oi->os);
    1182             :         }
    1183     4224153 :         return b;
    1184             : }
    1185             : 
    1186             : bool
    1187        3539 : os_obj_intransaction(objectset *os, struct sql_trans *tr, sql_base *b)
    1188             : {
    1189        3539 :         versionhead  *n = find_id(os, b->id);
    1190             : 
    1191        3539 :         if (n) {
    1192        3539 :                 objectversion *ov = n->ov;
    1193             : 
    1194        3539 :                 if (ov && os_atmc_get_state(ov) == active && ov->ts == tr->tid)
    1195             :                         return true;
    1196             :         }
    1197             :         return false;
    1198             : }
    1199             : 
    1200             : /* return true if this object set has changes pending for an other transaction */
    1201             : bool
    1202      177707 : os_has_changes(objectset *os, struct sql_trans *tr)
    1203             : {
    1204      177707 :         versionhead  *n = os->id_based_t;
    1205             : 
    1206      177707 :         if (n) {
    1207      173339 :                 objectversion *ov = n->ov;
    1208             : 
    1209      173339 :                 if (ov && os_atmc_get_state(ov) == active && ov->ts != tr->tid && ov->ts > TRANSACTION_ID_BASE)
    1210             :                         return true;
    1211             :         }
    1212             :         return false;
    1213             : }

Generated by: LCOV version 1.14