1 /* Copyright (c) 2002-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 /* Testing of the basic functions of a MyISAM rtree table */
17 /* Written by Alex Barkov who has a shared copyright to this code */
22 #ifdef HAVE_RTREE_KEYS
26 #define MAX_REC_LENGTH 1024
28 #define KEYALG HA_KEY_ALG_RTREE
30 static int read_with_pos(MI_INFO
* file
, int silent
);
31 static void create_record(uchar
*record
,uint rownr
);
32 static void create_record1(uchar
*record
,uint rownr
);
33 static void print_record(uchar
* record
,my_off_t offs
,const char * tail
);
34 static int run_test(const char *filename
);
36 static double rt_data
[]=
82 int main(int argc
__attribute__((unused
)),char *argv
[] __attribute__((unused
)))
85 exit(run_test("rt_test"));
89 static int run_test(const char *filename
)
92 MI_UNIQUEDEF uniquedef
;
93 MI_CREATE_INFO create_info
;
94 MI_COLUMNDEF recinfo
[20];
95 MI_KEYDEF keyinfo
[20];
102 int key_type
=HA_KEYTYPE_DOUBLE
;
105 int nrecords
=sizeof(rt_data
)/(sizeof(double)*4);/* 3000;*/
111 uchar record
[MAX_REC_LENGTH
];
112 uchar read_record
[MAX_REC_LENGTH
];
116 /* Define a column for NULLs and DEL markers*/
118 recinfo
[0].type
=FIELD_NORMAL
;
119 recinfo
[0].length
=1; /* For NULL bits */
122 /* Define 2*ndims columns for coordinates*/
124 for (i
=1; i
<=2*ndims
;i
++){
125 recinfo
[i
].type
=FIELD_NORMAL
;
126 recinfo
[i
].length
=key_length
;
127 rec_length
+=key_length
;
130 /* Define a key with 2*ndims segments */
132 keyinfo
[0].seg
=keyseg
;
133 keyinfo
[0].keysegs
=2*ndims
;
135 keyinfo
[0].key_alg
=KEYALG
;
137 for (i
=0; i
<2*ndims
; i
++){
138 keyinfo
[0].seg
[i
].type
= key_type
;
139 keyinfo
[0].seg
[i
].flag
=0; /* Things like HA_REVERSE_SORT */
140 keyinfo
[0].seg
[i
].start
= (key_length
*i
)+1;
141 keyinfo
[0].seg
[i
].length
=key_length
;
142 keyinfo
[0].seg
[i
].null_bit
= null_fields
? 2 : 0;
143 keyinfo
[0].seg
[i
].null_pos
=0;
144 keyinfo
[0].seg
[i
].language
=default_charset_info
->number
;
148 printf("- Creating isam-file\n");
150 bzero((char*) &create_info
,sizeof(create_info
));
151 create_info
.max_rows
=10000000;
153 if (mi_create(filename
,
156 1+2*ndims
+opt_unique
, /* columns */
157 recinfo
,uniques
,&uniquedef
,&create_info
,create_flag
))
161 printf("- Open isam-file\n");
163 if (!(file
=mi_open(filename
,2,HA_OPEN_ABORT_IF_LOCKED
)))
167 printf("- Writing key:s\n");
169 for (i
=0; i
<nrecords
; i
++ )
171 create_record(record
,i
);
172 error
=mi_write(file
,record
);
173 print_record(record
,mi_position(file
),"\n");
180 printf("mi_write: %d\n", error
);
185 if ((error
=read_with_pos(file
,silent
)))
189 printf("- Reading rows with key\n");
191 for (i
=0 ; i
< nrecords
; i
++)
194 create_record(record
,i
);
196 bzero((char*) read_record
,MAX_REC_LENGTH
);
197 error
=mi_rkey(file
,read_record
,0,record
+1,0,HA_READ_MBR_EQUAL
);
199 if (error
&& error
!=HA_ERR_KEY_NOT_FOUND
)
201 printf(" mi_rkey: %3d errno: %3d\n",error
,my_errno
);
204 if (error
== HA_ERR_KEY_NOT_FOUND
)
206 print_record(record
,mi_position(file
)," NOT FOUND\n");
209 print_record(read_record
,mi_position(file
),"\n");
213 printf("- Deleting rows\n");
214 for (i
=0; i
< nrecords
/4; i
++)
217 bzero((char*) read_record
,MAX_REC_LENGTH
);
218 error
=mi_rrnd(file
,read_record
,i
== 0 ? 0L : HA_OFFSET_ERROR
);
221 printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i
,error
,my_errno
);
224 print_record(read_record
,mi_position(file
),"\n");
226 error
=mi_delete(file
,read_record
);
229 printf("pos: %2d mi_delete: %3d errno: %3d\n",i
,error
,my_errno
);
235 printf("- Updating rows with position\n");
236 for (i
=0; i
< (nrecords
- nrecords
/4) ; i
++)
239 bzero((char*) read_record
,MAX_REC_LENGTH
);
240 error
=mi_rrnd(file
,read_record
,i
== 0 ? 0L : HA_OFFSET_ERROR
);
243 if (error
==HA_ERR_RECORD_DELETED
)
245 printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i
,error
,my_errno
);
248 print_record(read_record
,mi_position(file
),"");
249 create_record(record
,i
+nrecords
*upd
);
251 print_record(record
,mi_position(file
),"\n");
252 error
=mi_update(file
,read_record
,record
);
255 printf("pos: %2d mi_update: %3d errno: %3d\n",i
,error
,my_errno
);
260 if ((error
=read_with_pos(file
,silent
)))
264 printf("- Test mi_rkey then a sequence of mi_rnext_same\n");
266 create_record(record
, nrecords
*4/5);
267 print_record(record
,0," search for\n");
269 if ((error
=mi_rkey(file
,read_record
,0,record
+1,0,HA_READ_MBR_INTERSECT
)))
271 printf("mi_rkey: %3d errno: %3d\n",error
,my_errno
);
274 print_record(read_record
,mi_position(file
)," mi_rkey\n");
279 if ((error
=mi_rnext_same(file
,read_record
)))
281 if (error
==HA_ERR_END_OF_FILE
)
283 printf("mi_next: %3d errno: %3d\n",error
,my_errno
);
286 print_record(read_record
,mi_position(file
)," mi_rnext_same\n");
289 printf(" %d rows\n",row_count
);
292 printf("- Test mi_rfirst then a sequence of mi_rnext\n");
294 error
=mi_rfirst(file
,read_record
,0);
297 printf("mi_rfirst: %3d errno: %3d\n",error
,my_errno
);
301 print_record(read_record
,mi_position(file
)," mi_frirst\n");
303 for (i
=0;i
<nrecords
;i
++)
305 if ((error
=mi_rnext(file
,read_record
,0)))
307 if (error
==HA_ERR_END_OF_FILE
)
309 printf("mi_next: %3d errno: %3d\n",error
,my_errno
);
312 print_record(read_record
,mi_position(file
)," mi_rnext\n");
315 printf(" %d rows\n",row_count
);
318 printf("- Test mi_records_in_range()\n");
320 create_record1(record
, nrecords
*4/5);
321 print_record(record
,0,"\n");
324 range
.length
= 1000; /* Big enough */
325 range
.flag
= HA_READ_MBR_INTERSECT
;
326 hrows
= mi_records_in_range(file
, 0, &range
, (key_range
*) 0);
327 printf(" %ld rows\n", (long) hrows
);
329 if (mi_close(file
)) goto err
;
330 my_end(MY_CHECK_ERROR
);
335 printf("got error: %3d when using myisam-database\n",my_errno
);
336 return 1; /* skip warning */
341 static int read_with_pos (MI_INFO
* file
,int silent
)
345 uchar read_record
[MAX_REC_LENGTH
];
348 printf("- Reading rows with position\n");
352 bzero((char*) read_record
,MAX_REC_LENGTH
);
353 error
=mi_rrnd(file
,read_record
,i
== 0 ? 0L : HA_OFFSET_ERROR
);
356 if (error
==HA_ERR_END_OF_FILE
)
358 if (error
==HA_ERR_RECORD_DELETED
)
360 printf("pos: %2d mi_rrnd: %3d errno: %3d\n",i
,error
,my_errno
);
363 print_record(read_record
,mi_position(file
),"\n");
370 static void bprint_record(char * record
,
371 my_off_t offs
__attribute__((unused
)),
376 i
=(unsigned char)record
[0];
379 for( pos
=record
+1, i
=0; i
<32; i
++,pos
++){
380 int b
=(unsigned char)*pos
;
388 static void print_record(uchar
* record
,
389 my_off_t offs
__attribute__((unused
)),
396 printf(" rec=(%d)",(unsigned char)record
[0]);
397 for ( pos
=record
+1, i
=0; i
<2*ndims
; i
++)
399 memcpy(&c
,pos
,sizeof(c
));
404 printf("pos=%ld",(long int)offs
);
410 static void create_record1(uchar
*record
,uint rownr
)
416 bzero((char*) record
,MAX_REC_LENGTH
);
417 record
[0]=0x01; /* DEL marker */
419 for (pos
=record
+1, i
=0; i
<2*ndims
; i
++)
421 memcpy(pos
,&c
,sizeof(c
));
429 static void create_record0(uchar
*record
,uint rownr
)
436 bzero((char*) record
,MAX_REC_LENGTH
);
437 record
[0]=0x01; /* DEL marker */
439 for ( pos
=record
+1, i
=0; i
<ndims
; i
++)
441 memcpy(pos
,&c0
,sizeof(c0
));
444 memcpy(pos
,&c
,sizeof(c
));
452 static void create_record(uchar
*record
,uint rownr
)
456 double *data
= rt_data
+rownr
*4;
457 record
[0]=0x01; /* DEL marker */
458 for (pos
=record
+1, i
=0; i
<ndims
*2; i
++)
460 float8store(pos
,data
[i
]);
466 int main(int argc
__attribute__((unused
)),char *argv
[] __attribute__((unused
)))
470 #endif /*HAVE_RTREE_KEYS*/