1
/* Copyright (C) 2000-2005 MySQL AB
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.
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.
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 */
16
/* Testing of the basic functions of a MyISAM table */
19
#include <my_getopt.h>
22
#define MAX_REC_LENGTH 1024
26
static int rec_pointer_size=0, flags[50];
27
static int key_field=FIELD_SKIP_PRESPACE,extra_field=FIELD_SKIP_ENDSPACE;
28
static int key_type=HA_KEYTYPE_NUM;
29
static int create_flag=0;
31
static uint insert_count, update_count, remove_count;
32
static uint pack_keys=0, pack_seg=0, key_length;
33
static uint unique_key=HA_NOSAME;
34
static my_bool key_cacheing, null_fields, silent, skip_update, opt_unique,
36
static MI_COLUMNDEF recinfo[4];
37
static MI_KEYDEF keyinfo[10];
38
static HA_KEYSEG keyseg[10];
39
static HA_KEYSEG uniqueseg[10];
41
static int run_test(const char *filename);
42
static void get_options(int argc, char *argv[]);
43
static void create_key(uchar *key,uint rownr);
44
static void create_record(uchar *record,uint rownr);
45
static void update_record(uchar *record);
47
int main(int argc,char *argv[])
52
init_key_cache(dflt_key_cache,KEY_CACHE_BLOCK_SIZE,IO_SIZE*16,0,0);
53
get_options(argc,argv);
55
exit(run_test("test1"));
59
static int run_test(const char *filename)
62
int i,j,error,deleted,rec_length,uniques=0;
63
ha_rows found,row_count;
65
uchar record[MAX_REC_LENGTH],key[MAX_REC_LENGTH],read_record[MAX_REC_LENGTH];
66
MI_UNIQUEDEF uniquedef;
67
MI_CREATE_INFO create_info;
69
bzero((char*) recinfo,sizeof(recinfo));
71
/* First define 2 columns */
72
recinfo[0].type=FIELD_NORMAL; recinfo[0].length=1; /* For NULL bits */
73
recinfo[1].type=key_field;
74
recinfo[1].length= (key_field == FIELD_BLOB ? 4+portable_sizeof_char_ptr :
76
if (key_field == FIELD_VARCHAR)
77
recinfo[1].length+= HA_VARCHAR_PACKLENGTH(key_length);;
78
recinfo[2].type=extra_field;
79
recinfo[2].length= (extra_field == FIELD_BLOB ? 4 + portable_sizeof_char_ptr : 24);
80
if (extra_field == FIELD_VARCHAR)
81
recinfo[2].length+= HA_VARCHAR_PACKLENGTH(recinfo[2].length);
84
recinfo[3].type=FIELD_CHECK;
85
recinfo[3].length=MI_UNIQUE_HASH_LENGTH;
87
rec_length=recinfo[0].length+recinfo[1].length+recinfo[2].length+
90
if (key_type == HA_KEYTYPE_VARTEXT1 &&
92
key_type= HA_KEYTYPE_VARTEXT2;
94
/* Define a key over the first column */
95
keyinfo[0].seg=keyseg;
97
keyinfo[0].block_length= 0; /* Default block length */
98
keyinfo[0].key_alg=HA_KEY_ALG_BTREE;
99
keyinfo[0].seg[0].type= key_type;
100
keyinfo[0].seg[0].flag= pack_seg;
101
keyinfo[0].seg[0].start=1;
102
keyinfo[0].seg[0].length=key_length;
103
keyinfo[0].seg[0].null_bit= null_fields ? 2 : 0;
104
keyinfo[0].seg[0].null_pos=0;
105
keyinfo[0].seg[0].language= default_charset_info->number;
106
if (pack_seg & HA_BLOB_PART)
108
keyinfo[0].seg[0].bit_start=4; /* Length of blob length */
110
keyinfo[0].flag = (uint8) (pack_keys | unique_key);
112
bzero((uchar*) flags,sizeof(flags));
117
bzero((char*) &uniquedef,sizeof(uniquedef));
118
bzero((char*) uniqueseg,sizeof(uniqueseg));
119
uniquedef.seg=uniqueseg;
122
/* Make a unique over all columns (except first NULL fields) */
123
for (i=0, start=1 ; i < 2 ; i++)
125
uniqueseg[i].start=start;
126
start+=recinfo[i+1].length;
127
uniqueseg[i].length=recinfo[i+1].length;
128
uniqueseg[i].language= default_charset_info->number;
130
uniqueseg[0].type= key_type;
131
uniqueseg[0].null_bit= null_fields ? 2 : 0;
132
uniqueseg[1].type= HA_KEYTYPE_TEXT;
133
if (extra_field == FIELD_BLOB)
135
uniqueseg[1].length=0; /* The whole blob */
136
uniqueseg[1].bit_start=4; /* long blob */
137
uniqueseg[1].flag|= HA_BLOB_PART;
139
else if (extra_field == FIELD_VARCHAR)
140
uniqueseg[1].flag|= HA_VAR_LENGTH_PART;
146
printf("- Creating isam-file\n");
147
bzero((char*) &create_info,sizeof(create_info));
148
create_info.max_rows=(ulong) (rec_pointer_size ?
149
(1L << (rec_pointer_size*8))/40 :
151
if (mi_create(filename,1,keyinfo,3+opt_unique,recinfo,
152
uniques, &uniquedef, &create_info,
155
if (!(file=mi_open(filename,2,HA_OPEN_ABORT_IF_LOCKED)))
158
printf("- Writing key:s\n");
162
for (i=49 ; i>=1 ; i-=2 )
164
if (insert_count-- == 0) { VOID(mi_close(file)) ; exit(0) ; }
166
create_record(record,j);
167
error=mi_write(file,record);
171
if (verbose || error)
172
printf("J= %2d mi_write: %d errno: %d\n", j,error,my_errno);
175
/* Insert 2 rows with null values */
178
create_record(record,0);
179
error=mi_write(file,record);
182
if (verbose || error)
183
printf("J= NULL mi_write: %d errno: %d\n", error,my_errno);
184
error=mi_write(file,record);
187
if (verbose || error)
188
printf("J= NULL mi_write: %d errno: %d\n", error,my_errno);
197
printf("- Checking unique constraint\n");
198
create_record(record,j);
199
if (!mi_write(file,record) || my_errno != HA_ERR_FOUND_DUPP_UNIQUE)
201
printf("unique check failed\n");
205
printf("- Updating rows\n");
207
/* Update first last row to force extend of file */
208
if (mi_rsame(file,read_record,-1))
210
printf("Can't find last row with mi_rsame\n");
214
memcpy(record,read_record,rec_length);
215
update_record(record);
216
if (mi_update(file,read_record,record))
218
printf("Can't update last row: %.*s\n",
219
keyinfo[0].seg[0].length,read_record+1);
223
/* Read through all rows and update them */
226
while ((error=mi_rrnd(file,read_record,pos)) == 0)
228
if (update_count-- == 0) { VOID(mi_close(file)) ; exit(0) ; }
229
memcpy(record,read_record,rec_length);
230
update_record(record);
231
if (mi_update(file,read_record,record))
233
printf("Can't update row: %.*s, error: %d\n",
234
keyinfo[0].seg[0].length,record+1,my_errno);
239
if (found != row_count)
240
printf("Found %ld of %ld rows\n", (ulong) found, (ulong) row_count);
244
printf("- Reopening file\n");
245
if (mi_close(file)) goto err;
246
if (!(file=mi_open(filename,2,HA_OPEN_ABORT_IF_LOCKED))) goto err;
250
printf("- Removing keys\n");
252
for (i=0 ; i <= 10 ; i++)
255
if (remove_count-- == 0) { VOID(mi_close(file)) ; exit(0) ; }
261
if ((error = mi_rkey(file,read_record,0,key,HA_WHOLE_KEY,
264
if (verbose || (flags[j] >= 1 ||
265
(error && my_errno != HA_ERR_KEY_NOT_FOUND)))
266
printf("key: '%.*s' mi_rkey: %3d errno: %3d\n",
267
(int) key_length,key+test(null_fields),error,my_errno);
271
error=mi_delete(file,read_record);
272
if (verbose || error)
273
printf("key: '%.*s' mi_delete: %3d errno: %3d\n",
274
(int) key_length, key+test(null_fields), error, my_errno);
284
printf("- Reading rows with key\n");
285
for (i=0 ; i <= 25 ; i++)
289
error=mi_rkey(file,read_record,0,key,HA_WHOLE_KEY,HA_READ_KEY_EXACT);
291
(error == 0 && flags[i] == 0 && unique_key) ||
292
(error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND)))
294
printf("key: '%.*s' mi_rkey: %3d errno: %3d record: %s\n",
295
(int) key_length,key+test(null_fields),error,my_errno,record+1);
300
printf("- Reading rows with position\n");
301
for (i=1,found=0 ; i <= 30 ; i++)
304
if ((error=mi_rrnd(file,read_record,i == 1 ? 0L : HA_OFFSET_ERROR)) == -1)
306
if (found != row_count-deleted)
307
printf("Found only %ld of %ld rows\n", (ulong) found,
308
(ulong) (row_count - deleted));
313
if (verbose || (error != 0 && error != HA_ERR_RECORD_DELETED &&
314
error != HA_ERR_END_OF_FILE))
316
printf("pos: %2d mi_rrnd: %3d errno: %3d record: %s\n",
317
i-1,error,my_errno,read_record+1);
320
if (mi_close(file)) goto err;
321
my_end(MY_CHECK_ERROR);
325
printf("got error: %3d when using myisam-database\n",my_errno);
326
return 1; /* skip warning */
330
static void create_key_part(uchar *key,uint rownr)
333
rownr&=7; /* Some identical keys */
334
if (keyinfo[0].seg[0].type == HA_KEYTYPE_NUM)
336
sprintf((char*) key,"%*d",keyinfo[0].seg[0].length,rownr);
338
else if (keyinfo[0].seg[0].type == HA_KEYTYPE_VARTEXT1 ||
339
keyinfo[0].seg[0].type == HA_KEYTYPE_VARTEXT2)
341
/* Create a key that may be easily packed */
342
bfill(key,keyinfo[0].seg[0].length,rownr < 10 ? 'A' : 'B');
343
sprintf((char*) key+keyinfo[0].seg[0].length-2,"%-2d",rownr);
344
if ((rownr & 7) == 0)
346
/* Change the key to force a unpack of the next key */
347
bfill(key+3,keyinfo[0].seg[0].length-4,rownr < 10 ? 'a' : 'b');
352
if (keyinfo[0].seg[0].flag & HA_SPACE_PACK)
353
sprintf((char*) key,"%-*d",keyinfo[0].seg[0].length,rownr);
356
/* Create a key that may be easily packed */
357
bfill(key,keyinfo[0].seg[0].length,rownr < 10 ? 'A' : 'B');
358
sprintf((char*) key+keyinfo[0].seg[0].length-2,"%-2d",rownr);
359
if ((rownr & 7) == 0)
361
/* Change the key to force a unpack of the next key */
362
key[1]= (rownr < 10 ? 'a' : 'b');
369
static void create_key(uchar *key,uint rownr)
371
if (keyinfo[0].seg[0].null_bit)
375
key[0]=1; /* null key */
376
key[1]=0; /* Fore easy print of key */
381
if (keyinfo[0].seg[0].flag & (HA_BLOB_PART | HA_VAR_LENGTH_PART))
384
create_key_part(key+2,rownr);
385
tmp=strlen((char*) key+2);
389
create_key_part(key,rownr);
393
static uchar blob_key[MAX_REC_LENGTH];
394
static uchar blob_record[MAX_REC_LENGTH+20*20];
397
static void create_record(uchar *record,uint rownr)
400
bzero((char*) record,MAX_REC_LENGTH);
401
record[0]=1; /* delete marker */
402
if (rownr == 0 && keyinfo[0].seg[0].null_bit)
403
record[0]|=keyinfo[0].seg[0].null_bit; /* Null key */
406
if (recinfo[1].type == FIELD_BLOB)
410
create_key_part(blob_key,rownr);
411
tmp=strlen((char*) blob_key);
414
memcpy_fixed(pos+4,&ptr,sizeof(char*));
415
pos+=recinfo[1].length;
417
else if (recinfo[1].type == FIELD_VARCHAR)
419
uint tmp, pack_length= HA_VARCHAR_PACKLENGTH(recinfo[1].length-1);
420
create_key_part(pos+pack_length,rownr);
421
tmp= strlen((char*) pos+pack_length);
422
if (pack_length == 1)
423
*(uchar*) pos= (uchar) tmp;
426
pos+= recinfo[1].length;
430
create_key_part(pos,rownr);
431
pos+=recinfo[1].length;
433
if (recinfo[2].type == FIELD_BLOB)
437
sprintf((char*) blob_record,"... row: %d", rownr);
438
strappend((char*) blob_record,max(MAX_REC_LENGTH-rownr,10),' ');
439
tmp=strlen((char*) blob_record);
442
memcpy_fixed(pos+4,&ptr,sizeof(char*));
444
else if (recinfo[2].type == FIELD_VARCHAR)
446
uint tmp, pack_length= HA_VARCHAR_PACKLENGTH(recinfo[1].length-1);
447
sprintf((char*) pos+pack_length, "... row: %d", rownr);
448
tmp= strlen((char*) pos+pack_length);
449
if (pack_length == 1)
456
sprintf((char*) pos,"... row: %d", rownr);
457
strappend((char*) pos,recinfo[2].length,' ');
461
/* change row to test re-packing of rows and reallocation of keys */
463
static void update_record(uchar *record)
466
if (recinfo[1].type == FIELD_BLOB)
470
length=uint4korr(pos); /* Long blob */
471
memcpy_fixed(&column,pos+4,sizeof(char*));
472
memcpy(blob_key,column,length); /* Move old key */
474
memcpy_fixed(pos+4,&ptr,sizeof(char*)); /* Store pointer to new key */
475
if (keyinfo[0].seg[0].type != HA_KEYTYPE_NUM)
476
default_charset_info->cset->casedn(default_charset_info,
477
(char*) blob_key, length,
478
(char*) blob_key, length);
479
pos+=recinfo[1].length;
481
else if (recinfo[1].type == FIELD_VARCHAR)
483
uint pack_length= HA_VARCHAR_PACKLENGTH(recinfo[1].length-1);
484
uint length= pack_length == 1 ? (uint) *(uchar*) pos : uint2korr(pos);
485
default_charset_info->cset->casedn(default_charset_info,
486
(char*) pos + pack_length, length,
487
(char*) pos + pack_length, length);
488
pos+=recinfo[1].length;
492
if (keyinfo[0].seg[0].type != HA_KEYTYPE_NUM)
493
default_charset_info->cset->casedn(default_charset_info,
494
(char*) pos, keyinfo[0].seg[0].length,
495
(char*) pos, keyinfo[0].seg[0].length);
496
pos+=recinfo[1].length;
499
if (recinfo[2].type == FIELD_BLOB)
503
length=uint4korr(pos);
504
memcpy_fixed(&column,pos+4,sizeof(char*));
505
memcpy(blob_record,column,length);
506
bfill(blob_record+length,20,'.'); /* Make it larger */
508
int4store(pos,length);
510
memcpy_fixed(pos+4,&column,sizeof(char*));
512
else if (recinfo[2].type == FIELD_VARCHAR)
514
/* Second field is longer than 10 characters */
515
uint pack_length= HA_VARCHAR_PACKLENGTH(recinfo[1].length-1);
516
uint length= pack_length == 1 ? (uint) *(uchar*) pos : uint2korr(pos);
517
bfill(pos+pack_length+length,recinfo[2].length-length-pack_length,'.');
518
length=recinfo[2].length-pack_length;
519
if (pack_length == 1)
520
*(uchar*) pos= (uchar) length;
522
int2store(pos,length);
526
bfill(pos+recinfo[2].length-10,10,'.');
531
static struct my_option my_long_options[] =
533
{"checksum", 'c', "Undocumented",
534
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
536
{"debug", '#', "Undocumented",
537
0, 0, 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
539
{"delete_rows", 'd', "Undocumented", (uchar**) &remove_count,
540
(uchar**) &remove_count, 0, GET_UINT, REQUIRED_ARG, 1000, 0, 0, 0, 0, 0},
541
{"help", '?', "Display help and exit",
542
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
543
{"insert_rows", 'i', "Undocumented", (uchar**) &insert_count,
544
(uchar**) &insert_count, 0, GET_UINT, REQUIRED_ARG, 1000, 0, 0, 0, 0, 0},
545
{"key_alpha", 'a', "Use a key of type HA_KEYTYPE_TEXT",
546
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
547
{"key_binary_pack", 'B', "Undocumented",
548
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
549
{"key_blob", 'b', "Undocumented",
550
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
551
{"key_cache", 'K', "Undocumented", (uchar**) &key_cacheing,
552
(uchar**) &key_cacheing, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
553
{"key_length", 'k', "Undocumented", (uchar**) &key_length, (uchar**) &key_length,
554
0, GET_UINT, REQUIRED_ARG, 6, 0, 0, 0, 0, 0},
555
{"key_multiple", 'm', "Undocumented",
556
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
557
{"key_prefix_pack", 'P', "Undocumented",
558
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
559
{"key_space_pack", 'p', "Undocumented",
560
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
561
{"key_varchar", 'w', "Test VARCHAR keys",
562
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
563
{"null_fields", 'N', "Define fields with NULL",
564
(uchar**) &null_fields, (uchar**) &null_fields, 0, GET_BOOL, NO_ARG,
566
{"row_fixed_size", 'S', "Undocumented",
567
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
568
{"row_pointer_size", 'R', "Undocumented", (uchar**) &rec_pointer_size,
569
(uchar**) &rec_pointer_size, 0, GET_INT, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
570
{"silent", 's', "Undocumented",
571
(uchar**) &silent, (uchar**) &silent, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
572
{"skip_update", 'U', "Undocumented", (uchar**) &skip_update,
573
(uchar**) &skip_update, 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
574
{"unique", 'C', "Undocumented", (uchar**) &opt_unique, (uchar**) &opt_unique, 0,
575
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
576
{"update_rows", 'u', "Undocumented", (uchar**) &update_count,
577
(uchar**) &update_count, 0, GET_UINT, REQUIRED_ARG, 1000, 0, 0, 0, 0, 0},
578
{"verbose", 'v', "Be more verbose", (uchar**) &verbose, (uchar**) &verbose, 0,
579
GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
580
{"version", 'V', "Print version number and exit",
581
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
582
{ 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
587
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
588
char *argument __attribute__((unused)))
592
key_type= HA_KEYTYPE_TEXT;
595
create_flag|= HA_CREATE_CHECKSUM;
597
case 'R': /* Length of record pointer */
598
if (rec_pointer_size > 3)
602
pack_keys= HA_PACK_KEY; /* Use prefix compression */
605
pack_keys= HA_BINARY_PACK_KEY; /* Use binary compression */
608
if (key_field == FIELD_VARCHAR)
610
create_flag=0; /* Static sized varchar */
612
else if (key_field != FIELD_BLOB)
614
key_field=FIELD_NORMAL; /* static-size record */
615
extra_field=FIELD_NORMAL;
619
pack_keys=HA_PACK_KEY; /* Use prefix + space packing */
620
pack_seg=HA_SPACE_PACK;
621
key_type=HA_KEYTYPE_TEXT;
627
key_field=FIELD_BLOB; /* blob key */
628
extra_field= FIELD_BLOB;
629
pack_seg|= HA_BLOB_PART;
630
key_type= HA_KEYTYPE_VARTEXT1;
633
if (key_length < 4 || key_length > MI_MAX_KEY_LENGTH)
635
fprintf(stderr,"Wrong key length\n");
640
key_field=FIELD_VARCHAR; /* varchar keys */
641
extra_field= FIELD_VARCHAR;
642
key_type= HA_KEYTYPE_VARTEXT1;
643
pack_seg|= HA_VAR_LENGTH_PART;
644
create_flag|= HA_PACK_RECORD;
646
case 'K': /* Use key cacheing */
650
printf("test1 Ver 1.2 \n");
653
DBUG_PUSH (argument);
665
static void get_options(int argc, char *argv[])
669
if ((ho_error=handle_options(&argc, &argv, my_long_options, get_one_option)))
678
printf("Usage: %s [options]\n\n", my_progname);
679
my_print_help(my_long_options);
680
my_print_variables(my_long_options);
683
#include "mi_extrafunc.h"