MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hp_test1.c
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /* Test av heap-database */
17 /* Programmet skapar en heap-databas. Till denna skrivs ett antal poster.
18  Databasen st{ngs. D{refter |ppnas den p} nytt och en del av posterna
19  raderas.
20 */
21 
22 #include <my_global.h>
23 #include <my_sys.h>
24 #include <m_string.h>
25 #include "heap.h"
26 
27 static int get_options(int argc, char *argv[]);
28 
29 static int flag=0,verbose=0,remove_ant=0,flags[50];
30 
31 int main(int argc, char **argv)
32 {
33  int i,j,error,deleted;
34  HP_INFO *file;
35  uchar record[128],key[32];
36  const char *filename;
37  HP_KEYDEF keyinfo[10];
38  HA_KEYSEG keyseg[4];
39  HP_CREATE_INFO hp_create_info;
40  HP_SHARE *tmp_share;
41  my_bool unused;
42  MY_INIT(argv[0]);
43 
44  filename= "test1";
45  get_options(argc,argv);
46 
47  memset(&hp_create_info, 0, sizeof(hp_create_info));
48  hp_create_info.max_table_size= 1024L*1024L;
49  hp_create_info.keys= 1;
50  hp_create_info.keydef= keyinfo;
51  hp_create_info.reclength= 30;
52  hp_create_info.max_records= (ulong) flag*100000L;
53  hp_create_info.min_records= 10UL;
54 
55  keyinfo[0].keysegs=1;
56  keyinfo[0].seg=keyseg;
57  keyinfo[0].algorithm= HA_KEY_ALG_HASH;
58  keyinfo[0].seg[0].type=HA_KEYTYPE_BINARY;
59  keyinfo[0].seg[0].start=1;
60  keyinfo[0].seg[0].length=6;
61  keyinfo[0].seg[0].charset= &my_charset_latin1;
62  keyinfo[0].seg[0].null_bit= 0;
63  keyinfo[0].flag = HA_NOSAME;
64 
65  deleted=0;
66  memset(flags, 0, sizeof(flags));
67 
68  printf("- Creating heap-file\n");
69  if (heap_create(filename, &hp_create_info, &tmp_share, &unused) ||
70  !(file= heap_open(filename, 2)))
71  goto err;
72  printf("- Writing records:s\n");
73  strmov((char*) record," ..... key ");
74 
75  for (i=49 ; i>=1 ; i-=2 )
76  {
77  j=i%25 +1;
78  sprintf((char*) key,"%6d",j);
79  bmove(record+1,key,6);
80  error=heap_write(file,record);
81  if (heap_check_heap(file,0))
82  {
83  puts("Heap keys crashed");
84  goto err;
85  }
86  flags[j]=1;
87  if (verbose || error) printf("J= %2d heap_write: %d my_errno: %d\n",
88  j,error,my_errno);
89  }
90  if (heap_close(file))
91  goto err;
92  printf("- Reopening file\n");
93  if (!(file=heap_open(filename, 2)))
94  goto err;
95 
96  printf("- Removing records\n");
97  for (i=1 ; i<=10 ; i++)
98  {
99  if (i == remove_ant) { (void) heap_close(file); return (0) ; }
100  sprintf((char*) key,"%6d",(j=(int) ((rand() & 32767)/32767.*25)));
101  if ((error = heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT)))
102  {
103  if (verbose || (flags[j] == 1 ||
104  (error && my_errno != HA_ERR_KEY_NOT_FOUND)))
105  printf("key: %s rkey: %3d my_errno: %3d\n",(char*) key,error,my_errno);
106  }
107  else
108  {
109  error=heap_delete(file,record);
110  if (error || verbose)
111  printf("key: %s delete: %d my_errno: %d\n",(char*) key,error,my_errno);
112  flags[j]=0;
113  if (! error)
114  deleted++;
115  }
116  if (heap_check_heap(file,0))
117  {
118  puts("Heap keys crashed");
119  goto err;
120  }
121  }
122 
123  printf("- Reading records with key\n");
124  for (i=1 ; i<=25 ; i++)
125  {
126  sprintf((char*) key,"%6d",i);
127  bmove(record+1,key,6);
128  my_errno=0;
129  error=heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT);
130  if (verbose ||
131  (error == 0 && flags[i] != 1) ||
132  (error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND)))
133  {
134  printf("key: %s rkey: %3d my_errno: %3d record: %s\n",
135  (char*) key,error,my_errno,record+1);
136  }
137  }
138 
139 #ifdef OLD_HEAP_VERSION
140  {
141  int found;
142  printf("- Reading records with position\n");
143  for (i=1,found=0 ; i <= 30 ; i++)
144  {
145  my_errno=0;
146  if ((error=heap_rrnd(file,record,i == 1 ? 0L : (ulong) -1)) ==
147  HA_ERR_END_OF_FILE)
148  {
149  if (found != 25-deleted)
150  printf("Found only %d of %d records\n",found,25-deleted);
151  break;
152  }
153  if (!error)
154  found++;
155  if (verbose || (error != 0 && error != HA_ERR_RECORD_DELETED))
156  {
157  printf("pos: %2d ni_rrnd: %3d my_errno: %3d record: %s\n",
158  i-1,error,my_errno,(char*) record+1);
159  }
160  }
161  }
162 #endif
163 
164  if (heap_close(file) || hp_panic(HA_PANIC_CLOSE))
165  goto err;
166  my_end(MY_GIVE_INFO);
167  return(0);
168 err:
169  printf("got error: %d when using heap-database\n",my_errno);
170  return(1);
171 } /* main */
172 
173 
174 /* Read options */
175 
176 static int get_options(int argc, char **argv)
177 {
178  char *pos;
179 
180  while (--argc >0 && *(pos = *(++argv)) == '-' ) {
181  switch(*++pos) {
182  case 'B': /* Big file */
183  flag=1;
184  break;
185  case 'v': /* verbose */
186  verbose=1;
187  break;
188  case 'm':
189  remove_ant=atoi(++pos);
190  break;
191  case 'V':
192  printf("hp_test1 Ver 3.0 \n");
193  exit(0);
194  case '#':
195  DBUG_PUSH (++pos);
196  break;
197  }
198  }
199  return 0;
200 } /* get options */