1/* Copyright (c) 2000, 2010, 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
27static int get_options(int argc, char *argv[]);
28
29static int flag=0,verbose=0,remove_ant=0,flags[50];
30
31int 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 bzero(&hp_create_info, 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 bzero((uchar*) flags,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 if (heap_close(file) || hp_panic(HA_PANIC_CLOSE))
140 goto err;
141 my_end(MY_GIVE_INFO);
142 return(0);
143err:
144 printf("got error: %d when using heap-database\n",my_errno);
145 return(1);
146} /* main */
147
148
149/* Read options */
150
151static int get_options(int argc, char **argv)
152{
153 char *pos;
154
155 while (--argc >0 && *(pos = *(++argv)) == '-' ) {
156 switch(*++pos) {
157 case 'B': /* Big file */
158 flag=1;
159 break;
160 case 'v': /* verbose */
161 verbose=1;
162 break;
163 case 'm':
164 remove_ant=atoi(++pos);
165 break;
166 case 'V':
167 printf("hp_test1 Ver 3.0 \n");
168 exit(0);
169 case '#':
170 DBUG_PUSH (++pos);
171 break;
172 }
173 }
174 return 0;
175} /* get options */
176