1
by brian
clean slate |
1 |
/* Copyright (C) 2000-2003, 2006 MySQL AB
|
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
|
|
15 |
||
16 |
/* Test av isam-databas: stor test */
|
|
17 |
||
18 |
#ifndef USE_MY_FUNC /* We want to be able to dbug this !! */ |
|
19 |
#define USE_MY_FUNC
|
|
20 |
#endif
|
|
21 |
#ifdef DBUG_OFF
|
|
22 |
#undef DBUG_OFF
|
|
23 |
#endif
|
|
24 |
||
25 |
#include "heapdef.h" /* Because of hp_find_block */ |
|
26 |
#include <signal.h> |
|
27 |
||
28 |
#define MAX_RECORDS 100000
|
|
29 |
#define MAX_KEYS 4
|
|
30 |
||
31 |
static int get_options(int argc, char *argv[]); |
|
32 |
static int rnd(int max_value); |
|
33 |
static sig_handler endprog(int sig_number); |
|
34 |
||
35 |
static uint flag=0,verbose=0,testflag=0,recant=10000,silent=0; |
|
36 |
static uint keys=MAX_KEYS; |
|
37 |
static uint16 key1[1001]; |
|
38 |
static my_bool key3[MAX_RECORDS]; |
|
39 |
static int reclength=39; |
|
40 |
||
41 |
||
42 |
static int calc_check(uchar *buf,uint length); |
|
43 |
static void make_record(uchar *record, uint n1, uint n2, uint n3, |
|
44 |
const char *mark, uint count); |
|
45 |
||
46 |
/* Main program */
|
|
47 |
||
48 |
int main(int argc, char *argv[]) |
|
49 |
{
|
|
50 |
register uint i,j; |
|
51 |
uint ant,n1,n2,n3; |
|
52 |
uint write_count,update,opt_delete,check2,dupp_keys,found_key; |
|
53 |
int error; |
|
54 |
ulong pos; |
|
55 |
unsigned long key_check; |
|
56 |
uchar record[128],record2[128],record3[128],key[10]; |
|
57 |
const char *filename,*filename2; |
|
58 |
HP_INFO *file,*file2; |
|
59 |
HP_SHARE *tmp_share; |
|
60 |
HP_KEYDEF keyinfo[MAX_KEYS]; |
|
61 |
HA_KEYSEG keyseg[MAX_KEYS*5]; |
|
77.1.77
by Monty Taylor
A crapton more warning cleanups (I turned on more warnings) |
62 |
HEAP_PTR position= NULL; |
1
by brian
clean slate |
63 |
HP_CREATE_INFO hp_create_info; |
64 |
CHARSET_INFO *cs= &my_charset_latin1; |
|
65 |
MY_INIT(argv[0]); /* init my_sys library & pthreads */ |
|
66 |
||
67 |
filename= "test2"; |
|
68 |
filename2= "test2_2"; |
|
69 |
file=file2=0; |
|
70 |
get_options(argc,argv); |
|
71 |
||
72 |
bzero(&hp_create_info, sizeof(hp_create_info)); |
|
73 |
hp_create_info.max_table_size= 1024L*1024L; |
|
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 |
bzero((char*) key1,sizeof(key1)); |
|
122 |
bzero((char*) key3,sizeof(key3)); |
|
123 |
||
124 |
printf("- Creating heap-file\n"); |
|
125 |
if (heap_create(filename,keys,keyinfo,reclength,(ulong) flag*100000L, |
|
126 |
(ulong) recant/2, &hp_create_info, &tmp_share) || |
|
127 |
!(file= heap_open(filename, 2))) |
|
128 |
goto err; |
|
129 |
signal(SIGINT,endprog); |
|
130 |
||
131 |
printf("- Writing records:s\n"); |
|
132 |
strmov((char*) record," ..... key"); |
|
133 |
||
134 |
for (i=0 ; i < recant ; i++) |
|
135 |
{
|
|
136 |
n1=rnd(1000); n2=rnd(100); n3=rnd(min(recant*5,MAX_RECORDS)); |
|
137 |
make_record(record,n1,n2,n3,"Pos",write_count); |
|
138 |
||
139 |
if (heap_write(file,record)) |
|
140 |
{
|
|
141 |
if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0) |
|
142 |
{
|
|
143 |
printf("Error: %d in write at record: %d\n",my_errno,i); |
|
144 |
goto err; |
|
145 |
}
|
|
146 |
if (verbose) printf(" Double key: %d\n",n3); |
|
147 |
}
|
|
148 |
else
|
|
149 |
{
|
|
150 |
if (key3[n3] == 1) |
|
151 |
{
|
|
152 |
printf("Error: Didn't get error when writing second key: '%8d'\n",n3); |
|
153 |
goto err; |
|
154 |
}
|
|
155 |
write_count++; key1[n1]++; key3[n3]=1; |
|
156 |
key_check+=n1; |
|
157 |
}
|
|
158 |
if (testflag == 1 && heap_check_heap(file,0)) |
|
159 |
{
|
|
160 |
puts("Heap keys crashed"); |
|
161 |
goto err; |
|
162 |
}
|
|
163 |
}
|
|
164 |
if (testflag == 1) |
|
165 |
goto end; |
|
166 |
if (heap_check_heap(file,0)) |
|
167 |
{
|
|
168 |
puts("Heap keys crashed"); |
|
169 |
goto err; |
|
170 |
}
|
|
171 |
||
172 |
printf("- Delete\n"); |
|
173 |
for (i=0 ; i < write_count/10 ; i++) |
|
174 |
{
|
|
175 |
for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ; |
|
176 |
if (j != 0) |
|
177 |
{
|
|
178 |
sprintf((char*) key,"%6d",j); |
|
179 |
if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT)) |
|
180 |
{
|
|
181 |
printf("can't find key1: \"%s\"\n",(char*) key); |
|
182 |
goto err; |
|
183 |
}
|
|
184 |
#ifdef NOT_USED
|
|
185 |
if (file->current_ptr == hp_find_block(&file->s->block,0) || |
|
186 |
file->current_ptr == hp_find_block(&file->s->block,1)) |
|
187 |
continue; /* Don't remove 2 first records */ |
|
188 |
#endif
|
|
189 |
if (heap_delete(file,record)) |
|
190 |
{
|
|
191 |
printf("error: %d; can't delete record: \"%s\"\n", my_errno,(char*) record); |
|
192 |
goto err; |
|
193 |
}
|
|
194 |
opt_delete++; |
|
195 |
key1[atoi((char*) record+keyinfo[0].seg[0].start)]--; |
|
196 |
key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0; |
|
197 |
key_check-=atoi((char*) record); |
|
198 |
if (testflag == 2 && heap_check_heap(file,0)) |
|
199 |
{
|
|
200 |
puts("Heap keys crashed"); |
|
201 |
goto err; |
|
202 |
}
|
|
203 |
}
|
|
204 |
else
|
|
205 |
puts("Warning: Skipping delete test because no dupplicate keys"); |
|
206 |
}
|
|
207 |
if (testflag==2) goto end; |
|
208 |
if (heap_check_heap(file,0)) |
|
209 |
{
|
|
210 |
puts("Heap keys crashed"); |
|
211 |
goto err; |
|
212 |
}
|
|
213 |
||
214 |
printf("- Update\n"); |
|
215 |
for (i=0 ; i < write_count/10 ; i++) |
|
216 |
{
|
|
217 |
n1=rnd(1000); n2=rnd(100); n3=rnd(min(recant*2,MAX_RECORDS)); |
|
218 |
make_record(record2, n1, n2, n3, "XXX", update); |
|
219 |
if (rnd(2) == 1) |
|
220 |
{
|
|
221 |
if (heap_scan_init(file)) |
|
222 |
goto err; |
|
223 |
j=rnd(write_count-opt_delete); |
|
224 |
while ((error=heap_scan(file,record) == HA_ERR_RECORD_DELETED) || |
|
225 |
(!error && j)) |
|
226 |
{
|
|
227 |
if (!error) |
|
228 |
j--; |
|
229 |
}
|
|
230 |
if (error) |
|
231 |
goto err; |
|
232 |
}
|
|
233 |
else
|
|
234 |
{
|
|
235 |
for (j=rnd(1000)+1 ; j>0 && key1[j] == 0 ; j--) ; |
|
236 |
if (!key1[j]) |
|
237 |
continue; |
|
238 |
sprintf((char*) key,"%6d",j); |
|
239 |
if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT)) |
|
240 |
{
|
|
241 |
printf("can't find key1: \"%s\"\n",(char*) key); |
|
242 |
goto err; |
|
243 |
}
|
|
244 |
}
|
|
245 |
if (heap_update(file,record,record2)) |
|
246 |
{
|
|
247 |
if (my_errno != HA_ERR_FOUND_DUPP_KEY || key3[n3] == 0) |
|
248 |
{
|
|
249 |
printf("error: %d; can't update:\nFrom: \"%s\"\nTo: \"%s\"\n", |
|
250 |
my_errno,(char*) record, (char*) record2); |
|
251 |
goto err; |
|
252 |
}
|
|
253 |
if (verbose) |
|
254 |
printf("Double key when tried to update:\nFrom: \"%s\"\nTo: \"%s\"\n", |
|
255 |
(char*) record, (char*) record2); |
|
256 |
}
|
|
257 |
else
|
|
258 |
{
|
|
259 |
key1[atoi((char*) record+keyinfo[0].seg[0].start)]--; |
|
260 |
key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0; |
|
261 |
key1[n1]++; key3[n3]=1; |
|
262 |
update++; |
|
263 |
key_check=key_check-atoi((char*) record)+n1; |
|
264 |
}
|
|
265 |
if (testflag == 3 && heap_check_heap(file,0)) |
|
266 |
{
|
|
267 |
puts("Heap keys crashed"); |
|
268 |
goto err; |
|
269 |
}
|
|
270 |
}
|
|
271 |
if (testflag == 3) goto end; |
|
272 |
if (heap_check_heap(file,0)) |
|
273 |
{
|
|
274 |
puts("Heap keys crashed"); |
|
275 |
goto err; |
|
276 |
}
|
|
277 |
||
278 |
for (i=999, dupp_keys=found_key=0 ; i>0 ; i--) |
|
279 |
{
|
|
280 |
if (key1[i] > dupp_keys) { dupp_keys=key1[i]; found_key=i; } |
|
281 |
sprintf((char*) key,"%6d",found_key); |
|
282 |
}
|
|
283 |
||
284 |
if (dupp_keys > 3) |
|
285 |
{
|
|
286 |
if (!silent) |
|
287 |
printf("- Read first key - next - delete - next -> last\n"); |
|
288 |
DBUG_PRINT("progpos",("first - next - delete - next -> last")); |
|
289 |
||
290 |
if (heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT)) |
|
291 |
goto err; |
|
292 |
if (heap_rnext(file,record3)) goto err; |
|
293 |
if (heap_delete(file,record3)) goto err; |
|
294 |
key_check-=atoi((char*) record3); |
|
295 |
key1[atoi((char*) record+keyinfo[0].seg[0].start)]--; |
|
296 |
key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0; |
|
297 |
opt_delete++; |
|
298 |
ant=2; |
|
299 |
while ((error=heap_rnext(file,record3)) == 0 || |
|
300 |
error == HA_ERR_RECORD_DELETED) |
|
301 |
if (! error) |
|
302 |
ant++; |
|
303 |
if (ant != dupp_keys) |
|
304 |
{
|
|
305 |
printf("next: I can only find: %d records of %d\n", |
|
306 |
ant,dupp_keys); |
|
307 |
goto end; |
|
308 |
}
|
|
309 |
dupp_keys--; |
|
310 |
if (heap_check_heap(file,0)) |
|
311 |
{
|
|
312 |
puts("Heap keys crashed"); |
|
313 |
goto err; |
|
314 |
}
|
|
315 |
||
316 |
if (!silent) |
|
317 |
printf("- Read last key - delete - prev - prev - opt_delete - prev -> first\n"); |
|
318 |
||
319 |
if (heap_rlast(file,record3,0)) goto err; |
|
320 |
if (heap_delete(file,record3)) goto err; |
|
321 |
key_check-=atoi((char*) record3); |
|
322 |
key1[atoi((char*) record+keyinfo[0].seg[0].start)]--; |
|
323 |
key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0; |
|
324 |
opt_delete++; |
|
325 |
if (heap_rprev(file,record3) || heap_rprev(file,record3)) |
|
326 |
goto err; |
|
327 |
if (heap_delete(file,record3)) goto err; |
|
328 |
key_check-=atoi((char*) record3); |
|
329 |
key1[atoi((char*) record+keyinfo[0].seg[0].start)]--; |
|
330 |
key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0; |
|
331 |
opt_delete++; |
|
332 |
ant=3; |
|
333 |
while ((error=heap_rprev(file,record3)) == 0 || |
|
334 |
error == HA_ERR_RECORD_DELETED) |
|
335 |
{
|
|
336 |
if (! error) |
|
337 |
ant++; |
|
338 |
}
|
|
339 |
if (ant != dupp_keys) |
|
340 |
{
|
|
341 |
printf("next: I can only find: %d records of %d\n", |
|
342 |
ant,dupp_keys); |
|
343 |
goto end; |
|
344 |
}
|
|
345 |
dupp_keys-=2; |
|
346 |
if (heap_check_heap(file,0)) |
|
347 |
{
|
|
348 |
puts("Heap keys crashed"); |
|
349 |
goto err; |
|
350 |
}
|
|
351 |
}
|
|
352 |
else
|
|
353 |
puts("Warning: Not enough duplicated keys: Skipping delete key check"); |
|
354 |
||
355 |
if (!silent) |
|
356 |
printf("- Read (first) - next - delete - next -> last\n"); |
|
357 |
DBUG_PRINT("progpos",("first - next - delete - next -> last")); |
|
358 |
||
359 |
if (heap_scan_init(file)) |
|
360 |
goto err; |
|
361 |
while ((error=heap_scan(file,record3) == HA_ERR_RECORD_DELETED)) ; |
|
362 |
if (error) |
|
363 |
goto err; |
|
364 |
if (heap_delete(file,record3)) goto err; |
|
365 |
key_check-=atoi((char*) record3); |
|
366 |
opt_delete++; |
|
367 |
key1[atoi((char*) record+keyinfo[0].seg[0].start)]--; |
|
368 |
key3[atoi((char*) record+keyinfo[2].seg[0].start)]=0; |
|
369 |
ant=0; |
|
370 |
while ((error=heap_scan(file,record3)) == 0 || |
|
371 |
error == HA_ERR_RECORD_DELETED) |
|
372 |
if (! error) |
|
373 |
ant++; |
|
374 |
if (ant != write_count-opt_delete) |
|
375 |
{
|
|
376 |
printf("next: Found: %d records of %d\n",ant,write_count-opt_delete); |
|
377 |
goto end; |
|
378 |
}
|
|
379 |
if (heap_check_heap(file,0)) |
|
380 |
{
|
|
381 |
puts("Heap keys crashed"); |
|
382 |
goto err; |
|
383 |
}
|
|
384 |
||
385 |
puts("- Test if: Read rrnd - same - rkey - same"); |
|
386 |
DBUG_PRINT("progpos",("Read rrnd - same")); |
|
387 |
pos=rnd(write_count-opt_delete-5)+5; |
|
388 |
heap_scan_init(file); |
|
389 |
i=5; |
|
390 |
while ((error=heap_scan(file,record)) == HA_ERR_RECORD_DELETED || |
|
391 |
(error == 0 && pos)) |
|
392 |
{
|
|
393 |
if (!error) |
|
394 |
pos--; |
|
395 |
if (i-- == 0) |
|
396 |
{
|
|
397 |
bmove(record3,record,reclength); |
|
398 |
position=heap_position(file); |
|
399 |
}
|
|
400 |
}
|
|
401 |
if (error) |
|
402 |
goto err; |
|
403 |
bmove(record2,record,reclength); |
|
404 |
if (heap_rsame(file,record,-1) || heap_rsame(file,record2,2)) |
|
405 |
goto err; |
|
406 |
if (bcmp(record2,record,reclength)) |
|
407 |
{
|
|
408 |
puts("heap_rsame didn't find right record"); |
|
409 |
goto end; |
|
410 |
}
|
|
411 |
||
412 |
puts("- Test of read through position"); |
|
413 |
if (heap_rrnd(file,record,position)) |
|
414 |
goto err; |
|
415 |
if (bcmp(record3,record,reclength)) |
|
416 |
{
|
|
417 |
puts("heap_frnd didn't find right record"); |
|
418 |
goto end; |
|
419 |
}
|
|
420 |
||
421 |
printf("- heap_info\n"); |
|
422 |
{
|
|
423 |
HEAPINFO info; |
|
424 |
heap_info(file,&info,0); |
|
425 |
/* We have to test with opt_delete +1 as this may be the case if the last
|
|
426 |
inserted row was a duplicate key */
|
|
427 |
if (info.records != write_count-opt_delete || |
|
428 |
(info.deleted != opt_delete && info.deleted != opt_delete+1)) |
|
429 |
{
|
|
430 |
puts("Wrong info from heap_info"); |
|
431 |
printf("Got: records: %ld(%d) deleted: %ld(%d)\n", |
|
432 |
info.records,write_count-opt_delete,info.deleted,opt_delete); |
|
433 |
}
|
|
434 |
}
|
|
435 |
||
436 |
#ifdef OLD_HEAP_VERSION
|
|
437 |
{
|
|
438 |
uint check; |
|
439 |
printf("- Read through all records with rnd\n"); |
|
440 |
if (heap_extra(file,HA_EXTRA_RESET) || heap_extra(file,HA_EXTRA_CACHE)) |
|
441 |
{
|
|
442 |
puts("got error from heap_extra"); |
|
443 |
goto end; |
|
444 |
}
|
|
445 |
ant=check=0; |
|
446 |
while ((error=heap_rrnd(file,record,(ulong) -1)) != HA_ERR_END_OF_FILE && |
|
447 |
ant < write_count + 10) |
|
448 |
{
|
|
449 |
if (!error) |
|
450 |
{
|
|
451 |
ant++; |
|
452 |
check+=calc_check(record,reclength); |
|
453 |
}
|
|
454 |
}
|
|
455 |
if (ant != write_count-opt_delete) |
|
456 |
{
|
|
457 |
printf("rrnd: I can only find: %d records of %d\n", ant, |
|
458 |
write_count-opt_delete); |
|
459 |
goto end; |
|
460 |
}
|
|
461 |
if (heap_extra(file,HA_EXTRA_NO_CACHE)) |
|
462 |
{
|
|
463 |
puts("got error from heap_extra(HA_EXTRA_NO_CACHE)"); |
|
464 |
goto end; |
|
465 |
}
|
|
466 |
}
|
|
467 |
#endif
|
|
468 |
||
469 |
printf("- Read through all records with scan\n"); |
|
470 |
if (heap_reset(file) || heap_extra(file,HA_EXTRA_CACHE)) |
|
471 |
{
|
|
472 |
puts("got error from heap_extra"); |
|
473 |
goto end; |
|
474 |
}
|
|
475 |
ant=check2=0; |
|
476 |
heap_scan_init(file); |
|
477 |
while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE && |
|
478 |
ant < write_count + 10) |
|
479 |
{
|
|
480 |
if (!error) |
|
481 |
{
|
|
482 |
ant++; |
|
483 |
check2+=calc_check(record,reclength); |
|
484 |
}
|
|
485 |
}
|
|
486 |
if (ant != write_count-opt_delete) |
|
487 |
{
|
|
488 |
printf("scan: I can only find: %d records of %d\n", ant, |
|
489 |
write_count-opt_delete); |
|
490 |
goto end; |
|
491 |
}
|
|
492 |
#ifdef OLD_HEAP_VERSION
|
|
493 |
if (check != check2) |
|
494 |
{
|
|
495 |
puts("scan: Checksum didn't match reading with rrnd"); |
|
496 |
goto end; |
|
497 |
}
|
|
498 |
#endif
|
|
499 |
||
500 |
||
501 |
if (heap_extra(file,HA_EXTRA_NO_CACHE)) |
|
502 |
{
|
|
503 |
puts("got error from heap_extra(HA_EXTRA_NO_CACHE)"); |
|
504 |
goto end; |
|
505 |
}
|
|
506 |
||
507 |
for (i=999, dupp_keys=found_key=0 ; i>0 ; i--) |
|
508 |
{
|
|
509 |
if (key1[i] > dupp_keys) { dupp_keys=key1[i]; found_key=i; } |
|
510 |
sprintf((char*) key,"%6d",found_key); |
|
511 |
}
|
|
512 |
printf("- Read through all keys with first-next-last-prev\n"); |
|
513 |
ant=0; |
|
514 |
for (error=heap_rkey(file,record,0,key,6, HA_READ_KEY_EXACT); |
|
515 |
! error ; |
|
516 |
error=heap_rnext(file,record)) |
|
517 |
ant++; |
|
518 |
if (ant != dupp_keys) |
|
519 |
{
|
|
520 |
printf("first-next: I can only find: %d records of %d\n", ant, |
|
521 |
dupp_keys); |
|
522 |
goto end; |
|
523 |
}
|
|
524 |
||
525 |
ant=0; |
|
526 |
for (error=heap_rlast(file,record,0) ; |
|
527 |
! error ; |
|
528 |
error=heap_rprev(file,record)) |
|
529 |
{
|
|
530 |
ant++; |
|
531 |
check2+=calc_check(record,reclength); |
|
532 |
}
|
|
533 |
if (ant != dupp_keys) |
|
534 |
{
|
|
535 |
printf("last-prev: I can only find: %d records of %d\n", ant, |
|
536 |
dupp_keys); |
|
537 |
goto end; |
|
538 |
}
|
|
539 |
||
540 |
if (testflag == 4) goto end; |
|
541 |
||
542 |
printf("- Reading through all rows through keys\n"); |
|
543 |
if (!(file2=heap_open(filename, 2))) |
|
544 |
goto err; |
|
545 |
if (heap_scan_init(file)) |
|
546 |
goto err; |
|
547 |
while ((error=heap_scan(file,record)) != HA_ERR_END_OF_FILE) |
|
548 |
{
|
|
549 |
if (error == 0) |
|
550 |
{
|
|
551 |
if (heap_rkey(file2,record2,2,record+keyinfo[2].seg[0].start,8, |
|
552 |
HA_READ_KEY_EXACT)) |
|
553 |
{
|
|
554 |
printf("can't find key3: \"%.8s\"\n", |
|
555 |
record+keyinfo[2].seg[0].start); |
|
556 |
goto err; |
|
557 |
}
|
|
558 |
}
|
|
559 |
}
|
|
560 |
heap_close(file2); |
|
561 |
||
562 |
printf("- Creating output heap-file 2\n"); |
|
563 |
if (heap_create(filename2, 1, keyinfo, reclength, 0L, 0L, &hp_create_info, |
|
564 |
&tmp_share) || |
|
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 |
if (heap_close(file) || (file2 && heap_close(file2))) |
|
602 |
goto err; |
|
603 |
heap_delete_table(filename2); |
|
604 |
hp_panic(HA_PANIC_CLOSE); |
|
605 |
my_end(MY_GIVE_INFO); |
|
606 |
return(0); |
|
607 |
err: |
|
608 |
printf("Got error: %d when using heap-database\n",my_errno); |
|
609 |
VOID(heap_close(file)); |
|
610 |
return(1); |
|
611 |
} /* main */ |
|
612 |
||
613 |
||
614 |
/* Read options */
|
|
615 |
||
616 |
static int get_options(int argc,char *argv[]) |
|
617 |
{
|
|
618 |
char *pos,*progname; |
|
619 |
||
620 |
progname= argv[0]; |
|
621 |
||
622 |
while (--argc >0 && *(pos = *(++argv)) == '-' ) { |
|
623 |
switch(*++pos) { |
|
624 |
case 'B': /* Big file */ |
|
625 |
flag=1; |
|
626 |
break; |
|
627 |
case 'v': /* verbose */ |
|
628 |
verbose=1; |
|
629 |
break; |
|
630 |
case 'm': /* records */ |
|
631 |
recant=atoi(++pos); |
|
632 |
break; |
|
633 |
case 's': |
|
634 |
silent=1; |
|
635 |
break; |
|
636 |
case 't': |
|
637 |
testflag=atoi(++pos); /* testmod */ |
|
638 |
break; |
|
639 |
case 'V': |
|
640 |
case 'I': |
|
641 |
case '?': |
|
642 |
printf("%s Ver 1.1 for %s at %s\n",progname,SYSTEM_TYPE,MACHINE_TYPE); |
|
643 |
puts("TCX Datakonsult AB, by Monty, for your professional use\n"); |
|
644 |
printf("Usage: %s [-?ABIKLsWv] [-m#] [-t#]\n",progname); |
|
645 |
exit(0); |
|
646 |
case '#': |
|
647 |
DBUG_PUSH (++pos); |
|
648 |
break; |
|
649 |
}
|
|
650 |
}
|
|
651 |
return 0; |
|
652 |
} /* get options */ |
|
653 |
||
654 |
/* Generate a random value in intervall 0 <=x <= n */
|
|
655 |
||
656 |
static int rnd(int max_value) |
|
657 |
{
|
|
658 |
return (int) ((rand() & 32767)/32767.0*max_value); |
|
659 |
} /* rnd */ |
|
660 |
||
661 |
||
662 |
static sig_handler endprog(int sig_number __attribute__((unused))) |
|
663 |
{
|
|
664 |
{
|
|
665 |
hp_panic(HA_PANIC_CLOSE); |
|
666 |
my_end(1); |
|
667 |
exit(1); |
|
668 |
}
|
|
669 |
}
|
|
670 |
||
671 |
static int calc_check(uchar *buf, uint length) |
|
672 |
{
|
|
673 |
int check=0; |
|
674 |
while (length--) |
|
675 |
check+= (int) (uchar) *(buf++); |
|
676 |
return check; |
|
677 |
}
|
|
678 |
||
679 |
static void make_record(uchar *record, uint n1, uint n2, uint n3, |
|
680 |
const char *mark, uint count) |
|
681 |
{
|
|
682 |
bfill(record,reclength,' '); |
|
683 |
sprintf((char*) record,"%6d:%4d:%8d:%3.3s: %4d", |
|
684 |
n1,n2,n3,mark,count); |
|
685 |
record[37]='A'; /* Store A in null key */ |
|
686 |
record[38]=1; /* set as null */ |
|
687 |
}
|