~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
#ifndef data0data_h
10
#define data0data_h
11
12
#include "univ.i"
13
14
#include "data0types.h"
15
#include "data0type.h"
16
#include "mem0mem.h"
17
#include "dict0types.h"
18
19
typedef struct big_rec_struct		big_rec_t;
20
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
21
#ifdef UNIV_DEBUG
1 by brian
clean slate
22
/*************************************************************************
23
Gets pointer to the type struct of SQL data field. */
24
UNIV_INLINE
25
dtype_t*
26
dfield_get_type(
27
/*============*/
28
				/* out: pointer to the type struct */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
29
	const dfield_t*	field);	/* in: SQL data field */
30
/*************************************************************************
31
Gets pointer to the data in a field. */
32
UNIV_INLINE
33
void*
34
dfield_get_data(
35
/*============*/
36
				/* out: pointer to data */
37
	const dfield_t* field);	/* in: field */
38
#else /* UNIV_DEBUG */
39
# define dfield_get_type(field) (&(field)->type)
40
# define dfield_get_data(field) ((field)->data)
41
#endif /* UNIV_DEBUG */
1 by brian
clean slate
42
/*************************************************************************
43
Sets the type struct of SQL data field. */
44
UNIV_INLINE
45
void
46
dfield_set_type(
47
/*============*/
48
	dfield_t*	field,	/* in: SQL data field */
49
	dtype_t*	type);	/* in: pointer to data type struct */
50
/*************************************************************************
51
Gets length of field data. */
52
UNIV_INLINE
53
ulint
54
dfield_get_len(
55
/*===========*/
56
				/* out: length of data; UNIV_SQL_NULL if
57
				SQL null data */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
58
	const dfield_t* field);	/* in: field */
1 by brian
clean slate
59
/*************************************************************************
60
Sets length in a field. */
61
UNIV_INLINE
62
void
63
dfield_set_len(
64
/*===========*/
65
	dfield_t*	field,	/* in: field */
66
	ulint		len);	/* in: length or UNIV_SQL_NULL */
67
/*************************************************************************
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
68
Determines if a field is SQL NULL */
69
UNIV_INLINE
70
ulint
71
dfield_is_null(
72
/*===========*/
73
				/* out: nonzero if SQL null data */
74
	const dfield_t* field);	/* in: field */
75
/*************************************************************************
76
Determines if a field is externally stored */
77
UNIV_INLINE
78
ulint
79
dfield_is_ext(
80
/*==========*/
81
				/* out: nonzero if externally stored */
82
	const dfield_t* field);	/* in: field */
83
/*************************************************************************
84
Sets the "external storage" flag */
85
UNIV_INLINE
86
void
87
dfield_set_ext(
88
/*===========*/
89
	dfield_t*	field);	/* in/out: field */
90
/*************************************************************************
1 by brian
clean slate
91
Sets pointer to the data and length in a field. */
92
UNIV_INLINE
93
void
94
dfield_set_data(
95
/*============*/
96
	dfield_t*	field,	/* in: field */
97
	const void*	data,	/* in: data */
98
	ulint		len);	/* in: length or UNIV_SQL_NULL */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
99
/*************************************************************************
100
Sets a data field to SQL NULL. */
101
UNIV_INLINE
102
void
103
dfield_set_null(
104
/*============*/
105
	dfield_t*	field);	/* in/out: field */
1 by brian
clean slate
106
/**************************************************************************
107
Writes an SQL null field full of zeros. */
108
UNIV_INLINE
109
void
110
data_write_sql_null(
111
/*================*/
112
	byte*	data,	/* in: pointer to a buffer of size len */
113
	ulint	len);	/* in: SQL null size in bytes */
114
/*************************************************************************
115
Copies the data and len fields. */
116
UNIV_INLINE
117
void
118
dfield_copy_data(
119
/*=============*/
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
120
	dfield_t*	field1,	/* out: field to copy to */
121
	const dfield_t*	field2);/* in: field to copy from */
1 by brian
clean slate
122
/*************************************************************************
123
Copies a data field to another. */
124
UNIV_INLINE
125
void
126
dfield_copy(
127
/*========*/
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
128
	dfield_t*	field1,	/* out: field to copy to */
129
	const dfield_t*	field2);/* in: field to copy from */
130
/*************************************************************************
131
Copies the data pointed to by a data field. */
132
UNIV_INLINE
133
void
134
dfield_dup(
135
/*=======*/
136
	dfield_t*	field,	/* in/out: data field */
137
	mem_heap_t*	heap);	/* in: memory heap where allocated */
1 by brian
clean slate
138
/*************************************************************************
139
Tests if data length and content is equal for two dfields. */
140
UNIV_INLINE
141
ibool
142
dfield_datas_are_binary_equal(
143
/*==========================*/
144
				/* out: TRUE if equal */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
145
	const dfield_t*	field1,	/* in: field */
146
	const dfield_t*	field2);/* in: field */
1 by brian
clean slate
147
/*************************************************************************
148
Tests if dfield data length and content is equal to the given. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
149
UNIV_INTERN
1 by brian
clean slate
150
ibool
151
dfield_data_is_binary_equal(
152
/*========================*/
153
				/* out: TRUE if equal */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
154
	const dfield_t*	field,	/* in: field */
1 by brian
clean slate
155
	ulint		len,	/* in: data length or UNIV_SQL_NULL */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
156
	const byte*	data);	/* in: data */
1 by brian
clean slate
157
/*************************************************************************
158
Gets number of fields in a data tuple. */
159
UNIV_INLINE
160
ulint
161
dtuple_get_n_fields(
162
/*================*/
163
				/* out: number of fields */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
164
	const dtuple_t*	tuple);	/* in: tuple */
165
#ifdef UNIV_DEBUG
1 by brian
clean slate
166
/*************************************************************************
167
Gets nth field of a tuple. */
168
UNIV_INLINE
169
dfield_t*
170
dtuple_get_nth_field(
171
/*=================*/
172
				/* out: nth field */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
173
	const dtuple_t*	tuple,	/* in: tuple */
1 by brian
clean slate
174
	ulint		n);	/* in: index of field */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
175
#else /* UNIV_DEBUG */
176
# define dtuple_get_nth_field(tuple, n) ((tuple)->fields + (n))
177
#endif /* UNIV_DEBUG */
1 by brian
clean slate
178
/*************************************************************************
179
Gets info bits in a data tuple. */
180
UNIV_INLINE
181
ulint
182
dtuple_get_info_bits(
183
/*=================*/
184
				/* out: info bits */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
185
	const dtuple_t*	tuple);	/* in: tuple */
1 by brian
clean slate
186
/*************************************************************************
187
Sets info bits in a data tuple. */
188
UNIV_INLINE
189
void
190
dtuple_set_info_bits(
191
/*=================*/
192
	dtuple_t*	tuple,		/* in: tuple */
193
	ulint		info_bits);	/* in: info bits */
194
/*************************************************************************
195
Gets number of fields used in record comparisons. */
196
UNIV_INLINE
197
ulint
198
dtuple_get_n_fields_cmp(
199
/*====================*/
200
				/* out: number of fields used in comparisons
201
				in rem0cmp.* */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
202
	const dtuple_t*	tuple);	/* in: tuple */
1 by brian
clean slate
203
/*************************************************************************
204
Gets number of fields used in record comparisons. */
205
UNIV_INLINE
206
void
207
dtuple_set_n_fields_cmp(
208
/*====================*/
209
	dtuple_t*	tuple,		/* in: tuple */
210
	ulint		n_fields_cmp);	/* in: number of fields used in
211
					comparisons in rem0cmp.* */
212
/**************************************************************
213
Creates a data tuple to a memory heap. The default value for number
214
of fields used in record comparisons for this tuple is n_fields. */
215
UNIV_INLINE
216
dtuple_t*
217
dtuple_create(
218
/*==========*/
219
				/* out, own: created tuple */
220
	mem_heap_t*	heap,	/* in: memory heap where the tuple
221
				is created */
222
	ulint		n_fields); /* in: number of fields */
223
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
224
/**************************************************************
225
Wrap data fields in a tuple. The default value for number
226
of fields used in record comparisons for this tuple is n_fields. */
227
UNIV_INLINE
228
const dtuple_t*
229
dtuple_from_fields(
230
/*===============*/
231
					/* out: data tuple */
232
	dtuple_t*	tuple,		/* in: storage for data tuple */
233
	const dfield_t*	fields,		/* in: fields */
234
	ulint		n_fields);	/* in: number of fields */
235
1 by brian
clean slate
236
/*************************************************************************
237
Sets number of fields used in a tuple. Normally this is set in
238
dtuple_create, but if you want later to set it smaller, you can use this. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
239
UNIV_INTERN
1 by brian
clean slate
240
void
241
dtuple_set_n_fields(
242
/*================*/
243
	dtuple_t*	tuple,		/* in: tuple */
244
	ulint		n_fields);	/* in: number of fields */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
245
/*************************************************************************
246
Copies a data tuple to another.  This is a shallow copy; if a deep copy
247
is desired, dfield_dup() will have to be invoked on each field. */
248
UNIV_INLINE
249
dtuple_t*
250
dtuple_copy(
251
/*========*/
252
				/* out, own: copy of tuple */
253
	const dtuple_t*	tuple,	/* in: tuple to copy from */
254
	mem_heap_t*	heap);	/* in: memory heap
255
				where the tuple is created */
1 by brian
clean slate
256
/**************************************************************
257
The following function returns the sum of data lengths of a tuple. The space
258
occupied by the field structs or the tuple struct is not counted. */
259
UNIV_INLINE
260
ulint
261
dtuple_get_data_size(
262
/*=================*/
263
				/* out: sum of data lens */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
264
	const dtuple_t*	tuple);	/* in: typed data tuple */
265
/*************************************************************************
266
Computes the number of externally stored fields in a data tuple. */
267
UNIV_INLINE
268
ulint
269
dtuple_get_n_ext(
270
/*=============*/
271
				/* out: number of fields */
272
	const dtuple_t*	tuple);	/* in: tuple */
1 by brian
clean slate
273
/****************************************************************
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
274
Compare two data tuples, respecting the collation of character fields. */
275
UNIV_INTERN
276
int
277
dtuple_coll_cmp(
278
/*============*/
279
				/* out: 1, 0 , -1 if tuple1 is greater, equal,
280
				less, respectively, than tuple2 */
281
	const dtuple_t*	tuple1,	/* in: tuple 1 */
282
	const dtuple_t*	tuple2);/* in: tuple 2 */
1 by brian
clean slate
283
/****************************************************************
284
Folds a prefix given as the number of fields of a tuple. */
285
UNIV_INLINE
286
ulint
287
dtuple_fold(
288
/*========*/
289
				/* out: the folded value */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
290
	const dtuple_t*	tuple,	/* in: the tuple */
1 by brian
clean slate
291
	ulint		n_fields,/* in: number of complete fields to fold */
292
	ulint		n_bytes,/* in: number of bytes to fold in an
293
				incomplete last field */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
294
	dulint		tree_id)/* in: index tree id */
295
	__attribute__((pure));
1 by brian
clean slate
296
/***********************************************************************
297
Sets types of fields binary in a tuple. */
298
UNIV_INLINE
299
void
300
dtuple_set_types_binary(
301
/*====================*/
302
	dtuple_t*	tuple,	/* in: data tuple */
303
	ulint		n);	/* in: number of fields to set */
304
/**************************************************************************
305
Checks if a dtuple contains an SQL null value. */
306
UNIV_INLINE
307
ibool
308
dtuple_contains_null(
309
/*=================*/
310
				/* out: TRUE if some field is SQL null */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
311
	const dtuple_t*	tuple);	/* in: dtuple */
1 by brian
clean slate
312
/**************************************************************
313
Checks that a data field is typed. Asserts an error if not. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
314
UNIV_INTERN
1 by brian
clean slate
315
ibool
316
dfield_check_typed(
317
/*===============*/
318
				/* out: TRUE if ok */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
319
	const dfield_t*	field);	/* in: data field */
1 by brian
clean slate
320
/**************************************************************
321
Checks that a data tuple is typed. Asserts an error if not. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
322
UNIV_INTERN
1 by brian
clean slate
323
ibool
324
dtuple_check_typed(
325
/*===============*/
326
				/* out: TRUE if ok */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
327
	const dtuple_t*	tuple);	/* in: tuple */
1 by brian
clean slate
328
/**************************************************************
329
Checks that a data tuple is typed. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
330
UNIV_INTERN
1 by brian
clean slate
331
ibool
332
dtuple_check_typed_no_assert(
333
/*=========================*/
334
				/* out: TRUE if ok */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
335
	const dtuple_t*	tuple);	/* in: tuple */
1 by brian
clean slate
336
#ifdef UNIV_DEBUG
337
/**************************************************************
338
Validates the consistency of a tuple which must be complete, i.e,
339
all fields must have been set. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
340
UNIV_INTERN
1 by brian
clean slate
341
ibool
342
dtuple_validate(
343
/*============*/
344
				/* out: TRUE if ok */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
345
	const dtuple_t*	tuple);	/* in: tuple */
1 by brian
clean slate
346
#endif /* UNIV_DEBUG */
347
/*****************************************************************
348
Pretty prints a dfield value according to its data type. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
349
UNIV_INTERN
1 by brian
clean slate
350
void
351
dfield_print(
352
/*=========*/
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
353
	const dfield_t*	dfield);/* in: dfield */
1 by brian
clean slate
354
/*****************************************************************
355
Pretty prints a dfield value according to its data type. Also the hex string
356
is printed if a string contains non-printable characters. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
357
UNIV_INTERN
1 by brian
clean slate
358
void
359
dfield_print_also_hex(
360
/*==================*/
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
361
	const dfield_t*	dfield);	 /* in: dfield */
1 by brian
clean slate
362
/**************************************************************
363
The following function prints the contents of a tuple. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
364
UNIV_INTERN
1 by brian
clean slate
365
void
366
dtuple_print(
367
/*=========*/
368
	FILE*		f,	/* in: output stream */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
369
	const dtuple_t*	tuple);	/* in: tuple */
1 by brian
clean slate
370
/******************************************************************
371
Moves parts of long fields in entry to the big record vector so that
372
the size of tuple drops below the maximum record size allowed in the
373
database. Moves data only from those fields which are not necessary
374
to determine uniquely the insertion place of the tuple in the index. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
375
UNIV_INTERN
1 by brian
clean slate
376
big_rec_t*
377
dtuple_convert_big_rec(
378
/*===================*/
379
				/* out, own: created big record vector,
380
				NULL if we are not able to shorten
381
				the entry enough, i.e., if there are
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
382
				too many fixed-length or short fields
383
				in entry or the index is clustered */
1 by brian
clean slate
384
	dict_index_t*	index,	/* in: index */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
385
	dtuple_t*	entry,	/* in/out: index entry */
386
	ulint*		n_ext);	/* in/out: number of
387
				externally stored columns */
1 by brian
clean slate
388
/******************************************************************
389
Puts back to entry the data stored in vector. Note that to ensure the
390
fields in entry can accommodate the data, vector must have been created
391
from entry with dtuple_convert_big_rec. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
392
UNIV_INTERN
1 by brian
clean slate
393
void
394
dtuple_convert_back_big_rec(
395
/*========================*/
396
	dict_index_t*	index,	/* in: index */
397
	dtuple_t*	entry,	/* in: entry whose data was put to vector */
398
	big_rec_t*	vector);/* in, own: big rec vector; it is
399
				freed in this function */
400
/******************************************************************
401
Frees the memory in a big rec vector. */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
402
UNIV_INLINE
1 by brian
clean slate
403
void
404
dtuple_big_rec_free(
405
/*================*/
406
	big_rec_t*	vector);	/* in, own: big rec vector; it is
407
				freed in this function */
408
409
/*######################################################################*/
410
411
/* Structure for an SQL data field */
412
struct dfield_struct{
413
	void*		data;	/* pointer to data */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
414
	unsigned	ext:1;	/* TRUE=externally stored, FALSE=local */
415
	unsigned	len:32;	/* data length; UNIV_SQL_NULL if SQL null */
1 by brian
clean slate
416
	dtype_t		type;	/* type of data */
417
};
418
419
struct dtuple_struct {
420
	ulint		info_bits;	/* info bits of an index record:
421
					the default is 0; this field is used
422
					if an index record is built from
423
					a data tuple */
424
	ulint		n_fields;	/* number of fields in dtuple */
425
	ulint		n_fields_cmp;	/* number of fields which should
426
					be used in comparison services
427
					of rem0cmp.*; the index search
428
					is performed by comparing only these
429
					fields, others are ignored; the
430
					default value in dtuple creation is
431
					the same value as n_fields */
432
	dfield_t*	fields;		/* fields */
433
	UT_LIST_NODE_T(dtuple_t) tuple_list;
434
					/* data tuples can be linked into a
435
					list using this field */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
436
#ifdef UNIV_DEBUG
1 by brian
clean slate
437
	ulint		magic_n;
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
438
# define		DATA_TUPLE_MAGIC_N	65478679
439
#endif /* UNIV_DEBUG */
1 by brian
clean slate
440
};
441
442
/* A slot for a field in a big rec vector */
443
444
typedef struct big_rec_field_struct	big_rec_field_t;
445
struct big_rec_field_struct {
446
	ulint		field_no;	/* field number in record */
447
	ulint		len;		/* stored data len */
520.4.1 by Monty Taylor
Imported InnoDB plugin with changes.
448
	const void*	data;		/* stored data */
1 by brian
clean slate
449
};
450
451
/* Storage format for overflow data in a big record, that is, a record
452
which needs external storage of data fields */
453
454
struct big_rec_struct {
455
	mem_heap_t*	heap;		/* memory heap from which allocated */
456
	ulint		n_fields;	/* number of stored fields */
457
	big_rec_field_t* fields;	/* stored fields */
458
};
459
460
#ifndef UNIV_NONINL
461
#include "data0data.ic"
462
#endif
463
464
#endif