22 #define MAX_RECORDS 100000
25 static int get_options(
int argc,
char *argv[]);
26 static int rnd(
int max_value);
27 static sig_handler endprog(
int sig_number);
29 static uint flag=0,verbose=0,testflag=0,recant=10000,silent=0;
30 static uint keys=MAX_KEYS;
31 static uint16 key1[1001];
32 static my_bool key3[MAX_RECORDS];
33 static int reclength=39;
36 static int calc_check(uchar *
buf,uint length);
37 static void make_record(uchar *
record, uint n1, uint n2, uint n3,
38 const char *mark, uint count);
42 int main(
int argc,
char *argv[])
46 uint write_count,update,opt_delete,check2,dupp_keys,found_key;
49 unsigned long key_check;
50 uchar
record[128],record2[128],record3[128],key[10];
51 const char *filename,*filename2;
56 HEAP_PTR UNINIT_VAR(position);
65 get_options(argc,argv);
67 memset(&hp_create_info, 0,
sizeof(hp_create_info));
68 hp_create_info.max_table_size= 1024L*1024L;
69 hp_create_info.keys= keys;
70 hp_create_info.keydef= keyinfo;
71 hp_create_info.reclength= reclength;
72 hp_create_info.max_records= (ulong) flag*100000L;
73 hp_create_info.min_records= (ulong) recant/2;
75 write_count=update=opt_delete=0;
78 keyinfo[0].seg=keyseg;
81 keyinfo[0].algorithm= HA_KEY_ALG_HASH;
82 keyinfo[0].seg[0].type=HA_KEYTYPE_BINARY;
83 keyinfo[0].seg[0].start=0;
84 keyinfo[0].seg[0].length=6;
85 keyinfo[0].seg[0].null_bit=0;
86 keyinfo[0].seg[0].charset=cs;
87 keyinfo[1].seg=keyseg+1;
90 keyinfo[1].algorithm= HA_KEY_ALG_HASH;
91 keyinfo[1].seg[0].type=HA_KEYTYPE_BINARY;
92 keyinfo[1].seg[0].start=7;
93 keyinfo[1].seg[0].length=6;
94 keyinfo[1].seg[0].null_bit=0;
95 keyinfo[1].seg[0].charset=cs;
96 keyinfo[1].seg[1].type=HA_KEYTYPE_TEXT;
97 keyinfo[1].seg[1].start=0;
98 keyinfo[1].seg[1].length=6;
99 keyinfo[1].seg[1].null_bit=0;
100 keyinfo[1].seg[1].charset=cs;
101 keyinfo[2].seg=keyseg+3;
102 keyinfo[2].keysegs=1;
103 keyinfo[2].flag=HA_NOSAME;
104 keyinfo[2].algorithm= HA_KEY_ALG_HASH;
105 keyinfo[2].seg[0].type=HA_KEYTYPE_BINARY;
106 keyinfo[2].seg[0].start=12;
107 keyinfo[2].seg[0].length=8;
108 keyinfo[2].seg[0].null_bit=0;
109 keyinfo[2].seg[0].charset=cs;
110 keyinfo[3].seg=keyseg+4;
111 keyinfo[3].keysegs=1;
112 keyinfo[3].flag=HA_NOSAME;
113 keyinfo[3].algorithm= HA_KEY_ALG_HASH;
114 keyinfo[3].seg[0].type=HA_KEYTYPE_BINARY;
115 keyinfo[3].seg[0].start=37;
116 keyinfo[3].seg[0].length=1;
117 keyinfo[3].seg[0].null_bit=1;
118 keyinfo[3].seg[0].null_pos=38;
119 keyinfo[3].seg[0].charset=cs;
121 memset(key1, 0,
sizeof(key1));
122 memset(key3, 0,
sizeof(key3));
124 printf(
"- Creating heap-file\n");
125 if (heap_create(filename, &hp_create_info, &tmp_share, &unused) ||
126 !(file= heap_open(filename, 2)))
128 signal(SIGINT,endprog);
130 printf(
"- Writing records:s\n");
131 strmov((
char*) record,
" ..... key");
133 for (i=0 ; i < recant ; i++)
137 n3= rnd(MY_MIN(recant * 5, MAX_RECORDS));
138 make_record(record,n1,n2,n3,
"Pos",write_count);
140 if (heap_write(file,record))
142 if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
144 printf(
"Error: %d in write at record: %d\n",my_errno,i);
147 if (verbose) printf(
" Double key: %d\n",n3);
153 printf(
"Error: Didn't get error when writing second key: '%8d'\n",n3);
156 write_count++; key1[n1]++; key3[n3]=1;
159 if (testflag == 1 && heap_check_heap(file,0))
161 puts(
"Heap keys crashed");
167 if (heap_check_heap(file,0))
169 puts(
"Heap keys crashed");
173 printf(
"- Delete\n");
174 for (i=0 ; i < write_count/10 ; i++)
176 for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ;
179 sprintf((
char*) key,
"%6d",j);
180 if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT))
182 printf(
"can't find key1: \"%s\"\n",(
char*) key);
185 if (heap_delete(file,record))
187 printf(
"error: %d; can't delete record: \"%s\"\n", my_errno,(
char*) record);
191 key1[atoi((
char*) record+keyinfo[0].seg[0].start)]--;
192 key3[atoi((
char*) record+keyinfo[2].seg[0].start)]=0;
193 key_check-=atoi((
char*) record);
194 if (testflag == 2 && heap_check_heap(file,0))
196 puts(
"Heap keys crashed");
201 puts(
"Warning: Skipping delete test because no dupplicate keys");
203 if (testflag==2)
goto end;
204 if (heap_check_heap(file,0))
206 puts(
"Heap keys crashed");
210 printf(
"- Update\n");
211 for (i=0 ; i < write_count/10 ; i++)
215 n3= rnd(MY_MIN(recant * 2, MAX_RECORDS));
216 make_record(record2, n1, n2, n3,
"XXX", update);
219 if (heap_scan_init(file))
221 j=rnd(write_count-opt_delete);
222 while ((error=heap_scan(file,record) == HA_ERR_RECORD_DELETED) ||
233 for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ;
236 sprintf((
char*) key,
"%6d",j);
237 if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT))
239 printf(
"can't find key1: \"%s\"\n",(
char*) key);
243 if (heap_update(file,record,record2))
245 if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
247 printf(
"error: %d; can't update:\nFrom: \"%s\"\nTo: \"%s\"\n",
248 my_errno,(
char*) record, (
char*) record2);
252 printf(
"Double key when tried to update:\nFrom: \"%s\"\nTo: \"%s\"\n",
253 (
char*) record, (
char*) record2);
257 key1[atoi((
char*) record+keyinfo[0].seg[0].start)]--;
258 key3[atoi((
char*) record+keyinfo[2].seg[0].start)]=0;
259 key1[n1]++; key3[n3]=1;
261 key_check=key_check-atoi((
char*) record)+n1;
263 if (testflag == 3 && heap_check_heap(file,0))
265 puts(
"Heap keys crashed");
269 if (testflag == 3)
goto end;
270 if (heap_check_heap(file,0))
272 puts(
"Heap keys crashed");
276 for (i=999, dupp_keys=found_key=0 ; i>0 ; i--)
278 if (key1[i] > dupp_keys) { dupp_keys=key1[
i]; found_key=
i; }
279 sprintf((
char*) key,
"%6d",found_key);
285 printf(
"- Read first key - next - delete - next -> last\n");
286 DBUG_PRINT(
"progpos",(
"first - next - delete - next -> last"));
288 if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT))
290 if (heap_rnext(file,record3))
goto err;
291 if (heap_delete(file,record3))
goto err;
292 key_check-=atoi((
char*) record3);
293 key1[atoi((
char*) record+keyinfo[0].seg[0].start)]--;
294 key3[atoi((
char*) record+keyinfo[2].seg[0].start)]=0;
297 while ((error=heap_rnext(file,record3)) == 0 ||
298 error == HA_ERR_RECORD_DELETED)
301 if (ant != dupp_keys)
303 printf(
"next: I can only find: %d records of %d\n",
308 if (heap_check_heap(file,0))
310 puts(
"Heap keys crashed");
315 printf(
"- Read last key - delete - prev - prev - opt_delete - prev -> first\n");
317 if (heap_rlast(file,record3,0))
goto err;
318 if (heap_delete(file,record3))
goto err;
319 key_check-=atoi((
char*) record3);
320 key1[atoi((
char*) record+keyinfo[0].seg[0].start)]--;
321 key3[atoi((
char*) record+keyinfo[2].seg[0].start)]=0;
323 if (heap_rprev(file,record3) || heap_rprev(file,record3))
325 if (heap_delete(file,record3))
goto err;
326 key_check-=atoi((
char*) record3);
327 key1[atoi((
char*) record+keyinfo[0].seg[0].start)]--;
328 key3[atoi((
char*) record+keyinfo[2].seg[0].start)]=0;
331 while ((error=heap_rprev(file,record3)) == 0 ||
332 error == HA_ERR_RECORD_DELETED)
337 if (ant != dupp_keys)
339 printf(
"next: I can only find: %d records of %d\n",
344 if (heap_check_heap(file,0))
346 puts(
"Heap keys crashed");
351 puts(
"Warning: Not enough duplicated keys: Skipping delete key check");
354 printf(
"- Read (first) - next - delete - next -> last\n");
355 DBUG_PRINT(
"progpos",(
"first - next - delete - next -> last"));
357 if (heap_scan_init(file))
359 while ((error=heap_scan(file,record3) == HA_ERR_RECORD_DELETED)) ;
362 if (heap_delete(file,record3))
goto err;
363 key_check-=atoi((
char*) record3);
365 key1[atoi((
char*) record+keyinfo[0].seg[0].start)]--;
366 key3[atoi((
char*) record+keyinfo[2].seg[0].start)]=0;
368 while ((error=heap_scan(file,record3)) == 0 ||
369 error == HA_ERR_RECORD_DELETED)
372 if (ant != write_count-opt_delete)
374 printf(
"next: Found: %d records of %d\n",ant,write_count-opt_delete);
377 if (heap_check_heap(file,0))
379 puts(
"Heap keys crashed");
383 puts(
"- Test if: Read rrnd - same - rkey - same");
384 DBUG_PRINT(
"progpos",(
"Read rrnd - same"));
385 pos=rnd(write_count-opt_delete-5)+5;
386 heap_scan_init(file);
388 while ((error=heap_scan(file,record)) == HA_ERR_RECORD_DELETED ||
393 if (!error && (i-- == 0))
395 bmove(record3,record,reclength);
396 position=heap_position(file);
401 bmove(record2,record,reclength);
402 if (heap_rsame(file,record,-1) || heap_rsame(file,record2,2))
404 if (memcmp(record2,record,reclength))
406 puts(
"heap_rsame didn't find right record");
410 puts(
"- Test of read through position");
411 if (heap_rrnd(file,record,position))
413 if (memcmp(record3,record,reclength))
415 puts(
"heap_frnd didn't find right record");
419 printf(
"- heap_info\n");
422 heap_info(file,&info,0);
425 if (info.records != write_count-opt_delete ||
426 (info.deleted != opt_delete && info.deleted != opt_delete+1))
428 puts(
"Wrong info from heap_info");
429 printf(
"Got: records: %ld(%d) deleted: %ld(%d)\n",
430 info.records,write_count-opt_delete,info.deleted,opt_delete);
434 #ifdef OLD_HEAP_VERSION
437 printf(
"- Read through all records with rnd\n");
438 if (heap_extra(file,HA_EXTRA_RESET) || heap_extra(file,HA_EXTRA_CACHE))
440 puts(
"got error from heap_extra");
444 while ((error=heap_rrnd(file,record,(ulong) -1)) != HA_ERR_END_OF_FILE &&
445 ant < write_count + 10)
450 check+=calc_check(record,reclength);
453 if (ant != write_count-opt_delete)
455 printf(
"rrnd: I can only find: %d records of %d\n", ant,
456 write_count-opt_delete);
459 if (heap_extra(file,HA_EXTRA_NO_CACHE))
461 puts(
"got error from heap_extra(HA_EXTRA_NO_CACHE)");
467 printf(
"- Read through all records with scan\n");
468 if (heap_reset(file) || heap_extra(file,HA_EXTRA_CACHE))
470 puts(
"got error from heap_extra");
474 heap_scan_init(file);
475 while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE &&
476 ant < write_count + 10)
481 check2+=calc_check(record,reclength);
484 if (ant != write_count-opt_delete)
486 printf(
"scan: I can only find: %d records of %d\n", ant,
487 write_count-opt_delete);
490 #ifdef OLD_HEAP_VERSION
493 puts(
"scan: Checksum didn't match reading with rrnd");
499 if (heap_extra(file,HA_EXTRA_NO_CACHE))
501 puts(
"got error from heap_extra(HA_EXTRA_NO_CACHE)");
505 for (i=999, dupp_keys=found_key=0 ; i>0 ; i--)
507 if (key1[i] > dupp_keys) { dupp_keys=key1[
i]; found_key=
i; }
508 sprintf((
char*) key,
"%6d",found_key);
510 printf(
"- Read through all keys with first-next-last-prev\n");
512 for (error=heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT);
514 error=heap_rnext(file,record))
516 if (ant != dupp_keys)
518 printf(
"first-next: I can only find: %d records of %d\n", ant,
524 for (error=heap_rlast(file,record,0) ;
526 error=heap_rprev(file,record))
529 check2+=calc_check(record,reclength);
531 if (ant != dupp_keys)
533 printf(
"last-prev: I can only find: %d records of %d\n", ant,
538 if (testflag == 4)
goto end;
540 printf(
"- Reading through all rows through keys\n");
541 if (!(file2=heap_open(filename, 2)))
543 if (heap_scan_init(file))
545 while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE)
549 if (heap_rkey(file2,record2,2,record+keyinfo[2].seg[0].start,8,
552 printf(
"can't find key3: \"%.8s\"\n",
553 record+keyinfo[2].seg[0].start);
560 printf(
"- Creating output heap-file 2\n");
561 hp_create_info.keys= 1;
562 hp_create_info.max_records= 0;
563 hp_create_info.min_records= 0;
564 if (heap_create(filename2, &hp_create_info, &tmp_share, &unused) ||
565 !(file2= heap_open_from_share_and_register(tmp_share, 2)))
568 printf(
"- Copying and removing records\n");
569 if (heap_scan_init(file))
571 while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE)
575 if (heap_write(file2,record))
577 key_check-=atoi((
char*) record);
579 if (heap_delete(file,record))
585 printf(
"- Checking heap tables\n");
586 if (heap_check_heap(file,1) || heap_check_heap(file2,1))
588 puts(
"Heap keys crashed");
592 if (my_errno != HA_ERR_END_OF_FILE)
593 printf(
"error: %d from heap_rrnd\n",my_errno);
595 printf(
"error: Some read got wrong: check is %ld\n",(
long) key_check);
598 printf(
"\nFollowing test have been made:\n");
599 printf(
"Write records: %d\nUpdate records: %d\nDelete records: %d\n", write_count,update,opt_delete);
602 if (heap_close(file) || (file2 && heap_close(file2)))
604 heap_delete_table(filename2);
605 hp_panic(HA_PANIC_CLOSE);
606 my_end(MY_GIVE_INFO);
609 printf(
"Got error: %d when using heap-database\n",my_errno);
610 (void) heap_close(file);
617 static int get_options(
int argc,
char *argv[])
623 while (--argc >0 && *(pos = *(++argv)) ==
'-' ) {
638 testflag=atoi(++pos);
643 printf(
"%s Ver 1.1 for %s at %s\n",progname,SYSTEM_TYPE,MACHINE_TYPE);
644 puts(
"TCX Datakonsult AB, by Monty, for your professional use\n");
645 printf(
"Usage: %s [-?ABIKLsWv] [-m#] [-t#]\n",progname);
657 static int rnd(
int max_value)
659 return (
int) ((rand() & 32767)/32767.0*max_value);
663 static sig_handler endprog(
int sig_number __attribute__((unused)))
666 hp_panic(HA_PANIC_CLOSE);
672 static int calc_check(uchar *
buf, uint length)
676 check+= (int) (uchar) *(buf++);
680 static void make_record(uchar *record, uint n1, uint n2, uint n3,
681 const char *mark, uint count)
683 memset(record,
' ', reclength);
684 sprintf((
char*) record,
"%6d:%4d:%8d:%3.3s: %4d",
685 n1,n2,n3,mark,count);