-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathmi_dynrec.cc
1758 lines (1591 loc) · 59.4 KB
/
mi_dynrec.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, 2025, Oracle and/or its affiliates.
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 designed to work 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 either included with
the program or referenced in the documentation.
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 */
/*
Functions to handle space-packed-records and blobs
A row may be stored in one or more linked blocks.
The block size is between MI_MIN_BLOCK_LENGTH and MI_MAX_BLOCK_LENGTH.
Each block is aligned on MI_DYN_ALIGN_SIZE.
The reason for the max block size is to not have too many different types
of blocks. For the different block types, look at _mi_get_block_info()
*/
#include "my_config.h"
#include <fcntl.h>
#include <sys/types.h>
#include <algorithm>
#include "my_byteorder.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_inttypes.h"
#include "my_io.h"
#include "my_macros.h"
#include "my_pointer_arithmetic.h"
#include "sql/field.h"
#include "storage/myisam/myisam_sys.h"
#include "storage/myisam/myisamdef.h"
/* Enough for comparing if number is zero */
static char zero_string[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static int write_dynamic_record(MI_INFO *info, uchar *record, ulong reclength);
static int _mi_find_writepos(MI_INFO *info, ulong reclength, my_off_t *filepos,
ulong *length);
static int update_dynamic_record(MI_INFO *info, my_off_t filepos, uchar *record,
ulong reclength);
static int delete_dynamic_record(MI_INFO *info, my_off_t filepos,
uint second_read);
static int _mi_cmp_buffer(File file, const uchar *buff, my_off_t filepos,
uint length);
/* Interface function from MI_INFO */
/*
Create mmaped area for MyISAM handler
SYNOPSIS
mi_dynmap_file()
info MyISAM handler
RETURN
0 ok
1 error.
*/
bool mi_dynmap_file(MI_INFO *info, my_off_t size) {
DBUG_TRACE;
if (size == 0 || size > (my_off_t)(~((size_t)0))) {
if (size)
DBUG_PRINT("warning", ("File is too large for mmap"));
else
DBUG_PRINT("warning", ("Do not mmap zero-length"));
return true;
}
/*
I wonder if it is good to use MAP_NORESERVE. From the Linux man page:
MAP_NORESERVE
Do not reserve swap space for this mapping. When swap space is
reserved, one has the guarantee that it is possible to modify the
mapping. When swap space is not reserved one might get SIGSEGV
upon a write if no physical memory is available.
*/
info->s->file_map = (uchar *)my_mmap(
nullptr, (size_t)size,
info->s->mode == O_RDONLY ? PROT_READ : PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_NORESERVE, info->dfile, 0L);
if (info->s->file_map == (uchar *)MAP_FAILED) {
info->s->file_map = nullptr;
return true;
}
#if defined(HAVE_MADVISE)
madvise((char *)info->s->file_map, size, MADV_RANDOM);
#endif
info->s->mmaped_length = size;
info->s->file_read = mi_mmap_pread;
info->s->file_write = mi_mmap_pwrite;
return false;
}
/*
Destroy mmaped area for MyISAM handler
SYNOPSIS
mi_munmap_file()
info MyISAM handler
RETURN
0 ok
!0 error.
*/
int mi_munmap_file(MI_INFO *info) {
int ret;
DBUG_TRACE;
if ((ret = my_munmap((void *)info->s->file_map,
(size_t)info->s->mmaped_length)))
return ret;
info->s->file_read = mi_nommap_pread;
info->s->file_write = mi_nommap_pwrite;
info->s->file_map = nullptr;
info->s->mmaped_length = 0;
return 0;
}
/*
Resize mmaped area for MyISAM handler
SYNOPSIS
mi_remap_file()
info MyISAM handler
RETURN
*/
void mi_remap_file(MI_INFO *info, my_off_t size) {
if (info->s->file_map) {
mi_munmap_file(info);
mi_dynmap_file(info, size);
}
}
/*
Read bytes from MySAM handler, using mmap or pread
SYNOPSIS
mi_mmap_pread()
info MyISAM handler
Buffer Input buffer
Count Count of bytes for read
offset Start position
MyFlags
RETURN
0 ok
*/
size_t mi_mmap_pread(MI_INFO *info, uchar *Buffer, size_t Count,
my_off_t offset, myf MyFlags) {
DBUG_PRINT("info", ("mi_read with mmap %d\n", info->dfile));
if (info->s->concurrent_insert) mysql_rwlock_rdlock(&info->s->mmap_lock);
/*
The following test may fail in the following cases:
- We failed to remap a memory area (fragmented memory?)
- This thread has done some writes, but not yet extended the
memory mapped area.
*/
if (info->s->mmaped_length >= offset + Count) {
memcpy(Buffer, info->s->file_map + offset, Count);
if (info->s->concurrent_insert) mysql_rwlock_unlock(&info->s->mmap_lock);
return 0;
}
if (info->s->concurrent_insert) mysql_rwlock_unlock(&info->s->mmap_lock);
return mysql_file_pread(info->dfile, Buffer, Count, offset, MyFlags);
}
/* wrapper for mysql_file_pread in case if mmap isn't used */
size_t mi_nommap_pread(MI_INFO *info, uchar *Buffer, size_t Count,
my_off_t offset, myf MyFlags) {
return mysql_file_pread(info->dfile, Buffer, Count, offset, MyFlags);
}
/*
Write bytes to MySAM handler, using mmap or pwrite
SYNOPSIS
mi_mmap_pwrite()
info MyISAM handler
Buffer Output buffer
Count Count of bytes for write
offset Start position
MyFlags
RETURN
0 ok
!=0 error. In this case return error from pwrite
*/
size_t mi_mmap_pwrite(MI_INFO *info, const uchar *Buffer, size_t Count,
my_off_t offset, myf MyFlags) {
DBUG_PRINT("info", ("mi_write with mmap %d\n", info->dfile));
if (info->s->concurrent_insert) mysql_rwlock_rdlock(&info->s->mmap_lock);
/*
The following test may fail in the following cases:
- We failed to remap a memory area (fragmented memory?)
- This thread has done some writes, but not yet extended the
memory mapped area.
*/
if (info->s->mmaped_length >= offset + Count) {
memcpy(info->s->file_map + offset, Buffer, Count);
if (info->s->concurrent_insert) mysql_rwlock_unlock(&info->s->mmap_lock);
return 0;
}
info->s->nonmmaped_inserts++;
if (info->s->concurrent_insert) mysql_rwlock_unlock(&info->s->mmap_lock);
return mysql_file_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
}
/* wrapper for mysql_file_pwrite in case if mmap isn't used */
size_t mi_nommap_pwrite(MI_INFO *info, const uchar *Buffer, size_t Count,
my_off_t offset, myf MyFlags) {
return mysql_file_pwrite(info->dfile, Buffer, Count, offset, MyFlags);
}
int _mi_write_dynamic_record(MI_INFO *info, const uchar *record) {
ulong const reclength = _mi_rec_pack(info, info->rec_buff, record);
return (write_dynamic_record(info, info->rec_buff, reclength));
}
int _mi_update_dynamic_record(MI_INFO *info, my_off_t pos,
const uchar *record) {
uint const length = _mi_rec_pack(info, info->rec_buff, record);
return (update_dynamic_record(info, pos, info->rec_buff, length));
}
int _mi_write_blob_record(MI_INFO *info, const uchar *record) {
uchar *rec_buff;
int error;
ulong reclength, reclength2, extra;
extra = (ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER) + MI_SPLIT_LENGTH +
MI_DYN_DELETE_BLOCK_HEADER + 1);
reclength = (info->s->base.pack_reclength +
_my_calc_total_blob_length(info, record) + extra);
if (!(rec_buff = (uchar *)my_malloc(mi_key_memory_record_buffer, reclength,
MYF(0)))) {
set_my_errno(HA_ERR_OUT_OF_MEM); /* purecov: inspected */
return (-1);
}
reclength2 = _mi_rec_pack(
info, rec_buff + ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER), record);
DBUG_PRINT("info",
("reclength: %lu reclength2: %lu", reclength, reclength2));
assert(reclength2 <= reclength);
error = write_dynamic_record(
info, rec_buff + ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER), reclength2);
my_free(rec_buff);
return (error);
}
int _mi_update_blob_record(MI_INFO *info, my_off_t pos, const uchar *record) {
uchar *rec_buff;
int error;
ulong reclength, extra;
extra = (ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER) + MI_SPLIT_LENGTH +
MI_DYN_DELETE_BLOCK_HEADER);
reclength = (info->s->base.pack_reclength +
_my_calc_total_blob_length(info, record) + extra);
if (!(rec_buff = (uchar *)my_malloc(mi_key_memory_record_buffer, reclength,
MYF(0)))) {
set_my_errno(HA_ERR_OUT_OF_MEM); /* purecov: inspected */
return (-1);
}
reclength = _mi_rec_pack(info, rec_buff + ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER),
record);
error = update_dynamic_record(
info, pos, rec_buff + ALIGN_SIZE(MI_MAX_DYN_BLOCK_HEADER), reclength);
my_free(rec_buff);
return (error);
}
int _mi_delete_dynamic_record(MI_INFO *info) {
return delete_dynamic_record(info, info->lastpos, 0);
}
/* Write record to data-file */
static int write_dynamic_record(MI_INFO *info, uchar *record, ulong reclength) {
int flag;
ulong length;
my_off_t filepos;
DBUG_TRACE;
flag = 0;
/*
Check if we have enough room for the new record.
First we do simplified check to make usual case faster.
Then we do more precise check for the space left.
Though it still is not absolutely precise, as
we always use MI_MAX_DYN_BLOCK_HEADER while it can be
less in the most of the cases.
*/
if (unlikely(info->s->base.max_data_file_length -
info->state->data_file_length <
reclength + MI_MAX_DYN_BLOCK_HEADER)) {
if (info->s->base.max_data_file_length - info->state->data_file_length +
info->state->empty - info->state->del * MI_MAX_DYN_BLOCK_HEADER <
reclength + MI_MAX_DYN_BLOCK_HEADER) {
set_my_errno(HA_ERR_RECORD_FILE_FULL);
return 1;
}
}
do {
if (_mi_find_writepos(info, reclength, &filepos, &length)) goto err;
if (_mi_write_part_record(
info, filepos, length,
(info->append_insert_at_end ? HA_OFFSET_ERROR
: info->s->state.dellink),
&record, &reclength, &flag))
goto err;
} while (reclength);
return 0;
err:
return 1;
}
/* Get a block for data ; The given data-area must be used !! */
static int _mi_find_writepos(MI_INFO *info, ulong reclength, /* record length */
my_off_t *filepos, /* Return file pos */
ulong *length) /* length of block at filepos */
{
MI_BLOCK_INFO block_info;
ulong tmp;
DBUG_TRACE;
if (info->s->state.dellink != HA_OFFSET_ERROR &&
!info->append_insert_at_end) {
/* Deleted blocks exists; Get last used block */
*filepos = info->s->state.dellink;
block_info.second_read = 0;
info->rec_cache.seek_not_done = true;
if (!(_mi_get_block_info(&block_info, info->dfile, info->s->state.dellink) &
BLOCK_DELETED)) {
DBUG_PRINT("error", ("Delete link crashed"));
set_my_errno(HA_ERR_WRONG_IN_RECORD);
return -1;
}
info->s->state.dellink = block_info.next_filepos;
info->state->del--;
info->state->empty -= block_info.block_len;
*length = block_info.block_len;
} else {
/* No deleted blocks; Allocate a new block */
*filepos = info->state->data_file_length;
if ((tmp = reclength + 3 + (reclength >= (65520 - 3))) <
info->s->base.min_block_length)
tmp = info->s->base.min_block_length;
else
tmp = ((tmp + MI_DYN_ALIGN_SIZE - 1) & (~(ulong)(MI_DYN_ALIGN_SIZE - 1)));
if (info->state->data_file_length >
(info->s->base.max_data_file_length - tmp)) {
set_my_errno(HA_ERR_RECORD_FILE_FULL);
return -1;
}
if (tmp > MI_MAX_BLOCK_LENGTH) tmp = MI_MAX_BLOCK_LENGTH;
*length = tmp;
info->state->data_file_length += tmp;
info->s->state.split++;
info->update |= HA_STATE_WRITE_AT_END;
}
return 0;
} /* _mi_find_writepos */
/*
Unlink a deleted block from the deleted list.
This block will be combined with the preceding or next block to form
a big block.
*/
static bool unlink_deleted_block(MI_INFO *info, MI_BLOCK_INFO *block_info) {
DBUG_TRACE;
if (block_info->filepos == info->s->state.dellink) {
/* First deleted block; We can just use this ! */
info->s->state.dellink = block_info->next_filepos;
} else {
MI_BLOCK_INFO tmp;
tmp.second_read = 0;
/* Unlink block from the previous block */
if (!(_mi_get_block_info(&tmp, info->dfile, block_info->prev_filepos) &
BLOCK_DELETED))
return true; /* Something is wrong */
mi_sizestore(tmp.header + 4, block_info->next_filepos);
if (info->s->file_write(info, tmp.header + 4, 8,
block_info->prev_filepos + 4, MYF(MY_NABP)))
return true;
/* Unlink block from next block */
if (block_info->next_filepos != HA_OFFSET_ERROR) {
if (!(_mi_get_block_info(&tmp, info->dfile, block_info->next_filepos) &
BLOCK_DELETED))
return true; /* Something is wrong */
mi_sizestore(tmp.header + 12, block_info->prev_filepos);
if (info->s->file_write(info, tmp.header + 12, 8,
block_info->next_filepos + 12, MYF(MY_NABP)))
return true;
}
}
/* We now have one less deleted block */
info->state->del--;
info->state->empty -= block_info->block_len;
info->s->state.split--;
/*
If this was a block that we where accessing through table scan
(mi_rrnd() or mi_scan(), then ensure that we skip over this block
when doing next mi_rrnd() or mi_scan().
*/
if (info->nextpos == block_info->filepos)
info->nextpos += block_info->block_len;
return false;
}
/*
Add a backward link to delete block
SYNOPSIS
update_backward_delete_link()
info MyISAM handler
delete_block Position to delete block to update.
If this is 'HA_OFFSET_ERROR', nothing will be done
filepos Position to block that 'delete_block' should point to
RETURN
0 ok
1 error. In this case my_error is set.
*/
static int update_backward_delete_link(MI_INFO *info, my_off_t delete_block,
my_off_t filepos) {
MI_BLOCK_INFO block_info;
DBUG_TRACE;
if (delete_block != HA_OFFSET_ERROR) {
block_info.second_read = 0;
if (_mi_get_block_info(&block_info, info->dfile, delete_block) &
BLOCK_DELETED) {
uchar buff[8];
mi_sizestore(buff, filepos);
if (info->s->file_write(info, buff, 8, delete_block + 12, MYF(MY_NABP)))
return 1; /* Error on write */
} else {
set_my_errno(HA_ERR_WRONG_IN_RECORD);
return 1; /* Wrong delete link */
}
}
return 0;
}
/* Delete datarecord from database */
/* info->rec_cache.seek_not_done is updated in cmp_record */
static int delete_dynamic_record(MI_INFO *info, my_off_t filepos,
uint second_read) {
uint length, b_type;
MI_BLOCK_INFO block_info, del_block;
int error;
bool remove_next_block;
DBUG_TRACE;
/* First add a link from the last block to the new one */
error = update_backward_delete_link(info, info->s->state.dellink, filepos);
block_info.second_read = second_read;
do {
/* Remove block at 'filepos' */
if ((b_type = _mi_get_block_info(&block_info, info->dfile, filepos)) &
(BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
BLOCK_FATAL_ERROR) ||
(length = (uint)(block_info.filepos - filepos) + block_info.block_len) <
MI_MIN_BLOCK_LENGTH) {
set_my_errno(HA_ERR_WRONG_IN_RECORD);
return 1;
}
/* Check if next block is a delete block */
del_block.second_read = 0;
remove_next_block = false;
if (_mi_get_block_info(&del_block, info->dfile, filepos + length) &
BLOCK_DELETED &&
del_block.block_len + length < MI_DYN_MAX_BLOCK_LENGTH) {
/* We can't remove this yet as this block may be the head block */
remove_next_block = true;
length += del_block.block_len;
}
block_info.header[0] = 0;
mi_int3store(block_info.header + 1, length);
mi_sizestore(block_info.header + 4, info->s->state.dellink);
if (b_type & BLOCK_LAST)
memset(block_info.header + 12, 255, 8);
else
mi_sizestore(block_info.header + 12, block_info.next_filepos);
if (info->s->file_write(info, (uchar *)block_info.header, 20, filepos,
MYF(MY_NABP)))
return 1;
info->s->state.dellink = filepos;
info->state->del++;
info->state->empty += length;
filepos = block_info.next_filepos;
/* Now it's safe to unlink the deleted block directly after this one */
if (remove_next_block && unlink_deleted_block(info, &del_block)) error = 1;
} while (!(b_type & BLOCK_LAST));
return error;
}
/* Write a block to datafile */
int _mi_write_part_record(MI_INFO *info,
my_off_t filepos, /* points at empty block */
ulong length, /* length of block */
my_off_t next_filepos, /* Next empty block */
uchar **record, /* pointer to record ptr */
ulong *reclength, /* length of *record */
int *flag) /* *flag == 0 if header */
{
ulong head_length, res_length, extra_length, long_block, del_length;
uchar *pos, *record_end;
my_off_t next_delete_block;
uchar temp[MI_SPLIT_LENGTH + MI_DYN_DELETE_BLOCK_HEADER];
DBUG_TRACE;
next_delete_block = HA_OFFSET_ERROR;
res_length = extra_length = 0;
if (length > *reclength + MI_SPLIT_LENGTH) { /* Splitt big block */
res_length = MY_ALIGN(length - *reclength - MI_EXTEND_BLOCK_LENGTH,
MI_DYN_ALIGN_SIZE);
length -= res_length; /* Use this for first part */
}
long_block = (length < 65520L && *reclength < 65520L) ? 0 : 1;
if (length == *reclength + 3 + long_block) {
/* Block is exactly of the right length */
temp[0] = (uchar)(1 + *flag) + (uchar)long_block; /* Flag is 0 or 6 */
if (long_block) {
mi_int3store(temp + 1, *reclength);
head_length = 4;
} else {
mi_int2store(temp + 1, *reclength);
head_length = 3;
}
} else if (length - long_block < *reclength + 4) { /* To short block */
if (next_filepos == HA_OFFSET_ERROR)
next_filepos = (info->s->state.dellink != HA_OFFSET_ERROR &&
!info->append_insert_at_end
? info->s->state.dellink
: info->state->data_file_length);
if (*flag == 0) /* First block */
{
if (*reclength > MI_MAX_BLOCK_LENGTH) {
head_length = 16;
temp[0] = 13;
mi_int4store(temp + 1, *reclength);
mi_int3store(temp + 5, length - head_length);
mi_sizestore((uchar *)temp + 8, next_filepos);
} else {
head_length = 5 + 8 + long_block * 2;
temp[0] = 5 + (uchar)long_block;
if (long_block) {
mi_int3store(temp + 1, *reclength);
mi_int3store(temp + 4, length - head_length);
mi_sizestore((uchar *)temp + 7, next_filepos);
} else {
mi_int2store(temp + 1, *reclength);
mi_int2store(temp + 3, length - head_length);
mi_sizestore((uchar *)temp + 5, next_filepos);
}
}
} else {
head_length = 3 + 8 + long_block;
temp[0] = 11 + (uchar)long_block;
if (long_block) {
mi_int3store(temp + 1, length - head_length);
mi_sizestore((uchar *)temp + 4, next_filepos);
} else {
mi_int2store(temp + 1, length - head_length);
mi_sizestore((uchar *)temp + 3, next_filepos);
}
}
} else { /* Block with empty info last */
head_length = 4 + long_block;
extra_length = length - *reclength - head_length;
temp[0] = (uchar)(3 + *flag) + (uchar)long_block; /* 3,4 or 9,10 */
if (long_block) {
mi_int3store(temp + 1, *reclength);
temp[4] = (uchar)(extra_length);
} else {
mi_int2store(temp + 1, *reclength);
temp[3] = (uchar)(extra_length);
}
length = *reclength + head_length; /* Write only what is needed */
}
DBUG_DUMP("header", (uchar *)temp, head_length);
/* Make a long block for one write */
record_end = *record + length - head_length;
del_length = (res_length ? MI_DYN_DELETE_BLOCK_HEADER : 0);
memmove((uchar *)(*record - head_length), (uchar *)temp, head_length);
memcpy(temp, record_end, (size_t)(extra_length + del_length));
memset(record_end, 0, extra_length);
if (res_length) {
/* Check first if we can join this block with the next one */
MI_BLOCK_INFO del_block;
my_off_t const next_block = filepos + length + extra_length + res_length;
del_block.second_read = 0;
if (next_block < info->state->data_file_length &&
info->s->state.dellink != HA_OFFSET_ERROR) {
if ((_mi_get_block_info(&del_block, info->dfile, next_block) &
BLOCK_DELETED) &&
res_length + del_block.block_len < MI_DYN_MAX_BLOCK_LENGTH) {
if (unlink_deleted_block(info, &del_block)) goto err;
res_length += del_block.block_len;
}
}
/* Create a delete link of the last part of the block */
pos = record_end + extra_length;
pos[0] = '\0';
mi_int3store(pos + 1, res_length);
mi_sizestore(pos + 4, info->s->state.dellink);
memset(pos + 12, 255, 8); /* End link */
next_delete_block = info->s->state.dellink;
info->s->state.dellink = filepos + length + extra_length;
info->state->del++;
info->state->empty += res_length;
info->s->state.split++;
}
if (info->opt_flag & WRITE_CACHE_USED &&
info->update & HA_STATE_WRITE_AT_END) {
if (info->update & HA_STATE_EXTEND_BLOCK) {
info->update &= ~HA_STATE_EXTEND_BLOCK;
if (my_block_write(&info->rec_cache, (uchar *)*record - head_length,
length + extra_length + del_length, filepos))
goto err;
} else if (my_b_write(&info->rec_cache, (uchar *)*record - head_length,
length + extra_length + del_length))
goto err;
} else {
info->rec_cache.seek_not_done = true;
if (info->s->file_write(info, (uchar *)*record - head_length,
length + extra_length + del_length, filepos,
info->s->write_flag))
goto err;
}
memcpy(record_end, temp, (size_t)(extra_length + del_length));
*record = record_end;
*reclength -= (length - head_length);
*flag = 6;
if (del_length) {
/* link the next delete block to this */
if (update_backward_delete_link(info, next_delete_block,
info->s->state.dellink))
goto err;
}
return 0;
err:
DBUG_PRINT("exit", ("errno: %d", my_errno()));
return 1;
} /*_mi_write_part_record */
/* update record from datafile */
static int update_dynamic_record(MI_INFO *info, my_off_t filepos, uchar *record,
ulong reclength) {
int flag;
uint error;
ulong length;
MI_BLOCK_INFO block_info;
DBUG_TRACE;
flag = block_info.second_read = 0;
/*
Check if we have enough room for the record.
First we do simplified check to make usual case faster.
Then we do more precise check for the space left.
Though it still is not absolutely precise, as
we always use MI_MAX_DYN_BLOCK_HEADER while it can be
less in the most of the cases.
*/
/*
compare with just the reclength as we're going
to get some space from the old replaced record
*/
if (unlikely(info->s->base.max_data_file_length -
info->state->data_file_length <
reclength)) {
/*
let's read the old record's block to find out the length of the
old record
*/
if ((error = _mi_get_block_info(&block_info, info->dfile, filepos)) &
(BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR | BLOCK_FATAL_ERROR)) {
DBUG_PRINT("error", ("Got wrong block info"));
if (!(error & BLOCK_FATAL_ERROR)) set_my_errno(HA_ERR_WRONG_IN_RECORD);
goto err;
}
/*
if new record isn't longer, we can go on safely
*/
if (block_info.rec_len < reclength) {
if (info->s->base.max_data_file_length - info->state->data_file_length +
info->state->empty - info->state->del * MI_MAX_DYN_BLOCK_HEADER <
reclength - block_info.rec_len + MI_MAX_DYN_BLOCK_HEADER) {
set_my_errno(HA_ERR_RECORD_FILE_FULL);
goto err;
}
}
block_info.second_read = 0;
}
while (reclength > 0) {
if (filepos != info->s->state.dellink) {
block_info.next_filepos = HA_OFFSET_ERROR;
if ((error = _mi_get_block_info(&block_info, info->dfile, filepos)) &
(BLOCK_DELETED | BLOCK_ERROR | BLOCK_SYNC_ERROR |
BLOCK_FATAL_ERROR)) {
DBUG_PRINT("error", ("Got wrong block info"));
if (!(error & BLOCK_FATAL_ERROR)) set_my_errno(HA_ERR_WRONG_IN_RECORD);
goto err;
}
length = (ulong)(block_info.filepos - filepos) + block_info.block_len;
if (length < reclength) {
uint tmp = MY_ALIGN(reclength - length + 3 + (reclength >= 65520L),
MI_DYN_ALIGN_SIZE);
/* Don't create a block bigger than MI_MAX_BLOCK_LENGTH */
tmp = std::min(length + tmp, MI_MAX_BLOCK_LENGTH) - length;
/* Check if we can extend this block */
if (block_info.filepos + block_info.block_len ==
info->state->data_file_length &&
info->state->data_file_length <
info->s->base.max_data_file_length - tmp) {
/* extend file */
DBUG_PRINT("info", ("Extending file with %d bytes", tmp));
if (info->nextpos == info->state->data_file_length)
info->nextpos += tmp;
info->state->data_file_length += tmp;
info->update |= HA_STATE_WRITE_AT_END | HA_STATE_EXTEND_BLOCK;
length += tmp;
} else if (length < MI_MAX_BLOCK_LENGTH - MI_MIN_BLOCK_LENGTH) {
/*
Check if next block is a deleted block
Above we have MI_MIN_BLOCK_LENGTH to avoid the problem where
the next block is so small it can't be splited which could
cause problems
*/
MI_BLOCK_INFO del_block;
del_block.second_read = 0;
if (_mi_get_block_info(&del_block, info->dfile,
block_info.filepos + block_info.block_len) &
BLOCK_DELETED) {
/* Use; Unlink it and extend the current block */
DBUG_PRINT("info", ("Extending current block"));
if (unlink_deleted_block(info, &del_block)) goto err;
if ((length += del_block.block_len) > MI_MAX_BLOCK_LENGTH) {
/*
New block was too big, link overflow part back to
delete list
*/
my_off_t next_pos;
ulong rest_length = length - MI_MAX_BLOCK_LENGTH;
rest_length = std::max(rest_length, ulong(MI_MIN_BLOCK_LENGTH));
next_pos = del_block.filepos + del_block.block_len - rest_length;
if (update_backward_delete_link(info, info->s->state.dellink,
next_pos))
return 1;
/* create delete link for data that didn't fit into the page */
del_block.header[0] = 0;
mi_int3store(del_block.header + 1, rest_length);
mi_sizestore(del_block.header + 4, info->s->state.dellink);
memset(del_block.header + 12, 255, 8);
if (info->s->file_write(info, (uchar *)del_block.header, 20,
next_pos, MYF(MY_NABP)))
return 1;
info->s->state.dellink = next_pos;
info->s->state.split++;
info->state->del++;
info->state->empty += rest_length;
length -= rest_length;
}
}
}
}
} else {
if (_mi_find_writepos(info, reclength, &filepos, &length)) goto err;
}
if (_mi_write_part_record(info, filepos, length, block_info.next_filepos,
&record, &reclength, &flag))
goto err;
if ((filepos = block_info.next_filepos) == HA_OFFSET_ERROR) {
/* Start writing data on deleted blocks */
filepos = info->s->state.dellink;
}
}
if (block_info.next_filepos != HA_OFFSET_ERROR) {
/*
delete_dynamic_record() may change data file position.
IO cache must be notified as it may still have cached
data, which has to be flushed later.
*/
info->rec_cache.seek_not_done = true;
if (delete_dynamic_record(info, block_info.next_filepos, 1)) goto err;
}
return 0;
err:
return 1;
}
/* Pack a record. Return new reclength */
uint _mi_rec_pack(MI_INFO *info, uchar *to, const uchar *from) {
uint length, new_length, flag, bit, i;
const uchar *pos, *end, *startpos;
uchar *packpos;
enum en_fieldtype type;
MI_COLUMNDEF *rec;
MI_BLOB *blob;
DBUG_TRACE;
flag = 0;
bit = 1;
startpos = packpos = to;
to += info->s->base.pack_bits;
blob = info->blobs;
rec = info->s->rec;
for (i = info->s->base.fields; i-- > 0; from += length, rec++) {
length = (uint)rec->length;
if ((type = (enum en_fieldtype)rec->type) != FIELD_NORMAL) {
if (type == FIELD_BLOB) {
if (!blob->length)
flag |= bit;
else {
char *temp_pos;
size_t const tmp_length = length - portable_sizeof_char_ptr;
memcpy((uchar *)to, from, tmp_length);
memcpy(&temp_pos, from + tmp_length, sizeof(char *));
memcpy(to + tmp_length, temp_pos, (size_t)blob->length);
to += tmp_length + blob->length;
}
blob++;
} else if (type == FIELD_SKIP_ZERO) {
if (memcmp(from, zero_string, length) == 0)
flag |= bit;
else {
memcpy((uchar *)to, from, (size_t)length);
to += length;
}
} else if (type == FIELD_SKIP_ENDSPACE || type == FIELD_SKIP_PRESPACE) {
pos = from;
end = from + length;
if (type == FIELD_SKIP_ENDSPACE) { /* Pack trailing spaces */
while (end > from && *(end - 1) == ' ') end--;
} else { /* Pack pref-spaces */
while (pos < end && *pos == ' ') pos++;
}
new_length = (uint)(end - pos);
if (new_length + 1 + (rec->length > 255 && new_length > 127) < length) {
if (rec->length > 255 && new_length > 127) {
to[0] = (uchar)((new_length & 127) + 128);
to[1] = (uchar)(new_length >> 7);
to += 2;
} else
*to++ = (uchar)new_length;
memcpy((uchar *)to, pos, (size_t)new_length);
to += new_length;
flag |= bit;
} else {
memcpy(to, from, (size_t)length);
to += length;
}
} else if (type == FIELD_VARCHAR) {
uint const pack_length = HA_VARCHAR_PACKLENGTH(rec->length - 1);
uint tmp_length;
if (pack_length == 1) {
tmp_length = (uint)*from;
*to++ = *from;
} else {
tmp_length = uint2korr(from);
store_key_length_inc(to, tmp_length);
}
memcpy(to, from + pack_length, tmp_length);
to += tmp_length;
continue;
} else {
memcpy(to, from, (size_t)length);
to += length;
continue; /* Normal field */
}
if ((bit = bit << 1) >= 256) {
*packpos++ = (uchar)flag;
bit = 1;
flag = 0;
}
} else {
memcpy(to, from, (size_t)length);
to += length;
}
}
if (bit != 1) *packpos = (uchar)flag;
if (info->s->calc_checksum) *to++ = (uchar)info->checksum;
DBUG_PRINT("exit", ("packed length: %d", (int)(to - startpos)));
return (uint)(to - startpos);
} /* _mi_rec_pack */
/*
Check if a record was correctly packed. Used only by myisamchk
Returns 0 if record is ok.
*/
bool _mi_rec_check(MI_INFO *info, const uchar *record, uchar *rec_buff,
ulong packed_length, bool with_checksum) {
uint length, new_length, flag, bit, i;
const uchar *pos, *end, *packpos, *to;
enum en_fieldtype type;
MI_COLUMNDEF *rec;
DBUG_TRACE;
packpos = rec_buff;
to = rec_buff + info->s->base.pack_bits;
rec = info->s->rec;
flag = *packpos;
bit = 1;
for (i = info->s->base.fields; i-- > 0; record += length, rec++) {
length = (uint)rec->length;
if ((type = (enum en_fieldtype)rec->type) != FIELD_NORMAL) {
if (type == FIELD_BLOB) {
uint const blob_length =
_mi_calc_blob_length(length - portable_sizeof_char_ptr, record);
if (!blob_length && !(flag & bit)) goto err;
if (blob_length) to += length - portable_sizeof_char_ptr + blob_length;
} else if (type == FIELD_SKIP_ZERO) {
if (memcmp(record, zero_string, length) == 0) {
if (!(flag & bit)) goto err;
} else
to += length;
} else if (type == FIELD_SKIP_ENDSPACE || type == FIELD_SKIP_PRESPACE) {
pos = record;
end = record + length;
if (type == FIELD_SKIP_ENDSPACE) { /* Pack trailing spaces */
while (end > record && *(end - 1) == ' ') end--;
} else { /* Pack pre-spaces */
while (pos < end && *pos == ' ') pos++;
}
new_length = (uint)(end - pos);
if (new_length + 1 + (rec->length > 255 && new_length > 127) < length) {
if (!(flag & bit)) goto err;
if (rec->length > 255 && new_length > 127) {
/* purecov: begin inspected */
if (to[0] != (uchar)((new_length & 127) + 128) ||
to[1] != (uchar)(new_length >> 7))
goto err;
to += 2;
/* purecov: end */
} else if (*to++ != (uchar)new_length)
goto err;
to += new_length;