/
row0mysql.cc
5415 lines (4316 loc) · 167 KB
/
row0mysql.cc
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
Copyright (c) 2000, 2020, Oracle and/or its affiliates. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2.0, as published by the
Free Software Foundation.
This program is also distributed with certain software (including but not
limited to OpenSSL) that is licensed under separate terms, as designated in a
particular file or component or in included license documentation. The authors
of MySQL hereby grant you an additional permission to link the program and
your derivative works with the separately licensed software that they have
included with MySQL.
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, version 2.0,
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*****************************************************************************/
/** @file row/row0mysql.cc
Interface between Innobase row operations and MySQL.
Contains also create table and other data dictionary operations.
Created 9/17/2000 Heikki Tuuri
*******************************************************/
#include <debug_sync.h>
#include <gstream.h>
#include <spatial.h>
#include <sql_class.h>
#include <sql_const.h>
#include <sys/types.h>
#include <algorithm>
#include <deque>
#include <new>
#include <vector>
#include "btr0sea.h"
#include "dict0boot.h"
#include "dict0crea.h"
#include "dict0dd.h"
#include "dict0dict.h"
#include "dict0load.h"
#include "dict0priv.h"
#include "dict0stats.h"
#include "dict0stats_bg.h"
#include "fil0crypt.h"
#include "fil0fil.h"
#include "fsp0file.h"
#include "fsp0sysspace.h"
#include "fts0fts.h"
#include "fts0types.h"
#include "ha_prototypes.h"
#include "ibuf0ibuf.h"
#include "lock0lock.h"
#include "log0log.h"
#include "pars0pars.h"
#include "que0que.h"
#include "rem0cmp.h"
#include "row0ext.h"
#include "row0import.h"
#include "row0ins.h"
#include "row0merge.h"
#include "row0mysql.h"
#include "row0pread.h"
#include "row0row.h"
#include "row0sel.h"
#include "row0upd.h"
#include "trx0purge.h"
#include "trx0rec.h"
#include "trx0roll.h"
#include "trx0undo.h"
#include "ut0mpmcbq.h"
#include "ut0new.h"
#include "zlib.h"
#include "current_thd.h"
#include "my_dbug.h"
#include "my_io.h"
#include "sql/sql_zip_dict.h"
static const char *MODIFICATIONS_NOT_ALLOWED_MSG_FORCE_RECOVERY =
"innodb_force_recovery is on. We do not allow database modifications"
" by the user. Shut down mysqld and edit my.cnf to set"
" innodb_force_recovery=0";
/** Provide optional 4.x backwards compatibility for 5.0 and above */
ibool row_rollback_on_timeout = FALSE;
/**
Z_NO_COMPRESSION = 0
Z_BEST_SPEED = 1
Z_BEST_COMPRESSION = 9
Z_DEFAULT_COMPRESSION = -1
Compression level to be used by zlib for compressed-blob columns.
Settable by user.
*/
uint srv_compressed_columns_zip_level = DEFAULT_COMPRESSION_LEVEL;
/**
(Z_FILTERED | Z_HUFFMAN_ONLY | Z_RLE | Z_FIXED | Z_DEFAULT_STRATEGY)
The strategy parameter is used to tune the compression algorithm. Use the
value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only
(no string match), or Z_RLE to limit match distances to one
(run-length encoding). Filtered data consists mostly of small values with a
somewhat random distribution. In this case, the compression algorithm is
tuned to compress them better.
The effect of Z_FILTERED is to force more Huffman coding and less string
matching; it is somewhat intermediate between Z_DEFAULT_STRATEGY and
Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as Z_HUFFMAN_ONLY,
but give better compression for PNG image data. The strategy parameter only
affects the compression ratio but not the correctness of the compressed
output even if it is not set appropriately. Z_FIXED prevents the use of
dynamic Huffman codes, allowing for a simpler decoder for special
applications.
*/
const uint srv_compressed_columns_zlib_strategy = Z_DEFAULT_STRATEGY;
/** Compress the column if the data length exceeds this value. */
ulong srv_compressed_columns_threshold = 96;
/**
Determine if zlib needs to compute adler32 value for the compressed data.
This variables is similar to page_zip_zlib_wrap, but only used by
compressed blob columns.
*/
const bool srv_compressed_columns_zlib_wrap = true;
/**
Determine if zlib will use custom memory allocation functions based on
InnoDB memory heap routines (mem_heap_t*).
*/
const bool srv_compressed_columns_zlib_use_heap = false;
/** Chain node of the list of tables to drop in the background. */
struct row_mysql_drop_t {
char *table_name; /*!< table name */
UT_LIST_NODE_T(row_mysql_drop_t) row_mysql_drop_list;
/*!< list chain node */
};
/** @brief List of tables we should drop in background.
ALTER TABLE in MySQL requires that the table handler can drop the
table in background when there are no queries to it any
more. Protected by row_drop_list_mutex. */
static UT_LIST_BASE_NODE_T(row_mysql_drop_t) row_mysql_drop_list;
/** Mutex protecting the background table drop list. */
static ib_mutex_t row_drop_list_mutex;
/** Flag: has row_mysql_drop_list been initialized? */
static ibool row_mysql_drop_list_inited = FALSE;
/** If a table is not yet in the drop list, adds the table to the list of tables
which the master thread drops in background. We need this on Unix because in
ALTER TABLE MySQL may call drop table even if the table has running queries on
it. Also, if there are running foreign key checks on the table, we drop the
table lazily.
@return true if the table was not yet in the drop list, and was added there */
static ibool row_add_table_to_background_drop_list(
const char *name); /*!< in: table name */
#ifdef UNIV_DEBUG
/** Wait for the background drop list to become empty. */
void row_wait_for_background_drop_list_empty() {
bool empty = false;
while (!empty) {
mutex_enter(&row_drop_list_mutex);
empty = (UT_LIST_GET_LEN(row_mysql_drop_list) == 0);
mutex_exit(&row_drop_list_mutex);
os_thread_sleep(100000);
}
}
#endif /* UNIV_DEBUG */
/** Delays an INSERT, DELETE or UPDATE operation if the purge is lagging. */
static void row_mysql_delay_if_needed(void) {
if (srv_dml_needed_delay) {
os_thread_sleep(srv_dml_needed_delay);
}
}
void row_mysql_prebuilt_free_blob_heap(row_prebuilt_t *prebuilt) {
DBUG_TRACE;
DBUG_PRINT("row_mysql_prebuilt_free_blob_heap",
("blob_heap freeing: %p", prebuilt->blob_heap));
mem_heap_free(prebuilt->blob_heap);
prebuilt->blob_heap = nullptr;
}
/** Frees the compress heap in prebuilt when no longer needed.
@param[in] prebuilt prebuilt struct of a ha_innobase::table
handle */
void row_mysql_prebuilt_free_compress_heap(row_prebuilt_t *prebuilt) noexcept {
mem_heap_free(prebuilt->compress_heap);
prebuilt->compress_heap = nullptr;
}
/** Stores a >= 5.0.3 format true VARCHAR length to dest, in the MySQL row
format.
@return pointer to the data, we skip the 1 or 2 bytes at the start
that are used to store the len */
byte *row_mysql_store_true_var_len(
byte *dest, /*!< in: where to store */
ulint len, /*!< in: length, must fit in two bytes */
ulint lenlen) /*!< in: storage length of len: either 1 or 2 bytes */
{
if (lenlen == 2) {
ut_a(len < 256 * 256);
mach_write_to_2_little_endian(dest, len);
return (dest + 2);
}
ut_a(lenlen == 1);
ut_a(len < 256);
mach_write_to_1(dest, len);
return (dest + 1);
}
/** Reads a >= 5.0.3 format true VARCHAR length, in the MySQL row format, and
returns a pointer to the data.
@return pointer to the data, we skip the 1 or 2 bytes at the start
that are used to store the len */
const byte *row_mysql_read_true_varchar(
ulint *len, /*!< out: variable-length field length */
const byte *field, /*!< in: field in the MySQL format */
ulint lenlen) /*!< in: storage length of len: either 1
or 2 bytes */
{
if (lenlen == 2) {
*len = mach_read_from_2_little_endian(field);
return (field + 2);
}
ut_a(lenlen == 1);
*len = mach_read_from_1(field);
return (field + 1);
}
/**
Compressed BLOB header format:
---------------------------------------------------------------
| reserved | wrap | algorithm | len-len | compressed | unused |
| [1] | [1] | [5] | [3] | [1] | [5] |
---------------------------------------------------------------
| 0 0 | 1 1 | 2 6 | 7 9 | 10 10 | 11 15 |
---------------------------------------------------------------
* 'reserved' bit is planned to be used in future versions of the BLOB
header. In this version it must always be
'default_zip_column_reserved_value' (0).
* 'wrap' identifies if compression algorithm calculated a checksum
(adler32 in case of zlib) and appended it to the compressed data.
* 'algorithm' identifies which algoritm was used to compress this BLOB.
Currently, the only value 'default_zip_column_algorithm_value' (0) is
supported.
* 'len-len' field identifies the length of the column length data portion
followed by this header (see below).
* If 'compressed' bit is set to 1, then this header is immediately followed
by 1..8 bytes (depending on the value of 'len-len' bitfield) which
determine original (uncompressed) block size. These 'len-len' bytes are
followed by compressed representation of the original data.
* If 'compressed' bit is set to 0, every other bitfield ('wrap',
'algorithm' and 'le-len') must be ignored. In this case the header is
immediately followed by uncompressed (original) data.
*/
/**
Currently the only supported value for the 'reserved' field is
false (0).
*/
static constexpr bool default_zip_column_reserved_value = false;
/**
Currently the only supported value for the 'algorithm' field is 0, which
means 'zlib'.
*/
static constexpr uint default_zip_column_algorithm_value = 0;
static constexpr size_t zip_column_prefix_max_length =
ZIP_COLUMN_HEADER_LENGTH + 8;
static constexpr size_t zip_column_header_length = ZIP_COLUMN_HEADER_LENGTH;
/* 'reserved', bit 0 */
static constexpr uint zip_column_reserved = 0;
/* 0000 0000 0000 0001 */
static constexpr uint zip_column_reserved_mask = 0x0001;
/* 'wrap', bit 1 */
static constexpr uint zip_column_wrap = 1;
/* 0000 0000 0000 0010 */
static constexpr uint zip_column_wrap_mask = 0x0002;
/* 'algorithm', bit 2,3,4,5,6 */
static constexpr uint zip_column_algorithm = 2;
/* 0000 0000 0111 1100 */
static constexpr uint zip_column_algorithm_mask = 0x007C;
/* 'len-len', bit 7,8,9 */
static constexpr uint zip_column_data_length = 7;
/* 0000 0011 1000 0000 */
static constexpr uint zip_column_data_length_mask = 0x0380;
/* 'compressed', bit 10 */
static constexpr uint zip_column_compressed = 10;
/* 0000 0100 0000 0000 */
static constexpr uint zip_column_compressed_mask = 0x0400;
/** Updates compressed block header with the given components */
static void column_set_compress_header(byte *data, bool compressed,
ulint lenlen, uint alg, bool wrap,
bool reserved) noexcept {
ulint header = 0;
header |= (compressed << zip_column_compressed);
header |= (lenlen << zip_column_data_length);
header |= (alg << zip_column_algorithm);
header |= (wrap << zip_column_wrap);
header |= (reserved << zip_column_reserved);
mach_write_to_2(data, header);
}
/** Parse compressed block header into components */
static void column_get_compress_header(const byte *data, bool *compressed,
ulint *lenlen, uint *alg, bool *wrap,
bool *reserved) noexcept {
const ulint header = mach_read_from_2(data);
*compressed =
((header & zip_column_compressed_mask) >> zip_column_compressed);
*lenlen = ((header & zip_column_data_length_mask) >> zip_column_data_length);
*alg = ((header & zip_column_algorithm_mask) >> zip_column_algorithm);
*wrap = ((header & zip_column_wrap_mask) >> zip_column_wrap);
*reserved = ((header & zip_column_reserved_mask) >> zip_column_reserved);
}
/** Allocate memory for zlib.
@param[in,out] opaque memory heap
@param[in] items number of items to allocate
@param[in] size size of an item in bytes */
static void *column_zip_zalloc(void *opaque, uInt items, uInt size) noexcept {
return (mem_heap_zalloc(static_cast<mem_heap_t *>(opaque), items * size));
}
/** Deallocate memory for zlib.
@param[in] opaque memory heap
@param[in] address object to free */
static void column_zip_free(void *opaque MY_ATTRIBUTE((unused)),
void *address MY_ATTRIBUTE((unused))) noexcept {}
/** Configure the zlib allocator to use the given memory heap.
@param[in,out] stream zlib stream
@param[in] heap memory heap to use */
static void column_zip_set_alloc(void *stream, mem_heap_t *heap) noexcept {
auto *const strm = static_cast<z_stream *>(stream);
if (srv_compressed_columns_zlib_use_heap) {
strm->zalloc = column_zip_zalloc;
strm->zfree = column_zip_free;
strm->opaque = heap;
} else {
strm->zalloc = static_cast<alloc_func>(nullptr);
strm->zfree = static_cast<free_func>(nullptr);
strm->opaque = static_cast<voidpf>(nullptr);
}
}
/** Compress blob/text/varchar column using zlib
@param[in] data data in mysql (uncompressed) format
@param[in,out] len in: data length, out: length of compressed data
@param[in] lenlen bytes used to store the length of data
@param[in] dict_data optional dictionary data used for compression
@param[in] dict_data_len optional dictionary data length
@param[in] prebuilt use prebuilt->compress only here
@return pointer to the compressed data */
byte *row_compress_column(const byte *data, ulint *len, ulint lenlen,
const byte *dict_data, ulint dict_data_len,
row_prebuilt_t *prebuilt) {
int err = 0;
ulint comp_len = *len;
ulint buf_len = *len + zip_column_prefix_max_length;
byte *buf;
byte *ptr;
z_stream c_stream;
bool wrap = srv_compressed_columns_zlib_wrap;
int window_bits = wrap ? MAX_WBITS : -MAX_WBITS;
if (!prebuilt->compress_heap)
prebuilt->compress_heap = mem_heap_create(ut_max(UNIV_PAGE_SIZE, buf_len));
buf = static_cast<byte *>(mem_heap_zalloc(prebuilt->compress_heap, buf_len));
if (*len < srv_compressed_columns_threshold ||
srv_compressed_columns_zip_level == Z_NO_COMPRESSION)
goto do_not_compress;
ptr = buf + zip_column_header_length + lenlen;
/* init deflate object */
c_stream.next_in = const_cast<Bytef *>(data);
c_stream.avail_in = *len;
c_stream.next_out = ptr;
c_stream.avail_out = comp_len;
column_zip_set_alloc(&c_stream, prebuilt->compress_heap);
err = deflateInit2(&c_stream, srv_compressed_columns_zip_level, Z_DEFLATED,
window_bits, MAX_MEM_LEVEL,
srv_compressed_columns_zlib_strategy);
ut_a(err == Z_OK);
if (dict_data != 0 && dict_data_len != 0) {
err = deflateSetDictionary(&c_stream, dict_data, dict_data_len);
ut_a(err == Z_OK);
}
err = deflate(&c_stream, Z_FINISH);
if (err != Z_STREAM_END) {
deflateEnd(&c_stream);
if (err == Z_OK) err = Z_BUF_ERROR;
} else {
comp_len = c_stream.total_out;
err = deflateEnd(&c_stream);
}
switch (err) {
case Z_OK:
break;
case Z_BUF_ERROR:
/* data after compress is larger than uncompressed data*/
break;
default:
ib::error() << "failed to compress the column, error: " << err << '\n';
}
/* make sure the compressed data size is smaller than uncompressed data */
if (err == Z_OK && *len > (comp_len + zip_column_header_length + lenlen)) {
column_set_compress_header(buf, true, lenlen - 1,
default_zip_column_algorithm_value, wrap,
default_zip_column_reserved_value);
ptr = buf + zip_column_header_length;
/*store the uncompressed data length*/
switch (lenlen) {
case 1:
mach_write_to_1(ptr, *len);
break;
case 2:
mach_write_to_2(ptr, *len);
break;
case 3:
mach_write_to_3(ptr, *len);
break;
case 4:
mach_write_to_4(ptr, *len);
break;
default:
ut_error;
}
*len = comp_len + zip_column_header_length + lenlen;
return buf;
}
do_not_compress:
ptr = buf;
column_set_compress_header(ptr, false, 0, default_zip_column_algorithm_value,
false, default_zip_column_reserved_value);
ptr += zip_column_header_length;
memcpy(ptr, data, *len);
*len += zip_column_header_length;
return buf;
}
/** Uncompress blob/text/varchar column using zlib
@param[in] data data in InnoDB (compressed) format
@param[in,out] len in: data length; out: length of decompressed data
@param[in] dict_data optional dictionary data used for decompression
@param[in] dict_data_len optional dictionary data length
@param[in] prebuilt use prebuilt->compress_heap only here
@return pointer to the uncompressed data */
const byte *row_decompress_column(const byte *data, ulint *len,
const byte *dict_data, ulint dict_data_len,
row_prebuilt_t *prebuilt) {
ulint buf_len = 0;
byte *buf;
int err = 0;
int window_bits = 0;
z_stream d_stream;
bool is_compressed = false;
bool wrap = false;
bool reserved = false;
ulint lenlen = 0;
uint alg = 0;
ut_ad(*len != ULINT_UNDEFINED);
ut_ad(*len >= zip_column_header_length);
column_get_compress_header(data, &is_compressed, &lenlen, &alg, &wrap,
&reserved);
if (reserved != default_zip_column_reserved_value) {
ib::fatal() << "unsupported compressed BLOB header format\n";
}
if (alg != default_zip_column_algorithm_value) {
ib::fatal() << "unsupported 'algorithm' value in the compressed BLOB "
"header\n";
}
ut_a(lenlen < 4);
data += zip_column_header_length;
if (!is_compressed) { /* column not compressed */
*len -= zip_column_header_length;
return data;
}
lenlen++;
ulint comp_len = *len - zip_column_header_length - lenlen;
ulint uncomp_len = 0;
switch (lenlen) {
case 1:
uncomp_len = mach_read_from_1(data);
break;
case 2:
uncomp_len = mach_read_from_2(data);
break;
case 3:
uncomp_len = mach_read_from_3(data);
break;
case 4:
uncomp_len = mach_read_from_4(data);
break;
default:
ut_error;
}
data += lenlen;
/* data is compressed, decompress it*/
if (!prebuilt->compress_heap) {
prebuilt->compress_heap =
mem_heap_create(ut_max(UNIV_PAGE_SIZE, uncomp_len));
}
buf_len = uncomp_len;
buf = static_cast<byte *>(mem_heap_zalloc(prebuilt->compress_heap, buf_len));
/* init d_stream */
d_stream.next_in = const_cast<Bytef *>(data);
d_stream.avail_in = comp_len;
d_stream.next_out = buf;
d_stream.avail_out = buf_len;
column_zip_set_alloc(&d_stream, prebuilt->compress_heap);
window_bits = wrap ? MAX_WBITS : -MAX_WBITS;
err = inflateInit2(&d_stream, window_bits);
ut_a(err == Z_OK);
err = inflate(&d_stream, Z_FINISH);
if (err == Z_NEED_DICT) {
ut_a(dict_data != nullptr);
ut_a(dict_data_len != 0);
err = inflateSetDictionary(&d_stream, dict_data, dict_data_len);
ut_a(err == Z_OK);
err = inflate(&d_stream, Z_FINISH);
}
if (err != Z_STREAM_END) {
inflateEnd(&d_stream);
if (err == Z_BUF_ERROR && d_stream.avail_in == 0) err = Z_DATA_ERROR;
} else {
buf_len = d_stream.total_out;
err = inflateEnd(&d_stream);
}
switch (err) {
case Z_OK:
break;
case Z_BUF_ERROR:
ib::fatal() << "zlib buf error, this shouldn't happen\n";
break;
default:
ib::fatal() << "failed to decompress column, error: " << err << '\n';
}
if (err == Z_OK) {
if (buf_len != uncomp_len) {
ib::fatal() << "failed to decompress blob column, may be corrupted\n";
}
*len = buf_len;
return buf;
}
*len -= (zip_column_header_length + lenlen);
return data;
}
/** Stores a reference to a BLOB in the MySQL format. */
void row_mysql_store_blob_ref(
byte *dest, /*!< in: where to store */
ulint col_len, /*!< in: dest buffer size: determines into
how many bytes the BLOB length is stored,
the space for the length may vary from 1
to 4 bytes */
const void *data, /*!< in: BLOB data; if the value to store
is SQL NULL this should be NULL pointer */
ulint len, /*!< in: BLOB length; if the value to store
is SQL NULL this should be 0; remember
also to set the NULL bit in the MySQL record
header! */
bool need_decompression,
/*!< in: if the data need to be compressed*/
const byte *dict_data,
/*!< in: optional compression dictionary
data */
ulint dict_data_len,
/*!< in: optional compression dictionary data
length */
row_prebuilt_t *prebuilt)
/*<! in: use prebuilt->compress_heap only
here */
{
/* MySQL might assume the field is set to zero except the length and
the pointer fields */
memset(dest, '\0', col_len);
/* In dest there are 1 - 4 bytes reserved for the BLOB length,
and after that 8 bytes reserved for the pointer to the data.
In 32-bit architectures we only use the first 4 bytes of the pointer
slot. */
ut_a(col_len - 8 > 1 ||
len < 256 + (need_decompression ? ZIP_COLUMN_HEADER_LENGTH : 0));
ut_a(col_len - 8 > 2 ||
len < 256 * 256 + (need_decompression ? ZIP_COLUMN_HEADER_LENGTH : 0));
ut_a(col_len - 8 > 3 ||
len < 256 * 256 * 256 +
(need_decompression ? ZIP_COLUMN_HEADER_LENGTH : 0));
const byte *ptr = NULL;
if (need_decompression)
ptr = row_decompress_column((const byte *)data, &len, dict_data,
dict_data_len, prebuilt);
if (ptr)
memcpy(dest + col_len - 8, &ptr, sizeof ptr);
else
memcpy(dest + col_len - 8, &data, sizeof data);
mach_write_to_n_little_endian(dest, col_len - 8, len);
}
const byte *row_mysql_read_blob_ref(ulint *len, const byte *ref, ulint col_len,
bool need_compression,
const byte *dict_data, ulint dict_data_len,
row_prebuilt_t *prebuilt) {
byte *data = nullptr;
byte *ptr = nullptr;
*len = mach_read_from_n_little_endian(ref, col_len - 8);
memcpy(&data, ref + col_len - 8, sizeof data);
if (need_compression) {
ptr = row_compress_column(data, len, col_len - 8, dict_data, dict_data_len,
prebuilt);
if (ptr) data = ptr;
}
return (data);
}
/** Converting InnoDB geometry data format to MySQL data format. */
void row_mysql_store_geometry(
byte *dest, /*!< in/out: where to store */
ulint dest_len, /*!< in: dest buffer size: determines
into how many bytes the GEOMETRY length
is stored, the space for the length
may vary from 1 to 4 bytes */
const byte *src, /*!< in: GEOMETRY data; if the value to
store is SQL NULL this should be NULL
pointer */
ulint src_len) /*!< in: GEOMETRY length; if the value
to store is SQL NULL this should be 0;
remember also to set the NULL bit in
the MySQL record header! */
{
/* MySQL might assume the field is set to zero except the length and
the pointer fields */
UNIV_MEM_ASSERT_RW(src, src_len);
UNIV_MEM_ASSERT_W(dest, dest_len);
UNIV_MEM_INVALID(dest, dest_len);
memset(dest, '\0', dest_len);
/* In dest there are 1 - 4 bytes reserved for the BLOB length,
and after that 8 bytes reserved for the pointer to the data.
In 32-bit architectures we only use the first 4 bytes of the pointer
slot. */
ut_ad(dest_len - 8 > 1 || src_len < 1 << 8);
ut_ad(dest_len - 8 > 2 || src_len < 1 << 16);
ut_ad(dest_len - 8 > 3 || src_len < 1 << 24);
mach_write_to_n_little_endian(dest, dest_len - 8, src_len);
memcpy(dest + dest_len - 8, &src, sizeof src);
DBUG_EXECUTE_IF("row_print_geometry_data", {
String res;
Geometry_buffer buffer;
String wkt;
/** Show the meaning of geometry data. */
Geometry *g =
Geometry::construct(&buffer, (const char *)src, (uint32)src_len);
if (g) {
if (g->as_wkt(&wkt) == 0) {
ib::info(ER_IB_MSG_970) << "Write geometry data to"
" MySQL WKT format: "
<< wkt.c_ptr_safe() << ".";
}
}
});
}
/** Read geometry data in the MySQL format.
@return pointer to geometry data */
static const byte *row_mysql_read_geometry(
ulint *len, /*!< out: data length */
const byte *ref, /*!< in: geometry data in the
MySQL format */
ulint col_len) /*!< in: MySQL format length */
{
byte *data;
*len = mach_read_from_n_little_endian(ref, col_len - 8);
memcpy(&data, ref + col_len - 8, sizeof data);
DBUG_EXECUTE_IF("row_print_geometry_data", {
String res;
Geometry_buffer buffer;
String wkt;
/** Show the meaning of geometry data. */
Geometry *g =
Geometry::construct(&buffer, (const char *)data, (uint32)*len);
if (g) {
if (g->as_wkt(&wkt) == 0) {
ib::info(ER_IB_MSG_971) << "Read geometry data in"
" MySQL's WKT format: "
<< wkt.c_ptr_safe() << ".";
}
}
});
return (data);
}
/** Pad a column with spaces. */
void row_mysql_pad_col(ulint mbminlen, /*!< in: minimum size of a character,
in bytes */
byte *pad, /*!< out: padded buffer */
ulint len) /*!< in: number of bytes to pad */
{
const byte *pad_end;
switch (UNIV_EXPECT(mbminlen, 1)) {
default:
ut_error;
case 1:
/* space=0x20 */
memset(pad, 0x20, len);
break;
case 2:
/* space=0x0020 */
pad_end = pad + len;
ut_a(!(len % 2));
while (pad < pad_end) {
*pad++ = 0x00;
*pad++ = 0x20;
};
break;
case 4:
/* space=0x00000020 */
pad_end = pad + len;
ut_a(!(len % 4));
while (pad < pad_end) {
*pad++ = 0x00;
*pad++ = 0x00;
*pad++ = 0x00;
*pad++ = 0x20;
}
break;
}
}
/** Stores a non-SQL-NULL field given in the MySQL format in the InnoDB format.
The counterpart of this function is row_sel_field_store_in_mysql_format() in
row0sel.cc.
@return up to which byte we used buf in the conversion */
byte *row_mysql_store_col_in_innobase_format(
dfield_t *dfield, /*!< in/out: dfield where dtype
information must be already set when
this function is called! */
byte *buf, /*!< in/out: buffer for a converted
integer value; this must be at least
col_len long then! NOTE that dfield
may also get a pointer to 'buf',
therefore do not discard this as long
as dfield is used! */
ibool row_format_col, /*!< TRUE if the mysql_data is from
a MySQL row, FALSE if from a MySQL
key value;
in MySQL, a true VARCHAR storage
format differs in a row and in a
key value: in a key value the length
is always stored in 2 bytes! */
const byte *mysql_data, /*!< in: MySQL column value, not
SQL NULL; NOTE that dfield may also
get a pointer to mysql_data,
therefore do not discard this as long
as dfield is used! */
ulint col_len, /*!< in: MySQL column length; NOTE that
this is the storage length of the
column in the MySQL format row, not
necessarily the length of the actual
payload data; if the column is a true
VARCHAR then this is irrelevant */
ulint comp, /*!< in: nonzero=compact format */
bool need_compression,
/*!< in: if the data need to be
compressed*/
const byte *dict_data, /*!< in: optional compression
dictionary data */
ulint dict_data_len, /*!< in: optional compression
dictionary data length */
row_prebuilt_t *prebuilt) /*!< in: use prebuilt->compress_heap
only here */
{
const byte *ptr = mysql_data;
const dtype_t *dtype;
ulint type;
ulint lenlen;
dtype = dfield_get_type(dfield);
type = dtype->mtype;
if (type == DATA_INT) {
/* Store integer data in Innobase in a big-endian format,
sign bit negated if the data is a signed integer. In MySQL,
integers are stored in a little-endian format. */
byte *p = buf + col_len;
for (;;) {
p--;
*p = *mysql_data;
if (p == buf) {
break;
}
mysql_data++;
}
if (!(dtype->prtype & DATA_UNSIGNED)) {
*buf ^= 128;
}
ptr = buf;
buf += col_len;
} else if ((type == DATA_VARCHAR || type == DATA_VARMYSQL ||
type == DATA_BINARY)) {
if (dtype_get_mysql_type(dtype) == DATA_MYSQL_TRUE_VARCHAR) {
/* The length of the actual data is stored to 1 or 2
bytes at the start of the field */
if (row_format_col) {
if (dtype->prtype & DATA_LONG_TRUE_VARCHAR) {
lenlen = 2;
} else {
lenlen = 1;
}
} else {
/* In a MySQL key value, lenlen is always 2 */
lenlen = 2;
}
const byte *tmp_ptr =
row_mysql_read_true_varchar(&col_len, mysql_data, lenlen);
if (need_compression)
ptr = row_compress_column(tmp_ptr, &col_len, lenlen, dict_data,
dict_data_len, prebuilt);
else
ptr = tmp_ptr;
} else {
/* Remove trailing spaces from old style VARCHAR
columns. */
/* Handle Unicode strings differently. */
ulint mbminlen = dtype_get_mbminlen(dtype);
ptr = mysql_data;
switch (mbminlen) {
default:
ut_error;
case 4:
/* space=0x00000020 */
/* Trim "half-chars", just in case. */
col_len &= ~3;
while (col_len >= 4 && ptr[col_len - 4] == 0x00 &&
ptr[col_len - 3] == 0x00 && ptr[col_len - 2] == 0x00 &&
ptr[col_len - 1] == 0x20) {
col_len -= 4;
}
break;
case 2:
/* space=0x0020 */
/* Trim "half-chars", just in case. */
col_len &= ~1;
while (col_len >= 2 && ptr[col_len - 2] == 0x00 &&
ptr[col_len - 1] == 0x20) {
col_len -= 2;
}
break;
case 1:
/* space=0x20 */
while (col_len > 0 && ptr[col_len - 1] == 0x20) {
col_len--;
}
}
}
} else if (comp && type == DATA_MYSQL && dtype_get_mbminlen(dtype) == 1 &&
dtype_get_mbmaxlen(dtype) > 1) {
/* In some cases we strip trailing spaces from UTF-8 and other
multibyte charsets, from FIXED-length CHAR columns, to save
space. UTF-8 would otherwise normally use 3 * the string length
bytes to store an ASCII string! */
/* We assume that this CHAR field is encoded in a
variable-length character set where spaces have
1:1 correspondence to 0x20 bytes, such as UTF-8.
Consider a CHAR(n) field, a field of n characters.
It will contain between n * mbminlen and n * mbmaxlen bytes.
We will try to truncate it to n bytes by stripping
space padding. If the field contains single-byte
characters only, it will be truncated to n characters.
Consider a CHAR(5) field containing the string
".a " where "." denotes a 3-byte character represented
by the bytes "$%&". After our stripping, the string will
be stored as "$%&a " (5 bytes). The string
".abc " will be stored as "$%&abc" (6 bytes).
The space padding will be restored in row0sel.cc, function
row_sel_field_store_in_mysql_format(). */
ulint n_chars;
ut_a(!(dtype_get_len(dtype) % dtype_get_mbmaxlen(dtype)));
n_chars = dtype_get_len(dtype) / dtype_get_mbmaxlen(dtype);
/* Strip space padding. */
while (col_len > n_chars && ptr[col_len - 1] == 0x20) {
col_len--;
}
} else if (!row_format_col) {
/* if mysql data is from a MySQL key value
since the length is always stored in 2 bytes,
we need do nothing here. */
} else if (type == DATA_BLOB) {
ptr =
row_mysql_read_blob_ref(&col_len, mysql_data, col_len, need_compression,
dict_data, dict_data_len, prebuilt);
} else if (DATA_GEOMETRY_MTYPE(type)) {