~jlukas79/+junk/mysql-server

« back to all changes in this revision

Viewing changes to storage/maria/maria_def.h

manual merge 6.0-main --> 6.0-bka-review

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2006 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/* This file is included by all internal maria files */
 
17
 
 
18
#include "maria.h"                              /* Structs & some defines */
 
19
#include <myisampack.h>                         /* packing of keys */
 
20
#include <my_tree.h>
 
21
#include <my_bitmap.h>
 
22
#ifdef THREAD
 
23
#include <my_pthread.h>
 
24
#include <thr_lock.h>
 
25
#else
 
26
#include <my_no_pthread.h>
 
27
#endif
 
28
#include <hash.h>
 
29
#include "ma_loghandler.h"
 
30
#include "ma_control_file.h"
 
31
#include "ma_state.h"
 
32
 
 
33
/* For testing recovery */
 
34
#ifdef TO_BE_REMOVED
 
35
#define IDENTICAL_PAGES_AFTER_RECOVERY 1
 
36
#endif
 
37
/* Do extra sanity checking */
 
38
#define SANITY_CHECKS 1
 
39
#ifdef EXTRA_DEBUG
 
40
#define EXTRA_DEBUG_KEY_CHANGES
 
41
#endif
 
42
 
 
43
#define MAX_NONMAPPED_INSERTS 1000
 
44
#define MARIA_MAX_TREE_LEVELS 32
 
45
 
 
46
/* maria_open() flag, specific for maria_pack */
 
47
#define HA_OPEN_IGNORE_MOVED_STATE (1U << 30)
 
48
 
 
49
struct st_transaction;
 
50
 
 
51
/* undef map from my_nosys; We need test-if-disk full */
 
52
#undef my_write
 
53
 
 
54
#define CRC_SIZE 4
 
55
 
 
56
typedef struct st_maria_state_info
 
57
{
 
58
  struct
 
59
  {                                     /* Fileheader (24 bytes) */
 
60
    uchar file_version[4];
 
61
    uchar options[2];
 
62
    uchar header_length[2];
 
63
    uchar state_info_length[2];
 
64
    uchar base_info_length[2];
 
65
    uchar base_pos[2];
 
66
    uchar key_parts[2];                 /* Key parts */
 
67
    uchar unique_key_parts[2];          /* Key parts + unique parts */
 
68
    uchar keys;                         /* number of keys in file */
 
69
    uchar uniques;                      /* number of UNIQUE definitions */
 
70
    uchar language;                     /* Language for indexes */
 
71
    uchar fulltext_keys;
 
72
    uchar data_file_type;
 
73
    /* Used by mariapack to store the original data_file_type */
 
74
    uchar org_data_file_type;
 
75
  } header;
 
76
 
 
77
  MARIA_STATUS_INFO state;
 
78
  /* maria_ha->state points here for crash-safe but not versioned tables */
 
79
  MARIA_STATUS_INFO common;
 
80
  ha_rows split;                        /* number of split blocks */
 
81
  my_off_t dellink;                     /* Link to next removed block */
 
82
  pgcache_page_no_t first_bitmap_with_space;
 
83
  ulonglong auto_increment;
 
84
  TrID create_trid;                     /* Minum trid for file */
 
85
  ulong update_count;                   /* Updated for each write lock */
 
86
  ulong status;
 
87
  double *rec_per_key_part;
 
88
  ulong *nulls_per_key_part;
 
89
  ha_checksum checksum;                 /* Table checksum */
 
90
  my_off_t *key_root;                   /* Start of key trees */
 
91
  my_off_t key_del;                     /* delete links for index pages */
 
92
  my_off_t records_at_analyze;          /* Rows when calculating rec_per_key */
 
93
 
 
94
  ulong sec_index_changed;              /* Updated when new sec_index */
 
95
  ulong sec_index_used;                 /* which extra index are in use */
 
96
  ulonglong key_map;                    /* Which keys are in use */
 
97
  ulong version;                        /* timestamp of create */
 
98
  time_t create_time;                   /* Time when created database */
 
99
  time_t recover_time;                  /* Time for last recover */
 
100
  time_t check_time;                    /* Time for last check */
 
101
  uint sortkey;                         /* sorted by this key (not used) */
 
102
  uint open_count;
 
103
  uint changed;                         /* Changed since maria_chk */
 
104
  /**
 
105
     Birthday of the table: no record in the log before this LSN should ever
 
106
     be applied to the table. Updated when created, renamed, explicitely
 
107
     repaired (REPAIR|OPTIMIZE TABLE, ALTER TABLE ENABLE KEYS, maria_chk).
 
108
  */
 
109
  LSN create_rename_lsn;
 
110
  /** @brief Log horizon when state was last updated on disk */
 
111
  TRANSLOG_ADDRESS is_of_horizon;
 
112
  /**
 
113
     REDO phase should ignore any record before this LSN. UNDO phase
 
114
     shouldn't, this is the difference with create_rename_lsn.
 
115
     skip_redo_lsn >= create_rename_lsn.
 
116
     The distinction is for these cases:
 
117
     - after a repair at end of bulk insert (enabling indices), REDO phase
 
118
     should skip the table but UNDO phase should not, so only skip_redo_lsn is
 
119
     increased, not create_rename_lsn
 
120
     - if one table is corrupted and so recovery fails, user may repair the
 
121
     table with maria_chk and let recovery restart: that recovery should then
 
122
     skip the repaired table even in the UNDO phase, so create_rename_lsn is
 
123
     increased.
 
124
  */
 
125
  LSN skip_redo_lsn;
 
126
 
 
127
  /* the following isn't saved on disk */
 
128
  uint state_diff_length;               /* Should be 0 */
 
129
  uint state_length;                    /* Length of state header in file */
 
130
  ulong *key_info;
 
131
} MARIA_STATE_INFO;
 
132
 
 
133
 
 
134
#define MARIA_STATE_INFO_SIZE   \
 
135
  (24 + 2 + LSN_STORE_SIZE*3 + 4 + 11*8 + 4*4 + 8 + 3*4 + 5*8)
 
136
#define MARIA_FILE_OPEN_COUNT_OFFSET 0
 
137
#define MARIA_FILE_CHANGED_OFFSET 2
 
138
#define MARIA_FILE_CREATE_RENAME_LSN_OFFSET 4
 
139
#define MARIA_FILE_CREATE_TRID_OFFSET (4 + LSN_STORE_SIZE*3 + 11*8)
 
140
 
 
141
#define MARIA_STATE_KEY_SIZE    (8 + 4)
 
142
#define MARIA_STATE_KEYBLOCK_SIZE  8
 
143
#define MARIA_STATE_KEYSEG_SIZE 12
 
144
#define MARIA_STATE_EXTRA_SIZE (MARIA_MAX_KEY*MARIA_STATE_KEY_SIZE + MARIA_MAX_KEY*HA_MAX_KEY_SEG*MARIA_STATE_KEYSEG_SIZE)
 
145
#define MARIA_KEYDEF_SIZE       (2+ 5*2)
 
146
#define MARIA_UNIQUEDEF_SIZE    (2+1+1)
 
147
#define HA_KEYSEG_SIZE          (6+ 2*2 + 4*2)
 
148
#define MARIA_MAX_KEY_BUFF      (HA_MAX_KEY_BUFF + MAX_PACK_TRANSID_SIZE)
 
149
#define MARIA_COLUMNDEF_SIZE    (2*7+1+1+4)
 
150
#define MARIA_BASE_INFO_SIZE    (MY_UUID_SIZE + 5*8 + 6*4 + 11*2 + 6 + 5*2 + 1 + 16)
 
151
#define MARIA_INDEX_BLOCK_MARGIN 16     /* Safety margin for .MYI tables */
 
152
/* Internal management bytes needed to store 2 keys on an index page */
 
153
#define MAX_PACK_TRANSID_SIZE (TRANSID_SIZE+1)
 
154
#define MIN_TRANSID_PACK_PREFIX (256-TRANSID_SIZE*2)
 
155
#define MARIA_INDEX_OVERHEAD_SIZE (MAX_PACK_TRANSID_SIZE * 2)
 
156
#define MARIA_DELETE_KEY_NR  255        /* keynr for deleted blocks */
 
157
 
 
158
/*
 
159
  Basic information of the Maria table. This is stored on disk
 
160
  and not changed (unless we do DLL changes).
 
161
*/
 
162
 
 
163
typedef struct st_ma_base_info
 
164
{
 
165
  my_off_t keystart;                    /* Start of keys */
 
166
  my_off_t max_data_file_length;
 
167
  my_off_t max_key_file_length;
 
168
  my_off_t margin_key_file_length;
 
169
  ha_rows records, reloc;               /* Create information */
 
170
  ulong mean_row_length;                /* Create information */
 
171
  ulong reclength;                      /* length of unpacked record */
 
172
  ulong pack_reclength;                 /* Length of full packed rec */
 
173
  ulong min_pack_length;
 
174
  ulong max_pack_length;                /* Max possibly length of packed rec */
 
175
  ulong min_block_length;
 
176
  uint fields;                          /* fields in table */
 
177
  uint fixed_not_null_fields;
 
178
  uint fixed_not_null_fields_length;
 
179
  uint max_field_lengths;
 
180
  uint pack_fields;                     /* packed fields in table */
 
181
  uint varlength_fields;                /* char/varchar/blobs */
 
182
  /* Number of bytes in the index used to refer to a row (2-8) */
 
183
  uint rec_reflength;
 
184
  /* Number of bytes in the index used to refer to another index page (2-8) */
 
185
  uint key_reflength;                   /* = 2-8 */
 
186
  uint keys;                            /* same as in state.header */
 
187
  uint auto_key;                        /* Which key-1 is a auto key */
 
188
  uint blobs;                           /* Number of blobs */
 
189
  /* Length of packed bits (when table was created first time) */
 
190
  uint pack_bytes;
 
191
  /* Length of null bits (when table was created first time) */
 
192
  uint original_null_bytes;
 
193
  uint null_bytes;                      /* Null bytes in record */
 
194
  uint field_offsets;                   /* Number of field offsets */
 
195
  uint max_key_block_length;            /* Max block length */
 
196
  uint max_key_length;                  /* Max key length */
 
197
  /* Extra allocation when using dynamic record format */
 
198
  uint extra_alloc_bytes;
 
199
  uint extra_alloc_procent;
 
200
  uint is_nulls_extended;               /* 1 if new null bytes */
 
201
  uint default_row_flag;                /* 0 or ROW_FLAG_NULLS_EXTENDED */
 
202
  uint block_size;
 
203
  /* Size of initial record buffer */
 
204
  uint default_rec_buff_size;
 
205
  /* Extra number of bytes the row format require in the record buffer */
 
206
  uint extra_rec_buff_size;
 
207
  /* Tuning flags that can be ignored by older Maria versions */
 
208
  uint extra_options;
 
209
 
 
210
  /* The following are from the header */
 
211
  uint key_parts, all_key_parts;
 
212
  uchar uuid[MY_UUID_SIZE];
 
213
  /**
 
214
     @brief If false, we disable logging, versioning, transaction etc. Observe
 
215
     difference with MARIA_SHARE::now_transactional
 
216
  */
 
217
  my_bool born_transactional;
 
218
} MARIA_BASE_INFO;
 
219
 
 
220
 
 
221
/* Structs used intern in database */
 
222
 
 
223
typedef struct st_maria_blob            /* Info of record */
 
224
{
 
225
  ulong offset;                         /* Offset to blob in record */
 
226
  uint pack_length;                     /* Type of packed length */
 
227
  ulong length;                         /* Calc:ed for each record */
 
228
} MARIA_BLOB;
 
229
 
 
230
 
 
231
typedef struct st_maria_pack
 
232
{
 
233
  ulong header_length;
 
234
  uint ref_length;
 
235
  uchar version;
 
236
} MARIA_PACK;
 
237
 
 
238
typedef struct st_maria_file_bitmap
 
239
{
 
240
  uchar *map;
 
241
  pgcache_page_no_t page;              /* Page number for current bitmap */
 
242
  uint used_size;                      /* Size of bitmap head that is not 0 */
 
243
  my_bool changed;                     /* 1 if page needs to be flushed */
 
244
  my_bool flush_all_requested;         /**< If _ma_bitmap_flush_all waiting */
 
245
  uint non_flushable;                  /**< 0 if bitmap and log are in sync */
 
246
  PAGECACHE_FILE file;                 /* datafile where bitmap is stored */
 
247
 
 
248
#ifdef THREAD
 
249
  pthread_mutex_t bitmap_lock;
 
250
  pthread_cond_t bitmap_cond;          /**< When bitmap becomes flushable */
 
251
#endif
 
252
  /* Constants, allocated when initiating bitmaps */
 
253
  uint sizes[8];                      /* Size per bit combination */
 
254
  uint total_size;                    /* Total usable size of bitmap page */
 
255
  uint block_size;                    /* Block size of file */
 
256
  ulong pages_covered;                /* Pages covered by bitmap + 1 */
 
257
  DYNAMIC_ARRAY pinned_pages;         /**< not-yet-flushable bitmap pages */
 
258
} MARIA_FILE_BITMAP;
 
259
 
 
260
#define MARIA_CHECKPOINT_LOOKS_AT_ME 1
 
261
#define MARIA_CHECKPOINT_SHOULD_FREE_ME 2
 
262
#define MARIA_CHECKPOINT_SEEN_IN_LOOP 4
 
263
 
 
264
typedef struct st_maria_share
 
265
{                                       /* Shared between opens */
 
266
  MARIA_STATE_INFO state;
 
267
  MARIA_BASE_INFO base;
 
268
  MARIA_STATE_HISTORY *state_history;
 
269
  MARIA_KEYDEF ft2_keyinfo;             /* Second-level ft-key definition */
 
270
  MARIA_KEYDEF *keyinfo;                /* Key definitions */
 
271
  MARIA_UNIQUEDEF *uniqueinfo;          /* unique definitions */
 
272
  HA_KEYSEG *keyparts;                  /* key part info */
 
273
  MARIA_COLUMNDEF *columndef;           /* Pointer to column information */
 
274
  MARIA_PACK pack;                      /* Data about packed records */
 
275
  MARIA_BLOB *blobs;                    /* Pointer to blobs */
 
276
  uint16 *column_nr;                    /* Original column order */
 
277
  char *unique_file_name;               /* realpath() of index file */
 
278
  char *data_file_name;                 /* Resolved path names from symlinks */
 
279
  char *index_file_name;
 
280
  char *open_file_name;                 /* parameter to open filename */
 
281
  uchar *file_map;                      /* mem-map of file if possible */
 
282
  PAGECACHE *pagecache;                 /* ref to the current key cache */
 
283
  MARIA_DECODE_TREE *decode_trees;
 
284
  /*
 
285
    Previous auto-increment value. Used to verify if we can restore the
 
286
    auto-increment counter if we have to abort an insert (duplicate key).
 
287
  */
 
288
  ulonglong last_auto_increment;
 
289
  uint16 *decode_tables;
 
290
  uint16 id; /**< 2-byte id by which log records refer to the table */
 
291
  /* Called the first time the table instance is opened */
 
292
  my_bool (*once_init)(struct st_maria_share *, File);
 
293
  /* Called when the last instance of the table is closed */
 
294
  my_bool (*once_end)(struct st_maria_share *);
 
295
  /* Is called for every open of the table */
 
296
  my_bool (*init)(MARIA_HA *);
 
297
  /* Is called for every close of the table */
 
298
  void (*end)(MARIA_HA *);
 
299
  /* Called when we want to read a record from a specific position */
 
300
  int (*read_record)(MARIA_HA *, uchar *, MARIA_RECORD_POS);
 
301
  /* Initialize a scan */
 
302
  my_bool (*scan_init)(MARIA_HA *);
 
303
  /* Read next record while scanning */
 
304
  int (*scan)(MARIA_HA *, uchar *, MARIA_RECORD_POS, my_bool);
 
305
  /* End scan */
 
306
  void (*scan_end)(MARIA_HA *);
 
307
  int (*scan_remember_pos)(MARIA_HA *, MARIA_RECORD_POS*);
 
308
  void (*scan_restore_pos)(MARIA_HA *, MARIA_RECORD_POS);
 
309
  /* Pre-write of row (some handlers may do the actual write here) */
 
310
  MARIA_RECORD_POS (*write_record_init)(MARIA_HA *, const uchar *);
 
311
  /* Write record (or accept write_record_init) */
 
312
  my_bool (*write_record)(MARIA_HA *, const uchar *);
 
313
  /* Called when write failed */
 
314
  my_bool (*write_record_abort)(MARIA_HA *);
 
315
  my_bool (*update_record)(MARIA_HA *, MARIA_RECORD_POS,
 
316
                           const uchar *, const uchar *);
 
317
  my_bool (*delete_record)(MARIA_HA *, const uchar *record);
 
318
  my_bool (*compare_record)(MARIA_HA *, const uchar *);
 
319
  /* calculate checksum for a row */
 
320
  ha_checksum(*calc_checksum)(MARIA_HA *, const uchar *);
 
321
  /*
 
322
    Calculate checksum for a row during write. May be 0 if we calculate
 
323
    the checksum in write_record_init()
 
324
  */
 
325
  ha_checksum(*calc_write_checksum)(MARIA_HA *, const uchar *);
 
326
  /* calculate checksum for a row during check table */
 
327
  ha_checksum(*calc_check_checksum)(MARIA_HA *, const uchar *);
 
328
  /* Compare a row in memory with a row on disk */
 
329
  my_bool (*compare_unique)(MARIA_HA *, MARIA_UNIQUEDEF *,
 
330
                            const uchar *record, MARIA_RECORD_POS pos);
 
331
  my_off_t (*keypos_to_recpos)(struct st_maria_share *share, my_off_t pos);
 
332
  my_off_t (*recpos_to_keypos)(struct st_maria_share *share, my_off_t pos);
 
333
  my_bool (*row_is_visible)(MARIA_HA *);
 
334
 
 
335
  /* Mapings to read/write the data file */
 
336
  size_t (*file_read)(MARIA_HA *, uchar *, size_t, my_off_t, myf);
 
337
  size_t (*file_write)(MARIA_HA *, const uchar *, size_t, my_off_t, myf);
 
338
  invalidator_by_filename invalidator;  /* query cache invalidator */
 
339
  my_off_t current_key_del;             /* delete links for index pages */
 
340
  ulong this_process;                   /* processid */
 
341
  ulong last_process;                   /* For table-change-check */
 
342
  ulong last_version;                   /* Version on start */
 
343
  ulong options;                        /* Options used */
 
344
  ulong min_pack_length;                /* These are used by packed data */
 
345
  ulong max_pack_length;
 
346
  ulong state_diff_length;
 
347
  uint rec_reflength;                   /* rec_reflength in use now */
 
348
  uint unique_name_length;
 
349
  uint keypage_header;
 
350
  uint32 ftparsers;                     /* Number of distinct ftparsers
 
351
                                                   + 1 */
 
352
  PAGECACHE_FILE kfile;                 /* Shared keyfile */
 
353
  File data_file;                       /* Shared data file */
 
354
  int mode;                             /* mode of file on open */
 
355
  uint reopen;                          /* How many times reopened */
 
356
  uint in_trans;                        /* Number of references by trn */
 
357
  uint w_locks, r_locks, tot_locks;     /* Number of read/write locks */
 
358
  uint block_size;                      /* block_size of keyfile & data file*/
 
359
  /* Fixed length part of a packed row in BLOCK_RECORD format */
 
360
  uint base_length;
 
361
  myf write_flag;
 
362
  enum data_file_type data_file_type;
 
363
  enum pagecache_page_type page_type;   /* value depending transactional */
 
364
  uint8 in_checkpoint;               /**< if Checkpoint looking at table */
 
365
  my_bool temporary;
 
366
  /* Below flag is needed to make log tables work with concurrent insert */
 
367
  my_bool is_log_table;
 
368
 
 
369
  my_bool changed,                      /* If changed since lock */
 
370
    global_changed,                     /* If changed since open */
 
371
    not_flushed;
 
372
  my_bool lock_key_trees;               /* If we have to lock trees on read */
 
373
  my_bool non_transactional_concurrent_insert;
 
374
  my_bool delay_key_write;
 
375
  my_bool have_rtree;
 
376
  /**
 
377
     @brief if the table is transactional right now. It may have been created
 
378
     transactional (base.born_transactional==TRUE) but with transactionality
 
379
     (logging) temporarily disabled (now_transactional==FALSE). The opposite
 
380
     (FALSE, TRUE) is impossible.
 
381
  */
 
382
  my_bool now_transactional;
 
383
  my_bool have_versioning;
 
384
  my_bool used_key_del;                         /* != 0 if key_del is locked */
 
385
#ifdef THREAD
 
386
  THR_LOCK lock;
 
387
  void (*lock_restore_status)(void *);
 
388
  pthread_mutex_t intern_lock;          /* Locking for use with _locking */
 
389
  pthread_cond_t intern_cond;
 
390
#endif
 
391
  my_off_t mmaped_length;
 
392
  uint nonmmaped_inserts;               /* counter of writing in
 
393
                                                   non-mmaped area */
 
394
  MARIA_FILE_BITMAP bitmap;
 
395
  rw_lock_t mmap_lock;
 
396
  LSN lsn_of_file_id; /**< LSN of its last LOGREC_FILE_ID */
 
397
} MARIA_SHARE;
 
398
 
 
399
 
 
400
typedef uchar MARIA_BITMAP_BUFFER;
 
401
 
 
402
typedef struct st_maria_bitmap_block
 
403
{
 
404
  pgcache_page_no_t page;                       /* Page number */
 
405
  /* Number of continuous pages. TAIL_BIT is set if this is a tail page */
 
406
  uint page_count;
 
407
  uint empty_space;                     /* Set for head and tail pages */
 
408
  /*
 
409
    Number of BLOCKS for block-region (holds all non-blob-fields or one blob)
 
410
  */
 
411
  uint sub_blocks;
 
412
  /* set to <> 0 in write_record() if this block was actually used */
 
413
  uint8 used;
 
414
  uint8 org_bitmap_value;
 
415
} MARIA_BITMAP_BLOCK;
 
416
 
 
417
 
 
418
typedef struct st_maria_bitmap_blocks
 
419
{
 
420
  MARIA_BITMAP_BLOCK *block;
 
421
  uint count;
 
422
  my_bool tail_page_skipped;            /* If some tail pages was not used */
 
423
  my_bool page_skipped;                 /* If some full pages was not used */
 
424
} MARIA_BITMAP_BLOCKS;
 
425
 
 
426
 
 
427
/* Data about the currently read row */
 
428
typedef struct st_maria_row
 
429
{
 
430
  MARIA_BITMAP_BLOCKS insert_blocks;
 
431
  MARIA_BITMAP_BUFFER *extents;
 
432
  MARIA_RECORD_POS lastpos, nextpos;
 
433
  MARIA_RECORD_POS *tail_positions;
 
434
  ha_checksum checksum;
 
435
  LSN orig_undo_lsn;                    /* Lsn at start of row insert */
 
436
  TrID trid;                            /* Transaction id for current row */
 
437
  uchar *empty_bits, *field_lengths;
 
438
  uint *null_field_lengths;             /* All null field lengths */
 
439
  ulong *blob_lengths;                  /* Length for each blob */
 
440
  ulong min_length, normal_length, char_length, varchar_length;
 
441
  ulong blob_length, head_length, total_length;
 
442
  size_t extents_buffer_length;         /* Size of 'extents' buffer */
 
443
  uint field_lengths_length;            /* Length of data in field_lengths */
 
444
  uint extents_count;                   /* number of extents in 'extents' */
 
445
  uint full_page_count, tail_count;     /* For maria_chk */
 
446
  uint space_on_head_page;
 
447
} MARIA_ROW;
 
448
 
 
449
/* Data to scan row in blocked format */
 
450
typedef struct st_maria_block_scan
 
451
{
 
452
  uchar *bitmap_buff, *bitmap_pos, *bitmap_end, *page_buff;
 
453
  uchar *dir, *dir_end;
 
454
  pgcache_page_no_t bitmap_page;
 
455
  ulonglong bits;
 
456
  uint number_of_rows, bit_pos;
 
457
  MARIA_RECORD_POS row_base_page;
 
458
} MARIA_BLOCK_SCAN;
 
459
 
 
460
 
 
461
struct st_maria_handler
 
462
{
 
463
  MARIA_SHARE *s;                       /* Shared between open:s */
 
464
  struct st_transaction *trn;           /* Pointer to active transaction */
 
465
  MARIA_STATUS_INFO *state, state_save;
 
466
  MARIA_STATUS_INFO *state_start;       /* State at start of transaction */
 
467
  MARIA_ROW cur_row;                    /* The active row that we just read */
 
468
  MARIA_ROW new_row;                    /* Storage for a row during update */
 
469
  MARIA_KEY last_key;                   /* Last found key */
 
470
  MARIA_BLOCK_SCAN scan, *scan_save;
 
471
  MARIA_BLOB *blobs;                    /* Pointer to blobs */
 
472
  MARIA_BIT_BUFF bit_buff;
 
473
  DYNAMIC_ARRAY bitmap_blocks;
 
474
  DYNAMIC_ARRAY pinned_pages;
 
475
  /* accumulate indexfile changes between write's */
 
476
  TREE *bulk_insert;
 
477
  LEX_CUSTRING *log_row_parts;          /* For logging */
 
478
  DYNAMIC_ARRAY *ft1_to_ft2;            /* used only in ft1->ft2 conversion */
 
479
  MEM_ROOT      ft_memroot;             /* used by the parser               */
 
480
  MYSQL_FTPARSER_PARAM *ftparser_param; /* share info between init/deinit */
 
481
  uchar *buff;                          /* page buffer */
 
482
  uchar *keyread_buff;                   /* Buffer for last key read */
 
483
  uchar *lastkey_buff;                  /* Last used search key */
 
484
  uchar *lastkey_buff2;
 
485
  uchar *first_mbr_key;                 /* Searhed spatial key */
 
486
  uchar *rec_buff;                      /* Temp buffer for recordpack */
 
487
  uchar *blob_buff;                     /* Temp buffer for blobs */
 
488
  uchar *int_keypos,                    /* Save position for next/previous */
 
489
   *int_maxpos;                         /* -""- */
 
490
  uchar *update_field_data;             /* Used by update in rows-in-block */
 
491
  uint int_nod_flag;                    /* -""- */
 
492
  uint32 int_keytree_version;           /* -""- */
 
493
  int (*read_record)(MARIA_HA *, uchar*, MARIA_RECORD_POS);
 
494
  invalidator_by_filename invalidator;  /* query cache invalidator */
 
495
  ulonglong last_auto_increment;        /* auto value at start of statement */
 
496
  ulong this_unique;                    /* uniq filenumber or thread */
 
497
  ulong last_unique;                    /* last unique number */
 
498
  ulong this_loop;                      /* counter for this open */
 
499
  ulong last_loop;                      /* last used counter */
 
500
  MARIA_RECORD_POS save_lastpos;
 
501
  MARIA_RECORD_POS dup_key_pos;
 
502
  my_off_t pos;                         /* Intern variable */
 
503
  my_off_t last_keypage;                /* Last key page read */
 
504
  my_off_t last_search_keypage;         /* Last keypage when searching */
 
505
 
 
506
  /*
 
507
    QQ: the folloing two xxx_length fields should be removed,
 
508
     as they are not compatible with parallel repair
 
509
  */
 
510
  ulong packed_length, blob_length;     /* Length of found, packed record */
 
511
  size_t rec_buff_size, blob_buff_size;
 
512
  PAGECACHE_FILE dfile;                 /* The datafile */
 
513
  IO_CACHE rec_cache;                   /* When cacheing records */
 
514
  LIST open_list;
 
515
  MY_BITMAP changed_fields;
 
516
  ulong row_base_length;                /* Length of row header */
 
517
  uint row_flag;                        /* Flag to store in row header */
 
518
  uint opt_flag;                        /* Optim. for space/speed */
 
519
  uint update;                          /* If file changed since open */
 
520
  int lastinx;                          /* Last used index */
 
521
  uint last_rkey_length;                /* Last length in maria_rkey() */
 
522
  uint *last_rtree_keypos;              /* Last key positions for rtrees */
 
523
  uint bulk_insert_ref_length;          /* Lenght of row ref during bi */
 
524
  uint non_flushable_state;
 
525
  enum ha_rkey_function last_key_func;  /* CONTAIN, OVERLAP, etc */
 
526
  uint save_lastkey_data_length;
 
527
  uint save_lastkey_ref_length;
 
528
  uint pack_key_length;                 /* For MARIA_MRG */
 
529
  myf lock_wait;                        /* is 0 or MY_SHORT_WAIT */
 
530
  int errkey;                           /* Got last error on this key */
 
531
  int lock_type;                        /* How database was locked */
 
532
  int tmp_lock_type;                    /* When locked by readinfo */
 
533
  uint data_changed;                    /* Somebody has changed data */
 
534
  uint save_update;                     /* When using KEY_READ */
 
535
  int save_lastinx;
 
536
  uint preload_buff_size;               /* When preloading indexes */
 
537
  uint16 last_used_keyseg;              /* For MARIAMRG */
 
538
  uint8 used_key_del;                   /* != 0 if key_del is used */
 
539
  my_bool was_locked;                   /* Was locked in panic */
 
540
  my_bool append_insert_at_end;         /* Set if concurrent insert */
 
541
  my_bool quick_mode;
 
542
  /* Marker if key_del_changed */
 
543
  /* If info->keyread_buff can't be used for rnext */
 
544
  my_bool page_changed;
 
545
  /* If info->keyread_buff has to be re-read for rnext */
 
546
  my_bool keyread_buff_used;
 
547
  my_bool once_flags;                   /* For MARIA_MRG */
 
548
#ifdef __WIN__
 
549
  my_bool owned_by_merge;               /* This Maria table is part of a merge union */
 
550
#endif
 
551
#ifdef THREAD
 
552
  THR_LOCK_DATA lock;
 
553
#endif
 
554
  uchar *maria_rtree_recursion_state;   /* For RTREE */
 
555
  uchar length_buff[5];                 /* temp buff to store blob lengths */
 
556
  int maria_rtree_recursion_depth;
 
557
};
 
558
 
 
559
/* Some defines used by maria-functions */
 
560
 
 
561
#define USE_WHOLE_KEY   65535         /* Use whole key in _search() */
 
562
#define F_EXTRA_LCK     -1
 
563
 
 
564
/* bits in opt_flag */
 
565
#define MEMMAP_USED     32
 
566
#define REMEMBER_OLD_POS 64
 
567
 
 
568
#define WRITEINFO_UPDATE_KEYFILE        1
 
569
#define WRITEINFO_NO_UNLOCK             2
 
570
 
 
571
/* once_flags */
 
572
#define USE_PACKED_KEYS         1
 
573
#define RRND_PRESERVE_LASTINX   2
 
574
 
 
575
/* bits in state.changed */
 
576
 
 
577
#define STATE_CHANGED            1
 
578
#define STATE_CRASHED            2
 
579
#define STATE_CRASHED_ON_REPAIR  4
 
580
#define STATE_NOT_ANALYZED       8
 
581
#define STATE_NOT_OPTIMIZED_KEYS 16
 
582
#define STATE_NOT_SORTED_PAGES   32
 
583
#define STATE_NOT_OPTIMIZED_ROWS 64
 
584
#define STATE_NOT_ZEROFILLED     128
 
585
#define STATE_NOT_MOVABLE        256
 
586
#define STATE_MOVED              512 /* set if base->uuid != maria_uuid */
 
587
 
 
588
/* options to maria_read_cache */
 
589
 
 
590
#define READING_NEXT    1
 
591
#define READING_HEADER  2
 
592
 
 
593
/* Number of bytes on key pages to indicate used size */
 
594
#define KEYPAGE_USED_SIZE  2
 
595
#define KEYPAGE_KEYID_SIZE 1
 
596
#define KEYPAGE_FLAG_SIZE  1
 
597
#define KEYPAGE_CHECKSUM_SIZE 4
 
598
#define MAX_KEYPAGE_HEADER_SIZE (LSN_STORE_SIZE + KEYPAGE_USED_SIZE + \
 
599
                                 KEYPAGE_KEYID_SIZE + KEYPAGE_FLAG_SIZE + \
 
600
                                 TRANSID_SIZE)
 
601
#define KEYPAGE_FLAG_ISNOD      1
 
602
#define KEYPAGE_FLAG_HAS_TRANSID 2
 
603
/* Position to KEYPAGE_FLAG for transactional tables */
 
604
#define KEYPAGE_TRANSFLAG_OFFSET LSN_STORE_SIZE + TRANSID_SIZE + KEYPAGE_KEYID_SIZE
 
605
 
 
606
#define _ma_get_page_used(share,x) \
 
607
  ((uint) mi_uint2korr((x) + (share)->keypage_header - KEYPAGE_USED_SIZE))
 
608
#define _ma_store_page_used(share,x,y) \
 
609
  mi_int2store((x) + (share)->keypage_header - KEYPAGE_USED_SIZE, (y))
 
610
#define _ma_test_if_nod(share,x) \
 
611
  ((_ma_get_keypage_flag(share,x) & KEYPAGE_FLAG_ISNOD) ? (share)->base.key_reflength : 0)
 
612
 
 
613
#define _ma_get_used_and_nod(share,buff,length,nod)                     \
 
614
{                                                                      \
 
615
  (nod)=    _ma_test_if_nod((share),(buff));                            \
 
616
  (length)= _ma_get_page_used((share),(buff));                          \
 
617
}
 
618
#define _ma_get_used_and_nod_with_flag(share,flag,buff,length,nod)     \
 
619
{                                                                      \
 
620
  (nod)= (((flag) & KEYPAGE_FLAG_ISNOD) ? (share)->base.key_reflength : 0); \
 
621
  (length)= _ma_get_page_used((share),(buff));                          \
 
622
}
 
623
#define _ma_store_keynr(share, x, nr) x[(share)->keypage_header - KEYPAGE_KEYID_SIZE - KEYPAGE_FLAG_SIZE - KEYPAGE_USED_SIZE]= (nr)
 
624
#define _ma_get_keynr(share, x) ((uchar) x[(share)->keypage_header - KEYPAGE_KEYID_SIZE - KEYPAGE_FLAG_SIZE - KEYPAGE_USED_SIZE])
 
625
#define _ma_store_transid(buff, transid) \
 
626
  transid_store((buff) + LSN_STORE_SIZE, (transid))
 
627
#define _ma_korr_transid(buff) \
 
628
  transid_korr((buff) + LSN_STORE_SIZE)
 
629
#define _ma_get_keypage_flag(share,x) x[(share)->keypage_header - KEYPAGE_USED_SIZE - KEYPAGE_FLAG_SIZE]
 
630
#define _ma_store_keypage_flag(share,x,flag) x[(share)->keypage_header - KEYPAGE_USED_SIZE - KEYPAGE_FLAG_SIZE]= (flag)
 
631
#define _ma_mark_page_with_transid(share, x) x[(share)->keypage_header - KEYPAGE_USED_SIZE - KEYPAGE_FLAG_SIZE]|= KEYPAGE_FLAG_HAS_TRANSID
 
632
 
 
633
 
 
634
/*
 
635
  TODO: write int4store_aligned as *((uint32 *) (T))= (uint32) (A) for
 
636
  architectures where it is possible
 
637
*/
 
638
#define int4store_aligned(A,B) int4store((A),(B))
 
639
 
 
640
#define maria_mark_crashed(x) do{(x)->s->state.changed|= STATE_CRASHED; \
 
641
    DBUG_PRINT("error", ("Marked table crashed"));                      \
 
642
  }while(0)
 
643
#define maria_mark_crashed_share(x)                                     \
 
644
  do{(x)->state.changed|= STATE_CRASHED;                                \
 
645
    DBUG_PRINT("error", ("Marked table crashed"));                      \
 
646
  }while(0)
 
647
#define maria_mark_crashed_on_repair(x) do{(x)->s->state.changed|=      \
 
648
      STATE_CRASHED|STATE_CRASHED_ON_REPAIR;                            \
 
649
    (x)->update|= HA_STATE_CHANGED;                                     \
 
650
    DBUG_PRINT("error",                                                 \
 
651
               ("Marked table crashed"));                               \
 
652
  }while(0)
 
653
#define maria_is_crashed(x) ((x)->s->state.changed & STATE_CRASHED)
 
654
#define maria_is_crashed_on_repair(x) ((x)->s->state.changed & STATE_CRASHED_ON_REPAIR)
 
655
#ifdef EXTRA_DEBUG
 
656
/**
 
657
  Brings additional information in certain debug builds and in standalone
 
658
  (non-ha_maria) programs. To help debugging. Not in ha_maria, to not spam the
 
659
  user (some messages can be produced many times per statement, or even
 
660
  wrongly during some repair operations).
 
661
*/
 
662
#define maria_print_error(SHARE, ERRNO)                         \
 
663
  do{ if (!maria_in_ha_maria)                                   \
 
664
      _ma_report_error((ERRNO), (SHARE)->index_file_name); }    \
 
665
  while(0)
 
666
#else
 
667
#define maria_print_error(SHARE, ERRNO) while (0)
 
668
#endif
 
669
#define DBUG_DUMP_KEY(name, key) DBUG_DUMP(name, (key)->data, (key)->data_length + (key)->ref_length)
 
670
 
 
671
 
 
672
/* Functions to store length of space packed keys, VARCHAR or BLOB keys */
 
673
 
 
674
#define store_key_length(key,length) \
 
675
{ if ((length) < 255) \
 
676
  { *(key)=(length); } \
 
677
  else \
 
678
  { *(key)=255; mi_int2store((key)+1,(length)); } \
 
679
}
 
680
 
 
681
#define get_key_full_length(length,key) \
 
682
  { if (*(const uchar*) (key) != 255)            \
 
683
    length= ((uint) *(const uchar*) ((key)++))+1; \
 
684
  else \
 
685
  { length=mi_uint2korr((key)+1)+3; (key)+=3; } \
 
686
}
 
687
 
 
688
#define get_key_full_length_rdonly(length,key) \
 
689
{ if (*(const uchar*) (key) != 255) \
 
690
    length= ((uint) *(const uchar*) ((key)))+1; \
 
691
  else \
 
692
  { length=mi_uint2korr((key)+1)+3; } \
 
693
}
 
694
 
 
695
#define maria_max_key_length() ((maria_block_size - MAX_KEYPAGE_HEADER_SIZE)/2 - MARIA_INDEX_OVERHEAD_SIZE)
 
696
#define get_pack_length(length) ((length) >= 255 ? 3 : 1)
 
697
#define _ma_have_versioning(info) ((info)->row_flag & ROW_FLAG_TRANSID)
 
698
 
 
699
#define MARIA_MIN_BLOCK_LENGTH  20              /* Because of delete-link */
 
700
/* Don't use to small record-blocks */
 
701
#define MARIA_EXTEND_BLOCK_LENGTH       20
 
702
#define MARIA_SPLIT_LENGTH      ((MARIA_EXTEND_BLOCK_LENGTH+4)*2)
 
703
        /* Max prefix of record-block */
 
704
#define MARIA_MAX_DYN_BLOCK_HEADER      20
 
705
#define MARIA_BLOCK_INFO_HEADER_LENGTH 20
 
706
#define MARIA_DYN_DELETE_BLOCK_HEADER 20    /* length of delete-block-header */
 
707
#define MARIA_DYN_MAX_BLOCK_LENGTH      ((1L << 24)-4L)
 
708
#define MARIA_DYN_MAX_ROW_LENGTH        (MARIA_DYN_MAX_BLOCK_LENGTH - MARIA_SPLIT_LENGTH)
 
709
#define MARIA_DYN_ALIGN_SIZE      4     /* Align blocks on this */
 
710
#define MARIA_MAX_DYN_HEADER_BYTE 13    /* max header uchar for dynamic rows */
 
711
#define MARIA_MAX_BLOCK_LENGTH  ((((ulong) 1 << 24)-1) & (~ (ulong) (MARIA_DYN_ALIGN_SIZE-1)))
 
712
#define MARIA_REC_BUFF_OFFSET      ALIGN_SIZE(MARIA_DYN_DELETE_BLOCK_HEADER+sizeof(uint32))
 
713
 
 
714
#define MEMMAP_EXTRA_MARGIN     7       /* Write this as a suffix for file */
 
715
 
 
716
#define PACK_TYPE_SELECTED      1       /* Bits in field->pack_type */
 
717
#define PACK_TYPE_SPACE_FIELDS  2
 
718
#define PACK_TYPE_ZERO_FILL     4
 
719
#define MARIA_FOUND_WRONG_KEY 32738     /* Impossible value from ha_key_cmp */
 
720
 
 
721
#define MARIA_BLOCK_SIZE(key_length,data_pointer,key_pointer,block_size)  (((((key_length)+(data_pointer)+(key_pointer))*4+(key_pointer)+2)/(block_size)+1)*(block_size))
 
722
#define MARIA_MAX_KEYPTR_SIZE   5       /* For calculating block lengths */
 
723
 
 
724
/* Marker for impossible delete link */
 
725
#define IMPOSSIBLE_PAGE_NO LL(0xFFFFFFFFFF)
 
726
 
 
727
/* The UNIQUE check is done with a hashed long key */
 
728
 
 
729
#define MARIA_UNIQUE_HASH_TYPE  HA_KEYTYPE_ULONG_INT
 
730
#define maria_unique_store(A,B)    mi_int4store((A),(B))
 
731
 
 
732
#ifdef THREAD
 
733
extern pthread_mutex_t THR_LOCK_maria;
 
734
#endif
 
735
#if !defined(THREAD) || defined(DONT_USE_RW_LOCKS)
 
736
#define rw_wrlock(A) {}
 
737
#define rw_rdlock(A) {}
 
738
#define rw_unlock(A) {}
 
739
#endif
 
740
 
 
741
/* Some tuning parameters */
 
742
#define MARIA_MIN_KEYBLOCK_LENGTH 50    /* When to split delete blocks */
 
743
#define MARIA_MIN_SIZE_BULK_INSERT_TREE 16384   /* this is per key */
 
744
#define MARIA_MIN_ROWS_TO_USE_BULK_INSERT 100
 
745
#define MARIA_MIN_ROWS_TO_DISABLE_INDEXES 100
 
746
#define MARIA_MIN_ROWS_TO_USE_WRITE_CACHE 10
 
747
/* Keep a small buffer for tables only using small blobs */
 
748
#define MARIA_SMALL_BLOB_BUFFER 1024
 
749
#define MARIA_MAX_CONTROL_FILE_LOCK_RETRY 30     /* Retry this many times */
 
750
 
 
751
 
 
752
/* Some extern variables */
 
753
extern LIST *maria_open_list;
 
754
extern uchar maria_file_magic[], maria_pack_file_magic[];
 
755
extern uchar maria_uuid[MY_UUID_SIZE];
 
756
extern uint32 maria_read_vec[], maria_readnext_vec[];
 
757
extern uint maria_quick_table_bits;
 
758
extern char *maria_data_root;
 
759
extern uchar maria_zero_string[];
 
760
extern my_bool maria_inited, maria_in_ha_maria;
 
761
extern HASH maria_stored_state;
 
762
 
 
763
/* This is used by _ma_calc_xxx_key_length och _ma_store_key */
 
764
typedef struct st_maria_s_param
 
765
{
 
766
  const uchar *key;
 
767
  uchar *prev_key, *next_key_pos;
 
768
  uchar *key_pos;                               /* For balance page */
 
769
  uint ref_length, key_length, n_ref_length;
 
770
  uint n_length, totlength, part_of_prev_key, prev_length, pack_marker;
 
771
  uint changed_length;
 
772
  int move_length;                              /* For balance_page */
 
773
  my_bool store_not_null;
 
774
} MARIA_KEY_PARAM;
 
775
 
 
776
 
 
777
/* Used to store reference to pinned page */
 
778
typedef struct st_pinned_page
 
779
{
 
780
  PAGECACHE_BLOCK_LINK *link;
 
781
  enum pagecache_page_lock unlock, write_lock;
 
782
  my_bool changed;
 
783
} MARIA_PINNED_PAGE;
 
784
 
 
785
 
 
786
/* Prototypes for intern functions */
 
787
extern int _ma_read_dynamic_record(MARIA_HA *, uchar *, MARIA_RECORD_POS);
 
788
extern int _ma_read_rnd_dynamic_record(MARIA_HA *, uchar *, MARIA_RECORD_POS,
 
789
                                       my_bool);
 
790
extern my_bool _ma_write_dynamic_record(MARIA_HA *, const uchar *);
 
791
extern my_bool _ma_update_dynamic_record(MARIA_HA *, MARIA_RECORD_POS,
 
792
                                         const uchar *, const uchar *);
 
793
extern my_bool _ma_delete_dynamic_record(MARIA_HA *info, const uchar *record);
 
794
extern my_bool _ma_cmp_dynamic_record(MARIA_HA *info, const uchar *record);
 
795
extern my_bool _ma_write_blob_record(MARIA_HA *, const uchar *);
 
796
extern my_bool _ma_update_blob_record(MARIA_HA *, MARIA_RECORD_POS,
 
797
                                      const uchar *, const uchar *);
 
798
extern int _ma_read_static_record(MARIA_HA *info, uchar *, MARIA_RECORD_POS);
 
799
extern int _ma_read_rnd_static_record(MARIA_HA *, uchar *, MARIA_RECORD_POS,
 
800
                                      my_bool);
 
801
extern my_bool _ma_write_static_record(MARIA_HA *, const uchar *);
 
802
extern my_bool _ma_update_static_record(MARIA_HA *, MARIA_RECORD_POS,
 
803
                                        const uchar *, const uchar *);
 
804
extern my_bool _ma_delete_static_record(MARIA_HA *info, const uchar *record);
 
805
extern my_bool _ma_cmp_static_record(MARIA_HA *info, const uchar *record);
 
806
extern my_bool _ma_ck_write(MARIA_HA *info, MARIA_KEY *key);
 
807
extern int _ma_enlarge_root(MARIA_HA *info, MARIA_KEY *key,
 
808
                            MARIA_RECORD_POS *root);
 
809
extern int _ma_insert(MARIA_HA *info, MARIA_KEY *key, uchar *anc_buff,
 
810
                      uchar *key_pos, my_off_t anc_page, uchar *key_buff,
 
811
                      my_off_t father_page, uchar *father_buff,
 
812
                      MARIA_PINNED_PAGE *father_page_link,
 
813
                      uchar *father_key_pos, my_bool insert_last);
 
814
extern int _ma_ck_real_write_btree(MARIA_HA *info, MARIA_KEY *key,
 
815
                                   MARIA_RECORD_POS *root, uint32 comp_flag);
 
816
extern int _ma_split_page(MARIA_HA *info, MARIA_KEY *key, my_off_t split_page,
 
817
                          uchar *split_buff, uint org_split_length,
 
818
                          uchar *inserted_key_pos, uint changed_length,
 
819
                          int move_length,
 
820
                          uchar *key_buff, my_bool insert_last_key);
 
821
extern uchar *_ma_find_half_pos(MARIA_HA *info, MARIA_KEY *key, uint nod_flag,
 
822
                                uchar *page, uchar ** after_key);
 
823
extern int _ma_calc_static_key_length(const MARIA_KEY *key, uint nod_flag,
 
824
                                      uchar *key_pos, uchar *org_key,
 
825
                                      uchar *key_buff,
 
826
                                      MARIA_KEY_PARAM *s_temp);
 
827
extern int _ma_calc_var_key_length(const MARIA_KEY *key, uint nod_flag,
 
828
                                   uchar *key_pos, uchar *org_key,
 
829
                                   uchar *key_buff,
 
830
                                   MARIA_KEY_PARAM *s_temp);
 
831
extern int _ma_calc_var_pack_key_length(const MARIA_KEY *key,
 
832
                                        uint nod_flag, uchar *next_key,
 
833
                                        uchar *org_key, uchar *prev_key,
 
834
                                        MARIA_KEY_PARAM *s_temp);
 
835
extern int _ma_calc_bin_pack_key_length(const MARIA_KEY *key,
 
836
                                        uint nod_flag, uchar *next_key,
 
837
                                        uchar *org_key, uchar *prev_key,
 
838
                                        MARIA_KEY_PARAM *s_temp);
 
839
extern void _ma_store_static_key(MARIA_KEYDEF *keyinfo, uchar *key_pos,
 
840
                                 MARIA_KEY_PARAM *s_temp);
 
841
extern void _ma_store_var_pack_key(MARIA_KEYDEF *keyinfo, uchar *key_pos,
 
842
                                   MARIA_KEY_PARAM *s_temp);
 
843
#ifdef NOT_USED
 
844
extern void _ma_store_pack_key(MARIA_KEYDEF *keyinfo, uchar *key_pos,
 
845
                               MARIA_KEY_PARAM *s_temp);
 
846
#endif
 
847
extern void _ma_store_bin_pack_key(MARIA_KEYDEF *keyinfo, uchar *key_pos,
 
848
                                   MARIA_KEY_PARAM *s_temp);
 
849
 
 
850
extern int _ma_ck_delete(MARIA_HA *info, MARIA_KEY *key);
 
851
extern int _ma_ck_real_delete(register MARIA_HA *info, MARIA_KEY *key,
 
852
                              my_off_t *root);
 
853
extern int _ma_readinfo(MARIA_HA *info, int lock_flag, int check_keybuffer);
 
854
extern int _ma_writeinfo(MARIA_HA *info, uint options);
 
855
extern int _ma_test_if_changed(MARIA_HA *info);
 
856
extern int _ma_mark_file_changed(MARIA_HA *info);
 
857
extern void _ma_mark_file_crashed(MARIA_SHARE *share);
 
858
extern my_bool _ma_set_uuid(MARIA_HA *info, my_bool reset_uuid);
 
859
extern my_bool _ma_check_if_zero(uchar *pos, size_t size);
 
860
extern int _ma_decrement_open_count(MARIA_HA *info);
 
861
extern int _ma_check_index(MARIA_HA *info, int inx);
 
862
extern int _ma_search(MARIA_HA *info, MARIA_KEY *key, uint32 nextflag,
 
863
                      my_off_t pos);
 
864
extern int _ma_bin_search( const MARIA_KEY *key, uchar *page,
 
865
                          uint32 comp_flag, uchar **ret_pos, uchar *buff,
 
866
                          my_bool *was_last_key);
 
867
extern int _ma_seq_search(const MARIA_KEY *key, uchar *page,
 
868
                          uint comp_flag, uchar ** ret_pos, uchar *buff,
 
869
                          my_bool *was_last_key);
 
870
extern int _ma_prefix_search(const MARIA_KEY *key, uchar *page,
 
871
                             uint32 comp_flag, uchar ** ret_pos, uchar *buff,
 
872
                             my_bool *was_last_key);
 
873
extern my_off_t _ma_kpos(uint nod_flag, const uchar *after_key);
 
874
extern void _ma_kpointer(MARIA_HA *info, uchar *buff, my_off_t pos);
 
875
MARIA_RECORD_POS _ma_row_pos_from_key(const MARIA_KEY *key);
 
876
TrID _ma_trid_from_key(const MARIA_KEY *key);
 
877
extern MARIA_RECORD_POS _ma_rec_pos(MARIA_SHARE *share, uchar *ptr);
 
878
extern void _ma_dpointer(MARIA_SHARE *share, uchar *buff,
 
879
                         MARIA_RECORD_POS pos);
 
880
extern uint _ma_get_static_key(MARIA_KEY *key, uint page_flag, uint nod_flag,
 
881
                               uchar **page);
 
882
extern uchar *_ma_skip_static_key(MARIA_KEY *key, uint page_flag,
 
883
                           uint nod_flag, uchar *page);
 
884
extern uint _ma_get_pack_key(MARIA_KEY *key, uint page_flag, uint nod_flag,
 
885
                             uchar **page);
 
886
extern uchar *_ma_skip_pack_key(MARIA_KEY *key, uint page_flag,
 
887
                                uint nod_flag, uchar *page);
 
888
extern uint _ma_get_binary_pack_key(MARIA_KEY *key, uint page_flag,
 
889
                                    uint nod_flag, uchar **page_pos);
 
890
uchar *_ma_skip_binary_pack_key(MARIA_KEY *key, uint page_flag,
 
891
                                uint nod_flag, uchar *page);
 
892
extern uchar *_ma_get_last_key(MARIA_KEY *key, uchar *keypos, uchar *endpos);
 
893
extern uchar *_ma_get_key(MARIA_KEY *key, uchar *page, uchar *keypos);
 
894
extern uint _ma_keylength(MARIA_KEYDEF *keyinfo, const uchar *key);
 
895
extern uint _ma_keylength_part(MARIA_KEYDEF *keyinfo, const uchar *key,
 
896
                               HA_KEYSEG *end);
 
897
extern uchar *_qq_move_key(MARIA_KEYDEF *keyinfo, uchar *to,
 
898
                           const uchar *from);
 
899
 
 
900
extern int _ma_search_next(MARIA_HA *info, MARIA_KEY *key,
 
901
                           uint32 nextflag, my_off_t pos);
 
902
extern int _ma_search_first(MARIA_HA *info, MARIA_KEYDEF *keyinfo,
 
903
                            my_off_t pos);
 
904
extern int _ma_search_last(MARIA_HA *info, MARIA_KEYDEF *keyinfo,
 
905
                           my_off_t pos);
 
906
extern my_off_t _ma_static_keypos_to_recpos(MARIA_SHARE *share, my_off_t pos);
 
907
extern my_off_t _ma_static_recpos_to_keypos(MARIA_SHARE *share, my_off_t pos);
 
908
extern my_off_t _ma_transparent_recpos(MARIA_SHARE *share, my_off_t pos);
 
909
extern my_off_t _ma_transaction_keypos_to_recpos(MARIA_SHARE *, my_off_t pos);
 
910
extern my_off_t _ma_transaction_recpos_to_keypos(MARIA_SHARE *, my_off_t pos);
 
911
 
 
912
extern uchar *_ma_fetch_keypage(MARIA_HA *info,
 
913
                                const MARIA_KEYDEF *keyinfo,
 
914
                                my_off_t page, enum pagecache_page_lock lock,
 
915
                                int level, uchar *buff, int return_buffer,
 
916
                                MARIA_PINNED_PAGE **page_link);
 
917
extern int _ma_write_keypage(MARIA_HA *info,
 
918
                             const MARIA_KEYDEF *keyinfo,
 
919
                             my_off_t page, enum pagecache_page_lock lock,
 
920
                             int level, uchar *buff);
 
921
extern int _ma_dispose(MARIA_HA *info, my_off_t pos, my_bool page_not_read);
 
922
extern my_off_t _ma_new(register MARIA_HA *info, int level,
 
923
                        MARIA_PINNED_PAGE **page_link);
 
924
extern my_bool _ma_compact_keypage(MARIA_HA *info, MARIA_KEYDEF *keyinfo,
 
925
                                   my_off_t page_pos, uchar *page,
 
926
                                   TrID min_read_from);
 
927
extern uint transid_store_packed(MARIA_HA *info, uchar *to, ulonglong trid);
 
928
extern ulonglong transid_get_packed(MARIA_SHARE *share, const uchar *from);
 
929
#define transid_packed_length(data) \
 
930
  ((data)[0] < MIN_TRANSID_PACK_PREFIX ? 1 : \
 
931
   (uint) (257 - (uchar) (data)[0]))
 
932
#define key_has_transid(key) (*(key) & 1)
 
933
 
 
934
extern MARIA_KEY *_ma_make_key(MARIA_HA *info, MARIA_KEY *int_key, uint keynr,
 
935
                               uchar *key, const uchar *record,
 
936
                               MARIA_RECORD_POS filepos, ulonglong trid);
 
937
extern MARIA_KEY *_ma_pack_key(MARIA_HA *info, MARIA_KEY *int_key,
 
938
                               uint keynr, uchar *key,
 
939
                               const uchar *old, key_part_map keypart_map,
 
940
                               HA_KEYSEG ** last_used_keyseg);
 
941
extern void _ma_copy_key(MARIA_KEY *to, const MARIA_KEY *from);
 
942
extern int _ma_read_key_record(MARIA_HA *info, uchar *buf, MARIA_RECORD_POS);
 
943
extern my_bool _ma_read_cache(IO_CACHE *info, uchar *buff,
 
944
                              MARIA_RECORD_POS pos, size_t length,
 
945
                              uint re_read_if_possibly);
 
946
extern ulonglong ma_retrieve_auto_increment(const uchar *key, uint8 key_type);
 
947
extern my_bool _ma_alloc_buffer(uchar **old_addr, size_t *old_size,
 
948
                                size_t new_size);
 
949
extern ulong _ma_rec_unpack(MARIA_HA *info, uchar *to, uchar *from,
 
950
                            ulong reclength);
 
951
extern my_bool _ma_rec_check(MARIA_HA *info, const uchar *record,
 
952
                             uchar *packpos, ulong packed_length,
 
953
                             my_bool with_checkum, ha_checksum checksum);
 
954
extern int _ma_write_part_record(MARIA_HA *info, my_off_t filepos,
 
955
                                 ulong length, my_off_t next_filepos,
 
956
                                 uchar ** record, ulong *reclength,
 
957
                                 int *flag);
 
958
extern void _ma_print_key(FILE *stream, MARIA_KEY *key);
 
959
extern void _ma_print_keydata(FILE *stream, HA_KEYSEG *keyseg,
 
960
                              const uchar *key, uint length);
 
961
extern my_bool _ma_once_init_pack_row(MARIA_SHARE *share, File dfile);
 
962
extern my_bool _ma_once_end_pack_row(MARIA_SHARE *share);
 
963
extern int _ma_read_pack_record(MARIA_HA *info, uchar *buf,
 
964
                                MARIA_RECORD_POS filepos);
 
965
extern int _ma_read_rnd_pack_record(MARIA_HA *, uchar *, MARIA_RECORD_POS,
 
966
                                    my_bool);
 
967
extern int _ma_pack_rec_unpack(MARIA_HA *info, MARIA_BIT_BUFF *bit_buff,
 
968
                               uchar *to, uchar *from, ulong reclength);
 
969
extern ulonglong _ma_safe_mul(ulonglong a, ulonglong b);
 
970
extern int _ma_ft_update(MARIA_HA *info, uint keynr, uchar *keybuf,
 
971
                         const uchar *oldrec, const uchar *newrec,
 
972
                         my_off_t pos);
 
973
 
 
974
/*
 
975
  Parameter to _ma_get_block_info
 
976
  The dynamic row header is read into this struct. For an explanation of
 
977
  the fields, look at the function _ma_get_block_info().
 
978
*/
 
979
 
 
980
typedef struct st_maria_block_info
 
981
{
 
982
  uchar header[MARIA_BLOCK_INFO_HEADER_LENGTH];
 
983
  ulong rec_len;
 
984
  ulong data_len;
 
985
  ulong block_len;
 
986
  ulong blob_len;
 
987
  MARIA_RECORD_POS filepos;
 
988
  MARIA_RECORD_POS next_filepos;
 
989
  MARIA_RECORD_POS prev_filepos;
 
990
  uint second_read;
 
991
  uint offset;
 
992
} MARIA_BLOCK_INFO;
 
993
 
 
994
 
 
995
/* bits in return from _ma_get_block_info */
 
996
 
 
997
#define BLOCK_FIRST     1
 
998
#define BLOCK_LAST      2
 
999
#define BLOCK_DELETED   4
 
1000
#define BLOCK_ERROR     8                       /* Wrong data */
 
1001
#define BLOCK_SYNC_ERROR 16                     /* Right data at wrong place */
 
1002
#define BLOCK_FATAL_ERROR 32                    /* hardware-error */
 
1003
 
 
1004
#define NEED_MEM        ((uint) 10*4*(IO_SIZE+32)+32) /* Nead for recursion */
 
1005
#define MAXERR                  20
 
1006
#define BUFFERS_WHEN_SORTING    16              /* Alloc for sort-key-tree */
 
1007
#define WRITE_COUNT             MY_HOW_OFTEN_TO_WRITE
 
1008
#define INDEX_TMP_EXT           ".TMM"
 
1009
#define DATA_TMP_EXT            ".TMD"
 
1010
 
 
1011
#define UPDATE_TIME             1
 
1012
#define UPDATE_STAT             2
 
1013
#define UPDATE_SORT             4
 
1014
#define UPDATE_AUTO_INC         8
 
1015
#define UPDATE_OPEN_COUNT       16
 
1016
 
 
1017
#define USE_BUFFER_INIT         (((1024L*1024L*128-MALLOC_OVERHEAD)/8192)*8192)
 
1018
#define READ_BUFFER_INIT        (1024L*256L-MALLOC_OVERHEAD)
 
1019
#define SORT_BUFFER_INIT        (1024L*1024L*64-MALLOC_OVERHEAD)
 
1020
#define MIN_SORT_BUFFER         (4096-MALLOC_OVERHEAD)
 
1021
 
 
1022
#define fast_ma_writeinfo(INFO) if (!(INFO)->s->tot_locks) (void) _ma_writeinfo((INFO),0)
 
1023
#define fast_ma_readinfo(INFO) ((INFO)->lock_type == F_UNLCK) && _ma_readinfo((INFO),F_RDLCK,1)
 
1024
 
 
1025
extern uint _ma_get_block_info(MARIA_BLOCK_INFO *, File, my_off_t);
 
1026
extern uint _ma_rec_pack(MARIA_HA *info, uchar *to, const uchar *from);
 
1027
extern uint _ma_pack_get_block_info(MARIA_HA *maria, MARIA_BIT_BUFF *bit_buff,
 
1028
                                    MARIA_BLOCK_INFO *info, uchar **rec_buff_p,
 
1029
                                    size_t *rec_buff_size,
 
1030
                                    File file, my_off_t filepos);
 
1031
extern void _ma_store_blob_length(uchar *pos, uint pack_length, uint length);
 
1032
extern void _ma_report_error(int errcode, const char *file_name);
 
1033
extern my_bool _ma_memmap_file(MARIA_HA *info);
 
1034
extern void _ma_unmap_file(MARIA_HA *info);
 
1035
extern uint _ma_save_pack_length(uint version, uchar * block_buff,
 
1036
                                 ulong length);
 
1037
extern uint _ma_calc_pack_length(uint version, ulong length);
 
1038
extern ulong _ma_calc_blob_length(uint length, const uchar *pos);
 
1039
extern size_t _ma_mmap_pread(MARIA_HA *info, uchar *Buffer,
 
1040
                             size_t Count, my_off_t offset, myf MyFlags);
 
1041
extern size_t _ma_mmap_pwrite(MARIA_HA *info, const uchar *Buffer,
 
1042
                              size_t Count, my_off_t offset, myf MyFlags);
 
1043
extern size_t _ma_nommap_pread(MARIA_HA *info, uchar *Buffer,
 
1044
                               size_t Count, my_off_t offset, myf MyFlags);
 
1045
extern size_t _ma_nommap_pwrite(MARIA_HA *info, const uchar *Buffer,
 
1046
                                size_t Count, my_off_t offset, myf MyFlags);
 
1047
 
 
1048
uint _ma_state_info_write(MARIA_SHARE *share, uint pWrite);
 
1049
uint _ma_state_info_write_sub(File file, MARIA_STATE_INFO *state, uint pWrite);
 
1050
uint _ma_state_info_read_dsk(File file, MARIA_STATE_INFO *state);
 
1051
uint _ma_base_info_write(File file, MARIA_BASE_INFO *base);
 
1052
my_bool _ma_keyseg_write(File file, const HA_KEYSEG *keyseg);
 
1053
uchar *_ma_keyseg_read(uchar *ptr, HA_KEYSEG *keyseg);
 
1054
my_bool _ma_keydef_write(File file, MARIA_KEYDEF *keydef);
 
1055
uchar *_ma_keydef_read(uchar *ptr, MARIA_KEYDEF *keydef);
 
1056
my_bool _ma_uniquedef_write(File file, MARIA_UNIQUEDEF *keydef);
 
1057
uchar *_ma_uniquedef_read(uchar *ptr, MARIA_UNIQUEDEF *keydef);
 
1058
my_bool _ma_columndef_write(File file, MARIA_COLUMNDEF *columndef);
 
1059
uchar *_ma_columndef_read(uchar *ptr, MARIA_COLUMNDEF *columndef);
 
1060
my_bool _ma_column_nr_write(File file, uint16 *offsets, uint columns);
 
1061
uchar *_ma_column_nr_read(uchar *ptr, uint16 *offsets, uint columns);
 
1062
ulong _ma_calc_total_blob_length(MARIA_HA *info, const uchar *record);
 
1063
ha_checksum _ma_checksum(MARIA_HA *info, const uchar *buf);
 
1064
ha_checksum _ma_static_checksum(MARIA_HA *info, const uchar *buf);
 
1065
my_bool _ma_check_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
 
1066
                         uchar *record, ha_checksum unique_hash,
 
1067
                         MARIA_RECORD_POS pos);
 
1068
ha_checksum _ma_unique_hash(MARIA_UNIQUEDEF *def, const uchar *buf);
 
1069
my_bool _ma_cmp_static_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
 
1070
                              const uchar *record, MARIA_RECORD_POS pos);
 
1071
my_bool _ma_cmp_dynamic_unique(MARIA_HA *info, MARIA_UNIQUEDEF *def,
 
1072
                               const uchar *record, MARIA_RECORD_POS pos);
 
1073
my_bool _ma_unique_comp(MARIA_UNIQUEDEF *def, const uchar *a, const uchar *b,
 
1074
                        my_bool null_are_equal);
 
1075
void _ma_get_status(void *param, my_bool concurrent_insert);
 
1076
void _ma_update_status(void *param);
 
1077
void _ma_restore_status(void *param);
 
1078
void _ma_copy_status(void *to, void *from);
 
1079
my_bool _ma_check_status(void *param);
 
1080
void _ma_restore_status(void *param);
 
1081
void _ma_reset_status(MARIA_HA *maria);
 
1082
int _ma_def_scan_remember_pos(MARIA_HA *info, MARIA_RECORD_POS *lastpos);
 
1083
void _ma_def_scan_restore_pos(MARIA_HA *info, MARIA_RECORD_POS lastpos);
 
1084
 
 
1085
#include "ma_commit.h"
 
1086
 
 
1087
extern MARIA_HA *_ma_test_if_reopen(const char *filename);
 
1088
my_bool _ma_check_table_is_closed(const char *name, const char *where);
 
1089
int _ma_open_datafile(MARIA_HA *info, MARIA_SHARE *share, File file_to_dup);
 
1090
int _ma_open_keyfile(MARIA_SHARE *share);
 
1091
void _ma_setup_functions(register MARIA_SHARE *share);
 
1092
my_bool _ma_dynmap_file(MARIA_HA *info, my_off_t size);
 
1093
void _ma_remap_file(MARIA_HA *info, my_off_t size);
 
1094
 
 
1095
MARIA_RECORD_POS _ma_write_init_default(MARIA_HA *info, const uchar *record);
 
1096
my_bool _ma_write_abort_default(MARIA_HA *info);
 
1097
 
 
1098
C_MODE_START
 
1099
#define MARIA_FLUSH_DATA  1
 
1100
#define MARIA_FLUSH_INDEX 2
 
1101
int _ma_flush_table_files(MARIA_HA *info, uint flush_data_or_index,
 
1102
                          enum flush_type flush_type_for_data,
 
1103
                          enum flush_type flush_type_for_index);
 
1104
/*
 
1105
  Functions needed by _ma_check (are overridden in MySQL/ha_maria.cc).
 
1106
  See ma_check_standalone.h .
 
1107
*/
 
1108
volatile int *_ma_killed_ptr(HA_CHECK *param);
 
1109
void _ma_check_print_error _VARARGS((HA_CHECK *param, const char *fmt, ...))
 
1110
  ATTRIBUTE_FORMAT(printf, 2, 3);
 
1111
void _ma_check_print_warning _VARARGS((HA_CHECK *param, const char *fmt, ...))
 
1112
  ATTRIBUTE_FORMAT(printf, 2, 3);
 
1113
void _ma_check_print_info _VARARGS((HA_CHECK *param, const char *fmt, ...))
 
1114
  ATTRIBUTE_FORMAT(printf, 2, 3);
 
1115
my_bool write_log_record_for_repair(const HA_CHECK *param, MARIA_HA *info);
 
1116
C_MODE_END
 
1117
 
 
1118
int _ma_flush_pending_blocks(MARIA_SORT_PARAM *param);
 
1119
int _ma_sort_ft_buf_flush(MARIA_SORT_PARAM *sort_param);
 
1120
int _ma_thr_write_keys(MARIA_SORT_PARAM *sort_param);
 
1121
#ifdef THREAD
 
1122
pthread_handler_t _ma_thr_find_all_keys(void *arg);
 
1123
#endif
 
1124
 
 
1125
int _ma_sort_write_record(MARIA_SORT_PARAM *sort_param);
 
1126
int _ma_create_index_by_sort(MARIA_SORT_PARAM *info, my_bool no_messages,
 
1127
                             size_t);
 
1128
int _ma_sync_table_files(const MARIA_HA *info);
 
1129
int _ma_initialize_data_file(MARIA_SHARE *share, File dfile);
 
1130
int _ma_update_state_lsns(MARIA_SHARE *share,
 
1131
                          LSN lsn, TrID create_trid, my_bool do_sync,
 
1132
                          my_bool update_create_rename_lsn);
 
1133
int _ma_update_state_lsns_sub(MARIA_SHARE *share, LSN lsn,
 
1134
                              TrID create_trid, my_bool do_sync,
 
1135
                              my_bool update_create_rename_lsn);
 
1136
void _ma_set_data_pagecache_callbacks(PAGECACHE_FILE *file,
 
1137
                                      MARIA_SHARE *share);
 
1138
void _ma_set_index_pagecache_callbacks(PAGECACHE_FILE *file,
 
1139
                                       MARIA_SHARE *share);
 
1140
void _ma_tmp_disable_logging_for_table(MARIA_HA *info,
 
1141
                                       my_bool log_incomplete);
 
1142
my_bool _ma_reenable_logging_for_table(MARIA_HA *info, my_bool flush_pages);
 
1143
my_bool write_log_record_for_bulk_insert(MARIA_HA *info);
 
1144
 
 
1145
 
 
1146
#define MARIA_NO_CRC_NORMAL_PAGE 0xffffffff
 
1147
#define MARIA_NO_CRC_BITMAP_PAGE 0xfffffffe
 
1148
extern my_bool maria_page_crc_set_index(uchar *page,
 
1149
                                        pgcache_page_no_t page_no,
 
1150
                                        uchar *data_ptr);
 
1151
extern my_bool maria_page_crc_set_normal(uchar *page,
 
1152
                                         pgcache_page_no_t page_no,
 
1153
                                         uchar *data_ptr);
 
1154
extern my_bool maria_page_crc_check_bitmap(uchar *page,
 
1155
                                           pgcache_page_no_t page_no,
 
1156
                                           uchar *data_ptr);
 
1157
extern my_bool maria_page_crc_check_data(uchar *page,
 
1158
                                           pgcache_page_no_t page_no,
 
1159
                                           uchar *data_ptr);
 
1160
extern my_bool maria_page_crc_check_index(uchar *page,
 
1161
                                           pgcache_page_no_t page_no,
 
1162
                                           uchar *data_ptr);
 
1163
extern my_bool maria_page_crc_check_none(uchar *page,
 
1164
                                         pgcache_page_no_t page_no,
 
1165
                                         uchar *data_ptr);
 
1166
extern my_bool maria_page_filler_set_bitmap(uchar *page,
 
1167
                                            pgcache_page_no_t page_no,
 
1168
                                            uchar *data_ptr);
 
1169
extern my_bool maria_page_filler_set_normal(uchar *page,
 
1170
                                            pgcache_page_no_t page_no,
 
1171
                                            uchar *data_ptr);
 
1172
extern my_bool maria_page_filler_set_none(uchar *page,
 
1173
                                          pgcache_page_no_t page_no,
 
1174
                                          uchar *data_ptr);
 
1175
extern void maria_page_write_failure(uchar* data_ptr);
 
1176
extern my_bool maria_flush_log_for_page(uchar *page,
 
1177
                                        pgcache_page_no_t page_no,
 
1178
                                        uchar *data_ptr);
 
1179
extern my_bool maria_flush_log_for_page_none(uchar *page,
 
1180
                                             pgcache_page_no_t page_no,
 
1181
                                             uchar *data_ptr);
 
1182
void maria_concurrent_inserts(MARIA_HA *info, my_bool concurrent_insert);
 
1183
extern PAGECACHE *maria_log_pagecache;