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   page_range pages;
116 
117   bzero(&uniquedef, sizeof(uniquedef));
118   bzero(&create_info, sizeof(create_info));
119   bzero(recinfo, sizeof(recinfo));
120   bzero(keyinfo, sizeof(keyinfo));
121   bzero(keyseg, sizeof(keyseg));
122 
123   /* Define a column for NULLs and DEL markers*/
124 
125   recinfo[0].type=FIELD_NORMAL;
126   recinfo[0].length=1; /* For NULL bits */
127 
128   /* Define 2*ndims columns for coordinates*/
129 
130   for (i=1; i<=2*ndims ;i++){
131     recinfo[i].type=FIELD_NORMAL;
132     recinfo[i].length=key_length;
133   }
134 
135   /* Define a key with 2*ndims segments */
136 
137   keyinfo[0].seg=keyseg;
138   keyinfo[0].keysegs=2*ndims;
139   keyinfo[0].flag=0;
140   keyinfo[0].key_alg=KEYALG;
141 
142   for (i=0; i<2*ndims; i++){
143     keyinfo[0].seg[i].type= key_type;
144     keyinfo[0].seg[i].flag=0;          /* Things like HA_REVERSE_SORT */
145     keyinfo[0].seg[i].start= (key_length*i)+1;
146     keyinfo[0].seg[i].length=key_length;
147     keyinfo[0].seg[i].null_bit= null_fields ? 2 : 0;
148     keyinfo[0].seg[i].null_pos=0;
149     keyinfo[0].seg[i].language=default_charset_info->number;
150   }
151 
152   if (!silent)
153     printf("- Creating isam-file\n");
154 
155   create_info.max_rows=10000000;
156 
157   if (mi_create(filename,
158                 1,            /*  keys   */
159                 keyinfo,
160                 1+2*ndims+opt_unique, /* columns */
161                 recinfo,uniques,&uniquedef,&create_info,create_flag))
162     goto err;
163 
164   if (!silent)
165     printf("- Open isam-file\n");
166 
167   if (!(file=mi_open(filename,2,HA_OPEN_ABORT_IF_LOCKED)))
168     goto err;
169 
170   if (!silent)
171     printf("- Writing key:s\n");
172 
173   for (i=0; i<nrecords; i++ )
174   {
175     create_record(record,i);
176     error=mi_write(file,record);
177     print_record(record,mi_position(file),"\n");
178     if (!error)
179     {
180       row_count++;
181     }
182     else
183     {
184       printf("mi_write: %d\n", error);
185       goto err;
186     }
187   }
188 
189   if ((error=read_with_pos(file,silent)))
190     goto err;
191 
192   if (!silent)
193     printf("- Reading rows with key\n");
194 
195   for (i=0 ; i < nrecords ; i++)
196   {
197     my_errno=0;
198     create_record(record,i);
199 
200     bzero((char*) read_record,MAX_REC_LENGTH);
201     error=mi_rkey(file,read_record,0,record+1,HA_WHOLE_KEY,HA_READ_MBR_EQUAL);
202 
203     if (error && error!=HA_ERR_KEY_NOT_FOUND)
204     {
205       printf("     mi_rkey: %3d  errno: %3d\n",error,my_errno);
206       goto err;
207     }
208     if (error == HA_ERR_KEY_NOT_FOUND)
209     {
210       print_record(record,mi_position(file),"  NOT FOUND\n");
211       continue;
212     }
213     print_record(read_record,mi_position(file),"\n");
214   }
215 
216   if (!silent)
217     printf("- Deleting rows\n");
218   for (i=0; i < nrecords/4; i++)
219   {
220     my_errno=0;
221     bzero((char*) read_record,MAX_REC_LENGTH);
222     error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR);
223     if (error)
224     {
225       printf("pos: %2d  mi_rrnd: %3d  errno: %3d\n",i,error,my_errno);
226       goto err;
227     }
228     print_record(read_record,mi_position(file),"\n");
229 
230     error=mi_delete(file,read_record);
231     if (error)
232     {
233       printf("pos: %2d mi_delete: %3d errno: %3d\n",i,error,my_errno);
234       goto err;
235     }
236   }
237 
238   if (!silent)
239     printf("- Updating rows with position\n");
240   /* We are looking for nrecords-necords/2 non-deleted records */
241   for (i=0, max_i= nrecords - nrecords/2; i < max_i ; i++)
242   {
243     my_errno=0;
244     bzero((char*) read_record,MAX_REC_LENGTH);
245     error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR);
246     if (error)
247     {
248       if (error==HA_ERR_RECORD_DELETED)
249       {
250         printf("found deleted record\n");
251         max_i++; /* don't count such record */
252         continue;
253       }
254       printf("pos: %2d  mi_rrnd: %3d  errno: %3d\n",i,error,my_errno);
255       goto err;
256     }
257     print_record(read_record,mi_position(file),"");
258     create_record(record,i+nrecords*upd);
259     printf("\t-> ");
260     print_record(record,mi_position(file),"\n");
261     error=mi_update(file,read_record,record);
262     if (error)
263     {
264       printf("pos: %2d  mi_update: %3d  errno: %3d\n",i,error,my_errno);
265       goto err;
266     }
267   }
268 
269   if ((error=read_with_pos(file,silent)))
270     goto err;
271 
272   if (!silent)
273     printf("- Test mi_rkey then a sequence of mi_rnext_same\n");
274 
275   create_record(record, nrecords*4/5);
276   print_record(record,0,"  search for\n");
277 
278   if ((error=mi_rkey(file,read_record,0,record+1,HA_WHOLE_KEY,
279                      HA_READ_MBR_INTERSECT)))
280   {
281     printf("mi_rkey: %3d  errno: %3d\n",error,my_errno);
282     goto err;
283   }
284   print_record(read_record,mi_position(file),"  mi_rkey\n");
285   row_count=1;
286 
287   for (;;)
288   {
289     if ((error=mi_rnext_same(file,read_record)))
290     {
291       if (error==HA_ERR_END_OF_FILE)
292         break;
293       printf("mi_next: %3d  errno: %3d\n",error,my_errno);
294       goto err;
295     }
296     print_record(read_record,mi_position(file),"  mi_rnext_same\n");
297       row_count++;
298   }
299   printf("     %d rows\n",row_count);
300 
301   if (!silent)
302     printf("- Test mi_rfirst then a sequence of mi_rnext\n");
303 
304   error=mi_rfirst(file,read_record,0);
305   if (error)
306   {
307     printf("mi_rfirst: %3d  errno: %3d\n",error,my_errno);
308     goto err;
309   }
310   row_count=1;
311   print_record(read_record,mi_position(file),"  mi_frirst\n");
312 
313   for (i=0;i<nrecords;i++)
314   {
315     if ((error=mi_rnext(file,read_record,0)))
316     {
317       if (error==HA_ERR_END_OF_FILE)
318         break;
319       printf("mi_next: %3d  errno: %3d\n",error,my_errno);
320       goto err;
321     }
322     print_record(read_record,mi_position(file),"  mi_rnext\n");
323     row_count++;
324   }
325   printf("     %d rows\n",row_count);
326 
327   if (!silent)
328     printf("- Test mi_records_in_range()\n");
329 
330   create_record1(record, nrecords*4/5);
331   print_record(record,0,"\n");
332 
333   range.key= record+1;
334   range.length= 1000;                           /* Big enough */
335   range.flag= HA_READ_MBR_INTERSECT;
336   hrows= mi_records_in_range(file, 0, &range, (key_range*) 0, &pages);
337   printf("     %ld rows\n", (long) hrows);
338 
339   if (mi_close(file)) goto err;
340   my_end(MY_CHECK_ERROR);
341 
342   return 0;
343 
344 err:
345   printf("got error: %3d when using myisam-database\n",my_errno);
346   return 1;           /* skip warning */
347 }
348 
349 
350 
read_with_pos(MI_INFO * file,int silent)351 static int read_with_pos (MI_INFO * file,int silent)
352 {
353   int error;
354   int i;
355   uchar read_record[MAX_REC_LENGTH];
356 
357   if (!silent)
358     printf("- Reading rows with position\n");
359   for (i=0;;i++)
360   {
361     my_errno=0;
362     bzero((char*) read_record,MAX_REC_LENGTH);
363     error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR);
364     if (error)
365     {
366       if (error==HA_ERR_END_OF_FILE)
367         break;
368       if (error==HA_ERR_RECORD_DELETED)
369         continue;
370       printf("pos: %2d  mi_rrnd: %3d  errno: %3d\n",i,error,my_errno);
371       return error;
372     }
373     print_record(read_record,mi_position(file),"\n");
374   }
375   return 0;
376 }
377 
378 
print_record(uchar * record,my_off_t offs,const char * tail)379 static void print_record(uchar * record,
380 			 my_off_t offs __attribute__((unused)),
381 			 const char * tail)
382 {
383   int i;
384   uchar * pos;
385   double c;
386 
387   printf("     rec=(%d)",(unsigned char)record[0]);
388   for ( pos=record+1, i=0; i<2*ndims; i++)
389    {
390       memcpy(&c,pos,sizeof(c));
391       float8get(c,pos);
392       printf(" %.14g ",c);
393       pos+=sizeof(c);
394    }
395    printf("pos=%ld",(long int)offs);
396    printf("%s",tail);
397 }
398 
399 
400 
create_record1(uchar * record,uint rownr)401 static void create_record1(uchar *record,uint rownr)
402 {
403    int i;
404    uchar * pos;
405    double c=rownr+10;
406 
407    bzero((char*) record,MAX_REC_LENGTH);
408    record[0]=0x01; /* DEL marker */
409 
410    for (pos=record+1, i=0; i<2*ndims; i++)
411    {
412       memcpy(pos,&c,sizeof(c));
413       float8store(pos,c);
414       pos+=sizeof(c);
415    }
416 }
417 
418 
create_record(uchar * record,uint rownr)419 static void create_record(uchar *record,uint rownr)
420 {
421    int i;
422    uchar *pos;
423    double *data= rt_data+rownr*4;
424    record[0]=0x01; /* DEL marker */
425    for (pos=record+1, i=0; i<ndims*2; i++)
426    {
427      float8store(pos,data[i]);
428      pos+=8;
429    }
430 }
431 
432 #else
main(int argc,char * argv[])433 int main(int argc __attribute__((unused)),char *argv[] __attribute__((unused)))
434 {
435   exit(0);
436 }
437 #endif /*HAVE_RTREE_KEYS*/
438 
439 #include "mi_extrafunc.h"
440