MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hp_test2.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2  reserved
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 /* Test av isam-databas: stor test */
18 
19 #include "heapdef.h" /* Because of hp_find_block */
20 #include <signal.h>
21 
22 #define MAX_RECORDS 100000
23 #define MAX_KEYS 4
24 
25 static int get_options(int argc, char *argv[]);
26 static int rnd(int max_value);
27 static sig_handler endprog(int sig_number);
28 
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;
34 
35 
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);
39 
40 /* Main program */
41 
42 int main(int argc, char *argv[])
43 {
44  register uint i,j;
45  uint ant,n1,n2,n3;
46  uint write_count,update,opt_delete,check2,dupp_keys,found_key;
47  int error;
48  ulong pos;
49  unsigned long key_check;
50  uchar record[128],record2[128],record3[128],key[10];
51  const char *filename,*filename2;
52  HP_INFO *file,*file2;
53  HP_SHARE *tmp_share;
54  HP_KEYDEF keyinfo[MAX_KEYS];
55  HA_KEYSEG keyseg[MAX_KEYS*5];
56  HEAP_PTR UNINIT_VAR(position);
57  HP_CREATE_INFO hp_create_info;
58  CHARSET_INFO *cs= &my_charset_latin1;
59  my_bool unused;
60  MY_INIT(argv[0]); /* init my_sys library & pthreads */
61 
62  filename= "test2";
63  filename2= "test2_2";
64  file=file2=0;
65  get_options(argc,argv);
66 
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;
74 
75  write_count=update=opt_delete=0;
76  key_check=0;
77 
78  keyinfo[0].seg=keyseg;
79  keyinfo[0].keysegs=1;
80  keyinfo[0].flag= 0;
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;
88  keyinfo[1].keysegs=2;
89  keyinfo[1].flag=0;
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; /* key in two parts */
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;
120 
121  memset(key1, 0, sizeof(key1));
122  memset(key3, 0, sizeof(key3));
123 
124  printf("- Creating heap-file\n");
125  if (heap_create(filename, &hp_create_info, &tmp_share, &unused) ||
126  !(file= heap_open(filename, 2)))
127  goto err;
128  signal(SIGINT,endprog);
129 
130  printf("- Writing records:s\n");
131  strmov((char*) record," ..... key");
132 
133  for (i=0 ; i < recant ; i++)
134  {
135  n1= rnd(1000);
136  n2= rnd(100);
137  n3= rnd(MY_MIN(recant * 5, MAX_RECORDS));
138  make_record(record,n1,n2,n3,"Pos",write_count);
139 
140  if (heap_write(file,record))
141  {
142  if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
143  {
144  printf("Error: %d in write at record: %d\n",my_errno,i);
145  goto err;
146  }
147  if (verbose) printf(" Double key: %d\n",n3);
148  }
149  else
150  {
151  if (key3[n3] == 1)
152  {
153  printf("Error: Didn't get error when writing second key: '%8d'\n",n3);
154  goto err;
155  }
156  write_count++; key1[n1]++; key3[n3]=1;
157  key_check+=n1;
158  }
159  if (testflag == 1 && heap_check_heap(file,0))
160  {
161  puts("Heap keys crashed");
162  goto err;
163  }
164  }
165  if (testflag == 1)
166  goto end;
167  if (heap_check_heap(file,0))
168  {
169  puts("Heap keys crashed");
170  goto err;
171  }
172 
173  printf("- Delete\n");
174  for (i=0 ; i < write_count/10 ; i++)
175  {
176  for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ;
177  if (j != 0)
178  {
179  sprintf((char*) key,"%6d",j);
180  if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT))
181  {
182  printf("can't find key1: \"%s\"\n",(char*) key);
183  goto err;
184  }
185  if (heap_delete(file,record))
186  {
187  printf("error: %d; can't delete record: \"%s\"\n", my_errno,(char*) record);
188  goto err;
189  }
190  opt_delete++;
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))
195  {
196  puts("Heap keys crashed");
197  goto err;
198  }
199  }
200  else
201  puts("Warning: Skipping delete test because no dupplicate keys");
202  }
203  if (testflag==2) goto end;
204  if (heap_check_heap(file,0))
205  {
206  puts("Heap keys crashed");
207  goto err;
208  }
209 
210  printf("- Update\n");
211  for (i=0 ; i < write_count/10 ; i++)
212  {
213  n1= rnd(1000);
214  n2= rnd(100);
215  n3= rnd(MY_MIN(recant * 2, MAX_RECORDS));
216  make_record(record2, n1, n2, n3, "XXX", update);
217  if (rnd(2) == 1)
218  {
219  if (heap_scan_init(file))
220  goto err;
221  j=rnd(write_count-opt_delete);
222  while ((error=heap_scan(file,record) == HA_ERR_RECORD_DELETED) ||
223  (!error && j))
224  {
225  if (!error)
226  j--;
227  }
228  if (error)
229  goto err;
230  }
231  else
232  {
233  for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ;
234  if (!key1[j])
235  continue;
236  sprintf((char*) key,"%6d",j);
237  if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT))
238  {
239  printf("can't find key1: \"%s\"\n",(char*) key);
240  goto err;
241  }
242  }
243  if (heap_update(file,record,record2))
244  {
245  if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0)
246  {
247  printf("error: %d; can't update:\nFrom: \"%s\"\nTo: \"%s\"\n",
248  my_errno,(char*) record, (char*) record2);
249  goto err;
250  }
251  if (verbose)
252  printf("Double key when tried to update:\nFrom: \"%s\"\nTo: \"%s\"\n",
253  (char*) record, (char*) record2);
254  }
255  else
256  {
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;
260  update++;
261  key_check=key_check-atoi((char*) record)+n1;
262  }
263  if (testflag == 3 && heap_check_heap(file,0))
264  {
265  puts("Heap keys crashed");
266  goto err;
267  }
268  }
269  if (testflag == 3) goto end;
270  if (heap_check_heap(file,0))
271  {
272  puts("Heap keys crashed");
273  goto err;
274  }
275 
276  for (i=999, dupp_keys=found_key=0 ; i>0 ; i--)
277  {
278  if (key1[i] > dupp_keys) { dupp_keys=key1[i]; found_key=i; }
279  sprintf((char*) key,"%6d",found_key);
280  }
281 
282  if (dupp_keys > 3)
283  {
284  if (!silent)
285  printf("- Read first key - next - delete - next -> last\n");
286  DBUG_PRINT("progpos",("first - next - delete - next -> last"));
287 
288  if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT))
289  goto err;
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;
295  opt_delete++;
296  ant=2;
297  while ((error=heap_rnext(file,record3)) == 0 ||
298  error == HA_ERR_RECORD_DELETED)
299  if (! error)
300  ant++;
301  if (ant != dupp_keys)
302  {
303  printf("next: I can only find: %d records of %d\n",
304  ant,dupp_keys);
305  goto end;
306  }
307  dupp_keys--;
308  if (heap_check_heap(file,0))
309  {
310  puts("Heap keys crashed");
311  goto err;
312  }
313 
314  if (!silent)
315  printf("- Read last key - delete - prev - prev - opt_delete - prev -> first\n");
316 
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;
322  opt_delete++;
323  if (heap_rprev(file,record3) || heap_rprev(file,record3))
324  goto err;
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;
329  opt_delete++;
330  ant=3;
331  while ((error=heap_rprev(file,record3)) == 0 ||
332  error == HA_ERR_RECORD_DELETED)
333  {
334  if (! error)
335  ant++;
336  }
337  if (ant != dupp_keys)
338  {
339  printf("next: I can only find: %d records of %d\n",
340  ant,dupp_keys);
341  goto end;
342  }
343  dupp_keys-=2;
344  if (heap_check_heap(file,0))
345  {
346  puts("Heap keys crashed");
347  goto err;
348  }
349  }
350  else
351  puts("Warning: Not enough duplicated keys: Skipping delete key check");
352 
353  if (!silent)
354  printf("- Read (first) - next - delete - next -> last\n");
355  DBUG_PRINT("progpos",("first - next - delete - next -> last"));
356 
357  if (heap_scan_init(file))
358  goto err;
359  while ((error=heap_scan(file,record3) == HA_ERR_RECORD_DELETED)) ;
360  if (error)
361  goto err;
362  if (heap_delete(file,record3)) goto err;
363  key_check-=atoi((char*) record3);
364  opt_delete++;
365  key1[atoi((char*) record+keyinfo[0].seg[0].start)]--;
366  key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0;
367  ant=0;
368  while ((error=heap_scan(file,record3)) == 0 ||
369  error == HA_ERR_RECORD_DELETED)
370  if (! error)
371  ant++;
372  if (ant != write_count-opt_delete)
373  {
374  printf("next: Found: %d records of %d\n",ant,write_count-opt_delete);
375  goto end;
376  }
377  if (heap_check_heap(file,0))
378  {
379  puts("Heap keys crashed");
380  goto err;
381  }
382 
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);
387  i=5;
388  while ((error=heap_scan(file,record)) == HA_ERR_RECORD_DELETED ||
389  (error == 0 && pos))
390  {
391  if (!error)
392  pos--;
393  if (!error && (i-- == 0))
394  {
395  bmove(record3,record,reclength);
396  position=heap_position(file);
397  }
398  }
399  if (error)
400  goto err;
401  bmove(record2,record,reclength);
402  if (heap_rsame(file,record,-1) || heap_rsame(file,record2,2))
403  goto err;
404  if (memcmp(record2,record,reclength))
405  {
406  puts("heap_rsame didn't find right record");
407  goto end;
408  }
409 
410  puts("- Test of read through position");
411  if (heap_rrnd(file,record,position))
412  goto err;
413  if (memcmp(record3,record,reclength))
414  {
415  puts("heap_frnd didn't find right record");
416  goto end;
417  }
418 
419  printf("- heap_info\n");
420  {
421  HEAPINFO info;
422  heap_info(file,&info,0);
423  /* We have to test with opt_delete +1 as this may be the case if the last
424  inserted row was a duplicate key */
425  if (info.records != write_count-opt_delete ||
426  (info.deleted != opt_delete && info.deleted != opt_delete+1))
427  {
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);
431  }
432  }
433 
434 #ifdef OLD_HEAP_VERSION
435  {
436  uint check;
437  printf("- Read through all records with rnd\n");
438  if (heap_extra(file,HA_EXTRA_RESET) || heap_extra(file,HA_EXTRA_CACHE))
439  {
440  puts("got error from heap_extra");
441  goto end;
442  }
443  ant=check=0;
444  while ((error=heap_rrnd(file,record,(ulong) -1)) != HA_ERR_END_OF_FILE &&
445  ant < write_count + 10)
446  {
447  if (!error)
448  {
449  ant++;
450  check+=calc_check(record,reclength);
451  }
452  }
453  if (ant != write_count-opt_delete)
454  {
455  printf("rrnd: I can only find: %d records of %d\n", ant,
456  write_count-opt_delete);
457  goto end;
458  }
459  if (heap_extra(file,HA_EXTRA_NO_CACHE))
460  {
461  puts("got error from heap_extra(HA_EXTRA_NO_CACHE)");
462  goto end;
463  }
464  }
465 #endif
466 
467  printf("- Read through all records with scan\n");
468  if (heap_reset(file) || heap_extra(file,HA_EXTRA_CACHE))
469  {
470  puts("got error from heap_extra");
471  goto end;
472  }
473  ant=check2=0;
474  heap_scan_init(file);
475  while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE &&
476  ant < write_count + 10)
477  {
478  if (!error)
479  {
480  ant++;
481  check2+=calc_check(record,reclength);
482  }
483  }
484  if (ant != write_count-opt_delete)
485  {
486  printf("scan: I can only find: %d records of %d\n", ant,
487  write_count-opt_delete);
488  goto end;
489  }
490 #ifdef OLD_HEAP_VERSION
491  if (check != check2)
492  {
493  puts("scan: Checksum didn't match reading with rrnd");
494  goto end;
495  }
496 #endif
497 
498 
499  if (heap_extra(file,HA_EXTRA_NO_CACHE))
500  {
501  puts("got error from heap_extra(HA_EXTRA_NO_CACHE)");
502  goto end;
503  }
504 
505  for (i=999, dupp_keys=found_key=0 ; i>0 ; i--)
506  {
507  if (key1[i] > dupp_keys) { dupp_keys=key1[i]; found_key=i; }
508  sprintf((char*) key,"%6d",found_key);
509  }
510  printf("- Read through all keys with first-next-last-prev\n");
511  ant=0;
512  for (error=heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT);
513  ! error ;
514  error=heap_rnext(file,record))
515  ant++;
516  if (ant != dupp_keys)
517  {
518  printf("first-next: I can only find: %d records of %d\n", ant,
519  dupp_keys);
520  goto end;
521  }
522 
523  ant=0;
524  for (error=heap_rlast(file,record,0) ;
525  ! error ;
526  error=heap_rprev(file,record))
527  {
528  ant++;
529  check2+=calc_check(record,reclength);
530  }
531  if (ant != dupp_keys)
532  {
533  printf("last-prev: I can only find: %d records of %d\n", ant,
534  dupp_keys);
535  goto end;
536  }
537 
538  if (testflag == 4) goto end;
539 
540  printf("- Reading through all rows through keys\n");
541  if (!(file2=heap_open(filename, 2)))
542  goto err;
543  if (heap_scan_init(file))
544  goto err;
545  while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE)
546  {
547  if (error == 0)
548  {
549  if (heap_rkey(file2,record2,2,record+keyinfo[2].seg[0].start,8,
550  HA_READ_KEY_EXACT))
551  {
552  printf("can't find key3: \"%.8s\"\n",
553  record+keyinfo[2].seg[0].start);
554  goto err;
555  }
556  }
557  }
558  heap_close(file2);
559 
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)))
566  goto err;
567 
568  printf("- Copying and removing records\n");
569  if (heap_scan_init(file))
570  goto err;
571  while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE)
572  {
573  if (error == 0)
574  {
575  if (heap_write(file2,record))
576  goto err;
577  key_check-=atoi((char*) record);
578  write_count++;
579  if (heap_delete(file,record))
580  goto err;
581  opt_delete++;
582  }
583  pos++;
584  }
585  printf("- Checking heap tables\n");
586  if (heap_check_heap(file,1) || heap_check_heap(file2,1))
587  {
588  puts("Heap keys crashed");
589  goto err;
590  }
591 
592  if (my_errno != HA_ERR_END_OF_FILE)
593  printf("error: %d from heap_rrnd\n",my_errno);
594  if (key_check)
595  printf("error: Some read got wrong: check is %ld\n",(long) key_check);
596 
597 end:
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);
600  heap_clear(file);
601  heap_clear(file2);
602  if (heap_close(file) || (file2 && heap_close(file2)))
603  goto err;
604  heap_delete_table(filename2);
605  hp_panic(HA_PANIC_CLOSE);
606  my_end(MY_GIVE_INFO);
607  return(0);
608 err:
609  printf("Got error: %d when using heap-database\n",my_errno);
610  (void) heap_close(file);
611  return(1);
612 } /* main */
613 
614 
615  /* Read options */
616 
617 static int get_options(int argc,char *argv[])
618 {
619  char *pos,*progname;
620 
621  progname= argv[0];
622 
623  while (--argc >0 && *(pos = *(++argv)) == '-' ) {
624  switch(*++pos) {
625  case 'B': /* Big file */
626  flag=1;
627  break;
628  case 'v': /* verbose */
629  verbose=1;
630  break;
631  case 'm': /* records */
632  recant=atoi(++pos);
633  break;
634  case 's':
635  silent=1;
636  break;
637  case 't':
638  testflag=atoi(++pos); /* testmod */
639  break;
640  case 'V':
641  case 'I':
642  case '?':
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);
646  exit(0);
647  case '#':
648  DBUG_PUSH (++pos);
649  break;
650  }
651  }
652  return 0;
653 } /* get options */
654 
655  /* Generate a random value in intervall 0 <=x <= n */
656 
657 static int rnd(int max_value)
658 {
659  return (int) ((rand() & 32767)/32767.0*max_value);
660 } /* rnd */
661 
662 
663 static sig_handler endprog(int sig_number __attribute__((unused)))
664 {
665  {
666  hp_panic(HA_PANIC_CLOSE);
667  my_end(1);
668  exit(1);
669  }
670 }
671 
672 static int calc_check(uchar *buf, uint length)
673 {
674  int check=0;
675  while (length--)
676  check+= (int) (uchar) *(buf++);
677  return check;
678 }
679 
680 static void make_record(uchar *record, uint n1, uint n2, uint n3,
681  const char *mark, uint count)
682 {
683  memset(record, ' ', reclength);
684  sprintf((char*) record,"%6d:%4d:%8d:%3.3s: %4d",
685  n1,n2,n3,mark,count);
686  record[37]='A'; /* Store A in null key */
687  record[38]=1; /* set as null */
688 }