24 #define MAX_REC_LENGTH 1024
25 #define KEYALG HA_KEY_ALG_RTREE
27 static void create_linestring(uchar *
record,uint rownr);
28 static void print_record(uchar *
record,my_off_t offs,
const char * tail);
30 static void create_key(uchar *key,uint rownr);
31 static void print_key(
const uchar *key,
const char * tail);
33 static int run_test(
const char *filename);
34 static int read_with_pos(
MI_INFO *
file,
int silent);
36 static int rtree_CreateLineStringWKB(
double *ords, uint n_dims, uint n_points,
38 static void rtree_PrintWKB(uchar *wkb, uint n_dims);
40 static char blob_key[MAX_REC_LENGTH];
43 int main(
int argc __attribute__((unused)),
char *argv[])
46 exit(run_test(
"sp_test"));
50 int run_test(
const char *filename)
67 uchar
record[MAX_REC_LENGTH];
68 uchar key[MAX_REC_LENGTH];
69 uchar read_record[MAX_REC_LENGTH];
75 recinfo[0].type=FIELD_NORMAL;
81 recinfo[1].type=FIELD_BLOB;
82 recinfo[1].length=4 + portable_sizeof_char_ptr;
88 keyinfo[0].seg=keyseg;
90 keyinfo[0].flag=HA_SPATIAL;
91 keyinfo[0].key_alg=KEYALG;
93 keyinfo[0].seg[0].type= HA_KEYTYPE_BINARY;
94 keyinfo[0].seg[0].flag=0;
95 keyinfo[0].seg[0].start= 1;
96 keyinfo[0].seg[0].length=1;
97 keyinfo[0].seg[0].null_bit= null_fields ? 2 : 0;
98 keyinfo[0].seg[0].null_pos=0;
99 keyinfo[0].seg[0].language=default_charset_info->number;
100 keyinfo[0].seg[0].bit_start=4;
104 printf(
"- Creating isam-file\n");
106 memset(&create_info, 0,
sizeof(create_info));
107 create_info.max_rows=10000000;
109 if (mi_create(filename,
113 recinfo,uniques,&uniquedef,&create_info,create_flag))
117 printf(
"- Open isam-file\n");
119 if (!(file=mi_open(filename,2,HA_OPEN_ABORT_IF_LOCKED)))
123 printf(
"- Writing key:s\n");
125 for (i=0; i<nrecords; i++ )
127 create_linestring(record,i);
128 error=mi_write(file,record);
129 print_record(record,mi_position(file),
"\n");
136 printf(
"mi_write: %d\n", error);
141 if ((error=read_with_pos(file,silent)))
145 printf(
"- Deleting rows with position\n");
146 for (i=0; i < nrecords/4; i++)
149 memset(read_record, 0, MAX_REC_LENGTH);
150 error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR);
153 printf(
"pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno);
156 print_record(read_record,mi_position(file),
"\n");
157 error=mi_delete(file,read_record);
160 printf(
"pos: %2d mi_delete: %3d errno: %3d\n",i,error,my_errno);
166 printf(
"- Updating rows with position\n");
167 for (i=0; i < nrecords/2 ; i++)
170 memset(read_record, 0, MAX_REC_LENGTH);
171 error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR);
174 if (error==HA_ERR_RECORD_DELETED)
176 printf(
"pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno);
179 print_record(read_record,mi_position(file),
"");
180 create_linestring(record,i+nrecords*upd);
182 print_record(record,mi_position(file),
"\n");
183 error=mi_update(file,read_record,record);
186 printf(
"pos: %2d mi_update: %3d errno: %3d\n",i,error,my_errno);
191 if ((error=read_with_pos(file,silent)))
195 printf(
"- Test mi_rkey then a sequence of mi_rnext_same\n");
197 create_key(key, nrecords*4/5);
198 print_key(key,
" search for INTERSECT\n");
200 if ((error=mi_rkey(file,read_record,0,key,0,HA_READ_MBR_INTERSECT)))
202 printf(
"mi_rkey: %3d errno: %3d\n",error,my_errno);
205 print_record(read_record,mi_position(file),
" mi_rkey\n");
210 if ((error=mi_rnext_same(file,read_record)))
212 if (error==HA_ERR_END_OF_FILE)
214 printf(
"mi_next: %3d errno: %3d\n",error,my_errno);
217 print_record(read_record,mi_position(file),
" mi_rnext_same\n");
220 printf(
" %d rows\n",row_count);
223 printf(
"- Test mi_rfirst then a sequence of mi_rnext\n");
225 error=mi_rfirst(file,read_record,0);
228 printf(
"mi_rfirst: %3d errno: %3d\n",error,my_errno);
232 print_record(read_record,mi_position(file),
" mi_frirst\n");
234 for(i=0;i<nrecords;i++) {
235 if ((error=mi_rnext(file,read_record,0)))
237 if (error==HA_ERR_END_OF_FILE)
239 printf(
"mi_next: %3d errno: %3d\n",error,my_errno);
242 print_record(read_record,mi_position(file),
" mi_rnext\n");
245 printf(
" %d rows\n",row_count);
248 printf(
"- Test mi_records_in_range()\n");
250 create_key(key, nrecords*upd);
251 print_key(key,
" INTERSECT\n");
253 min_range.length= 1000;
254 min_range.flag= HA_READ_MBR_INTERSECT;
255 max_range.key= record+1;
256 max_range.length= 1000;
257 max_range.flag= HA_READ_KEY_EXACT;
258 hrows= mi_records_in_range(file, 0, &min_range, &max_range);
259 printf(
" %ld rows\n", (
long) hrows);
261 if (mi_close(file))
goto err;
262 my_end(MY_CHECK_ERROR);
266 printf(
"got error: %3d when using myisam-database\n",my_errno);
271 static int read_with_pos (
MI_INFO * file,
int silent)
275 uchar read_record[MAX_REC_LENGTH];
279 printf(
"- Reading rows with position\n");
283 memset(read_record, 0, MAX_REC_LENGTH);
284 error=mi_rrnd(file,read_record,i == 0 ? 0L : HA_OFFSET_ERROR);
287 if (error==HA_ERR_END_OF_FILE)
289 if (error==HA_ERR_RECORD_DELETED)
291 printf(
"pos: %2d mi_rrnd: %3d errno: %3d\n",i,error,my_errno);
295 print_record(read_record,mi_position(file),
"\n");
297 printf(
" %d rows\n",rows);
302 static void print_record(uchar * record, my_off_t offs,
const char * tail)
308 printf(
" rec=(%d)",(
unsigned char)record[0]);
312 printf(
" len=%d ",len);
313 memcpy(&ptr, pos,
sizeof(
char*));
315 rtree_PrintWKB((uchar*) ptr,SPDIMS);
318 printf(
" offs=%ld ",(
long int)offs);
323 static void create_linestring(uchar *record,uint rownr)
332 for(j=0;j<npoints;j++)
333 for(i=0;i<SPDIMS;i++)
334 x[i+j*SPDIMS]=rownr*j;
336 memset(record, 0, MAX_REC_LENGTH);
340 memset(blob_key,0,
sizeof(blob_key));
341 tmp=rtree_CreateLineStringWKB(x,SPDIMS,npoints, (uchar*) blob_key);
347 memcpy(pos, &ptr,
sizeof(
char*));
351 static void create_key(uchar *key,uint rownr)
357 memset(key, 0, MAX_REC_LENGTH);
358 for (pos=key, i=0; i<2*SPDIMS; i++)
365 static void print_key(
const uchar *key,
const char * tail)
371 for (i=0; i<2*SPDIMS; i++)
381 static int rtree_CreateLineStringWKB(
double *ords, uint n_dims, uint n_points,
385 uint n_ords = n_dims * n_points;
389 int4store(wkb, wkbLineString);
391 int4store(wkb, n_points);
393 for (i=0; i < n_ords; ++
i)
395 float8store(wkb, ords[i]);
398 return 9 + n_points * n_dims * 8;
402 static void rtree_PrintWKB(uchar *wkb, uint n_dims)
407 wkb_type = uint4korr(wkb);
410 switch ((
enum wkbType)wkb_type)
418 for (i=0; i < n_dims; ++
i)
422 printf(
"%.14g", ord);
436 printf(
"LineString(");
437 n_points = uint4korr(wkb);
439 for (p=0; p < n_points; ++p)
441 for (i=0; i < n_dims; ++
i)
445 printf(
"%.14g", ord);
449 if (p < n_points - 1)
458 printf(
"POLYGON(...)");
463 printf(
"MULTIPOINT(...)");
466 case wkbMultiLineString:
468 printf(
"MULTILINESTRING(...)");
471 case wkbMultiPolygon:
473 printf(
"MULTIPOLYGON(...)");
476 case wkbGeometryCollection:
478 printf(
"GEOMETRYCOLLECTION(...)");
483 printf(
"UNKNOWN GEOMETRY TYPE");
490 int main(
int argc __attribute__((unused)),
char *argv[] __attribute__((unused)))
496 #include "mi_extrafunc.h"