summaryrefslogtreecommitdiff
path: root/storage/innobase/include/row0row.ic
blob: c9db1d2a37e9994c964b9620253c2e0584c3b538 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*****************************************************************************

Copyright (c) 1996, 2015, Oracle and/or its affiliates. All Rights Reserved.
Copyright (c) 2017, 2018, MariaDB Corporation.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Suite 500, Boston, MA 02110-1335 USA

*****************************************************************************/

/**************************************************//**
@file include/row0row.ic
General row routines

Created 4/20/1996 Heikki Tuuri
*******************************************************/

#include "dict0dict.h"
#include "rem0rec.h"
#include "trx0undo.h"

/*********************************************************************//**
Gets the offset of the DB_TRX_ID field, in bytes relative to the origin of
a clustered index record.
@return offset of DATA_TRX_ID */
UNIV_INLINE
ulint
row_get_trx_id_offset(
/*==================*/
	const dict_index_t*	index,	/*!< in: clustered index */
	const ulint*		offsets)/*!< in: record offsets */
{
	ulint	offset;
	ulint	len;

	ut_ad(rec_offs_validate(NULL, index, offsets));

	offset = rec_get_nth_field_offs(offsets, index->db_trx_id(), &len);

	ut_ad(len == DATA_TRX_ID_LEN);

	return(offset);
}

/*********************************************************************//**
Reads the trx id field from a clustered index record.
@return value of the field */
UNIV_INLINE
trx_id_t
row_get_rec_trx_id(
/*===============*/
	const rec_t*		rec,	/*!< in: record */
	const dict_index_t*	index,	/*!< in: clustered index */
	const ulint*		offsets)/*!< in: rec_get_offsets(rec, index) */
{
	ulint	offset;

	ut_ad(dict_index_is_clust(index));
	ut_ad(rec_offs_validate(rec, index, offsets));

	offset = index->trx_id_offset;

	if (!offset) {
		offset = row_get_trx_id_offset(index, offsets);
	}

	return(trx_read_trx_id(rec + offset));
}

/*********************************************************************//**
Reads the roll pointer field from a clustered index record.
@return value of the field */
UNIV_INLINE
roll_ptr_t
row_get_rec_roll_ptr(
/*=================*/
	const rec_t*		rec,	/*!< in: record */
	const dict_index_t*	index,	/*!< in: clustered index */
	const ulint*		offsets)/*!< in: rec_get_offsets(rec, index) */
{
	ulint	offset;

	ut_ad(dict_index_is_clust(index));
	ut_ad(rec_offs_validate(rec, index, offsets));

	offset = index->trx_id_offset;

	if (!offset) {
		offset = row_get_trx_id_offset(index, offsets);
	}

	return(trx_read_roll_ptr(rec + offset + DATA_TRX_ID_LEN));
}

/*****************************************************************//**
When an insert or purge to a table is performed, this function builds
the entry to be inserted into or purged from an index on the table.
@return index entry which should be inserted or purged, or NULL if the
externally stored columns in the clustered index record are
unavailable and ext != NULL */
UNIV_INLINE
dtuple_t*
row_build_index_entry(
/*==================*/
	const dtuple_t*		row,	/*!< in: row which should be
					inserted or purged */
	const row_ext_t*	ext,	/*!< in: externally stored column
					prefixes, or NULL */
	const dict_index_t*	index,	/*!< in: index on the table */
	mem_heap_t*		heap)	/*!< in,out: memory heap from which
					the memory for the index entry
					is allocated */
{
	dtuple_t*	entry;

	ut_ad(dtuple_check_typed(row));
	entry = row_build_index_entry_low(row, ext, index, heap,
					  ROW_BUILD_NORMAL);
	ut_ad(!entry || dtuple_check_typed(entry));
	return(entry);
}

/*******************************************************************//**
Builds from a secondary index record a row reference with which we can
search the clustered index record. */
UNIV_INLINE
void
row_build_row_ref_fast(
/*===================*/
	dtuple_t*	ref,	/*!< in/out: typed data tuple where the
				reference is built */
	const ulint*	map,	/*!< in: array of field numbers in rec
				telling how ref should be built from
				the fields of rec */
	const rec_t*	rec,	/*!< in: secondary index record;
				must be preserved while ref is used, as we do
				not copy field values to heap */
	const ulint*	offsets)/*!< in: array returned by rec_get_offsets() */
{
	dfield_t*	dfield;
	const byte*	field;
	ulint		len;
	ulint		ref_len;
	ulint		field_no;
	ulint		i;

	ut_ad(rec_offs_validate(rec, NULL, offsets));
	ut_ad(!rec_offs_any_extern(offsets));
	ref_len = dtuple_get_n_fields(ref);

	for (i = 0; i < ref_len; i++) {
		dfield = dtuple_get_nth_field(ref, i);

		field_no = *(map + i);

		if (field_no != ULINT_UNDEFINED) {

			field = rec_get_nth_field(rec, offsets,
						  field_no, &len);
			dfield_set_data(dfield, field, len);
		}
	}
}

/** Parse the integer data from specified data, which could be
DATA_INT, DATA_FLOAT or DATA_DOUBLE. If the value is less than 0
and the type is not unsigned then we reset the value to 0
@param[in]	data		data to read
@param[in]	len		length of data
@param[in]	mtype		mtype of data
@param[in]	unsigned_type	if the data is unsigned
@return the integer value from the data */
ib_uint64_t
row_parse_int(
	const byte*	data,
	ulint		len,
	ulint		mtype,
	bool		unsigned_type)
{
	ib_uint64_t	value = 0;

	switch (mtype) {
	case DATA_INT:

		ut_a(len <= sizeof value);
		value = mach_read_int_type(data, len, unsigned_type);
		break;

	case DATA_FLOAT:

		ut_a(len == sizeof(float));
		value = static_cast<ib_uint64_t>(mach_float_read(data));
		break;

	case DATA_DOUBLE:

		ut_a(len == sizeof(double));
		value = static_cast<ib_uint64_t>(mach_double_read(data));
		break;

	default:
		ut_error;

	}

	if (!unsigned_type && static_cast<int64_t>(value) < 0) {
		value = 0;
	}

	return(value);
}