~ubuntu-43/undrop-for-innodb/pierre

« back to all changes in this revision

Viewing changes to include/data0data.ic

  • Committer: root
  • Date: 2014-06-22 09:59:20 UTC
  • Revision ID: root@twindb-dev-20140622095920-0atlkgy80imht7hj
daily commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/************************************************************************
 
2
SQL data field and tuple
 
3
 
 
4
(c) 1994-1996 Innobase Oy
 
5
 
 
6
Created 5/30/1994 Heikki Tuuri
 
7
*************************************************************************/
 
8
 
 
9
#include "mem0mem.h"
 
10
#include "ut0rnd.h"
 
11
 
 
12
extern byte data_error;
 
13
 
 
14
/*************************************************************************
 
15
Gets pointer to the type struct of SQL data field. */
 
16
UNIV_INLINE
 
17
dtype_t*
 
18
dfield_get_type(
 
19
/*============*/
 
20
                                /* out: pointer to the type struct */
 
21
        dfield_t*       field)  /* in: SQL data field */
 
22
{
 
23
        ut_ad(field);
 
24
 
 
25
        return(&(field->type));
 
26
}
 
27
 
 
28
/*************************************************************************
 
29
Sets the type struct of SQL data field. */
 
30
UNIV_INLINE
 
31
void
 
32
dfield_set_type(
 
33
/*============*/
 
34
        dfield_t*       field,  /* in: SQL data field */
 
35
        dtype_t*        type)   /* in: pointer to data type struct */
 
36
{
 
37
        ut_ad(field && type);
 
38
 
 
39
        field->type = *type;
 
40
}
 
41
 
 
42
/*************************************************************************
 
43
Gets pointer to the data in a field. */
 
44
UNIV_INLINE
 
45
void* 
 
46
dfield_get_data(
 
47
/*============*/
 
48
                                /* out: pointer to data */
 
49
        dfield_t* field)        /* in: field */
 
50
{
 
51
        ut_ad(field);
 
52
        ut_ad((field->len == UNIV_SQL_NULL)
 
53
              || (field->data != &data_error)); 
 
54
 
 
55
        return(field->data);
 
56
}
 
57
 
 
58
/*************************************************************************
 
59
Gets length of field data. */
 
60
UNIV_INLINE
 
61
ulint
 
62
dfield_get_len(
 
63
/*===========*/
 
64
                                /* out: length of data; UNIV_SQL_NULL if 
 
65
                                SQL null data */
 
66
        dfield_t* field)        /* in: field */
 
67
{
 
68
        ut_ad(field);
 
69
        ut_ad((field->len == UNIV_SQL_NULL)
 
70
              || (field->data != &data_error));
 
71
 
 
72
        return(field->len);
 
73
}
 
74
 
 
75
/*************************************************************************
 
76
Sets length in a field. */
 
77
UNIV_INLINE
 
78
void 
 
79
dfield_set_len(
 
80
/*===========*/
 
81
        dfield_t*       field,  /* in: field */
 
82
        ulint           len)    /* in: length or UNIV_SQL_NULL */
 
83
{
 
84
        ut_ad(field);
 
85
 
 
86
        field->len = len;
 
87
}
 
88
 
 
89
/*************************************************************************
 
90
Sets pointer to the data and length in a field. */
 
91
UNIV_INLINE
 
92
void 
 
93
dfield_set_data(
 
94
/*============*/
 
95
        dfield_t*       field,  /* in: field */
 
96
        const void*     data,   /* in: data */
 
97
        ulint           len)    /* in: length or UNIV_SQL_NULL */
 
98
{
 
99
        ut_ad(field);
 
100
 
 
101
        field->data = (void*) data;
 
102
        field->len = len;
 
103
}
 
104
 
 
105
/*************************************************************************
 
106
Copies the data and len fields. */
 
107
UNIV_INLINE
 
108
void 
 
109
dfield_copy_data(
 
110
/*=============*/
 
111
        dfield_t*       field1, /* in: field to copy to */
 
112
        dfield_t*       field2) /* in: field to copy from */
 
113
{
 
114
        ut_ad(field1 && field2);
 
115
 
 
116
        field1->data = field2->data;
 
117
        field1->len = field2->len;
 
118
}
 
119
 
 
120
/*************************************************************************
 
121
Copies a data field to another. */
 
122
UNIV_INLINE
 
123
void
 
124
dfield_copy(
 
125
/*========*/
 
126
        dfield_t*       field1, /* in: field to copy to */
 
127
        dfield_t*       field2) /* in: field to copy from */
 
128
{
 
129
        *field1 = *field2;
 
130
}
 
131
 
 
132
/*************************************************************************
 
133
Tests if data length and content is equal for two dfields. */
 
134
UNIV_INLINE
 
135
ibool
 
136
dfield_datas_are_binary_equal(
 
137
/*==========================*/
 
138
                                /* out: TRUE if equal */
 
139
        dfield_t*       field1, /* in: field */
 
140
        dfield_t*       field2) /* in: field */
 
141
{
 
142
        ulint   len;
 
143
 
 
144
        len = field1->len;
 
145
        
 
146
        if ((len != field2->len)
 
147
            || ((len != UNIV_SQL_NULL)
 
148
                && (0 != ut_memcmp(field1->data, field2->data, len)))) {
 
149
                
 
150
                return(FALSE);
 
151
        }
 
152
 
 
153
        return(TRUE);
 
154
}
 
155
 
 
156
/*************************************************************************
 
157
Gets info bits in a data tuple. */
 
158
UNIV_INLINE
 
159
ulint
 
160
dtuple_get_info_bits(
 
161
/*=================*/
 
162
                                /* out: info bits */
 
163
        dtuple_t*       tuple)  /* in: tuple */
 
164
{
 
165
        ut_ad(tuple);
 
166
 
 
167
        return(tuple->info_bits);
 
168
}
 
169
 
 
170
/*************************************************************************
 
171
Sets info bits in a data tuple. */
 
172
UNIV_INLINE
 
173
void
 
174
dtuple_set_info_bits(
 
175
/*=================*/
 
176
        dtuple_t*       tuple,          /* in: tuple */
 
177
        ulint           info_bits)      /* in: info bits */
 
178
{
 
179
        ut_ad(tuple);
 
180
 
 
181
        tuple->info_bits = info_bits;
 
182
}
 
183
 
 
184
/*************************************************************************
 
185
Gets number of fields used in record comparisons. */
 
186
UNIV_INLINE
 
187
ulint
 
188
dtuple_get_n_fields_cmp(
 
189
/*====================*/
 
190
                                /* out: number of fields used in comparisons
 
191
                                in rem0cmp.* */
 
192
        dtuple_t*       tuple)  /* in: tuple */
 
193
{
 
194
        ut_ad(tuple);
 
195
 
 
196
        return(tuple->n_fields_cmp);
 
197
}
 
198
 
 
199
/*************************************************************************
 
200
Sets number of fields used in record comparisons. */
 
201
UNIV_INLINE
 
202
void
 
203
dtuple_set_n_fields_cmp(
 
204
/*====================*/
 
205
        dtuple_t*       tuple,          /* in: tuple */
 
206
        ulint           n_fields_cmp)   /* in: number of fields used in
 
207
                                        comparisons in rem0cmp.* */
 
208
{
 
209
        ut_ad(tuple);
 
210
        ut_ad(n_fields_cmp <= tuple->n_fields);
 
211
 
 
212
        tuple->n_fields_cmp = n_fields_cmp;
 
213
}
 
214
 
 
215
/*************************************************************************
 
216
Gets number of fields in a data tuple. */
 
217
UNIV_INLINE
 
218
ulint
 
219
dtuple_get_n_fields(
 
220
/*================*/
 
221
                                /* out: number of fields */
 
222
        dtuple_t*       tuple)  /* in: tuple */
 
223
{
 
224
        ut_ad(tuple);
 
225
 
 
226
        return(tuple->n_fields);
 
227
}
 
228
 
 
229
/*************************************************************************
 
230
Gets nth field of a tuple. */
 
231
UNIV_INLINE
 
232
dfield_t* 
 
233
dtuple_get_nth_field(
 
234
/*=================*/
 
235
                                /* out: nth field */
 
236
        dtuple_t*       tuple,  /* in: tuple */
 
237
        ulint           n)      /* in: index of field */
 
238
{
 
239
        ut_ad(tuple);
 
240
        ut_ad(n < tuple->n_fields);
 
241
 
 
242
        return(tuple->fields + n);
 
243
}
 
244
 
 
245
/**************************************************************
 
246
Creates a data tuple to a memory heap. The default value for number
 
247
of fields used in record comparisons for this tuple is n_fields. */
 
248
UNIV_INLINE
 
249
dtuple_t*
 
250
dtuple_create(
 
251
/*==========*/
 
252
                                /* out, own: created tuple */
 
253
        mem_heap_t*     heap,   /* in: memory heap where the tuple
 
254
                                is created */
 
255
        ulint           n_fields) /* in: number of fields */    
 
256
{
 
257
        dtuple_t*       tuple;  
 
258
 
 
259
        ut_ad(heap);
 
260
 
 
261
        tuple = (dtuple_t*) mem_heap_alloc(heap, sizeof(dtuple_t)
 
262
                                     + n_fields * sizeof(dfield_t));
 
263
        tuple->info_bits = 0;
 
264
        tuple->n_fields = n_fields;
 
265
        tuple->n_fields_cmp = n_fields;
 
266
        tuple->fields = (dfield_t*)(((byte*)tuple) + sizeof(dtuple_t));
 
267
 
 
268
#ifdef UNIV_DEBUG
 
269
        tuple->magic_n = DATA_TUPLE_MAGIC_N;
 
270
 
 
271
        {       /* In the debug version, initialize fields to an error value */
 
272
                ulint   i;
 
273
                
 
274
                for (i = 0; i < n_fields; i++) {
 
275
                        (tuple->fields + i)->data = &data_error;
 
276
                        dfield_get_type(tuple->fields + i)->mtype = DATA_ERROR;
 
277
                }
 
278
        }
 
279
#endif
 
280
        return(tuple);  
 
281
}
 
282
 
 
283
/**************************************************************
 
284
The following function returns the sum of data lengths of a tuple. The space
 
285
occupied by the field structs or the tuple struct is not counted. Neither
 
286
is possible space in externally stored parts of the field. */
 
287
UNIV_INLINE
 
288
ulint
 
289
dtuple_get_data_size(
 
290
/*=================*/
 
291
                                /* out: sum of data lengths */
 
292
        dtuple_t*       tuple)  /* in: typed data tuple */
 
293
{
 
294
        dfield_t*       field;
 
295
        ulint           n_fields;
 
296
        ulint           len;
 
297
        ulint           i;
 
298
        ulint           sum     = 0;
 
299
 
 
300
        ut_ad(tuple);
 
301
        ut_ad(dtuple_check_typed(tuple));
 
302
        ut_ad(tuple->magic_n == DATA_TUPLE_MAGIC_N);
 
303
 
 
304
        n_fields = tuple->n_fields;
 
305
 
 
306
        for (i = 0; i < n_fields; i++) {
 
307
                field = dtuple_get_nth_field(tuple,  i);
 
308
                len = dfield_get_len(field);
 
309
 
 
310
                if (len == UNIV_SQL_NULL) {
 
311
                        len = dtype_get_sql_null_size(dfield_get_type(field));
 
312
                }
 
313
 
 
314
                sum += len;
 
315
        }
 
316
        
 
317
        return(sum);
 
318
}
 
319
 
 
320
/***********************************************************************
 
321
Sets types of fields binary in a tuple. */
 
322
UNIV_INLINE
 
323
void
 
324
dtuple_set_types_binary(
 
325
/*====================*/
 
326
        dtuple_t*       tuple,  /* in: data tuple */
 
327
        ulint           n)      /* in: number of fields to set */
 
328
{
 
329
        dtype_t*        dfield_type;
 
330
        ulint           i;
 
331
        
 
332
        for (i = 0; i < n; i++) {
 
333
                dfield_type = dfield_get_type(dtuple_get_nth_field(tuple, i));
 
334
                dtype_set(dfield_type, DATA_BINARY, 0, 0, 0);
 
335
        }
 
336
}
 
337
 
 
338
/****************************************************************
 
339
Folds a prefix given as the number of fields of a tuple. */
 
340
UNIV_INLINE
 
341
ulint
 
342
dtuple_fold(
 
343
/*========*/
 
344
                                /* out: the folded value */
 
345
        dtuple_t*       tuple,  /* in: the tuple */
 
346
        ulint           n_fields,/* in: number of complete fields to fold */
 
347
        ulint           n_bytes,/* in: number of bytes to fold in an
 
348
                                incomplete last field */
 
349
        dulint          tree_id)/* in: index tree id */
 
350
{
 
351
        dfield_t*       field;
 
352
        ulint           i;
 
353
        byte*           data;
 
354
        ulint           len;
 
355
        ulint           fold;
 
356
 
 
357
        ut_ad(tuple);
 
358
        ut_ad(tuple->magic_n == DATA_TUPLE_MAGIC_N);
 
359
        ut_ad(dtuple_check_typed(tuple));
 
360
 
 
361
        fold = ut_fold_dulint(tree_id);
 
362
 
 
363
        for (i = 0; i < n_fields; i++) {
 
364
                field = dtuple_get_nth_field(tuple, i);
 
365
 
 
366
                data = (byte*) dfield_get_data(field);
 
367
                len = dfield_get_len(field);    
 
368
                
 
369
                if (len != UNIV_SQL_NULL) {
 
370
                        fold = ut_fold_ulint_pair(fold, 
 
371
                                                  ut_fold_binary(data, len));
 
372
                }
 
373
        }
 
374
 
 
375
        if (n_bytes > 0) {
 
376
                field = dtuple_get_nth_field(tuple, i);
 
377
 
 
378
                data = (byte*) dfield_get_data(field);
 
379
                len = dfield_get_len(field);    
 
380
                
 
381
                if (len != UNIV_SQL_NULL) {
 
382
                        if (len > n_bytes) {
 
383
                                len = n_bytes;
 
384
                        }
 
385
 
 
386
                        fold = ut_fold_ulint_pair(fold, 
 
387
                                                  ut_fold_binary(data, len));
 
388
                }
 
389
        }       
 
390
 
 
391
        return(fold);
 
392
}
 
393
 
 
394
/**************************************************************************
 
395
Writes an SQL null field full of zeros. */
 
396
UNIV_INLINE
 
397
void
 
398
data_write_sql_null(
 
399
/*================*/
 
400
        byte*   data,   /* in: pointer to a buffer of size len */
 
401
        ulint   len)    /* in: SQL null size in bytes */
 
402
{
 
403
        ulint   j;
 
404
 
 
405
        for (j = 0; j < len; j++) {
 
406
                data[j] = '\0';
 
407
        }
 
408
}
 
409
 
 
410
/**************************************************************************
 
411
Checks if a dtuple contains an SQL null value. */
 
412
UNIV_INLINE
 
413
ibool
 
414
dtuple_contains_null(
 
415
/*=================*/
 
416
                                /* out: TRUE if some field is SQL null */
 
417
        dtuple_t*       tuple)  /* in: dtuple */
 
418
{
 
419
        ulint   n;
 
420
        ulint   i;
 
421
 
 
422
        n = dtuple_get_n_fields(tuple);
 
423
 
 
424
        for (i = 0; i < n; i++) {
 
425
                if (dfield_get_len(dtuple_get_nth_field(tuple, i))
 
426
                    == UNIV_SQL_NULL) {
 
427
 
 
428
                        return(TRUE);
 
429
                }
 
430
        }
 
431
 
 
432
        return(FALSE);
 
433
}