/
ma_blockrec.c
7554 lines (6654 loc) · 248 KB
/
ma_blockrec.c
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) 2007-2008 Michael Widenius
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/*
Storage of records in block
Some clarifications about the abbrev used:
NULL fields -> Fields that may have contain a NULL value.
Not null fields -> Fields that may not contain a NULL value.
Critical fields -> Fields that can't be null and can't be dropped without
causing a table reorganization.
Maria will have a LSN at start of each page (excluding the bitmap pages)
The different page types that are in a data file are:
Bitmap pages Map of free pages in the next extent (8192 page size
gives us 256M of mapped pages / bitmap)
Head page Start of rows are stored on this page.
A rowid always points to a head page
Blob page This page is totally filled with data from one blob or by
a set of long VARCHAR/CHAR fields
Tail page This contains the last part from different rows, blobs
or varchar fields.
The data file starts with a bitmap page, followed by as many data
pages as the bitmap can cover. After this there is a new bitmap page
and more data pages etc.
For information about the bitmap page, see ma_bitmap.c
Structure of data and tail page:
The page has a row directory at end of page to allow us to do deletes
without having to reorganize the page. It also allows us to later store
some more bytes after each row to allow them to grow without having to move
around other rows.
Page header:
LSN 7 bytes Log position for last page change
PAGE_TYPE 1 uchar 1 for head / 2 for tail / 3 for blob
DIR_COUNT 1 uchar Number of row/tail entries on page
FREE_DIR_LINK 1 uchar Pointer to first free director entry or 255 if no
empty space 2 bytes Empty space on page
The most significant bit in PAGE_TYPE is set to 1 if the data on the page
can be compacted to get more space. (PAGE_CAN_BE_COMPACTED)
Row data
Row directory of NO entries, that consist of the following for each row
(in reverse order; i.e., first record is stored last):
Position 2 bytes Position of row on page
Length 2 bytes Length of entry
For Position and Length, the 1 most significant bit of the position and
the 1 most significant bit of the length could be used for some states of
the row (in other words, we should try to keep these reserved)
Position is 0 if the entry is not used. In this case length[0] points
to a previous free entry (255 if no previous entry) and length[1]
to the next free entry (or 255 if last free entry). This works because
the directory entry 255 can never be marked free (if the first directory
entry is freed, the directory is shrinked).
checksum 4 bytes Reserved for full page read testing and live backup.
----------------
Structure of blob pages:
LSN 7 bytes Log position for last page change
PAGE_TYPE 1 uchar 3
data
-----------------
Row data structure:
Flag 1 uchar Marker of which header field exists
TRANSID 6 bytes TRANSID of changing transaction
(optional, added on insert and first
update/delete)
VER_PTR 7 bytes Pointer to older version in log
(undo record)
(optional, added after first
update/delete)
DELETE_TRANSID 6 bytes (optional). TRANSID of original row.
Added on delete.
Nulls_extended 1 uchar To allow us to add new DEFAULT NULL
fields (optional, added after first
change of row after alter table)
Number of ROW_EXTENT's 1-3 uchar Length encoded, optional
This is the number of extents the
row is split into
First row_extent 7 uchar Pointer to first row extent (optional)
Total length of length array 1-3 uchar Only used if we have
char/varchar/blob fields.
Row checksum 1 uchar Only if table created with checksums
Null_bits .. One bit for each NULL field (a field that may
have the value NULL)
Empty_bits .. One bit for each field that may be 'empty'.
(Both for null and not null fields).
This bit is 1 if the value for the field is
0 or empty string.
field_offsets 2 byte/offset
For each 32'th field, there is one offset
that points to where the field information
starts in the block. This is to provide
fast access to later field in the row
when we only need to return a small
set of fields.
TODO: Implement this.
Things marked above as 'optional' will only be present if the
corresponding bit is set in 'Flag' field. Flag gives us a way to
get more space on a page when doing page compaction as we don't need
to store TRANSID that have committed before the smallest running
transaction we have in memory.
Data in the following order:
(Field order is precalculated when table is created)
Critical fixed length, not null, fields. (Note, these can't be dropped)
Fixed length, null fields
Length array, 1-4 uchar per field for all CHAR/VARCHAR/BLOB fields.
Number of bytes used in length array per entry is depending on max length
for field.
ROW_EXTENT's
CHAR data (space stripped)
VARCHAR data
BLOB data
Fields marked in null_bits or empty_bits are not stored in data part or
length array.
If row doesn't fit into the given block, then the first EXTENT will be
stored last on the row. This is done so that we don't break any field
data in the middle.
We first try to store the full row into one block. If that's not possible
we move out each big blob into their own extents. If this is not enough we
move out a concatenation of all varchars to their own extent.
Each blob and the concatenated char/varchar fields are stored the following
way:
- Store the parts in as many full-contiguous pages as possible.
- The last part, that doesn't fill a full page, is stored in tail page.
When doing an insert of a new row, we don't have to have
VER_PTR in the row. This will make rows that are not changed stored
efficiently. On update and delete we would add TRANSID (if it was an old
committed row) and VER_PTR to
the row. On row page compaction we can easily detect rows where
TRANSID was committed before the longest running transaction
started and we can then delete TRANSID and VER_PTR from the row to
gain more space.
If a row is deleted in Maria, we change TRANSID to the deleting
transaction's id, change VER_PTR to point to the undo record for the delete,
and add DELETE_TRANSID (the id of the transaction which last
inserted/updated the row before its deletion). DELETE_TRANSID allows an old
transaction to avoid reading the log to know if it can see the last version
before delete (in other words it reduces the probability of having to follow
VER_PTR). TODO: depending on a compilation option, evaluate the performance
impact of not storing DELETE_TRANSID (which would make the row smaller).
Description of the different parts:
Flag is coded as:
Description bit
TRANS_ID_exists 0
VER_PTR_exists 1
Row is deleted 2 (Means that DELETE_TRANSID exists)
Nulls_extended_exists 3
Row is split 7 This means that 'Number_of_row_extents' exists
Nulls_extended is the number of new DEFAULT NULL fields in the row
compared to the number of DEFAULT NULL fields when the first version
of the table was created. If Nulls_extended doesn't exist in the row,
we know it's 0 as this must be one of the original rows from when the
table was created first time. This coding allows us to add 255*8 =
2048 new fields without requiring a full alter table.
Empty_bits is used to allow us to store 0, 0.0, empty string, empty
varstring and empty blob efficiently. (This is very good for data
warehousing where NULL's are often regarded as evil). Having this
bitmap also allows us to drop information of a field during a future
delete if field was deleted with ALTER TABLE DROP COLUMN. To be able
to handle DROP COLUMN, we must store in the index header the fields
that has been dropped. When unpacking a row we will ignore dropped
fields. When storing a row, we will mark a dropped field either with a
null in the null bit map or in the empty_bits and not store any data
for it.
TODO: Add code for handling dropped fields.
A ROW EXTENT is range of pages. One ROW_EXTENT is coded as:
START_PAGE 5 bytes
PAGE_COUNT 2 bytes. Bit 16 is set if this is a tail page.
Bit 15 is to set if this is start of a new
blob extent.
With 8K pages, we can cover 256M in one extent. This coding gives us a
maximum file size of 2^40*8192 = 8192 tera
As an example of ROW_EXTENT handling, assume a row with one integer
field (value 5), two big VARCHAR fields (size 250 and 8192*3), and 2
big BLOB fields that we have updated.
The record format for storing this into an empty file would be:
Page 1:
00 00 00 00 00 00 00 LSN
01 Only one row in page
FF No free dir entry
xx xx Empty space on page
10 Flag: row split, VER_PTR exists
01 00 00 00 00 00 TRANSID 1
00 00 00 00 00 01 00 VER_PTR to first block in LOG file 1
5 Number of row extents
02 00 00 00 00 03 00 VARCHAR's are stored in full pages 2,3,4
0 No null fields
0 No empty fields
05 00 00 00 00 00 80 Tail page for VARCHAR, rowid 0
06 00 00 00 00 80 00 First blob, stored at page 6-133
05 00 00 00 00 01 80 Tail of first blob (896 bytes) at page 5
86 00 00 00 00 80 00 Second blob, stored at page 134-262
05 00 00 00 00 02 80 Tail of second blob (896 bytes) at page 5
05 00 5 integer
FA Length of first varchar field (size 250)
00 60 Length of second varchar field (size 8192*3)
00 60 10 First medium BLOB, 1M
01 00 10 00 Second BLOB, 1M
xx xx xx xx xx xx Varchars are stored here until end of page
..... until end of page
09 00 F4 1F Start position 9, length 8180
xx xx xx xx Checksum
A data page is allowed to have a wrong CRC and header as long as it is
marked empty in the bitmap and its directory's count is 0.
*/
#include "maria_def.h"
#include "ma_blockrec.h"
#include "trnman.h"
#include "ma_key_recover.h"
#include "ma_recovery_util.h"
#include <lf.h>
/*
Struct for having a cursor over a set of extent.
This is used to loop over all extents for a row when reading
the row data. It's also used to store the tail positions for
a read row to be used by a later update/delete command.
*/
typedef struct st_maria_extent_cursor
{
/*
Pointer to packed uchar array of extents for the row.
Format is described above in the header
*/
uchar *extent;
/* Where data starts on page; Only for debugging */
uchar *data_start;
/* Position to all tails in the row. Updated when reading a row */
MARIA_RECORD_POS *tail_positions;
/* Current page */
pgcache_page_no_t page;
/* How many pages in the page region */
uint page_count;
/* What kind of lock to use for tail pages */
enum pagecache_page_lock lock_for_tail_pages;
/* Total number of extents (i.e., entries in the 'extent' slot) */
uint extent_count;
/* <> 0 if current extent is a tail page; Set while using cursor */
uint tail;
/* Position for tail on tail page */
uint tail_row_nr;
/*
== 1 if we are working on the first extent (i.e., the one that is stored in
the row header, not an extent that is stored as part of the row data).
*/
my_bool first_extent;
} MARIA_EXTENT_CURSOR;
/**
@brief Structure for passing down info to write_hook_for_clr_end().
This hooks needs to know the variation of the live checksum caused by the
current operation to update state.checksum under log's mutex,
needs to know the transaction's previous undo_lsn to set
trn->undo_lsn under log mutex, and needs to know the type of UNDO being
undone now to modify state.records under log mutex.
*/
/** S:share,D:checksum_delta,E:expression,P:pointer_into_record,L:length */
#define store_checksum_in_rec(S,D,E,P,L) do \
{ \
D= 0; \
if ((S)->calc_checksum != NULL) \
{ \
D= (E); \
ha_checksum_store(P, D); \
L+= HA_CHECKSUM_STORE_SIZE; \
} \
} while (0)
static my_bool delete_tails(MARIA_HA *info, MARIA_RECORD_POS *tails);
static my_bool delete_head_or_tail(MARIA_HA *info,
pgcache_page_no_t page, uint record_number,
my_bool head, my_bool from_update);
#ifndef DBUG_OFF
static void _ma_print_directory(MARIA_SHARE *share,
FILE *file, uchar *buff, uint block_size);
#endif
static uchar *store_page_range(MARIA_SHARE *share,
uchar *to, MARIA_BITMAP_BLOCK *block,
ulong length,
uint *tot_ranges);
static size_t fill_insert_undo_parts(MARIA_HA *info, const uchar *record,
LEX_CUSTRING *log_parts,
uint *log_parts_count);
static size_t fill_update_undo_parts(MARIA_HA *info, const uchar *oldrec,
const uchar *newrec,
LEX_CUSTRING *log_parts,
uint *log_parts_count);
/****************************************************************************
Initialization
****************************************************************************/
/*
Initialize data needed for block structures
*/
/* Size of the different header elements for a row */
static uchar header_sizes[]=
{
TRANSID_SIZE,
VERPTR_SIZE,
TRANSID_SIZE, /* Delete transid */
1 /* Null extends */
};
/*
Calculate array of all used headers
Used to speed up:
size= 1;
if (flag & 1)
size+= TRANSID_SIZE;
if (flag & 2)
size+= VERPTR_SIZE;
if (flag & 4)
size+= TRANSID_SIZE
if (flag & 8)
size+= 1;
NOTES
This is called only once at startup of Maria
*/
static uchar total_header_size[1 << array_elements(header_sizes)];
#define PRECALC_HEADER_BITMASK (array_elements(total_header_size) -1)
void _ma_init_block_record_data(void)
{
uint i;
bzero(total_header_size, sizeof(total_header_size));
total_header_size[0]= FLAG_SIZE; /* Flag uchar */
for (i= 1; i < array_elements(total_header_size); i++)
{
uint size= FLAG_SIZE, j, bit;
for (j= 0; (bit= (1 << j)) <= i; j++)
{
if (i & bit)
size+= header_sizes[j];
}
total_header_size[i]= size;
}
}
my_bool _ma_once_init_block_record(MARIA_SHARE *share, File data_file)
{
my_bool res;
pgcache_page_no_t last_page;
/*
First calculate the max file length with can have with a pointer of size
rec_reflength.
The 'rec_reflength - 1' is because one byte is used for row
position withing the page.
The /2 comes from _ma_transaction_recpos_to_keypos() where we use
the lowest bit to mark if there is a transid following the rownr.
*/
last_page= ((ulonglong) 1 << ((share->base.rec_reflength-1)*8))/2;
if (!last_page) /* Overflow; set max size */
last_page= ~(pgcache_page_no_t) 0;
res= _ma_bitmap_init(share, data_file, &last_page);
share->base.max_data_file_length= _ma_safe_mul(last_page + 1,
share->block_size);
#if SIZEOF_OFF_T == 4
set_if_smaller(share->base.max_data_file_length, INT_MAX32);
#endif
return res;
}
my_bool _ma_once_end_block_record(MARIA_SHARE *share)
{
int res= _ma_bitmap_end(share);
if (share->bitmap.file.file >= 0)
{
if (flush_pagecache_blocks(share->pagecache, &share->bitmap.file,
((share->temporary || share->deleting) ?
FLUSH_IGNORE_CHANGED :
FLUSH_RELEASE)))
res= 1;
/*
File must be synced as it is going out of the maria_open_list and so
becoming unknown to Checkpoint.
*/
if (share->now_transactional &&
mysql_file_sync(share->bitmap.file.file, MYF(MY_WME)))
res= 1;
if (mysql_file_close(share->bitmap.file.file, MYF(MY_WME)))
res= 1;
/*
Trivial assignment to guard against multiple invocations
(May happen if file are closed but we want to keep the maria object
around a bit longer)
*/
share->bitmap.file.file= -1;
}
if (share->id != 0)
{
/*
We de-assign the id even though index has not been flushed, this is ok
as close_lock serializes us with a Checkpoint looking at our share.
*/
translog_deassign_id_from_share(share);
}
return res;
}
/* Init info->cur_row structure */
my_bool _ma_init_block_record(MARIA_HA *info)
{
MARIA_ROW *row= &info->cur_row, *new_row= &info->new_row;
MARIA_SHARE *share= info->s;
uint default_extents;
DBUG_ENTER("_ma_init_block_record");
if (!my_multi_malloc(MY_WME,
&row->empty_bits, share->base.pack_bytes,
&row->field_lengths,
share->base.max_field_lengths + 2,
&row->blob_lengths, sizeof(ulong) * share->base.blobs,
&row->null_field_lengths, (sizeof(uint) *
(share->base.fields -
share->base.blobs +
EXTRA_LENGTH_FIELDS)),
&row->tail_positions, (sizeof(MARIA_RECORD_POS) *
(share->base.blobs + 2)),
&new_row->empty_bits, share->base.pack_bytes,
&new_row->field_lengths,
share->base.max_field_lengths + 2,
&new_row->blob_lengths,
sizeof(ulong) * share->base.blobs,
&new_row->null_field_lengths, (sizeof(uint) *
(share->base.fields -
share->base.blobs +
EXTRA_LENGTH_FIELDS)),
&info->log_row_parts,
sizeof(*info->log_row_parts) *
(TRANSLOG_INTERNAL_PARTS + 3 +
share->base.fields + 3),
&info->update_field_data,
(share->base.fields * 4 +
share->base.max_field_lengths + 1 + 4),
NullS, 0))
DBUG_RETURN(1);
/* Skip over bytes used to store length of field length for logging */
row->field_lengths+= 2;
new_row->field_lengths+= 2;
/* Reserve some initial space to avoid mallocs during execution */
default_extents= (ELEMENTS_RESERVED_FOR_MAIN_PART + 1 +
(AVERAGE_BLOB_SIZE /
FULL_PAGE_SIZE(share) /
BLOB_SEGMENT_MIN_SIZE));
if (my_init_dynamic_array(&info->bitmap_blocks,
sizeof(MARIA_BITMAP_BLOCK), default_extents,
64, MYF(0)))
goto err;
info->cur_row.extents_buffer_length= default_extents * ROW_EXTENT_SIZE;
if (!(info->cur_row.extents= my_malloc(info->cur_row.extents_buffer_length,
MYF(MY_WME))))
goto err;
info->row_base_length= share->base_length;
info->row_flag= share->base.default_row_flag;
/*
We need to reserve 'EXTRA_LENGTH_FIELDS' number of parts in
null_field_lengths to allow splitting of rows in 'find_where_to_split_row'
*/
row->null_field_lengths+= EXTRA_LENGTH_FIELDS;
new_row->null_field_lengths+= EXTRA_LENGTH_FIELDS;
DBUG_RETURN(0);
err:
_ma_end_block_record(info);
DBUG_RETURN(1);
}
void _ma_end_block_record(MARIA_HA *info)
{
DBUG_ENTER("_ma_end_block_record");
my_free(info->cur_row.empty_bits);
delete_dynamic(&info->bitmap_blocks);
my_free(info->cur_row.extents);
my_free(info->blob_buff);
/*
The data file is closed, when needed, in ma_once_end_block_record().
The following protects us from doing an extra, not allowed, close
in maria_close()
*/
info->dfile.file= -1;
DBUG_VOID_RETURN;
}
/****************************************************************************
Helper functions
****************************************************************************/
/*
Return the next unused postion on the page after a directory entry.
SYNOPSIS
start_of_next_entry()
dir Directory entry to be used. This can not be the
the last entry on the page!
RETURN
# Position in page where next entry starts.
Everything between the '*dir' and this are free to be used.
*/
static inline uint start_of_next_entry(uchar *dir)
{
uchar *prev;
/*
Find previous used entry. (There is always a previous entry as
the directory never starts with a deleted entry)
*/
for (prev= dir - DIR_ENTRY_SIZE ;
prev[0] == 0 && prev[1] == 0 ;
prev-= DIR_ENTRY_SIZE)
{}
return (uint) uint2korr(prev);
}
/*
Return the offset where the previous entry ends (before on page)
SYNOPSIS
end_of_previous_entry()
dir Address for current directory entry
end Address to last directory entry
RETURN
# Position where previous entry ends (smallest address on page)
Everything between # and current entry are free to be used.
*/
static inline uint end_of_previous_entry(MARIA_SHARE *share,
uchar *dir, uchar *end)
{
uchar *pos;
for (pos= dir + DIR_ENTRY_SIZE ; pos < end ; pos+= DIR_ENTRY_SIZE)
{
uint offset;
if ((offset= uint2korr(pos)))
return offset + uint2korr(pos+2);
}
return PAGE_HEADER_SIZE(share);
}
#ifndef DBUG_OFF
static void _ma_print_directory(MARIA_SHARE *share,
FILE *file, uchar *buff, uint block_size)
{
uint max_entry= (uint) ((uchar *) buff)[DIR_COUNT_OFFSET], row= 0;
uint end_of_prev_row= PAGE_HEADER_SIZE(share);
uchar *dir, *end;
dir= dir_entry_pos(buff, block_size, max_entry-1);
end= dir_entry_pos(buff, block_size, 0);
DBUG_LOCK_FILE; /* If using DBUG_FILE */
fprintf(file,"Directory dump (pos:length):\n");
for (row= 1; dir <= end ; end-= DIR_ENTRY_SIZE, row++)
{
uint offset= uint2korr(end);
uint length= uint2korr(end+2);
fprintf(file, " %4u:%4u", offset, offset ? length : 0);
if (!(row % (80/12)))
fputc('\n', file);
if (offset)
{
DBUG_ASSERT(offset >= end_of_prev_row);
end_of_prev_row= offset + length;
}
}
fputc('\n', file);
fflush(file);
DBUG_UNLOCK_FILE;
}
static void check_directory(MARIA_SHARE *share,
uchar *buff, uint block_size, uint min_row_length,
uint real_empty_size)
{
uchar *dir, *end;
uint max_entry= (uint) buff[DIR_COUNT_OFFSET];
uint start_of_dir, deleted;
uint end_of_prev_row= PAGE_HEADER_SIZE(share);
uint empty_size_on_page;
uint empty_size;
uchar free_entry, prev_free_entry;
dir= dir_entry_pos(buff, block_size, max_entry-1);
start_of_dir= (uint) (dir - buff);
end= dir_entry_pos(buff, block_size, 0);
deleted= empty_size= 0;
empty_size_on_page= (real_empty_size != (uint) -1 ? real_empty_size :
uint2korr(buff + EMPTY_SPACE_OFFSET));
/* Ensure that all rows are in increasing order and no overlaps */
for (; dir <= end ; end-= DIR_ENTRY_SIZE)
{
uint offset= uint2korr(end);
uint length= uint2korr(end+2);
if (offset)
{
DBUG_ASSERT(offset >= end_of_prev_row);
DBUG_ASSERT(!length || length >= min_row_length);
empty_size+= offset - end_of_prev_row;
end_of_prev_row= offset + length;
}
else
deleted++;
}
empty_size+= start_of_dir - end_of_prev_row;
DBUG_ASSERT(end_of_prev_row <= start_of_dir);
DBUG_ASSERT(empty_size == empty_size_on_page);
/* check free links */
free_entry= buff[DIR_FREE_OFFSET];
prev_free_entry= END_OF_DIR_FREE_LIST;
while (free_entry != END_OF_DIR_FREE_LIST)
{
uchar *dir= dir_entry_pos(buff, block_size, free_entry);
DBUG_ASSERT(dir[0] == 0 && dir[1] == 0);
DBUG_ASSERT(dir[2] == prev_free_entry);
prev_free_entry= free_entry;
free_entry= dir[3];
deleted--;
}
DBUG_ASSERT(deleted == 0);
}
#else
#define check_directory(A,B,C,D,E)
#endif /* DBUG_OFF */
/**
@brief Calculate if there is enough entries on the page
*/
static my_bool enough_free_entries(uchar *buff, uint block_size,
uint wanted_entries)
{
uint entries= (uint) buff[DIR_COUNT_OFFSET];
uint needed_free_entries, free_entry;
if (entries + wanted_entries <= MAX_ROWS_PER_PAGE)
return 1;
/* Check if enough free entries in free list */
needed_free_entries= entries + wanted_entries - MAX_ROWS_PER_PAGE;
free_entry= (uint) buff[DIR_FREE_OFFSET];
while (free_entry != END_OF_DIR_FREE_LIST)
{
uchar *dir;
if (!--needed_free_entries)
return 1;
dir= dir_entry_pos(buff, block_size, free_entry);
free_entry= dir[3];
}
return 0; /* Not enough entries */
}
/**
@brief Check if there is room for more rows on page
@fn enough_free_entries_on_page
@return 0 Directory is full
@return 1 There is room for more entries on the page
*/
my_bool enough_free_entries_on_page(MARIA_SHARE *share,
uchar *page_buff)
{
enum en_page_type page_type;
page_type= (enum en_page_type) (page_buff[PAGE_TYPE_OFFSET] &
~(uchar) PAGE_CAN_BE_COMPACTED);
if (page_type == HEAD_PAGE)
{
uint row_count= (uint) page_buff[DIR_COUNT_OFFSET];
return !(row_count == MAX_ROWS_PER_PAGE &&
page_buff[DIR_FREE_OFFSET] == END_OF_DIR_FREE_LIST);
}
return enough_free_entries(page_buff, share->block_size,
1 + share->base.blobs);
}
/**
@brief Extend a record area to fit a given size block
@fn extend_area_on_page()
@param info Handler
@param buff Page buffer
@param dir Pointer to dir entry in buffer
@param rownr Row number we working on
@param block_size Block size of buffer
@param request_length How much data we want to put at [dir]
@param empty_space Total empty space in buffer
This is updated with length after dir
is allocated and current block freed
@param head_page 1 if head page, 0 for tail page
@implementation
The logic is as follows (same as in _ma_update_block_record())
- If new data fits in old block, use old block.
- Extend block with empty space before block. If enough, use it.
- Extend block with empty space after block. If enough, use it.
- Use _ma_compact_block_page() to get all empty space at dir.
@note
The given directory entry is set to rec length.
empty_space doesn't include the new directory entry
@return
@retval 0 ok
@retval ret_offset Pointer to store offset to found area
@retval ret_length Pointer to store length of found area
@retval [dir] rec_offset is store here too
@retval 1 error (wrong info in block)
*/
static my_bool extend_area_on_page(MARIA_HA *info,
uchar *buff, uchar *dir,
uint rownr,
uint request_length,
uint *empty_space, uint *ret_offset,
uint *ret_length,
my_bool head_page)
{
uint rec_offset, length, org_rec_length;
uint max_entry= (uint) buff[DIR_COUNT_OFFSET];
MARIA_SHARE *share= info->s;
uint block_size= share->block_size;
DBUG_ENTER("extend_area_on_page");
/*
We can't check for min length here as we may have called
extend_directory() to create a new (empty) entry just before
*/
check_directory(share, buff, block_size, 0, *empty_space);
rec_offset= uint2korr(dir);
if (rec_offset)
{
/* Extending old row; Mark current space as 'free' */
length= org_rec_length= uint2korr(dir + 2);
DBUG_PRINT("info", ("rec_offset: %u length: %u request_length: %u "
"empty_space: %u",
rec_offset, org_rec_length, request_length,
*empty_space));
*empty_space+= org_rec_length;
}
else
{
/* Reusing free directory entry; Free it from the directory list */
if (dir[2] == END_OF_DIR_FREE_LIST)
buff[DIR_FREE_OFFSET]= dir[3];
else
{
uchar *prev_dir= dir_entry_pos(buff, block_size, (uint) dir[2]);
DBUG_ASSERT(uint2korr(prev_dir) == 0 && prev_dir[3] == (uchar) rownr);
prev_dir[3]= dir[3];
}
if (dir[3] != END_OF_DIR_FREE_LIST)
{
uchar *next_dir= dir_entry_pos(buff, block_size, (uint) dir[3]);
DBUG_ASSERT(uint2korr(next_dir) == 0 && next_dir[2] == (uchar) rownr);
next_dir[2]= dir[2];
}
rec_offset= start_of_next_entry(dir);
length= 0;
}
if (length < request_length)
{
uint old_rec_offset;
/*
New data did not fit in old position.
Find first possible position where to put new data.
*/
old_rec_offset= rec_offset;
rec_offset= end_of_previous_entry(share,
dir, buff + block_size -
PAGE_SUFFIX_SIZE);
length+= (uint) (old_rec_offset - rec_offset);
DBUG_ASSERT(old_rec_offset);
/*
'length' is 0 if we are doing an insert into a not allocated block.
This can only happen during "REDO of INSERT" or "UNDO of DELETE."
*/
if (length < request_length)
{
/*
Did not fit in current block + empty space. Extend with
empty space after block.
*/
if (rownr == max_entry - 1)
{
/* Last entry; Everything is free between this and directory */
length= ((block_size - PAGE_SUFFIX_SIZE - DIR_ENTRY_SIZE * max_entry) -
rec_offset);
}
else
length= start_of_next_entry(dir) - rec_offset;
DBUG_ASSERT((int) length >= 0);
if (length < request_length)
{
/* Not enough continuous space, compact page to get more */
int2store(dir, rec_offset);
/* Reset length, as this may be a deleted block */
int2store(dir+2, 0);
_ma_compact_block_page(share,
buff, rownr, 1,
head_page ? info->trn->min_read_from: 0,
head_page ? share->base.min_block_length : 0);
rec_offset= uint2korr(dir);
length= uint2korr(dir+2);
if (length < request_length)
{
DBUG_PRINT("error", ("Not enough space: "
"length: %u request_length: %u",
length, request_length));
_ma_set_fatal_error(share, HA_ERR_WRONG_IN_RECORD);
DBUG_RETURN(1); /* Error in block */
}
*empty_space= length; /* All space is here */
}
}
}
int2store(dir, rec_offset);
int2store(dir + 2, length);
*ret_offset= rec_offset;
*ret_length= length;
check_directory(share,
buff, block_size,
head_page ? share->base.min_block_length : 0,
*empty_space - length);
DBUG_RETURN(0);
}
/**
@brief Copy not changed fields from 'from' to 'to'
@notes
Assumption is that most fields are not changed!
(Which is why we don't test if all bits are set for some bytes in bitmap)
*/
void copy_not_changed_fields(MARIA_HA *info, MY_BITMAP *changed_fields,
uchar *to, uchar *from)
{
MARIA_COLUMNDEF *column, *end_column;
uchar *bitmap= (uchar*) changed_fields->bitmap;
MARIA_SHARE *share= info->s;
uint bit= 1;
for (column= share->columndef, end_column= column+ share->base.fields;
column < end_column; column++)
{
if (!(*bitmap & bit))
{
uint field_length= column->length;
if (column->type == FIELD_VARCHAR)
{
if (column->fill_length == 1)
field_length= (uint) from[column->offset] + 1;
else
field_length= uint2korr(from + column->offset) + 2;
}
memcpy(to + column->offset, from + column->offset, field_length);
}
if ((bit= (bit << 1)) == 256)
{
bitmap++;
bit= 1;
}
}
}
#ifdef NOT_YET_NEEDED
/* Calculate empty space on a page */
static uint empty_space_on_page(uchar *buff, uint block_size)
{
enum en_page_type;
page_type= (enum en_page_type) (buff[PAGE_TYPE_OFFSET] &
~(uchar) PAGE_CAN_BE_COMPACTED);
if (page_type == UNALLOCATED_PAGE)
return block_size;
if ((uint) page_type <= TAIL_PAGE)
return uint2korr(buff+EMPTY_SPACE_OFFSET);
return 0; /* Blob page */
}
#endif
/*
@brief Ensure we have space for new directory entries
@fn make_space_for_directory()
@param info Handler