~vkolesnikov/pbxt/pbxt-preload-test-bug

« back to all changes in this revision

Viewing changes to pbxt/src/datadic_xt.cc

  • Committer: paul-mccullagh
  • Date: 2008-03-10 11:36:34 UTC
  • Revision ID: paul-mccullagh-417ebf175a9c8ee6e5b3777d9e2398e1fb197391
Implemented full durability

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2005 SNAP Innovation GmbH
 
1
/* Copyright (c) 2005 PrimeBase Technologies GmbH
2
2
 *
3
3
 * PrimeBase XT
4
4
 *
1400
1400
                XTTableHPtr     tab;
1401
1401
 
1402
1402
                /* Find the table... */
1403
 
                pushsr_(tab, xt_heap_release, xt_use_table(self, tab_name, TRUE));
 
1403
                pushsr_(tab, xt_heap_release, xt_use_table(self, tab_name, FALSE, TRUE));
1404
1404
 
1405
1405
                /* Clone the foreign key definitions: */
1406
1406
                if (tab && tab->tab_dic.dic_table) {
1741
1741
        xtBool                          no_null = TRUE;
1742
1742
        XTOpenTablePtr          ot;
1743
1743
        XTIdxSearchKeyRec       search_key;
1744
 
        xtWord8                         xn_id;
 
1744
        xtXactID                        xn_id;
1745
1745
        int                                     action = after_buf ? tr_fkey->fk_on_update : tr_fkey->fk_on_delete;
1746
1746
        u_int                           after_key_len = 0;
1747
1747
        xtWord1                         *after_key = NULL;
1756
1756
                return false;
1757
1757
 
1758
1758
        search_key.sk_key_value.sv_flags = 0;
1759
 
        search_key.sk_key_value.sv_record = 0;
 
1759
        search_key.sk_key_value.sv_rec_id = 0;
 
1760
        search_key.sk_key_value.sv_row_id = 0;
1760
1761
        search_key.sk_key_value.sv_key = search_key.sk_key_buf;
1761
1762
        search_key.sk_key_value.sv_length = myxt_create_foreign_key_from_row(loc_ind, search_key.sk_key_buf, before_buf, ind, &no_null);
1762
1763
        search_key.sk_on_key = FALSE;
1765
1766
                return true;
1766
1767
 
1767
1768
        if (after_buf) {
1768
 
                if (!(after_key = (xtWord1 *) xt_sys_malloc(XT_IDX_MAX_KEY_SIZE)))
 
1769
                if (!(after_key = (xtWord1 *) xt_malloc_ns(XT_INDEX_MAX_KEY_SIZE)))
1769
1770
                        return false;
1770
1771
                after_key_len = myxt_create_foreign_key_from_row(loc_ind, after_key, after_buf, ind, NULL);
1771
1772
                
1779
1780
        }
1780
1781
 
1781
1782
        /* Search for the key in the child (referencing) table: */
1782
 
        if (!(ot = xt_open_table_from_pool(tr_fkey->co_table->dt_table, thread)))
 
1783
        if (!(ot = xt_db_open_table_using_tab(tr_fkey->co_table->dt_table, thread)))
1783
1784
                goto failed;
1784
1785
 
1785
1786
        retry:
1786
1787
        if (!xt_idx_search(ot, ind, &search_key))
1787
1788
                goto failed_2;
1788
1789
                
1789
 
        while (ot->ot_curr_rec && search_key.sk_on_key) {
1790
 
                switch (xt_tab_maybe_committed(ot, ot->ot_curr_rec, &xn_id, &ot->ot_curr_row_id, &ot->ot_curr_updated)) {
 
1790
        while (ot->ot_curr_rec_id && search_key.sk_on_key) {
 
1791
                switch (xt_tab_maybe_committed(ot, ot->ot_curr_rec_id, &xn_id, &ot->ot_curr_row_id, &ot->ot_curr_updated)) {
1791
1792
                        case XT_MAYBE:
1792
 
                                if (!xt_xn_wait_for_xact(thread, xn_id))
 
1793
                                if (!xt_xn_wait_for_xact(thread, xn_id, FALSE))
1793
1794
                                        goto failed_2;
1794
1795
                                goto retry;
1795
1796
                        case XT_ERR:
1800
1801
                                        case XT_KEY_ACTION_CASCADE:
1801
1802
                                                if (after_buf) {
1802
1803
                                                        /* Do a cascaded update: */
1803
 
                                                        if (!xt_tab_load_record(ot, ot->ot_curr_rec, &after_info))
 
1804
                                                        if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &after_info))
1804
1805
                                                                goto failed_2;
1805
1806
 
1806
1807
                                                        if (!myxt_create_row_from_key(ot, ind, after_key, after_key_len, after_info.ib_db.db_data))
1820
1821
                                                }
1821
1822
                                                break;
1822
1823
                                        case XT_KEY_ACTION_SET_NULL:
1823
 
                                                if (!xt_tab_load_record(ot, ot->ot_curr_rec, &after_info))
 
1824
                                                if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &after_info))
1824
1825
                                                        goto failed_2;
1825
1826
 
1826
1827
                                                myxt_set_null_row_from_key(ot, ind, after_info.ib_db.db_data);
1841
1842
        }
1842
1843
 
1843
1844
        /* No matching children, all OK: */
1844
 
        xt_return_table_to_pool(ot);
 
1845
        xt_db_return_table_to_pool_ns(ot);
1845
1846
 
1846
1847
        success:
1847
1848
        xt_ib_free(NULL, &after_info);
1848
1849
        if (after_key)
1849
 
                xt_sys_free(after_key);
 
1850
                xt_free_ns(after_key);
1850
1851
        return true;
1851
1852
 
1852
1853
        failed_2:
1853
 
        xt_return_table_to_pool(ot);
 
1854
        xt_db_return_table_to_pool_ns(ot);
1854
1855
 
1855
1856
        failed:
1856
1857
        xt_ib_free(NULL, &after_info);
1857
1858
        if (after_key)
1858
 
                xt_sys_free(after_key);
 
1859
                xt_free_ns(after_key);
1859
1860
        return false;
1860
1861
}
1861
1862
 
1870
1871
        if (!tr_fkey->getIndexPtr())
1871
1872
                throw_();
1872
1873
 
1873
 
        if (!(ot = xt_open_table_from_pool(tr_fkey->co_table->dt_table, self)))
 
1874
        if (!(ot = xt_db_open_table_using_tab(tr_fkey->co_table->dt_table, self)))
1874
1875
                throw_();
1875
1876
 
1876
 
        row_count = ((xtInt8) ot->ot_table->tab_row_eof >> XT_TAB_ROW_SHIFTS) - 1;
 
1877
        row_count = ((xtInt8) ot->ot_table->tab_row_eof_id) - 1;
1877
1878
        row_count -= (xtInt8) ot->ot_table->tab_row_fnum;
1878
1879
 
1879
 
        xt_return_table_to_pool(ot);
 
1880
        xt_db_return_table_to_pool_ns(ot);
1880
1881
 
1881
1882
        if (row_count > 0)
1882
1883
                xt_throw_ixterr(XT_CONTEXT, XT_ERR_ROW_IS_REFERENCED, tr_fkey->co_name);
2059
2060
        xtBool                          no_null = TRUE;
2060
2061
        XTOpenTablePtr          ot;
2061
2062
        XTIdxSearchKeyRec       search_key;
2062
 
        xtWord8                         xn_id;
 
2063
        xtXactID                        xn_id;
2063
2064
 
2064
2065
        /* This lock ensures that the foreign key references are not
2065
2066
         * changed.
2073
2074
                goto failed;
2074
2075
 
2075
2076
        search_key.sk_key_value.sv_flags = 0;
2076
 
        search_key.sk_key_value.sv_record = 0;
 
2077
        search_key.sk_key_value.sv_rec_id = 0;
 
2078
        search_key.sk_key_value.sv_row_id = 0;
2077
2079
        search_key.sk_key_value.sv_key = search_key.sk_key_buf;
2078
2080
        search_key.sk_key_value.sv_length = myxt_create_foreign_key_from_row(loc_ind, search_key.sk_key_buf, rec_buf, ind, &no_null);
2079
2081
        search_key.sk_on_key = FALSE;
2083
2085
 
2084
2086
        if (before_buf) {
2085
2087
                u_int   before_key_len;
2086
 
                xtWord1 before_key[XT_IDX_MAX_KEY_SIZE];
 
2088
                xtWord1 before_key[XT_INDEX_MAX_KEY_SIZE];
2087
2089
 
2088
2090
                /* If there is a before buffer, this insert was an update, so check
2089
2091
                 * if the key value has changed. If not, we need not do anything.
2099
2101
        }
2100
2102
 
2101
2103
        /* Search for the key in the parent (referenced) table: */
2102
 
        if (!(ot = xt_open_table_from_pool(fk_ref_table->dt_table, thread)))
 
2104
        if (!(ot = xt_db_open_table_using_tab(fk_ref_table->dt_table, thread)))
2103
2105
                goto failed;
2104
2106
 
2105
2107
        retry:
2106
2108
        if (!xt_idx_search(ot, ind, &search_key))
2107
2109
                goto failed_2;
2108
2110
                
2109
 
        while (ot->ot_curr_rec) {
 
2111
        while (ot->ot_curr_rec_id) {
2110
2112
                if (!search_key.sk_on_key)
2111
2113
                        break;
2112
2114
 
2113
 
                switch (xt_tab_maybe_committed(ot, ot->ot_curr_rec, &xn_id, &ot->ot_curr_row_id, &ot->ot_curr_updated)) {
 
2115
                switch (xt_tab_maybe_committed(ot, ot->ot_curr_rec_id, &xn_id, &ot->ot_curr_row_id, &ot->ot_curr_updated)) {
2114
2116
                        case XT_MAYBE:
2115
2117
                                /* We should not get a deadlock here because the thread
2116
2118
                                 * that we are waiting for, should not doing
2117
2119
                                 * data definition (i.e. should not be trying to
2118
2120
                                 * get an exclusive lock on dt_ref_lock.
2119
2121
                                 */
2120
 
                                if (!xt_xn_wait_for_xact(thread, xn_id))
 
2122
                                if (!xt_xn_wait_for_xact(thread, xn_id, FALSE))
2121
2123
                                        goto failed_2;
2122
2124
                                goto retry;                     
2123
2125
                        case XT_ERR:
2124
2126
                                goto failed_2;
2125
2127
                        case TRUE:
2126
2128
                                /* We found a matching parent: */
2127
 
                                xt_return_table_to_pool(ot);
 
2129
                                xt_db_return_table_to_pool_ns(ot);
2128
2130
                                goto success;
2129
2131
                        case FALSE:
2130
2132
                                if (!xt_idx_next(ot, ind, &search_key))
2136
2138
        xt_register_ixterr(XT_REG_CONTEXT, XT_ERR_NO_REFERENCED_ROW, co_name);
2137
2139
 
2138
2140
        failed_2:
2139
 
        xt_return_table_to_pool(ot);
 
2141
        xt_db_return_table_to_pool_ns(ot);
2140
2142
 
2141
2143
        failed:
2142
2144
        xt_rwlock_unlock(&co_table->dt_ref_lock);
2348
2350
/*
2349
2351
 * This function assumes the database table list is locked!
2350
2352
 */
2351
 
void XTDDTable::attachReferences(XTThreadPtr self)
 
2353
void XTDDTable::attachReferences(XTThreadPtr self, XTDatabaseHPtr db)
2352
2354
{
2353
2355
        XTDDForeignKey  *fk;
2354
2356
        XTTableHPtr             tab;
2360
2362
                fk = dt_fkeys.itemAt(i);
2361
2363
                fk->removeReference(self);
2362
2364
 
2363
 
                if (!(tab = xt_use_table_no_lock(self, self->st_database, fk->fk_ref_tab_name, TRUE, FALSE, NULL))) {
 
2365
                if (!(tab = xt_use_table_no_lock(self, db, fk->fk_ref_tab_name, TRUE, FALSE, NULL))) {
2364
2366
                        /* CHeck if the table references itself! */
2365
2367
                        if (xt_tab_compare_names(fk->fk_ref_tab_name, this->dt_table->tab_name) == 0) {
2366
2368
                                tab = this->dt_table;
2457
2459
                }
2458
2460
 
2459
2461
                /* Currently I allow foreign keys to be created on tables that do not yet exist!
2460
 
                pushsr_(tab, xt_heap_release, xt_use_table(self, fk->fk_ref_tab_name, FALSE));
 
2462
                pushsr_(tab, xt_heap_release, xt_use_table(self, fk->fk_ref_tab_name, FALSE FALSE));
2461
2463
                if ((dt = tab->tab_dic.dic_table))
2462
2464
                        dt->checkForeignKeyReference(self, fk);
2463
2465
                freer_(); // xt_heap_release(tab)
2523
2525
 
2524
2526
        rec_buf.ib_free = FALSE;
2525
2527
        if (!rec_ptr) {
2526
 
                if (!xt_tab_load_record(ot, ot->ot_curr_rec, &rec_buf))
 
2528
                if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &rec_buf))
2527
2529
                        return false;
2528
2530
                rec_ptr = rec_buf.ib_db.db_data;
2529
2531
                
2549
2551
 
2550
2552
        rec_buf.ib_free = FALSE;
2551
2553
        if (!rec_ptr) {
2552
 
                if (!xt_tab_load_record(ot, ot->ot_curr_rec, &rec_buf))
 
2554
                if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &rec_buf))
2553
2555
                        return false;
2554
2556
                rec_ptr = rec_buf.ib_db.db_data;
2555
2557
                
2614
2616
        xt_rwlock_rdlock(&dt_ref_lock);
2615
2617
        if ((tr = dt_trefs)) {
2616
2618
                if (!before) {
2617
 
                        if (!xt_tab_load_record(ot, ot->ot_curr_rec, &before_buf))
 
2619
                        if (!xt_tab_load_record(ot, ot->ot_curr_rec_id, &before_buf))
2618
2620
                                return false;
2619
2621
                        before = before_buf.ib_db.db_data;
2620
2622
                }