1 /* Copyright (c) 2002, 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-1335 USA */
15
16 /* Testing of the basic functions of a MyISAM rtree table */
17 /* Written by Alex Barkov who has a shared copyright to this code */
18
19
20 #include <my_global.h>
21 #include "myisam.h"
22
23 #ifdef HAVE_RTREE_KEYS
24
25 #include "rt_index.h"
26
27 #define MAX_REC_LENGTH 1024
28 #define ndims 2
29 #define KEYALG HA_KEY_ALG_RTREE
30
31 static int read_with_pos(MI_INFO * file, int silent);
32 static void create_record(uchar *record,uint rownr);
33 static void create_record1(uchar *record,uint rownr);
34 static void print_record(uchar * record,my_off_t offs,const char * tail);
35 static int run_test(const char *filename);
36
37 static double rt_data[]=
38 {
39 /*1*/ 0,10,0,10,
40 /*2*/ 5,15,0,10,
41 /*3*/ 0,10,5,15,
42 /*4*/ 10,20,10,20,
43 /*5*/ 0,10,0,10,
44 /*6*/ 5,15,0,10,
45 /*7*/ 0,10,5,15,
46 /*8*/ 10,20,10,20,
47 /*9*/ 0,10,0,10,
48 /*10*/ 5,15,0,10,
49 /*11*/ 0,10,5,15,
50 /*12*/ 10,20,10,20,
51 /*13*/ 0,10,0,10,
52 /*14*/ 5,15,0,10,
53 /*15*/ 0,10,5,15,
54 /*16*/ 10,20,10,20,
55 /*17*/ 5,15,0,10,
56 /*18*/ 0,10,5,15,
57 /*19*/ 10,20,10,20,
58 /*20*/ 0,10,0,10,
59
60 /*1*/ 100,110,0,10,
61 /*2*/ 105,115,0,10,
62 /*3*/ 100,110,5,15,
63 /*4*/ 110,120,10,20,
64 /*5*/ 100,110,0,10,
65 /*6*/ 105,115,0,10,
66 /*7*/ 100,110,5,15,
67 /*8*/ 110,120,10,20,
68 /*9*/ 100,110,0,10,
69 /*10*/ 105,115,0,10,
70 /*11*/ 100,110,5,15,
71 /*12*/ 110,120,10,20,
72 /*13*/ 100,110,0,10,
73 /*14*/ 105,115,0,10,
74 /*15*/ 100,110,5,15,
75 /*16*/ 110,120,10,20,
76 /*17*/ 105,115,0,10,
77 /*18*/ 100,110,5,15,
78 /*19*/ 110,120,10,20,
79 /*20*/ 100,110,0,10,
80 -1
81 };
82
main(int argc,char * argv[])83 int main(int argc __attribute__((unused)),char *argv[] __attribute__((unused)))
84 {
85 MY_INIT(argv[0]);
86 exit(run_test("rt_test"));
87 }
88
89
run_test(const char * filename)90 static int run_test(const char *filename)
91 {
92 MI_INFO *file;
93 MI_UNIQUEDEF uniquedef;
94 MI_CREATE_INFO create_info;
95 MI_COLUMNDEF recinfo[20];
96 MI_KEYDEF keyinfo[20];
97 HA_KEYSEG keyseg[20];
98 key_range range;
99
100 int silent=0;
101 int opt_unique=0;
102 int create_flag=0;
103 int key_type=HA_KEYTYPE_DOUBLE;
104 int key_length=8;
105 int null_fields=0;
106 int nrecords=sizeof(rt_data)/(sizeof(double)*4);/* 3000;*/
107 int uniques=0;
108 int i, max_i;
109 int error;
110 int row_count=0;
111 uchar record[MAX_REC_LENGTH];
112 uchar read_record[MAX_REC_LENGTH];
113 int upd= 10;
114 ha_rows hrows;
115
116 bzero(&uniquedef, sizeof(uniquedef));
117 bzero(&create_info, sizeof(create_info));
118 bzero(recinfo, sizeof(recinfo));
119 bzero(keyinfo, sizeof(keyinfo));
120 bzero(keyseg, sizeof(keyseg));
121
122 /* Define a column for NULLs and DEL markers*/
123
124 recinfo[0].type=FIELD_NORMAL;
125 recinfo[0].length=1; /* For NULL bits */
126
127 /* Define 2*ndims columns for coordinates*/
128
129 for (i=1; i<=2*ndims ;i++){
130 recinfo[i].type=FIELD_NORMAL;
131 recinfo[i].length=key_length;
132 }
133
134 /* Define a key with 2*ndims segments */
135
136 keyinfo[0].seg=keyseg;
137 keyinfo[0].keysegs=2*ndims;
138 keyinfo[0].flag=0;
139 keyinfo[0].key_alg=KEYALG;
140
141 for (i=0; i<2*ndims; i++){
142 keyinfo[0].seg[i].type= key_type;
143 keyinfo[0].seg[i].flag=0; /* Things like HA_REVERSE_SORT */
144 keyinfo[0].seg[i].start= (key_length*i)+1;
145 keyinfo[0].seg[i].length=key_length;
146 keyinfo[0].seg[i].null_bit= null_fields ? 2 : 0;
147 keyinfo[0].seg[i].null_pos=0;
148 keyinfo[0].seg[i].language=default_charset_info->number;
149 }
150
151 if (!silent)
152 printf("- Creating isam-file\n");
153
154 create_info.max_rows=10000000;
155
156 if (mi_create(filename,
157 1, /* keys */
158 keyinfo,
159 1+2*ndims+opt_unique, /* columns */
160 recinfo,uniques,&uniquedef,&create_info,create_flag))
161 goto err;
162
163 if (!silent)
164 printf("- Open isam-file\n");
165
166 if (!(file=mi_open(filename,2,HA_OPEN_ABORT_IF_LOCKED)))
167 goto err;
168
169 if (!silent)
170 printf("- Writing key:s\n");
171
172 for (i=0; i<nrecords; i++ )
173 {
174 create_record(record,i);
175 error=mi_write(file,record);
176 print_record(record,mi_position(file),"\n");
177 if (!error)
178 {
179 row_count++;
180 }
181 else
182 {
183 printf("mi_write: %d\n", error);
184 goto err;
185 }
186 }
187
188 if ((error=read_with_pos(file,silent)))
189 goto err;
190
191 if (!silent)
192 printf("- Reading rows with key\n");
193
194 for (i=0 ; i < nrecords ; i++)
195 {
196 my_errno=0;
197 create_record(record,i);
198
199 bzero((char*) read_record,MAX_REC_LENGTH);
200 error=mi_rkey(file,read_record,0,record+1,HA_WHOLE_KEY,HA_READ_MBR_EQUAL);
201
202 if (error && error!=HA_ERR_KEY_NOT_FOUND)
203 {
204 printf(" mi_rkey: %3d errno: %3d\n",error,my_errno);
205 goto err;
206 }
207 if (error == HA_ERR_KEY_NOT_FOUND)
208 {
209 print_record(record,mi_position(file)," NOT FOUND\n");
210 continue;
211 }
212 print_record(read_record,mi_position(file),"\n");
213 }
214
215 if (!silent)
216 printf("- Deleting rows\n");
217 for (i=0; i < nrecords/4; i++)
218 {
219 my_errno=0;
220 bzero((char*) read_record,MAX_REC_LENGTH);
221 error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR);
222 if (error)
223 {
224 printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno);
225 goto err;
226 }
227 print_record(read_record,mi_position(file),"\n");
228
229 error=mi_delete(file,read_record);
230 if (error)
231 {
232 printf("pos: %2d mi_delete: %3d errno: %3d\n",i,error,my_errno);
233 goto err;
234 }
235 }
236
237 if (!silent)
238 printf("- Updating rows with position\n");
239 /* We are looking for nrecords-necords/2 non-deleted records */
240 for (i=0, max_i= nrecords - nrecords/2; i < max_i ; i++)
241 {
242 my_errno=0;
243 bzero((char*) read_record,MAX_REC_LENGTH);
244 error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR);
245 if (error)
246 {
247 if (error==HA_ERR_RECORD_DELETED)
248 {
249 printf("found deleted record\n");
250 max_i++; /* don't count such record */
251 continue;
252 }
253 printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno);
254 goto err;
255 }
256 print_record(read_record,mi_position(file),"");
257 create_record(record,i+nrecords*upd);
258 printf("\t-> ");
259 print_record(record,mi_position(file),"\n");
260 error=mi_update(file,read_record,record);
261 if (error)
262 {
263 printf("pos: %2d mi_update: %3d errno: %3d\n",i,error,my_errno);
264 goto err;
265 }
266 }
267
268 if ((error=read_with_pos(file,silent)))
269 goto err;
270
271 if (!silent)
272 printf("- Test mi_rkey then a sequence of mi_rnext_same\n");
273
274 create_record(record, nrecords*4/5);
275 print_record(record,0," search for\n");
276
277 if ((error=mi_rkey(file,read_record,0,record+1,HA_WHOLE_KEY,
278 HA_READ_MBR_INTERSECT)))
279 {
280 printf("mi_rkey: %3d errno: %3d\n",error,my_errno);
281 goto err;
282 }
283 print_record(read_record,mi_position(file)," mi_rkey\n");
284 row_count=1;
285
286 for (;;)
287 {
288 if ((error=mi_rnext_same(file,read_record)))
289 {
290 if (error==HA_ERR_END_OF_FILE)
291 break;
292 printf("mi_next: %3d errno: %3d\n",error,my_errno);
293 goto err;
294 }
295 print_record(read_record,mi_position(file)," mi_rnext_same\n");
296 row_count++;
297 }
298 printf(" %d rows\n",row_count);
299
300 if (!silent)
301 printf("- Test mi_rfirst then a sequence of mi_rnext\n");
302
303 error=mi_rfirst(file,read_record,0);
304 if (error)
305 {
306 printf("mi_rfirst: %3d errno: %3d\n",error,my_errno);
307 goto err;
308 }
309 row_count=1;
310 print_record(read_record,mi_position(file)," mi_frirst\n");
311
312 for (i=0;i<nrecords;i++)
313 {
314 if ((error=mi_rnext(file,read_record,0)))
315 {
316 if (error==HA_ERR_END_OF_FILE)
317 break;
318 printf("mi_next: %3d errno: %3d\n",error,my_errno);
319 goto err;
320 }
321 print_record(read_record,mi_position(file)," mi_rnext\n");
322 row_count++;
323 }
324 printf(" %d rows\n",row_count);
325
326 if (!silent)
327 printf("- Test mi_records_in_range()\n");
328
329 create_record1(record, nrecords*4/5);
330 print_record(record,0,"\n");
331
332 range.key= record+1;
333 range.length= 1000; /* Big enough */
334 range.flag= HA_READ_MBR_INTERSECT;
335 hrows= mi_records_in_range(file, 0, &range, (key_range*) 0);
336 printf(" %ld rows\n", (long) hrows);
337
338 if (mi_close(file)) goto err;
339 my_end(MY_CHECK_ERROR);
340
341 return 0;
342
343 err:
344 printf("got error: %3d when using myisam-database\n",my_errno);
345 return 1; /* skip warning */
346 }
347
348
349
read_with_pos(MI_INFO * file,int silent)350 static int read_with_pos (MI_INFO * file,int silent)
351 {
352 int error;
353 int i;
354 uchar read_record[MAX_REC_LENGTH];
355
356 if (!silent)
357 printf("- Reading rows with position\n");
358 for (i=0;;i++)
359 {
360 my_errno=0;
361 bzero((char*) read_record,MAX_REC_LENGTH);
362 error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR);
363 if (error)
364 {
365 if (error==HA_ERR_END_OF_FILE)
366 break;
367 if (error==HA_ERR_RECORD_DELETED)
368 continue;
369 printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno);
370 return error;
371 }
372 print_record(read_record,mi_position(file),"\n");
373 }
374 return 0;
375 }
376
377
print_record(uchar * record,my_off_t offs,const char * tail)378 static void print_record(uchar * record,
379 my_off_t offs __attribute__((unused)),
380 const char * tail)
381 {
382 int i;
383 uchar * pos;
384 double c;
385
386 printf(" rec=(%d)",(unsigned char)record[0]);
387 for ( pos=record+1, i=0; i<2*ndims; i++)
388 {
389 memcpy(&c,pos,sizeof(c));
390 float8get(c,pos);
391 printf(" %.14g ",c);
392 pos+=sizeof(c);
393 }
394 printf("pos=%ld",(long int)offs);
395 printf("%s",tail);
396 }
397
398
399
create_record1(uchar * record,uint rownr)400 static void create_record1(uchar *record,uint rownr)
401 {
402 int i;
403 uchar * pos;
404 double c=rownr+10;
405
406 bzero((char*) record,MAX_REC_LENGTH);
407 record[0]=0x01; /* DEL marker */
408
409 for (pos=record+1, i=0; i<2*ndims; i++)
410 {
411 memcpy(pos,&c,sizeof(c));
412 float8store(pos,c);
413 pos+=sizeof(c);
414 }
415 }
416
417
create_record(uchar * record,uint rownr)418 static void create_record(uchar *record,uint rownr)
419 {
420 int i;
421 uchar *pos;
422 double *data= rt_data+rownr*4;
423 record[0]=0x01; /* DEL marker */
424 for (pos=record+1, i=0; i<ndims*2; i++)
425 {
426 float8store(pos,data[i]);
427 pos+=8;
428 }
429 }
430
431 #else
main(int argc,char * argv[])432 int main(int argc __attribute__((unused)),char *argv[] __attribute__((unused)))
433 {
434 exit(0);
435 }
436 #endif /*HAVE_RTREE_KEYS*/
437
438 #include "mi_extrafunc.h"
439