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, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is also distributed with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23 /* Test av heap-database */
24 /* Programmet skapar en heap-databas. Till denna skrivs ett antal poster.
25 Databasen st{ngs. D{refter |ppnas den p} nytt och en del av posterna
26 raderas.
27 */
28
29 #include <my_global.h>
30 #include <my_sys.h>
31 #include <m_string.h>
32 #include "heap.h"
33
34 static int get_options(int argc, char *argv[]);
35
36 static int flag=0,verbose=0,remove_ant=0,flags[50];
37
main(int argc,char ** argv)38 int main(int argc, char **argv)
39 {
40 int i,j,error,deleted;
41 HP_INFO *file;
42 uchar record[128],key[32];
43 const char *filename;
44 HP_KEYDEF keyinfo[10];
45 HA_KEYSEG keyseg[4];
46 HP_CREATE_INFO hp_create_info;
47 HP_SHARE *tmp_share;
48 my_bool unused;
49 MY_INIT(argv[0]);
50
51 filename= "test1";
52 get_options(argc,argv);
53
54 memset(&hp_create_info, 0, sizeof(hp_create_info));
55 hp_create_info.max_table_size= 1024L*1024L;
56 hp_create_info.keys= 1;
57 hp_create_info.keydef= keyinfo;
58 hp_create_info.reclength= 30;
59 hp_create_info.max_records= (ulong) flag*100000L;
60 hp_create_info.min_records= 10UL;
61
62 keyinfo[0].keysegs=1;
63 keyinfo[0].seg=keyseg;
64 keyinfo[0].algorithm= HA_KEY_ALG_HASH;
65 keyinfo[0].seg[0].type=HA_KEYTYPE_BINARY;
66 keyinfo[0].seg[0].start=1;
67 keyinfo[0].seg[0].length=6;
68 keyinfo[0].seg[0].charset= &my_charset_latin1;
69 keyinfo[0].seg[0].null_bit= 0;
70 keyinfo[0].flag = HA_NOSAME;
71
72 deleted=0;
73 memset(flags, 0, sizeof(flags));
74
75 printf("- Creating heap-file\n");
76 if (heap_create(filename, &hp_create_info, &tmp_share, &unused) ||
77 !(file= heap_open(filename, 2)))
78 goto err;
79 printf("- Writing records:s\n");
80 strmov((char*) record," ..... key ");
81
82 for (i=49 ; i>=1 ; i-=2 )
83 {
84 j=i%25 +1;
85 sprintf((char*) key,"%6d",j);
86 bmove(record+1,key,6);
87 error=heap_write(file,record);
88 if (heap_check_heap(file,0))
89 {
90 puts("Heap keys crashed");
91 goto err;
92 }
93 flags[j]=1;
94 if (verbose || error) printf("J= %2d heap_write: %d my_errno: %d\n",
95 j,error,my_errno);
96 }
97 if (heap_close(file))
98 goto err;
99 printf("- Reopening file\n");
100 if (!(file=heap_open(filename, 2)))
101 goto err;
102
103 printf("- Removing records\n");
104 for (i=1 ; i<=10 ; i++)
105 {
106 if (i == remove_ant) { (void) heap_close(file); return (0) ; }
107 sprintf((char*) key,"%6d",(j=(int) ((rand() & 32767)/32767.*25)));
108 if ((error = heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT)))
109 {
110 if (verbose || (flags[j] == 1 ||
111 (error && my_errno != HA_ERR_KEY_NOT_FOUND)))
112 printf("key: %s rkey: %3d my_errno: %3d\n",(char*) key,error,my_errno);
113 }
114 else
115 {
116 error=heap_delete(file,record);
117 if (error || verbose)
118 printf("key: %s delete: %d my_errno: %d\n",(char*) key,error,my_errno);
119 flags[j]=0;
120 if (! error)
121 deleted++;
122 }
123 if (heap_check_heap(file,0))
124 {
125 puts("Heap keys crashed");
126 goto err;
127 }
128 }
129
130 printf("- Reading records with key\n");
131 for (i=1 ; i<=25 ; i++)
132 {
133 sprintf((char*) key,"%6d",i);
134 bmove(record+1,key,6);
135 my_errno=0;
136 error=heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT);
137 if (verbose ||
138 (error == 0 && flags[i] != 1) ||
139 (error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND)))
140 {
141 printf("key: %s rkey: %3d my_errno: %3d record: %s\n",
142 (char*) key,error,my_errno,record+1);
143 }
144 }
145
146 #ifdef OLD_HEAP_VERSION
147 {
148 int found;
149 printf("- Reading records with position\n");
150 for (i=1,found=0 ; i <= 30 ; i++)
151 {
152 my_errno=0;
153 if ((error=heap_rrnd(file,record,i == 1 ? 0L : (ulong) -1)) ==
154 HA_ERR_END_OF_FILE)
155 {
156 if (found != 25-deleted)
157 printf("Found only %d of %d records\n",found,25-deleted);
158 break;
159 }
160 if (!error)
161 found++;
162 if (verbose || (error != 0 && error != HA_ERR_RECORD_DELETED))
163 {
164 printf("pos: %2d ni_rrnd: %3d my_errno: %3d record: %s\n",
165 i-1,error,my_errno,(char*) record+1);
166 }
167 }
168 }
169 #endif
170
171 if (heap_close(file) || hp_panic(HA_PANIC_CLOSE))
172 goto err;
173 my_end(MY_GIVE_INFO);
174 return(0);
175 err:
176 printf("got error: %d when using heap-database\n",my_errno);
177 return(1);
178 } /* main */
179
180
181 /* Read options */
182
get_options(int argc,char ** argv)183 static int get_options(int argc, char **argv)
184 {
185 char *pos;
186
187 while (--argc >0 && *(pos = *(++argv)) == '-' ) {
188 switch(*++pos) {
189 case 'B': /* Big file */
190 flag=1;
191 break;
192 case 'v': /* verbose */
193 verbose=1;
194 break;
195 case 'm':
196 remove_ant=atoi(++pos);
197 break;
198 case 'V':
199 printf("hp_test1 Ver 3.0 \n");
200 exit(0);
201 case '#':
202 DBUG_PUSH (++pos);
203 break;
204 }
205 }
206 return 0;
207 } /* get options */
208