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

« back to all changes in this revision

Viewing changes to pbxt/src/table_xt.h

  • 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
 *
29
29
#include "index_xt.h"
30
30
#include "util_xt.h"
31
31
#include "heap_xt.h"
 
32
#include "tabcache_xt.h"
 
33
#include "xactlog_xt.h"
 
34
#include "lock_xt.h"
32
35
 
33
36
struct XTDatabase;
34
37
struct XTThread;
35
38
struct XTCache;
36
39
struct XTOpenTable;
37
40
 
38
 
#define XT_TAB_INCOMPATIBLE_VERSION     3
39
 
#define XT_TAB_CURRENT_VERSION          4
 
41
#define XT_TAB_INCOMPATIBLE_VERSION     4
 
42
#define XT_TAB_CURRENT_VERSION          5
40
43
 
41
44
#define XT_IND_CURRENT_VERSION          1
42
45
 
43
46
#define XT_HEAD_BUFFER_SIZE                     1024
44
47
 
45
 
 
46
48
/*
47
49
 * NOTE: Records may only be freed (placed on the free list), after
48
50
 * all currently running transactions have ended.
59
61
#define XT_TAB_STATUS_FREED                     0x00                    /* On the free list. */
60
62
#define XT_TAB_STATUS_DELETE            0x01                    /* A transactional delete record (an "update" that indicates a delete). */
61
63
#define XT_TAB_STATUS_FIXED                     0x02
62
 
#define XT_TAB_STATUS_VARIABLE          0x03
63
 
#define XT_TAB_STATUS_EXTENDED          0x04
 
64
#define XT_TAB_STATUS_VARIABLE          0x03                    /* Uses one block, but has the variable format. */
 
65
#define XT_TAB_STATUS_EXT_DLOG          0x04                    /* Variable format, and the trailing part of the record in the data log. */
 
66
#define XT_TAB_STATUS_EXT_HDATA         0x05                    /* Variable format, and the trailing part of the record in the handle data file. */
 
67
#define XT_TAB_STATUS_DATA                      0x06                    /* A block of data with a next pointer (5 bytes overhead). */
 
68
#define XT_TAB_STATUS_END_DATA          0x07                    /* An block of data without an end pointer (1 byte overhead). */
 
69
#define XT_TAB_STATUS_MASK                      0x0F
64
70
 
65
71
#define XT_TAB_STATUS_DEL_CLEAN         (XT_TAB_STATUS_DELETE | XT_TAB_STATUS_CLEANED_BIT)
66
72
#define XT_TAB_STATUS_FIX_CLEAN         (XT_TAB_STATUS_FIXED | XT_TAB_STATUS_CLEANED_BIT)
67
73
#define XT_TAB_STATUS_VAR_CLEAN         (XT_TAB_STATUS_VARIABLE | XT_TAB_STATUS_CLEANED_BIT)
68
 
#define XT_TAB_STATUS_EXT_CLEAN         (XT_TAB_STATUS_EXTENDED | XT_TAB_STATUS_CLEANED_BIT)
 
74
#define XT_TAB_STATUS_EXT_CLEAN         (XT_TAB_STATUS_EXT_DLOG | XT_TAB_STATUS_CLEANED_BIT)
69
75
 
70
76
#define XT_TAB_STATUS_CLEANED_BIT       0x80                    /* This bit is set when the record is cleaned and committed. */
71
 
#define XT_TAB_STATUS_REMOVED_BIT       0x40                    /* This bit is set when the record is removed which means
72
 
                                                                                                         * associated data and index references have been removed,
73
 
                                                                                                         * but the record has not yet been added to the free list.
74
 
                                                                                                         */
75
77
 
76
78
#define XT_REC_IS_CLEAN(x)                      ((x) & XT_TAB_STATUS_CLEANED_BIT)
77
 
#define XT_REC_IS_REMOVED(x)            ((x) & XT_TAB_STATUS_REMOVED_BIT)
78
 
#define XT_REC_NOT_VISIBLE(x)           ((x) <= XT_TAB_STATUS_DELETE || XT_REC_IS_REMOVED(x))
79
 
#define XT_TAB_IS_DELETED(x)            ((x) == XT_TAB_STATUS_FREED || XT_REC_IS_REMOVED(x))
80
 
 
81
 
#define XT_ROW_LOCK_TABLE_SIZE          221
 
79
#define XT_REC_IS_FREE(x)                       (((x) & XT_TAB_STATUS_MASK) == XT_TAB_STATUS_FREED)
 
80
#define XT_REC_IS_DELETE(x)                     (((x) & XT_TAB_STATUS_MASK) == XT_TAB_STATUS_DELETE)
 
81
#define XT_REC_IS_FIXED(x)                      (((x) & XT_TAB_STATUS_MASK) == XT_TAB_STATUS_FIXED)
 
82
#define XT_REC_IS_VARIABLE(x)           (((x) & XT_TAB_STATUS_MASK) == XT_TAB_STATUS_VARIABLE)
 
83
#define XT_REC_IS_EXT_DLOG(x)           (((x) & XT_TAB_STATUS_MASK) == XT_TAB_STATUS_EXT_DLOG)
 
84
#define XT_REC_IS_EXT_HDATA(x)          (((x) & XT_TAB_STATUS_MASK) == XT_TAB_STATUS_EXT_HDATA)
 
85
#define XT_REC_NOT_VALID(x)                     (XT_REC_IS_FREE(x) || XT_REC_IS_DELETE(x))
82
86
 
83
87
/* Results for xt_use_table_by_id(): */
84
88
#define XT_TAB_OK                                       0
85
89
#define XT_TAB_NOT_FOUND                        1
86
90
#define XT_TAB_NO_DICTIONARY            2
87
91
#define XT_TAB_POOL_CLOSED                      3                               /* Cannot open table at the moment, the pool is closed. */
 
92
#define XT_TAB_FAILED                           4
88
93
 
89
94
/* ------- TABLE DATA FILE ------- */
90
95
 
93
98
/*
94
99
 * This header ensures that no record in the data file has the offset 0.
95
100
 */
96
 
typedef struct XTTabDataHead {
97
 
        XTDiskValue4                    dh_magic_4;
98
 
        XTDiskValue4                    dh_reserved_4;
99
 
} XTTabDataHeadDRec, *XTTabDataHeadDPtr;
 
101
typedef struct XTTableHead {
 
102
        XTDiskValue4                    th_head_size_4;                                                 /* The size of the table header. */
 
103
        XTDiskValue4                    th_op_seq_4;
 
104
        XTDiskValue6                    th_row_free_6;
 
105
        XTDiskValue6                    th_row_eof_6;
 
106
        XTDiskValue6                    th_row_fnum_6;
 
107
        XTDiskValue6                    th_rec_free_6;
 
108
        XTDiskValue6                    th_rec_eof_6;
 
109
        XTDiskValue6                    th_rec_fnum_6;
 
110
} XTTableHeadDRec, *XTTableHeadDPtr;
 
111
 
 
112
#define XT_FORMAT_DEF_SPACE             512
 
113
 
 
114
typedef struct XTTableFormat {
 
115
        XTDiskValue4                    tf_format_size_4;                                               /* The size of this structure (table format). */
 
116
        XTDiskValue4                    tf_tab_head_size_4;                                                     /* The offset of the first record in the data handle file. */
 
117
        XTDiskValue2                    tf_tab_version_2;                                               /* The table version number. */
 
118
        XTDiskValue2                    tf_unused_2;                                                    /* - */
 
119
        XTDiskValue4                    tf_rec_size_4;                                                  /* The maximum size of records in the table. */
 
120
        XTDiskValue1                    tf_rec_fixed_1;                                                 /* Set to 1 if this table contains fixed length records. */
 
121
        XTDiskValue1                    tf_reserved_1;                                                  /* - */
 
122
        XTDiskValue8                    tf_min_auto_inc_8;                                              /* This is the minimum auto-increment value. */
 
123
        xtWord1                                 tf_reserved[64];                                                /* Reserved, set to 0. */
 
124
        char                                    tf_definition[XT_VAR_LENGTH];                   /* A cstring, currently it only contains the foreign key information. */
 
125
} XTTableFormatDRec, *XTTableFormatDPtr;
100
126
 
101
127
#define XT_STAT_ID_MASK(x)      ((x) & (u_int) 0x000000FF)
102
128
 
103
129
/* A record that fits completely in the data file record */
104
130
typedef struct XTTabRecHead {
105
 
        xtWord1                                 tr_rec_type_1;                  /* 0 means deleted. */
 
131
        xtWord1                                 tr_rec_type_1;
106
132
        xtWord1                                 tr_stat_id_1;
107
 
        XTDiskValue6                    tr_xact_id_6;                   /* The transaction ID. */
108
 
        XTDiskValue6                    tr_prev_var_6;                  /* The previous variation of this record. */
 
133
        xtDiskRecordID4                 tr_prev_rec_id_4;               /* The previous variation of this record. */
 
134
        XTDiskValue4                    tr_xact_id_4;                   /* The transaction ID. */
109
135
        XTDiskValue4                    tr_row_id_4;                    /* The row ID of this record. */
110
136
} XTTabRecHeadDRec, *XTTabRecHeadDPtr;
111
137
 
112
138
typedef struct XTTabRecFix {
113
 
        xtWord1                                 tr_rec_type_1;                  /* 0 means deleted. */
 
139
        xtWord1                                 tr_rec_type_1;                  /* XT_TAB_STATUS_FREED, XT_TAB_STATUS_DELETE,
 
140
                                                                                                         * XT_TAB_STATUS_FIXED, XT_TAB_STATUS_VARIABLE */
114
141
        xtWord1                                 tr_stat_id_1;
115
 
        XTDiskValue6                    tr_xact_id_6;                   /* The transaction ID. */
116
 
        XTDiskValue6                    tr_prev_var_6;                  /* The previous variation of this record. */
 
142
        xtDiskRecordID4                 tr_prev_rec_id_4;               /* The previous variation of this record. */
 
143
        XTDiskValue4                    tr_xact_id_4;                   /* The transaction ID. */
117
144
        XTDiskValue4                    tr_row_id_4;                    /* The row ID of this record. */
118
145
        xtWord1                                 rf_data[XT_VAR_LENGTH]; /* NOTE: This data is in RAW MySQL format. */
119
146
} XTTabRecFixDRec, *XTTabRecFixDPtr;
120
147
 
121
148
/* An extended record that overflows into the log file: */
122
149
typedef struct XTTabRecExt {
123
 
        xtWord1                                 tr_rec_type_1;
 
150
        xtWord1                                 tr_rec_type_1;                  /* XT_TAB_STATUS_EXT_DLOG */
124
151
        xtWord1                                 tr_stat_id_1;
125
 
        XTDiskValue6                    tr_xact_id_6;                   /* The transaction ID. */
126
 
        XTDiskValue6                    tr_prev_var_6;                  /* The previous variation of this record. */
 
152
        xtDiskRecordID4                 tr_prev_rec_id_4;               /* The previous variation of this record. */
 
153
        XTDiskValue4                    tr_xact_id_4;                   /* The transaction ID. */
127
154
        XTDiskValue4                    tr_row_id_4;                    /* The row ID of this record. */
128
 
        XTDiskValue6                    re_log_rec_6;                   /* Reference to the overflow area in a log file. */
 
155
        XTDiskValue2                    re_log_id_2;                    /* Reference to overflow area, log ID */
 
156
        XTDiskValue6                    re_log_offs_6;                  /* Reference to the overflow area, log offset */
129
157
        XTDiskValue4                    re_log_dat_siz_4;               /* Size of the overflow data. */
130
158
        xtWord1                                 re_data[XT_VAR_LENGTH]; /* This data is in packed PBXT format. */
131
159
} XTTabRecExtDRec, *XTTabRecExtDPtr;
132
160
 
 
161
typedef struct XTTabRecExtHdat {
 
162
        xtWord1                                 tr_rec_type_1;                  /* XT_TAB_STATUS_EXT_HDATA */
 
163
        xtWord1                                 tr_stat_id_1;
 
164
        xtDiskRecordID4                 tr_prev_rec_id_4;               /* The previous variation of this record. */
 
165
        XTDiskValue4                    tr_xact_id_4;                   /* The transaction ID. */
 
166
        XTDiskValue4                    tr_row_id_4;                    /* The row ID of this record. */
 
167
        XTDiskValue4                    eh_blk_rec_id_4;                /* The record ID of the next block. */
 
168
        XTDiskValue2                    eh_blk_siz_2;                   /* The total size of the data in the trailing blocks */
 
169
        xtWord1                                 eh_data[XT_VAR_LENGTH]; /* This data is in packed PBXT format. */
 
170
} XTTabRecExtHdatDRec, *XTTabRecExtHdatDPtr;
 
171
 
 
172
typedef struct XTTabRecData {
 
173
        xtWord1                                 tr_rec_type_1;                  /* XT_TAB_STATUS_DATA */
 
174
        XTDiskValue4                    rd_blk_rec_id_4;                /* The record ID of the next block. */
 
175
        xtWord1                                 rd_data[XT_VAR_LENGTH]; /* This data is in packed PBXT format. */
 
176
} XTTabRecDataDRec, *XTTabRecDataDPtr;
 
177
 
 
178
typedef struct XTTabRecEndDat {
 
179
        xtWord1                                 tr_rec_type_1;                  /* XT_TAB_STATUS_END_DATA */
 
180
        xtWord1                                 ed_data[XT_VAR_LENGTH]; /* This data is in packed PBXT format. */
 
181
} XTTabRecEndDatDRec, *XTTabRecEndDatDPtr;
 
182
 
133
183
#define XT_REC_FIX_HEADER_SIZE          sizeof(XTTabRecHeadDRec)
134
184
#define XT_REC_EXT_HEADER_SIZE          offsetof(XTTabRecExtDRec, re_data)
135
185
#define XT_REC_FIX_EXT_HEADER_DIFF      (XT_REC_EXT_HEADER_SIZE - XT_REC_FIX_HEADER_SIZE)
136
186
 
137
187
typedef struct XTTabRecFree {
138
188
        xtWord1                                 tr_rec_type_1;
139
 
        xtWord1                                 tr_stat_id_1;
140
 
        XTDiskValue6                    rf_next_block_6;                /* The next block on the free list. */
 
189
        xtWord1                                 rf_not_used_1;
 
190
        xtDiskRecordID4                 rf_next_rec_id_4;               /* The next block on the free list. */
141
191
} XTTabRecFreeDRec, *XTTabRecFreeDPtr;
142
192
 
143
193
typedef struct XTTabRecInfo {
144
194
        XTTabRecFixDPtr                 ri_fix_rec_buf;                 /* This references the start of the buffer (set for all types of records) */
145
195
        XTTabRecExtDPtr                 ri_ext_rec;                             /* This is only set for extended records. */
146
196
        xtWord4                                 ri_rec_buf_size;
147
 
        XTDataLogBufferDPtr             ri_log_buf;
 
197
        XTactExtRecEntryDPtr    ri_log_buf;
148
198
        xtWord4                                 ri_log_data_size;               /* This size of the data in the log record. */
149
 
        off_t                                   ri_rec_address;                 /* The address of the record. */
 
199
        xtRecordID                              ri_rec_id;                              /* The address of the record. */
150
200
} XTTabRecInfoRec, *XTTabRecInfoPtr;
151
201
 
152
202
/* ------- TABLE ROW FILE ------- */
153
203
 
154
 
#define XT_TAB_ROW_SHIFTS               3
 
204
#define XT_TAB_ROW_SHIFTS               2
155
205
#define XT_TAB_ROW_MAGIC                0x4567CDEF
156
 
#define XT_TAB_ROW_FREE                 0
157
 
#define XT_TAB_ROW_IN_USE               1
 
206
//#define XT_TAB_ROW_FREE                       0
 
207
//#define XT_TAB_ROW_IN_USE             1
158
208
 
159
209
/*
160
210
 * NOTE: The shift count assumes the size of a table row
161
 
 * reference is 8 bytes
 
211
 * reference is 8 bytes (XT_TAB_ROW_SHIFTS)
162
212
 */
163
213
typedef struct XTTabRowRef {
164
 
        xtWord1                                 rr_rec_type_1;                  /* 0 means free. */
165
 
        xtWord1                                 rr_unused_1;                    /* Currently unused (set to zero). */
166
 
        XTDiskValue6                    rr_variation_6;                 /* Pointer to the first variation. */
 
214
        XTDiskValue4                    rr_ref_id_4;                    /* 4-byte reference, could be a RowID or a RecordID
 
215
                                                                                                         * If this row is free, then it is a RowID, which
 
216
                                                                                                         * references the next free row.
 
217
                                                                                                         * If it is in use, then it is a RecordID which
 
218
                                                                                                         * points to the first record in the variation
 
219
                                                                                                         * list for the row.
 
220
                                                                                                         */
167
221
} XTTabRowRefDRec, *XTTabRowRefDPtr;
168
222
 
169
223
/*
170
224
 * This is the header for the row file. The size MUST be a
171
 
 * multiple of sizeof(XTTabRowRefDRec)
 
225
 * the same size as sizeof(XTTabRowRefDRec)
172
226
 */
173
227
typedef struct XTTabRowHead {
174
228
        XTDiskValue4                    rh_magic_4;
175
 
        XTDiskValue4                    rh_reserved_4;
176
229
} XTTabRowHeadDRec, *XTTabRowHeadDPtr;
177
230
 
178
231
/* ------- TABLE INDEX FILE ------- */
179
232
 
180
 
typedef struct XTTabPointers {
181
 
        XTDiskValue4                    tp_head_size_4;
182
 
 
183
 
        /* These fields reference row IDs (i.e. offset = ID * sizeof(XTTabRowRefRec), zero is invalid!): */
184
 
        XTDiskValue4                    tp_tab_eof_4;                                                   
185
 
        XTDiskValue4                    tp_tab_free_4;                  /* Free list of records. */
186
 
        XTDiskValue4                    tp_tab_fnum_4;                  /* The number of records on the free list. */
187
 
 
188
 
        XTDiskValue6                    tp_data_eof_6;
189
 
        XTDiskValue6                    tp_data_free_6;
190
 
        XTDiskValue4                    tp_data_fnum_4;                 /* The number of data records on the free list. */
 
233
typedef struct XTIndexHead {
 
234
        XTDiskValue4                    tp_head_size_4;                                                 /* The size of the index header. */
 
235
 
 
236
        XTDiskValue4                    tp_rec_log_id_4;                                                /* Index recovery start point. */
 
237
        XTDiskValue6                    tp_rec_log_offs_6;
191
238
 
192
239
        XTDiskValue6                    tp_ind_eof_6;
193
240
        XTDiskValue6                    tp_ind_free_6;
194
241
 
195
 
        /* The index roots follow. Each is tf_node_ref_size_1 size. */
 
242
        /* The index roots follow. Each is if_node_ref_size_1 size. */
196
243
        xtWord1                                 tp_data[XT_VAR_LENGTH];
197
 
} XTTabPointersDRec, *XTTabPointersDPtr;
 
244
} XTIndexHeadDRec, *XTIndexHeadDPtr;
198
245
 
199
 
typedef struct XTTabFormat {
200
 
        XTDiskValue4                    tf_format_size_4;                                               /* The size of this structure. */
201
 
        XTDiskValue2                    tf_tab_version_2;                                               /* The table version number. */
202
 
        XTDiskValue2                    tf_ind_version_2;                                               /* The index version number. */
203
 
        XTDiskValue1                    tf_node_ref_size_1;                                             /* This size of index node reference in indexes (default 6 bytes). */
204
 
        XTDiskValue1                    tf_rec_ref_size_1;                                              /* The size of record references in the indexes (default 4 bytes). */
205
 
        XTDiskValue4                    tf_rec_size_4;                                                  /* The maximum size of records in the table. */
206
 
        XTDiskValue1                    tf_rec_fixed_1;                                                 /* Set to 1 if this table contains fixed length records. */
207
 
        XTDiskValue8                    tf_min_auto_inc_8;                                              /* This is the minimum auto-increment value. */
208
 
        XTDiskValue1                    tf_no_of_data_logs_2;                                   /* The number of data logs (currently fixed at 4). */
209
 
        xtWord1                                 tf_reserved[64];                                                /* Reserved, set to 0. */
210
 
        char                                    tf_definition[XT_VAR_LENGTH];                   /* A cstring, currently it only contains the foreign key information. */
211
 
} XTTabFormatDRec, *XTTabFormatDPtr;
 
246
typedef struct XTIndexFormat {
 
247
        XTDiskValue4                    if_format_size_4;                                               /* The size of this structure (index format). */
 
248
        XTDiskValue2                    if_tab_version_2;                                               /* The table version number. */
 
249
        XTDiskValue2                    if_ind_version_2;                                               /* The index version number. */
 
250
        XTDiskValue1                    if_node_ref_size_1;                                             /* This size of index node reference in indexes (default 4 bytes). */
 
251
        XTDiskValue1                    if_rec_ref_size_1;                                              /* The size of record references in the indexes (default 4 bytes). */
 
252
} XTIndexFormatDRec, *XTIndexFormatDPtr;
212
253
 
213
254
/* ------- TABLE & OPEN TABLES & TABLE LISTING ------- */
214
255
 
216
257
        struct XTDatabase               *tab_db;                        /* Heap pointer */
217
258
        char                                    *tab_name;
218
259
        xtBool                                  tab_free_locks;
219
 
        xtWord4                                 tab_id;
 
260
        xtTableID                               tab_id;
220
261
 
221
262
        xtWord8                                 tab_auto_inc;                                                   /* The next value to be issued as an auto-increment value. */
222
263
        xt_mutex_type                   tab_ainc_lock;                                                  /* Lock for the auto-increment counter. */
223
264
 
224
 
        size_t                                  tab_head_size;
225
 
        XTTabPointersDPtr               tab_pointers;
 
265
        size_t                                  tab_index_head_size;
 
266
        XTIndexHeadDPtr                 tab_index_head;
 
267
        size_t                                  tab_table_head_size;
226
268
        XTDictionaryRec                 tab_dic;
227
269
        xt_mutex_type                   dic_field_lock;                                                 /* Lock for setting field->ptr!. */
228
270
 
229
 
        xt_mutex_type                   tab_open_lock;                                                  /* Lock for the opening tables. */
230
 
        xt_cond_type                    tab_open_cond;                                                  /* Condition for waiting to open a table. */
231
 
        xtBool                                  tab_will_close;                                                 /* Set to TRUE when the table is to be closed */
232
 
        u_int                                   tab_open_count;                                                 /* Number of open tables. */
233
 
        struct XTOpenTable              *tab_open_pool;                                                 /* The open table pool. */
234
 
 
235
 
        //D XTTableSeqRec                       tab_seq;                                                                /* The table operation sequence. */
236
 
 
237
 
        char                                    *tab_row_file;
238
 
        off_t                                   tab_row_eof;                                                    /* Indicates the EOF of the table row file. */
239
 
        off_t                                   tab_row_free;                                                   /* The start of the free list in the table row file. */
 
271
        XTRowLocksRec                   tab_locks;                                                              /* The locks held on this table. */
 
272
 
 
273
        XTTableSeqRec                   tab_seq;                                                                /* The table operation sequence. */
 
274
        XTTabCacheRec                   tab_rows;
 
275
        XTTabCacheRec                   tab_recs;
 
276
 
 
277
        /* Used to apply operations to the database in order. */
 
278
        XTSortedListPtr                 tab_op_list;                                                    /* The operation list. Operations to be applied. */
 
279
        /* Values that belong in the header when flushed! */
 
280
        xtBool                                  tab_flush_pending;                                              /* TRUE if the table needs to be flushed */
 
281
        xtOpSeqNo                               tab_head_op_seq;                                                /* The number of the operation last applied to the database. */
 
282
        xtRowID                                 tab_head_row_free_id;
 
283
        xtRowID                                 tab_head_row_eof_id;
 
284
        xtWord4                                 tab_head_row_fnum;
 
285
        xtRecordID                              tab_head_rec_free_id;
 
286
        xtRecordID                              tab_head_rec_eof_id;
 
287
        xtWord4                                 tab_head_rec_fnum;
 
288
 
 
289
        xtOpSeqNo                               tab_co_op_seq;                                                  /* The operation last applied by the compactor. */
 
290
 
 
291
        xtBool                                  tab_wr_wake_freeer;                                             /* Set to TRUE if the writer must wake the freeer. */
 
292
        xtOpSeqNo                               tab_wake_freeer_op;                                             /* Set to the sequence number the freeer is waiting for. */
 
293
 
 
294
        XTFilePtr                               tab_row_file;
 
295
        xtRowID                                 tab_row_eof_id;                                                 /* Indicates the EOF of the table row file. */
 
296
        xtRowID                                 tab_row_free_id;                                                /* The start of the free list in the table row file. */
240
297
        xtWord4                                 tab_row_fnum;                                                   /* The count of the number of free rows on the free list. */
241
298
        xt_mutex_type                   tab_row_lock;                                                   /* Lock for updating the EOF and free list. */
242
 
        xt_rwlock_type                  tab_row_locks[XT_ROW_LOCK_TABLE_SIZE];  /* Used to lock a row during update. */
243
 
 
244
 
        xtBool                                  tab_head_dirty;                                                 /* TRUE if index header data is updated. */
245
 
        char                                    *tab_ind_file;
 
299
        xt_rwlock_type                  tab_row_rwlock[XT_ROW_RWLOCKS];                 /* Used to lock a row during update. */
 
300
 
 
301
        XTFilePtr                               tab_rec_file;
 
302
        xtRecordID                              tab_rec_eof_id;                                                 /* This value can only grow. */
 
303
        xtRecordID                              tab_rec_free_id;
 
304
        xtWord4                                 tab_rec_fnum;                                                   /* The count of the number of free rows on the free list. */
 
305
        xt_mutex_type                   tab_rec_lock;                                                   /* Lock for the free list. */
 
306
 
 
307
        xtBool                                  tab_ind_flush_pending;                                  /* TRUE if some part of the index file has been written. */
 
308
        xtBool                                  tab_ind_head_dirty;                                             /* TRUE if index header data is updated. */
 
309
        xtLogID                                 tab_ind_rec_log_id;                                             /* The point before which index entries have been written. */
 
310
        xtLogOffset                             tab_ind_rec_log_offset;                                 /* The log offset of the write point. */
 
311
        XTFilePtr                               tab_ind_file;
246
312
        off_t                                   tab_ind_eof;                                                    /* This value can only grow. */
247
313
        off_t                                   tab_ind_free;
248
314
        xt_mutex_type                   tab_ind_lock;                                                   /* Lock for reading and writing the buffer. */
249
 
 
250
 
        xt_mutex_type                   tab_log_lock;                                                   /* Lock for the log list for adding, using and removing logs. */
251
 
        u_int                                   tab_log_curr;                                                   /* The index of the current log (write log). */
252
 
        XTDataLogRec                    tab_log_list[XT_NO_OF_DATA_LOGS];               /* Complete list of data logs for the table. */
253
 
 
254
 
        char                                    *tab_data_file;
255
 
        off_t                                   tab_data_eof;                                                   /* This value can only grow. */
256
 
        off_t                                   tab_data_free;
257
 
        xtWord4                                 tab_data_fnum;                                                  /* The count of the number of free rows on the free list. */
258
 
        xt_mutex_type                   tab_free_lock;                                                  /* Lock for the free list. */
259
 
 
260
 
        u_int                                   tab_buf_size;                                                   /* Size of tab_data_buf and ot_read_buf. */
261
 
        u_int                                   tab_buf_offset;                                                 /* Current write position in buffer. */
262
 
        xt_rwlock_type                  tab_buf_rwlock;                                                 /* Lock for reading and writing the buffer. */
263
 
        xtWord1                                 *tab_data_buf;                                                  /* The table log write buffer. */
264
315
} XTTableHRec, *XTTableHPtr;            /* Heap pointer */
265
316
 
266
317
/* Used for an in-memory list of the tables, ordered by ID. */
267
318
typedef struct XTTableEntry {
268
 
        xtWord4                                 te_tab_id;
 
319
        xtTableID                               te_tab_id;
269
320
        char                                    *te_tab_name;
270
321
        XTTableHPtr                             te_table;
271
322
} XTTableEntryRec, *XTTableEntryPtr;
274
325
        struct XTThread                 *ot_thread;                                                             /* The thread currently using this open table. */
275
326
        XTTableHPtr                             ot_table;                                                               /* PBXT table information. */
276
327
 
277
 
        struct XTOpenTable              *ot_pool_next;                                                  /* Next pointer for open table pool. */
278
 
 
279
 
        XTOpenFilePtr                   ot_data_file;
 
328
        struct XTOpenTable              *ot_otp_next_free;                                              /* Next free open table in the open table pool. */
 
329
        struct XTOpenTable              *ot_otp_mr_used;
 
330
        struct XTOpenTable              *ot_otp_lr_used;
 
331
 
 
332
        //struct XTOpenTable    *ot_pool_next;                                                  /* Next pointer for open table pool. */
 
333
 
 
334
        XTOpenFilePtr                   ot_rec_file;
280
335
        XTOpenFilePtr                   ot_ind_file;
281
336
        XTOpenFilePtr                   ot_row_file;
282
337
        u_int                                   ot_err_index_no;                                                /* The number of the index on which the last error occurred */
285
340
        size_t                                  ot_rec_size;                                                    /* Cached from table for quick access. */
286
341
        
287
342
        char                                    ot_error_key[XT_IDENTIFIER_NAME_SIZE];
 
343
        xtBool                                  ot_for_update;                                                  /* True if reading FOR UPDATE. */
 
344
        xtBool                                  ot_is_modify;                                                   /* True if UPDATE or DELETE. */
 
345
        xtRowID                                 ot_temp_row_lock;                                               /* The temporary row lock set on this table. */
 
346
        u_int                                   ot_cols_req;                                                    /* The number of columns required from the table. */
288
347
 
289
348
        /* GOTCHA: Separate buffers for reading and writing rows because
290
349
         * of blob references, to this buffer, as in this test:
310
369
        size_t                                  ot_row_wbuf_size;                                               /* The current size of the write row buffer (resized dynamically). */
311
370
        xtWord1                                 *ot_row_wbuffer;                                                /* The row buffer for writing rows. */
312
371
 
313
 
        XTOpenFilePtr                   ot_log_files[XT_NO_OF_DATA_LOGS];
314
 
        u_int                                   ot_log_curr;                                                    /* The index of the current log (write log). */
315
 
        off_t                                   ot_log_buf_offs;                                                /* The file offset of the log buffer. */
316
 
        u_int                                   ot_log_buf_len;                                                 /* Amount of data written to the log buffer. */
317
 
        xtWord1                                 *ot_log_buffer;                                                 /* The log buffer. */
318
 
        u_int                                   ot_log_buf_size;                                                /* The size of the log buffer. */
319
 
 
320
372
        /* Details of the current record: */
321
 
        off_t                                   ot_curr_rec;                                                    /* The offset of the current record. */
322
 
        xtWord4                                 ot_curr_row_id;                                                 /* The row ID of the current record. */
 
373
        xtRecordID                              ot_curr_rec_id;                                                 /* The offset of the current record. */
 
374
        xtRowID                                 ot_curr_row_id;                                                 /* The row ID of the current record. */
323
375
        xtBool                                  ot_curr_updated;                                                /* TRUE if the current record was updated by the current transaction. */
324
376
 
325
377
        /* GOTCHA: Separate buffers for reading and writing the index are required
329
381
        XTIdxItemRec                    ot_ind_state;                                                   /* Decribes the state of the index buffer. */
330
382
        XTIdxBranchDRec                 ot_ind_rbuf;                                                    /* The index read buffer. */
331
383
        XTIdxBranchDRec                 ot_ind_wbuf;                                                    /* Buffer for the current index node for writing. */
332
 
        xtWord1                                 ot_ind_wbuf2[XT_IDX_PAGE_SIZE];                 /* Overflow for the write buffer when a node is too big. */
 
384
        xtWord1                                 ot_ind_wbuf2[XT_INDEX_PAGE_SIZE];                       /* Overflow for the write buffer when a node is too big. */
333
385
 
334
 
        off_t                                   ot_seq_pos;                                                             /* Current position of a sequential scan. */
335
 
        off_t                                   ot_seq_eof;                                                             /* The EOF at the start of the sequential scan. */
336
 
        off_t                                   ot_buf_pos;                                                             /* The current read position of the buffer. */
337
 
        u_int                                   ot_buf_len;                                                             /* The length of the data in the buffer. */
338
 
        xtWord1                                 ot_read_buf[XT_VAR_LENGTH];                             /* The table sequential read buffer. */
 
386
        /* Note: the fields below ot_ind_rbuf are not zero'ed out on creation
 
387
         * of this structure!
 
388
         */
 
389
        xtRecordID                              ot_seq_rec_id;                                                  /* Current position of a sequential scan. */
 
390
        xtRecordID                              ot_seq_eof_id;                                                  /* The EOF at the start of the sequential scan. */
 
391
        XTTabCachePagePtr               ot_seq_page;                                                    /* If non-NULL, then a page has been locked! */
 
392
        size_t                                  ot_seq_offset;                                                  /* Offset on the current page. */
339
393
} XTOpenTableRec, *XTOpenTablePtr;
340
394
 
341
395
#define XT_DATABASE_NAME_SIZE           XT_IDENTIFIER_NAME_SIZE
342
396
 
343
397
typedef struct XTTableDesc {
344
398
        char                                    td_tab_name[XT_TABLE_NAME_SIZE+4];      // 4 extra for DEL# (tables being deleted)
345
 
        xtWord4                                 td_tab_id;
 
399
        xtTableID                               td_tab_id;
346
400
        char                                    *td_file_name;
347
401
        XTOpenDirPtr                    td_open_dir;
348
402
} XTTableDescRec, *XTTableDescPtr;
352
406
        int                                             ft_state;
353
407
        struct XTDatabase               *ft_db;
354
408
        char                                    *ft_tab_name;
355
 
        xtWord4                                 ft_tab_id;
 
409
        xtTableID                               ft_tab_id;
356
410
        char                                    ft_file_path[PATH_MAX];
357
411
} XTFilesOfTableRec, *XTFilesOfTablePtr;
358
412
 
364
418
char                            *xt_tab_file_to_name(size_t size, char *tab_name, char *file_name);
365
419
 
366
420
void                            xt_create_table(struct XTThread *self, char *name, XTDictionaryPtr dic, xtBool delete_if_exists);
367
 
XTTableHPtr                     xt_use_table(struct XTThread *self, char *name, xtBool missing_ok);
 
421
XTTableHPtr                     xt_use_table(struct XTThread *self, char *name, xtBool no_load, xtBool missing_ok);
 
422
void                            xt_flush_table(struct XTThread *self, XTOpenTablePtr ot);
368
423
XTTableHPtr                     xt_use_table_no_lock(XTThreadPtr self, struct XTDatabase *db, char *name, xtBool no_load, xtBool missing_ok, XTDictionaryPtr dic);
369
 
int                                     xt_use_table_by_id(struct XTThread *self, XTTableHPtr *tab, struct XTDatabase *db, xtWord4 tab_id);
 
424
int                                     xt_use_table_by_id(struct XTThread *self, XTTableHPtr *tab, struct XTDatabase *db, xtTableID tab_id);
370
425
XTOpenTablePtr          xt_open_table(XTTableHPtr tab);
371
 
XTOpenTablePtr          xt_open_table_by_name(struct XTThread *self, char *name);
372
426
void                            xt_close_table(XTOpenTablePtr ot);
373
 
xtBool                          xt_flush_table(XTOpenTablePtr ot);
 
427
xtBool                          xt_flush_table_index(XTOpenTablePtr ot);
374
428
void                            xt_drop_table(struct XTThread *self, char *name);
 
429
void                            xt_check_table(XTThreadPtr self, XTOpenTablePtr tab);
375
430
void                            xt_rename_table(struct XTThread *self, char *old_name, char *new_name);
376
431
void                            xt_move_table(struct XTThread *self, char *old_name, struct XTDatabase *new_db, char *new_name);
377
432
 
378
 
XTOpenTablePtr          xt_open_table_from_pool(XTTableHPtr tab, struct XTThread *thread);
379
 
void                            xt_return_table_to_pool(XTOpenTablePtr ot);
380
 
void                            xt_close_all_open_tables(XTThreadPtr self, XTTableHPtr tab);
381
 
 
382
433
void                            xt_describe_tables_init(struct XTThread *self, struct XTDatabase *db, XTTableDescPtr td);
383
434
xtBool                          xt_describe_tables_next(struct XTThread *self, XTTableDescPtr td);
384
435
void                            xt_describe_tables_exit(struct XTThread *self, XTTableDescPtr td);
388
439
void                            xt_enum_tables_init(u_int *edx);
389
440
XTTableEntryPtr         xt_enum_tables_next(struct XTThread *self, struct XTDatabase *db, u_int *edx);
390
441
 
391
 
void                            xt_enum_files_of_tables_init(struct XTDatabase *db, char *tab_name, xtWord4 tab_id, XTFilesOfTablePtr ft);
 
442
void                            xt_enum_files_of_tables_init(struct XTDatabase *db, char *tab_name, xtTableID tab_id, XTFilesOfTablePtr ft);
392
443
xtBool                          xt_enum_files_of_tables_next(XTFilesOfTablePtr ft);
393
444
 
394
445
xtBool                          xt_tab_seq_init(XTOpenTablePtr ot);
 
446
void                            xt_tab_seq_reset(XTOpenTablePtr ot);
 
447
void                            xt_tab_seq_exit(XTOpenTablePtr ot);
395
448
xtBool                          xt_tab_seq_next(XTOpenTablePtr ot, xtWord1 *buffer, xtBool *eof);
396
449
 
397
 
xtBool                          xt_tab_flush_data(XTOpenTablePtr ot);
398
450
xtBool                          xt_tab_new_record(XTOpenTablePtr ot, xtWord1 *buffer);
399
451
xtBool                          xt_tab_delete_record(XTOpenTablePtr ot, xtWord1 *buffer);
400
452
xtBool                          xt_tab_update_record(XTOpenTablePtr ot, xtWord1 *before_buf, xtWord1 *after_buf);
401
453
int                                     xt_tab_visible(XTOpenTablePtr ot);
402
454
int                                     xt_tab_read_record(register XTOpenTablePtr ot, xtWord1 *buffer);
403
 
xtBool                          xt_tab_load_record(register XTOpenTablePtr ot, off_t address, XTInfoBufferPtr rec_buf);
404
 
int                                     xt_tab_remove_record(XTOpenTablePtr ot, off_t address, xtWord1 *rec_data, off_t *prev_var, xtBool clean_delete);
405
 
int                                     xt_tab_maybe_committed(XTOpenTablePtr ot, off_t record, xtWord8 *tn_id, xtWord4 *out_rowid, xtBool *out_updated);
406
 
xtBool                          xt_tab_free_record(XTOpenTablePtr ot, off_t address);
407
 
 
408
 
xtBool                          xt_tab_get_row(register XTOpenTablePtr ot, xtWord4 row_id, off_t *variation);
409
 
xtBool                          xt_tab_set_row(XTOpenTablePtr ot, xtWord4 row_id, off_t variation, xtBool write_thru);
410
 
xtBool                          xt_tab_free_row(XTOpenTablePtr ot, XTTableHPtr tab, xtWord4 row_id);
411
 
 
412
 
xtBool                          xt_tab_put_data(XTOpenTablePtr ot, off_t address, u_int size, xtWord1 *buffer);
413
 
xtBool                          xt_tab_get_data(register XTOpenTablePtr ot, off_t address, u_int size, xtWord1 *buffer, u_int *red_size);
414
 
xtBool                          xt_tab_get_record(register XTOpenTablePtr ot, off_t address, u_int size, xtWord1 *buffer);
415
 
 
416
 
void                            xt_tab_load_index_roots(XTTableHPtr tab);
417
 
 
418
 
void                            xt_tab_get_stats(struct XTThread *self, XTTableHPtr tab, xtWord4 *file_size, xtWord4 *free_count);
419
 
void                            xt_tab_io_failed(XTOpenFilePtr of);
 
455
int                                     xt_tab_dirty_read_record(register XTOpenTablePtr ot, xtWord1 *buffer);
 
456
void                            xt_tab_load_row_pointers(XTThreadPtr self, XTOpenTablePtr ot);
 
457
xtBool                          xt_tab_load_record(register XTOpenTablePtr ot, xtRecordID rec_id, XTInfoBufferPtr rec_buf);
 
458
int                                     xt_tab_remove_record(XTOpenTablePtr ot, xtRecordID rec_id, xtWord1 *rec_data, xtRecordID *prev_var_rec_id, xtBool clean_delete, xtRowID row_id, xtXactID xn_id);
 
459
int                                     xt_tab_maybe_committed(XTOpenTablePtr ot, xtRecordID rec_id, xtXactID *xn_id, xtRowID *out_rowid, xtBool *out_updated);
 
460
xtBool                          xt_tab_free_record(XTOpenTablePtr ot, u_int status, xtRecordID rec_id, xtBool clean_delete);
 
461
void                            xt_tab_store_header(struct XTThread *self, XTOpenTablePtr ot);
 
462
 
 
463
xtBool                          xt_tab_get_row(register XTOpenTablePtr ot, xtRowID row_id, xtRecordID *var_rec_id);
 
464
xtBool                          xt_tab_set_row(XTOpenTablePtr ot, u_int status, xtRowID row_id, xtRecordID var_rec_id);
 
465
xtBool                          xt_tab_free_row(XTOpenTablePtr ot, XTTableHPtr tab, xtRowID row_id);
 
466
 
 
467
xtBool                          xt_tab_load_ext_data(XTOpenTablePtr ot, xtRecordID load_rec_id, xtWord1 *buffer, u_int cols_req);
 
468
xtBool                          xt_tab_put_rec_data(XTOpenTablePtr ot, xtRecordID rec_id, size_t size, xtWord1 *buffer, xtOpSeqNo *op_seq);
 
469
xtBool                          xt_tab_put_log_op_rec_data(XTOpenTablePtr ot, u_int status, xtRecordID free_rec_id, xtRecordID rec_id, size_t size, xtWord1 *buffer);
 
470
xtBool                          xt_tab_put_log_rec_data(XTOpenTablePtr ot, u_int status, xtRecordID free_rec_id, xtRecordID rec_id, size_t size, xtWord1 *buffer, xtOpSeqNo *op_seq);
 
471
xtBool                          xt_tab_get_rec_data(register XTOpenTablePtr ot, xtRecordID rec_id, size_t size, xtWord1 *buffer);
 
472
 
 
473
inline off_t            xt_row_id_to_row_offset(register XTTableHPtr tab, xtRowID row_id)
 
474
{
 
475
        return (off_t) tab->tab_rows.tci_header_size + (off_t) (row_id - 1) * (off_t) tab->tab_rows.tci_rec_size;
 
476
}
 
477
 
 
478
inline  xtRowID         xt_row_offset_row_id(register XTTableHPtr tab, off_t rec_offs)
 
479
{
 
480
#ifdef DEBUG
 
481
        if (((rec_offs - (off_t) tab->tab_rows.tci_header_size) % (off_t) tab->tab_rows.tci_rec_size) != 0) {
 
482
                printf("ERROR! Not a valid record offset!\n");
 
483
        }
 
484
#endif
 
485
        return (xtRowID) ((rec_offs - (off_t) tab->tab_rows.tci_header_size) / (off_t) tab->tab_rows.tci_rec_size) + 1;
 
486
}
 
487
 
 
488
inline off_t            xt_rec_id_to_rec_offset(register XTTableHPtr tab, xtRefID ref_id)
 
489
{
 
490
        if (!ref_id)
 
491
                return (off_t) 0;
 
492
        return (off_t) tab->tab_recs.tci_header_size + (off_t) (ref_id-1) * (off_t) tab->tab_recs.tci_rec_size;
 
493
}
 
494
 
 
495
inline  xtRefID         xt_rec_offset_rec_id(register XTTableHPtr tab, off_t ref_offs)
 
496
{
 
497
        if (!ref_offs)
 
498
                return (xtRefID) 0;
 
499
#ifdef DEBUG
 
500
        if (((ref_offs - (off_t) tab->tab_recs.tci_header_size) % (off_t) tab->tab_recs.tci_rec_size) != 0) {
 
501
                printf("ERROR! Not a valid record offset!\n");
 
502
        }
 
503
#endif
 
504
                
 
505
        return (xtRefID) ((ref_offs - (off_t) tab->tab_recs.tci_header_size) / (off_t) tab->tab_recs.tci_rec_size)+1;
 
506
}
 
507
 
420
508
 
421
509
#define XT_RESIZE_ROW_BUFFER(thr, rb, size) \
422
510
        do { \