ma_blockrec.h 13.9 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4
/* Copyright (C) 2007 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
5
   the Free Software Foundation; version 2 of the License.
unknown's avatar
unknown committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

   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
*/

#define LSN_SIZE		7
21
#define DIR_COUNT_SIZE		1	/* Stores number of rows on page */
22
#define DIR_FREE_SIZE		1	/* Pointer to first free dir entry */
unknown's avatar
unknown committed
23 24
#define EMPTY_SPACE_SIZE	2	/* Stores empty space on page */
#define PAGE_TYPE_SIZE		1
25 26 27
#define PAGE_SUFFIX_SIZE	4	/* Bytes for checksum */
#define PAGE_HEADER_SIZE	(LSN_SIZE + DIR_COUNT_SIZE + DIR_FREE_SIZE +\
                                 EMPTY_SPACE_SIZE + PAGE_TYPE_SIZE)
unknown's avatar
unknown committed
28 29 30 31
#define PAGE_OVERHEAD_SIZE	(PAGE_HEADER_SIZE + DIR_ENTRY_SIZE + \
                                 PAGE_SUFFIX_SIZE)
#define BLOCK_RECORD_POINTER_SIZE	6

32 33
#define FULL_PAGE_SIZE(block_size) ((block_size) - LSN_SIZE - \
                                    PAGE_TYPE_SIZE - PAGE_SUFFIX_SIZE)
unknown's avatar
unknown committed
34 35 36

#define ROW_EXTENT_PAGE_SIZE	5
#define ROW_EXTENT_COUNT_SIZE   2
37 38
#define SUB_RANGE_SIZE		2
#define BLOCK_FILLER_SIZE	2
unknown's avatar
unknown committed
39 40
#define ROW_EXTENT_SIZE		(ROW_EXTENT_PAGE_SIZE + ROW_EXTENT_COUNT_SIZE)
#define TAIL_BIT		0x8000	/* Bit in page_count to signify tail */
41 42 43
#define START_EXTENT_BIT	0x4000	/* Bit in page_count to signify start*/
/* page_count set by bitmap code for tail pages */
#define TAIL_PAGE_COUNT_MARKER  0xffff
44
/* Number of extents reserved MARIA_BITMAP_BLOCKS to store head part */
unknown's avatar
unknown committed
45
#define ELEMENTS_RESERVED_FOR_MAIN_PART 4
46 47 48 49 50
/* This is just used to prealloc a dynamic array */
#define AVERAGE_BLOB_SIZE      1024L*1024L
/* Number of pages to store continuous blob parts */
#define BLOB_SEGMENT_MIN_SIZE 128

51
/* Fields before 'row->null_field_lengths' used by find_where_to_split_row */
unknown's avatar
unknown committed
52 53
#define EXTRA_LENGTH_FIELDS		3

54
/* Size for the different parts in the row header (and head page) */
unknown's avatar
unknown committed
55 56 57
#define FLAG_SIZE		1
#define VERPTR_SIZE		7
#define DIR_ENTRY_SIZE		4
58
#define FIELD_OFFSET_SIZE	2      /* size of pointers to field starts */
unknown's avatar
unknown committed
59 60 61 62 63

/* Minimum header size needed for a new row */
#define BASE_ROW_HEADER_SIZE FLAG_SIZE
#define TRANS_ROW_EXTRA_HEADER_SIZE TRANSID_SIZE

64
#define PAGE_TYPE_MASK 7
unknown's avatar
unknown committed
65
enum en_page_type { UNALLOCATED_PAGE, HEAD_PAGE, TAIL_PAGE, BLOB_PAGE, MAX_PAGE_TYPE };
unknown's avatar
unknown committed
66
#define PAGE_CAN_BE_COMPACTED   128             /* Bit in PAGE_TYPE */
unknown's avatar
unknown committed
67 68

#define PAGE_TYPE_OFFSET        LSN_SIZE
69 70 71
#define DIR_COUNT_OFFSET        (LSN_SIZE+PAGE_TYPE_SIZE)
#define DIR_FREE_OFFSET         (DIR_COUNT_OFFSET+DIR_COUNT_SIZE)
#define EMPTY_SPACE_OFFSET      (DIR_FREE_OFFSET+DIR_FREE_SIZE)
unknown's avatar
unknown committed
72

unknown's avatar
unknown committed
73
/* Bits used for flag uchar (one byte, first in record) */
unknown's avatar
unknown committed
74 75 76 77 78 79 80
#define ROW_FLAG_TRANSID                1
#define ROW_FLAG_VER_PTR                2
#define ROW_FLAG_DELETE_TRANSID         4
#define ROW_FLAG_NULLS_EXTENDED         8
#define ROW_FLAG_EXTENTS                128
#define ROW_FLAG_ALL			(1+2+4+8+128)

Michael Widenius's avatar
Michael Widenius committed
81 82 83 84
/* Size for buffer to hold information about bitmap */
#define MAX_BITMAP_INFO_LENGTH ((MARIA_MAX_KEY_BLOCK_LENGTH*8/3)*(61*11/60)+10)


85 86 87
/******** Variables that affects how data pages are utilized ********/

/* Minium size of tail segment */
unknown's avatar
unknown committed
88 89
#define MIN_TAIL_SIZE           32

90 91 92 93
/*
  Fixed length part of Max possible header size; See row data structure
  table in ma_blockrec.c.
*/
unknown's avatar
unknown committed
94 95
#define MAX_FIXED_HEADER_SIZE (FLAG_SIZE + 3 + ROW_EXTENT_SIZE + 3)
#define TRANS_MAX_FIXED_HEADER_SIZE (MAX_FIXED_HEADER_SIZE + \
unknown's avatar
unknown committed
96
                                     TRANSID_SIZE + VERPTR_SIZE + \
unknown's avatar
unknown committed
97 98
                                     TRANSID_SIZE)

unknown's avatar
unknown committed
99
/* We use 1 uchar in record header to store number of directory entries */
unknown's avatar
unknown committed
100
#define MAX_ROWS_PER_PAGE	255
101
#define END_OF_DIR_FREE_LIST	((uchar) 255)
unknown's avatar
unknown committed
102 103

/* Bits for MARIA_BITMAP_BLOCKS->used */
104
/* We stored data on disk in the block */
unknown's avatar
unknown committed
105
#define BLOCKUSED_USED		 1
106
/* Bitmap on disk is block->org_bitmap_value ; Happens only on update */
unknown's avatar
unknown committed
107
#define BLOCKUSED_USE_ORG_BITMAP 2
108
/* We stored tail data on disk for the block */
unknown's avatar
unknown committed
109 110
#define BLOCKUSED_TAIL		 4

111
/******* defines that affects allocation (density) of data *******/
unknown's avatar
unknown committed
112

113
/*
114
  If the tail part (from the main block or a blob) would use more than 75 % of
115 116 117
  the size of page, store the tail on a full page instead of a shared
 tail page.
*/
unknown's avatar
unknown committed
118 119
#define MAX_TAIL_SIZE(block_size) ((block_size) *3 / 4)

120 121 122
/* Don't allocate memory for too many row extents on the stack */
#define ROW_EXTENTS_ON_STACK	32

unknown's avatar
unknown committed
123 124
/* Functions to convert MARIA_RECORD_POS to/from page:offset */

125 126
static inline MARIA_RECORD_POS ma_recordpos(pgcache_page_no_t page,
                                            uint dir_entry)
unknown's avatar
unknown committed
127
{
128
  DBUG_ASSERT(dir_entry <= 255);
129
  DBUG_ASSERT(page > 0); /* page 0 is bitmap, not data page */
130
  return (MARIA_RECORD_POS) (((ulonglong) page << 8) | dir_entry);
unknown's avatar
unknown committed
131 132
}

133
static inline pgcache_page_no_t ma_recordpos_to_page(MARIA_RECORD_POS record_pos)
unknown's avatar
unknown committed
134
{
135
  return (pgcache_page_no_t) (record_pos >> 8);
unknown's avatar
unknown committed
136 137
}

138
static inline uint ma_recordpos_to_dir_entry(MARIA_RECORD_POS record_pos)
unknown's avatar
unknown committed
139
{
140
  return (uint) (record_pos & 255);
unknown's avatar
unknown committed
141 142
}

143 144 145 146 147 148
static inline uchar *dir_entry_pos(uchar *buff, uint block_size, uint pos)
{
  return (buff + block_size - DIR_ENTRY_SIZE * pos - PAGE_SUFFIX_SIZE -
          DIR_ENTRY_SIZE);
}

unknown's avatar
unknown committed
149 150
/* ma_blockrec.c */
void _ma_init_block_record_data(void);
151 152 153 154
my_bool _ma_once_init_block_record(MARIA_SHARE *share, File dfile);
my_bool _ma_once_end_block_record(MARIA_SHARE *share);
my_bool _ma_init_block_record(MARIA_HA *info);
void _ma_end_block_record(MARIA_HA *info);
unknown's avatar
unknown committed
155 156

my_bool _ma_update_block_record(MARIA_HA *info, MARIA_RECORD_POS pos,
unknown's avatar
unknown committed
157 158 159
                                const uchar *oldrec, const uchar *newrec);
my_bool _ma_delete_block_record(MARIA_HA *info, const uchar *record);
int     _ma_read_block_record(MARIA_HA *info, uchar *record,
unknown's avatar
unknown committed
160
                              MARIA_RECORD_POS record_pos);
unknown's avatar
unknown committed
161 162 163
int _ma_read_block_record2(MARIA_HA *info, uchar *record,
                           uchar *data, uchar *end_of_data);
int     _ma_scan_block_record(MARIA_HA *info, uchar *record,
unknown's avatar
unknown committed
164 165
                              MARIA_RECORD_POS, my_bool);
my_bool _ma_cmp_block_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
unknown's avatar
unknown committed
166
                             const uchar *record, MARIA_RECORD_POS pos);
unknown's avatar
unknown committed
167 168
my_bool _ma_scan_init_block_record(MARIA_HA *info);
void _ma_scan_end_block_record(MARIA_HA *info);
169 170 171 172
int _ma_scan_remember_block_record(MARIA_HA *info,
                                   MARIA_RECORD_POS *lastpos);
void _ma_scan_restore_block_record(MARIA_HA *info,
                                   MARIA_RECORD_POS lastpos);
unknown's avatar
unknown committed
173 174

MARIA_RECORD_POS _ma_write_init_block_record(MARIA_HA *info,
unknown's avatar
unknown committed
175 176
                                             const uchar *record);
my_bool _ma_write_block_record(MARIA_HA *info, const uchar *record);
unknown's avatar
unknown committed
177 178
my_bool _ma_write_abort_block_record(MARIA_HA *info);
my_bool _ma_compare_block_record(register MARIA_HA *info,
unknown's avatar
unknown committed
179
                                 register const uchar *record);
unknown's avatar
unknown committed
180
void    _ma_compact_block_page(uchar *buff, uint block_size, uint rownr,
unknown's avatar
unknown committed
181 182
                               my_bool extend_block, TrID min_read_from,
                               uint min_row_length);
183
my_bool enough_free_entries_on_page(MARIA_SHARE *share, uchar *page_buff);
unknown's avatar
unknown committed
184 185
TRANSLOG_ADDRESS
maria_page_get_lsn(uchar *page, pgcache_page_no_t page_no, uchar* data_ptr);
unknown's avatar
unknown committed
186 187

/* ma_bitmap.c */
Michael Widenius's avatar
Michael Widenius committed
188 189
extern const char *bits_to_txt[];

unknown's avatar
unknown committed
190 191
my_bool _ma_bitmap_init(MARIA_SHARE *share, File file);
my_bool _ma_bitmap_end(MARIA_SHARE *share);
unknown's avatar
unknown committed
192
my_bool _ma_bitmap_flush(MARIA_SHARE *share);
unknown's avatar
unknown committed
193
my_bool _ma_bitmap_flush_all(MARIA_SHARE *share);
194
void _ma_bitmap_reset_cache(MARIA_SHARE *share);
unknown's avatar
unknown committed
195 196 197
my_bool _ma_bitmap_find_place(MARIA_HA *info, MARIA_ROW *row,
                              MARIA_BITMAP_BLOCKS *result_blocks);
my_bool _ma_bitmap_release_unused(MARIA_HA *info, MARIA_BITMAP_BLOCKS *blocks);
unknown's avatar
unknown committed
198
my_bool _ma_bitmap_free_full_pages(MARIA_HA *info, const uchar *extents,
unknown's avatar
unknown committed
199
                                   uint count);
200
my_bool _ma_bitmap_set(MARIA_HA *info, pgcache_page_no_t pos, my_bool head,
unknown's avatar
unknown committed
201
                       uint empty_space);
unknown's avatar
unknown committed
202 203
my_bool _ma_bitmap_reset_full_page_bits(MARIA_HA *info,
                                        MARIA_FILE_BITMAP *bitmap,
204 205
                                        pgcache_page_no_t page,
                                        uint page_count);
unknown's avatar
unknown committed
206 207
my_bool _ma_bitmap_set_full_page_bits(MARIA_HA *info,
                                      MARIA_FILE_BITMAP *bitmap,
208
                                      pgcache_page_no_t page, uint page_count);
unknown's avatar
unknown committed
209 210
uint _ma_free_size_to_head_pattern(MARIA_FILE_BITMAP *bitmap, uint size);
my_bool _ma_bitmap_find_new_place(MARIA_HA *info, MARIA_ROW *new_row,
211
                                  pgcache_page_no_t page, uint free_size,
unknown's avatar
unknown committed
212 213
                                  MARIA_BITMAP_BLOCKS *result_blocks);
my_bool _ma_check_bitmap_data(MARIA_HA *info,
214
                              enum en_page_type page_type,
Michael Widenius's avatar
Michael Widenius committed
215
                              uint empty_space, uint bitmap_pattern);
unknown's avatar
unknown committed
216 217
my_bool _ma_check_if_right_bitmap_type(MARIA_HA *info,
                                       enum en_page_type page_type,
218
                                       pgcache_page_no_t page,
unknown's avatar
unknown committed
219
                                       uint *bitmap_pattern);
220
uint _ma_bitmap_get_page_bits(MARIA_HA *info, MARIA_FILE_BITMAP *bitmap,
221
                              pgcache_page_no_t page);
unknown's avatar
unknown committed
222
void _ma_bitmap_delete_all(MARIA_SHARE *share);
unknown's avatar
unknown committed
223
int  _ma_bitmap_create_first(MARIA_SHARE *share);
224
void _ma_bitmap_flushable(MARIA_HA *info, int non_flushable_inc);
225 226
void _ma_bitmap_lock(MARIA_SHARE *share);
void _ma_bitmap_unlock(MARIA_SHARE *share);
unknown's avatar
unknown committed
227 228
void _ma_bitmap_set_pagecache_callbacks(PAGECACHE_FILE *file,
                                        MARIA_SHARE *share);
229 230
#ifndef DBUG_OFF
void _ma_print_bitmap(MARIA_FILE_BITMAP *bitmap, uchar *data,
231
                      pgcache_page_no_t page);
232
#endif
Michael Widenius's avatar
Michael Widenius committed
233 234 235 236
void _ma_get_bitmap_description(MARIA_FILE_BITMAP *bitmap,
                                uchar *bitmap_data,
                                pgcache_page_no_t page,
                                char *out);
237

238 239
uint _ma_apply_redo_insert_row_head_or_tail(MARIA_HA *info, LSN lsn,
                                            uint page_type,
240
                                            my_bool new_page,
241 242
                                            const uchar *header,
                                            const uchar *data,
243 244 245
                                            size_t data_length);
uint _ma_apply_redo_purge_row_head_or_tail(MARIA_HA *info, LSN lsn,
                                           uint page_type,
246
                                           const uchar *header);
247
uint _ma_apply_redo_free_blocks(MARIA_HA *info, LSN lsn, LSN rec_lsn,
248 249 250
                                const uchar *header);
uint _ma_apply_redo_free_head_or_tail(MARIA_HA *info, LSN lsn,
                                      const uchar *header);
unknown's avatar
unknown committed
251
uint _ma_apply_redo_insert_row_blobs(MARIA_HA *info, LSN lsn,
252 253 254 255 256
                                     const uchar *header, LSN redo_lsn,
                                     uint * const number_of_blobs,
                                     uint * const number_of_ranges,
                                     pgcache_page_no_t * const first_page,
                                     pgcache_page_no_t * const last_page);
unknown's avatar
unknown committed
257 258
my_bool _ma_apply_redo_bitmap_new_page(MARIA_HA *info, LSN lsn,
                                       const uchar *header);
259 260
my_bool _ma_apply_undo_row_insert(MARIA_HA *info, LSN undo_lsn,
                                  const uchar *header);
unknown's avatar
unknown committed
261
my_bool _ma_apply_undo_row_delete(MARIA_HA *info, LSN undo_lsn,
unknown's avatar
unknown committed
262
                                  const uchar *header, size_t length);
unknown's avatar
unknown committed
263
my_bool _ma_apply_undo_row_update(MARIA_HA *info, LSN undo_lsn,
unknown's avatar
unknown committed
264
                                  const uchar *header, size_t length);
265
my_bool _ma_apply_undo_bulk_insert(MARIA_HA *info, LSN undo_lsn);
unknown's avatar
unknown committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284

my_bool write_hook_for_redo(enum translog_record_type type,
                            TRN *trn, MARIA_HA *tbl_info, LSN *lsn,
                            void *hook_arg);
my_bool write_hook_for_undo(enum translog_record_type type,
                            TRN *trn, MARIA_HA *tbl_info, LSN *lsn,
                            void *hook_arg);
my_bool write_hook_for_redo_delete_all(enum translog_record_type type,
                                       TRN *trn, MARIA_HA *tbl_info,
                                       LSN *lsn, void *hook_arg);
my_bool write_hook_for_undo_row_insert(enum translog_record_type type,
                                       TRN *trn, MARIA_HA *tbl_info,
                                       LSN *lsn, void *hook_arg);
my_bool write_hook_for_undo_row_delete(enum translog_record_type type,
                                       TRN *trn, MARIA_HA *tbl_info,
                                       LSN *lsn, void *hook_arg);
my_bool write_hook_for_undo_row_update(enum translog_record_type type,
                                       TRN *trn, MARIA_HA *tbl_info,
                                       LSN *lsn, void *hook_arg);
285 286 287
my_bool write_hook_for_undo_bulk_insert(enum translog_record_type type,
                                        TRN *trn, MARIA_HA *tbl_info,
                                        LSN *lsn, void *hook_arg);
unknown's avatar
unknown committed
288 289 290
my_bool write_hook_for_file_id(enum translog_record_type type,
                               TRN *trn, MARIA_HA *tbl_info, LSN *lsn,
                               void *hook_arg);
291 292 293
my_bool write_hook_for_commit(enum translog_record_type type,
                              TRN *trn, MARIA_HA *tbl_info, LSN *lsn,
                              void *hook_arg);
294
void _ma_block_get_status(void *param, my_bool concurrent_insert);
295 296
my_bool _ma_block_start_trans(void* param);
my_bool _ma_block_start_trans_no_versioning(void *param);
unknown's avatar
unknown committed
297 298 299
void _ma_block_update_status(void *param);
void _ma_block_restore_status(void *param);
my_bool _ma_block_check_status(void *param);