1
/* Copyright (C) 2000-2003, 2006 MySQL AB
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
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
22
#include <my_global.h>
27
static int get_options(int argc, char *argv[]);
29
static int flag=0,verbose=0,remove_ant=0,flags[50];
31
int main(int argc, char **argv)
33
int i,j,error,deleted;
35
uchar record[128],key[32];
37
HP_KEYDEF keyinfo[10];
39
HP_CREATE_INFO hp_create_info;
44
get_options(argc,argv);
46
bzero(&hp_create_info, sizeof(hp_create_info));
47
hp_create_info.max_table_size= 1024L*1024L;
50
keyinfo[0].seg=keyseg;
51
keyinfo[0].algorithm= HA_KEY_ALG_HASH;
52
keyinfo[0].seg[0].type=HA_KEYTYPE_BINARY;
53
keyinfo[0].seg[0].start=1;
54
keyinfo[0].seg[0].length=6;
55
keyinfo[0].seg[0].charset= &my_charset_latin1;
56
keyinfo[0].seg[0].null_bit= 0;
57
keyinfo[0].flag = HA_NOSAME;
60
bzero((uchar*) flags,sizeof(flags));
62
printf("- Creating heap-file\n");
63
if (heap_create(filename,1,keyinfo,30,(ulong) flag*100000L,10L,
64
&hp_create_info, &tmp_share) ||
65
!(file= heap_open(filename, 2)))
67
printf("- Writing records:s\n");
68
strmov((char*) record," ..... key ");
70
for (i=49 ; i>=1 ; i-=2 )
73
sprintf((char*) key,"%6d",j);
74
bmove(record+1,key,6);
75
error=heap_write(file,record);
76
if (heap_check_heap(file,0))
78
puts("Heap keys crashed");
82
if (verbose || error) printf("J= %2d heap_write: %d my_errno: %d\n",
87
printf("- Reopening file\n");
88
if (!(file=heap_open(filename, 2)))
91
printf("- Removing records\n");
92
for (i=1 ; i<=10 ; i++)
94
if (i == remove_ant) { VOID(heap_close(file)) ; return (0) ; }
95
sprintf((char*) key,"%6d",(j=(int) ((rand() & 32767)/32767.*25)));
96
if ((error = heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT)))
98
if (verbose || (flags[j] == 1 ||
99
(error && my_errno != HA_ERR_KEY_NOT_FOUND)))
100
printf("key: %s rkey: %3d my_errno: %3d\n",(char*) key,error,my_errno);
104
error=heap_delete(file,record);
105
if (error || verbose)
106
printf("key: %s delete: %d my_errno: %d\n",(char*) key,error,my_errno);
111
if (heap_check_heap(file,0))
113
puts("Heap keys crashed");
118
printf("- Reading records with key\n");
119
for (i=1 ; i<=25 ; i++)
121
sprintf((char*) key,"%6d",i);
122
bmove(record+1,key,6);
124
error=heap_rkey(file,record,0,key,6,HA_READ_KEY_EXACT);
126
(error == 0 && flags[i] != 1) ||
127
(error && (flags[i] != 0 || my_errno != HA_ERR_KEY_NOT_FOUND)))
129
printf("key: %s rkey: %3d my_errno: %3d record: %s\n",
130
(char*) key,error,my_errno,record+1);
134
#ifdef OLD_HEAP_VERSION
137
printf("- Reading records with position\n");
138
for (i=1,found=0 ; i <= 30 ; i++)
141
if ((error=heap_rrnd(file,record,i == 1 ? 0L : (ulong) -1)) ==
144
if (found != 25-deleted)
145
printf("Found only %d of %d records\n",found,25-deleted);
150
if (verbose || (error != 0 && error != HA_ERR_RECORD_DELETED))
152
printf("pos: %2d ni_rrnd: %3d my_errno: %3d record: %s\n",
153
i-1,error,my_errno,(char*) record+1);
159
if (heap_close(file) || hp_panic(HA_PANIC_CLOSE))
161
my_end(MY_GIVE_INFO);
164
printf("got error: %d when using heap-database\n",my_errno);
171
static int get_options(int argc, char **argv)
175
while (--argc >0 && *(pos = *(++argv)) == '-' ) {
177
case 'B': /* Big file */
180
case 'v': /* verbose */
184
remove_ant=atoi(++pos);
187
printf("hp_test1 Ver 3.0 \n");