1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
|
/* Copyright (C) 2006 MySQL AB & Ramil Kalimullin & MySQL Finland AB
& TCX DataKonsult AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA */
#include "maria_def.h"
#ifdef HAVE_RTREE_KEYS
#include "ma_rt_index.h"
#include "ma_rt_mbr.h"
#define INTERSECT_CMP(amin, amax, bmin, bmax) ((amin > bmax) || (bmin > amax))
#define CONTAIN_CMP(amin, amax, bmin, bmax) ((bmin > amin) || (bmax < amax))
#define WITHIN_CMP(amin, amax, bmin, bmax) ((amin > bmin) || (amax < bmax))
#define DISJOINT_CMP(amin, amax, bmin, bmax) ((amin <= bmax) && (bmin <= amax))
#define EQUAL_CMP(amin, amax, bmin, bmax) ((amin != bmin) || (amax != bmax))
#define FCMP(A, B) ((int)(A) - (int)(B))
#define p_inc(A, B, X) {A += X; B += X;}
#define RT_CMP(nextflag) \
if (nextflag & MBR_INTERSECT) \
{ \
if (INTERSECT_CMP(amin, amax, bmin, bmax)) \
return 1; \
} \
else if (nextflag & MBR_CONTAIN) \
{ \
if (CONTAIN_CMP(amin, amax, bmin, bmax)) \
return 1; \
} \
else if (nextflag & MBR_WITHIN) \
{ \
if (WITHIN_CMP(amin, amax, bmin, bmax)) \
return 1; \
} \
else if (nextflag & MBR_EQUAL) \
{ \
if (EQUAL_CMP(amin, amax, bmin, bmax)) \
return 1; \
} \
else if (nextflag & MBR_DISJOINT) \
{ \
if (DISJOINT_CMP(amin, amax, bmin, bmax)) \
return 1; \
}\
else /* if unknown comparison operator */ \
{ \
DBUG_ASSERT(0); \
}
#define RT_CMP_KORR(type, korr_func, len, nextflag) \
{ \
type amin, amax, bmin, bmax; \
amin= korr_func(a); \
bmin= korr_func(b); \
amax= korr_func(a+len); \
bmax= korr_func(b+len); \
RT_CMP(nextflag); \
}
#define RT_CMP_GET(type, get_func, len, nextflag) \
{ \
type amin, amax, bmin, bmax; \
get_func(amin, a); \
get_func(bmin, b); \
get_func(amax, a+len); \
get_func(bmax, b+len); \
RT_CMP(nextflag); \
}
/*
Compares two keys a and b depending on nextflag
nextflag can contain these flags:
MBR_INTERSECT(a,b) a overlaps b
MBR_CONTAIN(a,b) a contains b
MBR_DISJOINT(a,b) a disjoint b
MBR_WITHIN(a,b) a within b
MBR_EQUAL(a,b) All coordinates of MBRs are equal
MBR_DATA(a,b) Data reference is the same
Returns 0 on success.
*/
int maria_rtree_key_cmp(HA_KEYSEG *keyseg, const uchar *b, const uchar *a,
uint key_length, uint32 nextflag)
{
for (; (int) key_length > 0; keyseg += 2 )
{
uint32 keyseg_length;
switch ((enum ha_base_keytype) keyseg->type) {
case HA_KEYTYPE_INT8:
RT_CMP_KORR(int8, mi_sint1korr, 1, nextflag);
break;
case HA_KEYTYPE_BINARY:
RT_CMP_KORR(uint8, mi_uint1korr, 1, nextflag);
break;
case HA_KEYTYPE_SHORT_INT:
RT_CMP_KORR(int16, mi_sint2korr, 2, nextflag);
break;
case HA_KEYTYPE_USHORT_INT:
RT_CMP_KORR(uint16, mi_uint2korr, 2, nextflag);
break;
case HA_KEYTYPE_INT24:
RT_CMP_KORR(int32, mi_sint3korr, 3, nextflag);
break;
case HA_KEYTYPE_UINT24:
RT_CMP_KORR(uint32, mi_uint3korr, 3, nextflag);
break;
case HA_KEYTYPE_LONG_INT:
RT_CMP_KORR(int32, mi_sint4korr, 4, nextflag);
break;
case HA_KEYTYPE_ULONG_INT:
RT_CMP_KORR(uint32, mi_uint4korr, 4, nextflag);
break;
#ifdef HAVE_LONG_LONG
case HA_KEYTYPE_LONGLONG:
RT_CMP_KORR(longlong, mi_sint8korr, 8, nextflag)
break;
case HA_KEYTYPE_ULONGLONG:
RT_CMP_KORR(ulonglong, mi_uint8korr, 8, nextflag)
break;
#endif
case HA_KEYTYPE_FLOAT:
/* The following should be safe, even if we compare doubles */
RT_CMP_GET(float, mi_float4get, 4, nextflag);
break;
case HA_KEYTYPE_DOUBLE:
RT_CMP_GET(double, mi_float8get, 8, nextflag);
break;
case HA_KEYTYPE_END:
goto end;
default:
return 1;
}
keyseg_length= keyseg->length * 2;
key_length-= keyseg_length;
a+= keyseg_length;
b+= keyseg_length;
}
end:
if (nextflag & MBR_DATA)
{
const uchar *end= a + keyseg->length;
do
{
if (*a++ != *b++)
return FCMP(a[-1], b[-1]);
} while (a != end);
}
return 0;
}
#define RT_VOL_KORR(type, korr_func, len, cast) \
{ \
type amin, amax; \
amin= korr_func(a); \
amax= korr_func(a+len); \
res *= (cast(amax) - cast(amin)); \
}
#define RT_VOL_GET(type, get_func, len, cast) \
{ \
type amin, amax; \
get_func(amin, a); \
get_func(amax, a+len); \
res *= (cast(amax) - cast(amin)); \
}
/*
Calculates rectangle volume
*/
double maria_rtree_rect_volume(HA_KEYSEG *keyseg, uchar *a, uint key_length)
{
double res= 1;
for (; (int)key_length > 0; keyseg += 2)
{
uint32 keyseg_length;
switch ((enum ha_base_keytype) keyseg->type) {
case HA_KEYTYPE_INT8:
RT_VOL_KORR(int8, mi_sint1korr, 1, (double));
break;
case HA_KEYTYPE_BINARY:
RT_VOL_KORR(uint8, mi_uint1korr, 1, (double));
break;
case HA_KEYTYPE_SHORT_INT:
RT_VOL_KORR(int16, mi_sint2korr, 2, (double));
break;
case HA_KEYTYPE_USHORT_INT:
RT_VOL_KORR(uint16, mi_uint2korr, 2, (double));
break;
case HA_KEYTYPE_INT24:
RT_VOL_KORR(int32, mi_sint3korr, 3, (double));
break;
case HA_KEYTYPE_UINT24:
RT_VOL_KORR(uint32, mi_uint3korr, 3, (double));
break;
case HA_KEYTYPE_LONG_INT:
RT_VOL_KORR(int32, mi_sint4korr, 4, (double));
break;
case HA_KEYTYPE_ULONG_INT:
RT_VOL_KORR(uint32, mi_uint4korr, 4, (double));
break;
#ifdef HAVE_LONG_LONG
case HA_KEYTYPE_LONGLONG:
RT_VOL_KORR(longlong, mi_sint8korr, 8, (double));
break;
case HA_KEYTYPE_ULONGLONG:
RT_VOL_KORR(longlong, mi_sint8korr, 8, ulonglong2double);
break;
#endif
case HA_KEYTYPE_FLOAT:
RT_VOL_GET(float, mi_float4get, 4, (double));
break;
case HA_KEYTYPE_DOUBLE:
RT_VOL_GET(double, mi_float8get, 8, (double));
break;
case HA_KEYTYPE_END:
key_length= 0;
break;
default:
return -1;
}
keyseg_length= keyseg->length * 2;
key_length-= keyseg_length;
a+= keyseg_length;
}
return res;
}
#define RT_D_MBR_KORR(type, korr_func, len, cast) \
{ \
type amin, amax; \
amin= korr_func(a); \
amax= korr_func(a+len); \
*res++= cast(amin); \
*res++= cast(amax); \
}
#define RT_D_MBR_GET(type, get_func, len, cast) \
{ \
type amin, amax; \
get_func(amin, a); \
get_func(amax, a+len); \
*res++= cast(amin); \
*res++= cast(amax); \
}
/*
Creates an MBR as an array of doubles.
Fills *res.
*/
int maria_rtree_d_mbr(const HA_KEYSEG *keyseg, const uchar *a,
uint key_length, double *res)
{
for (; (int)key_length > 0; keyseg += 2)
{
uint32 keyseg_length;
switch ((enum ha_base_keytype) keyseg->type) {
case HA_KEYTYPE_INT8:
RT_D_MBR_KORR(int8, mi_sint1korr, 1, (double));
break;
case HA_KEYTYPE_BINARY:
RT_D_MBR_KORR(uint8, mi_uint1korr, 1, (double));
break;
case HA_KEYTYPE_SHORT_INT:
RT_D_MBR_KORR(int16, mi_sint2korr, 2, (double));
break;
case HA_KEYTYPE_USHORT_INT:
RT_D_MBR_KORR(uint16, mi_uint2korr, 2, (double));
break;
case HA_KEYTYPE_INT24:
RT_D_MBR_KORR(int32, mi_sint3korr, 3, (double));
break;
case HA_KEYTYPE_UINT24:
RT_D_MBR_KORR(uint32, mi_uint3korr, 3, (double));
break;
case HA_KEYTYPE_LONG_INT:
RT_D_MBR_KORR(int32, mi_sint4korr, 4, (double));
break;
case HA_KEYTYPE_ULONG_INT:
RT_D_MBR_KORR(uint32, mi_uint4korr, 4, (double));
break;
#ifdef HAVE_LONG_LONG
case HA_KEYTYPE_LONGLONG:
RT_D_MBR_KORR(longlong, mi_sint8korr, 8, (double));
break;
case HA_KEYTYPE_ULONGLONG:
RT_D_MBR_KORR(longlong, mi_sint8korr, 8, ulonglong2double);
break;
#endif
case HA_KEYTYPE_FLOAT:
RT_D_MBR_GET(float, mi_float4get, 4, (double));
break;
case HA_KEYTYPE_DOUBLE:
RT_D_MBR_GET(double, mi_float8get, 8, (double));
break;
case HA_KEYTYPE_END:
key_length= 0;
break;
default:
return 1;
}
keyseg_length= keyseg->length * 2;
key_length-= keyseg_length;
a+= keyseg_length;
}
return 0;
}
#define RT_COMB_KORR(type, korr_func, store_func, len) \
{ \
type amin, amax, bmin, bmax; \
amin= korr_func(a); \
bmin= korr_func(b); \
amax= korr_func(a+len); \
bmax= korr_func(b+len); \
amin= MY_MIN(amin, bmin); \
amax= MY_MAX(amax, bmax); \
store_func(c, amin); \
store_func(c+len, amax); \
}
#define RT_COMB_GET(type, get_func, store_func, len) \
{ \
type amin, amax, bmin, bmax; \
get_func(amin, a); \
get_func(bmin, b); \
get_func(amax, a+len); \
get_func(bmax, b+len); \
amin= MY_MIN(amin, bmin); \
amax= MY_MAX(amax, bmax); \
store_func(c, amin); \
store_func(c+len, amax); \
}
/*
Creates common minimal bounding rectungle
for two input rectagnles a and b
Result is written to c
*/
int maria_rtree_combine_rect(const HA_KEYSEG *keyseg, const uchar* a,
const uchar* b, uchar* c,
uint key_length)
{
for ( ; (int) key_length > 0 ; keyseg += 2)
{
uint32 keyseg_length;
switch ((enum ha_base_keytype) keyseg->type) {
case HA_KEYTYPE_INT8:
RT_COMB_KORR(int8, mi_sint1korr, mi_int1store, 1);
break;
case HA_KEYTYPE_BINARY:
RT_COMB_KORR(uint8, mi_uint1korr, mi_int1store, 1);
break;
case HA_KEYTYPE_SHORT_INT:
RT_COMB_KORR(int16, mi_sint2korr, mi_int2store, 2);
break;
case HA_KEYTYPE_USHORT_INT:
RT_COMB_KORR(uint16, mi_uint2korr, mi_int2store, 2);
break;
case HA_KEYTYPE_INT24:
RT_COMB_KORR(int32, mi_sint3korr, mi_int3store, 3);
break;
case HA_KEYTYPE_UINT24:
RT_COMB_KORR(uint32, mi_uint3korr, mi_int3store, 3);
break;
case HA_KEYTYPE_LONG_INT:
RT_COMB_KORR(int32, mi_sint4korr, mi_int4store, 4);
break;
case HA_KEYTYPE_ULONG_INT:
RT_COMB_KORR(uint32, mi_uint4korr, mi_int4store, 4);
break;
#ifdef HAVE_LONG_LONG
case HA_KEYTYPE_LONGLONG:
RT_COMB_KORR(longlong, mi_sint8korr, mi_int8store, 8);
break;
case HA_KEYTYPE_ULONGLONG:
RT_COMB_KORR(ulonglong, mi_uint8korr, mi_int8store, 8);
break;
#endif
case HA_KEYTYPE_FLOAT:
RT_COMB_GET(float, mi_float4get, mi_float4store, 4);
break;
case HA_KEYTYPE_DOUBLE:
RT_COMB_GET(double, mi_float8get, mi_float8store, 8);
break;
case HA_KEYTYPE_END:
return 0;
default:
return 1;
}
keyseg_length= keyseg->length * 2;
key_length-= keyseg_length;
a+= keyseg_length;
b+= keyseg_length;
c+= keyseg_length;
}
return 0;
}
#define RT_OVL_AREA_KORR(type, korr_func, len) \
{ \
type amin, amax, bmin, bmax; \
amin= korr_func(a); \
bmin= korr_func(b); \
amax= korr_func(a+len); \
bmax= korr_func(b+len); \
amin= MY_MAX(amin, bmin); \
amax= MY_MIN(amax, bmax); \
if (amin >= amax) \
return 0; \
res *= amax - amin; \
}
#define RT_OVL_AREA_GET(type, get_func, len) \
{ \
type amin, amax, bmin, bmax; \
get_func(amin, a); \
get_func(bmin, b); \
get_func(amax, a+len); \
get_func(bmax, b+len); \
amin= MY_MAX(amin, bmin); \
amax= MY_MIN(amax, bmax); \
if (amin >= amax) \
return 0; \
res *= amax - amin; \
}
/*
Calculates overlapping area of two MBRs a & b
*/
double maria_rtree_overlapping_area(HA_KEYSEG *keyseg, uchar* a, uchar* b,
uint key_length)
{
double res= 1;
for (; (int) key_length > 0 ; keyseg += 2)
{
uint32 keyseg_length;
switch ((enum ha_base_keytype) keyseg->type) {
case HA_KEYTYPE_INT8:
RT_OVL_AREA_KORR(int8, mi_sint1korr, 1);
break;
case HA_KEYTYPE_BINARY:
RT_OVL_AREA_KORR(uint8, mi_uint1korr, 1);
break;
case HA_KEYTYPE_SHORT_INT:
RT_OVL_AREA_KORR(int16, mi_sint2korr, 2);
break;
case HA_KEYTYPE_USHORT_INT:
RT_OVL_AREA_KORR(uint16, mi_uint2korr, 2);
break;
case HA_KEYTYPE_INT24:
RT_OVL_AREA_KORR(int32, mi_sint3korr, 3);
break;
case HA_KEYTYPE_UINT24:
RT_OVL_AREA_KORR(uint32, mi_uint3korr, 3);
break;
case HA_KEYTYPE_LONG_INT:
RT_OVL_AREA_KORR(int32, mi_sint4korr, 4);
break;
case HA_KEYTYPE_ULONG_INT:
RT_OVL_AREA_KORR(uint32, mi_uint4korr, 4);
break;
#ifdef HAVE_LONG_LONG
case HA_KEYTYPE_LONGLONG:
RT_OVL_AREA_KORR(longlong, mi_sint8korr, 8);
break;
case HA_KEYTYPE_ULONGLONG:
RT_OVL_AREA_KORR(longlong, mi_sint8korr, 8);
break;
#endif
case HA_KEYTYPE_FLOAT:
RT_OVL_AREA_GET(float, mi_float4get, 4);
break;
case HA_KEYTYPE_DOUBLE:
RT_OVL_AREA_GET(double, mi_float8get, 8);
break;
case HA_KEYTYPE_END:
return res;
default:
return -1;
}
keyseg_length= keyseg->length * 2;
key_length-= keyseg_length;
a+= keyseg_length;
b+= keyseg_length;
}
return res;
}
#define RT_AREA_INC_KORR(type, korr_func, len) \
{ \
type amin, amax, bmin, bmax; \
amin= korr_func(a); \
bmin= korr_func(b); \
amax= korr_func(a+len); \
bmax= korr_func(b+len); \
a_area *= (((double)amax) - ((double)amin)); \
loc_ab_area *= ((double)MY_MAX(amax, bmax) - (double)MY_MIN(amin, bmin)); \
}
#define RT_AREA_INC_GET(type, get_func, len)\
{\
type amin, amax, bmin, bmax; \
get_func(amin, a); \
get_func(bmin, b); \
get_func(amax, a+len); \
get_func(bmax, b+len); \
a_area *= (((double)amax) - ((double)amin)); \
loc_ab_area *= ((double)MY_MAX(amax, bmax) - (double)MY_MIN(amin, bmin)); \
}
/*
Calculates MBR_AREA(a+b) - MBR_AREA(a)
Fills *ab_area.
Note: when 'a' and 'b' objects are far from each other,
the area increase can be really big, so this function
can return 'inf' as a result.
*/
double maria_rtree_area_increase(const HA_KEYSEG *keyseg, const uchar *a,
const uchar *b,
uint key_length, double *ab_area)
{
double a_area= 1.0;
double loc_ab_area= 1.0;
*ab_area= 1.0;
for (; (int)key_length > 0; keyseg += 2)
{
uint32 keyseg_length;
if (keyseg->null_bit) /* Handle NULL part */
return -1;
switch ((enum ha_base_keytype) keyseg->type) {
case HA_KEYTYPE_INT8:
RT_AREA_INC_KORR(int8, mi_sint1korr, 1);
break;
case HA_KEYTYPE_BINARY:
RT_AREA_INC_KORR(uint8, mi_uint1korr, 1);
break;
case HA_KEYTYPE_SHORT_INT:
RT_AREA_INC_KORR(int16, mi_sint2korr, 2);
break;
case HA_KEYTYPE_USHORT_INT:
RT_AREA_INC_KORR(uint16, mi_uint2korr, 2);
break;
case HA_KEYTYPE_INT24:
RT_AREA_INC_KORR(int32, mi_sint3korr, 3);
break;
case HA_KEYTYPE_UINT24:
RT_AREA_INC_KORR(int32, mi_uint3korr, 3);
break;
case HA_KEYTYPE_LONG_INT:
RT_AREA_INC_KORR(int32, mi_sint4korr, 4);
break;
case HA_KEYTYPE_ULONG_INT:
RT_AREA_INC_KORR(uint32, mi_uint4korr, 4);
break;
#ifdef HAVE_LONG_LONG
case HA_KEYTYPE_LONGLONG:
RT_AREA_INC_KORR(longlong, mi_sint8korr, 8);
break;
case HA_KEYTYPE_ULONGLONG:
RT_AREA_INC_KORR(longlong, mi_sint8korr, 8);
break;
#endif
case HA_KEYTYPE_FLOAT:
RT_AREA_INC_GET(float, mi_float4get, 4);
break;
case HA_KEYTYPE_DOUBLE:
RT_AREA_INC_GET(double, mi_float8get, 8);
break;
case HA_KEYTYPE_END:
goto safe_end;
default:
return -1;
}
keyseg_length= keyseg->length * 2;
key_length-= keyseg_length;
a+= keyseg_length;
b+= keyseg_length;
}
safe_end:
*ab_area= loc_ab_area;
return loc_ab_area - a_area;
}
#define RT_PERIM_INC_KORR(type, korr_func, len) \
{ \
type amin, amax, bmin, bmax; \
amin= korr_func(a); \
bmin= korr_func(b); \
amax= korr_func(a+len); \
bmax= korr_func(b+len); \
a_perim+= (((double)amax) - ((double)amin)); \
*ab_perim+= ((double)MY_MAX(amax, bmax) - (double)MY_MIN(amin, bmin)); \
}
#define RT_PERIM_INC_GET(type, get_func, len)\
{\
type amin, amax, bmin, bmax; \
get_func(amin, a); \
get_func(bmin, b); \
get_func(amax, a+len); \
get_func(bmax, b+len); \
a_perim+= (((double)amax) - ((double)amin)); \
*ab_perim+= ((double)MY_MAX(amax, bmax) - (double)MY_MIN(amin, bmin)); \
}
/*
Calculates MBR_PERIMETER(a+b) - MBR_PERIMETER(a)
*/
double maria_rtree_perimeter_increase(HA_KEYSEG *keyseg, uchar* a, uchar* b,
uint key_length, double *ab_perim)
{
double a_perim= 0.0;
*ab_perim= 0.0;
for (; (int)key_length > 0; keyseg += 2)
{
uint32 keyseg_length;
if (keyseg->null_bit) /* Handle NULL part */
return -1;
switch ((enum ha_base_keytype) keyseg->type) {
case HA_KEYTYPE_INT8:
RT_PERIM_INC_KORR(int8, mi_sint1korr, 1);
break;
case HA_KEYTYPE_BINARY:
RT_PERIM_INC_KORR(uint8, mi_uint1korr, 1);
break;
case HA_KEYTYPE_SHORT_INT:
RT_PERIM_INC_KORR(int16, mi_sint2korr, 2);
break;
case HA_KEYTYPE_USHORT_INT:
RT_PERIM_INC_KORR(uint16, mi_uint2korr, 2);
break;
case HA_KEYTYPE_INT24:
RT_PERIM_INC_KORR(int32, mi_sint3korr, 3);
break;
case HA_KEYTYPE_UINT24:
RT_PERIM_INC_KORR(int32, mi_uint3korr, 3);
break;
case HA_KEYTYPE_LONG_INT:
RT_PERIM_INC_KORR(int32, mi_sint4korr, 4);
break;
case HA_KEYTYPE_ULONG_INT:
RT_PERIM_INC_KORR(uint32, mi_uint4korr, 4);
break;
#ifdef HAVE_LONG_LONG
case HA_KEYTYPE_LONGLONG:
RT_PERIM_INC_KORR(longlong, mi_sint8korr, 8);
break;
case HA_KEYTYPE_ULONGLONG:
RT_PERIM_INC_KORR(longlong, mi_sint8korr, 8);
break;
#endif
case HA_KEYTYPE_FLOAT:
RT_PERIM_INC_GET(float, mi_float4get, 4);
break;
case HA_KEYTYPE_DOUBLE:
RT_PERIM_INC_GET(double, mi_float8get, 8);
break;
case HA_KEYTYPE_END:
return *ab_perim - a_perim;
default:
return -1;
}
keyseg_length= keyseg->length * 2;
key_length-= keyseg_length;
a+= keyseg_length;
b+= keyseg_length;
}
return *ab_perim - a_perim;
}
#define RT_PAGE_MBR_KORR(share, type, korr_func, store_func, len, to) \
{ \
type amin, amax, bmin, bmax; \
amin= korr_func(k + inc); \
amax= korr_func(k + inc + len); \
k= rt_PAGE_NEXT_KEY(share, k, k_len, nod_flag); \
for (; k < last; k= rt_PAGE_NEXT_KEY(share, k, k_len, nod_flag)) \
{ \
bmin= korr_func(k + inc); \
bmax= korr_func(k + inc + len); \
if (amin > bmin) \
amin= bmin; \
if (amax < bmax) \
amax= bmax; \
} \
store_func(to, amin); \
to+= len; \
store_func(to, amax); \
to += len; \
inc += 2 * len; \
}
#define RT_PAGE_MBR_GET(share, type, get_func, store_func, len, to) \
{ \
type amin, amax, bmin, bmax; \
get_func(amin, k + inc); \
get_func(amax, k + inc + len); \
k= rt_PAGE_NEXT_KEY(share, k, k_len, nod_flag); \
for (; k < last; k= rt_PAGE_NEXT_KEY(share, k, k_len, nod_flag)) \
{ \
get_func(bmin, k + inc); \
get_func(bmax, k + inc + len); \
if (amin > bmin) \
amin= bmin; \
if (amax < bmax) \
amax= bmax; \
} \
store_func(to, amin); \
to+= len; \
store_func(to, amax); \
to+= len; \
inc += 2 * len; \
}
/*
Calculates key page total MBR= MBR(key1) + MBR(key2) + ...
Stores into *to.
*/
int maria_rtree_page_mbr(const HA_KEYSEG *keyseg,
MARIA_PAGE *page,
uchar *to, uint key_length)
{
MARIA_HA *info= page->info;
MARIA_SHARE *share= info->s;
uint inc= 0;
uint k_len= key_length;
uint nod_flag= page->node;
const uchar *k;
const uchar *last= rt_PAGE_END(page);
for (; (int)key_length > 0; keyseg += 2)
{
key_length -= keyseg->length * 2;
/* Handle NULL part */
if (keyseg->null_bit)
{
return 1;
}
k= rt_PAGE_FIRST_KEY(share, page->buff, nod_flag);
switch ((enum ha_base_keytype) keyseg->type) {
case HA_KEYTYPE_INT8:
RT_PAGE_MBR_KORR(share, int8, mi_sint1korr, mi_int1store, 1, to);
break;
case HA_KEYTYPE_BINARY:
RT_PAGE_MBR_KORR(share, uint8, mi_uint1korr, mi_int1store, 1, to);
break;
case HA_KEYTYPE_SHORT_INT:
RT_PAGE_MBR_KORR(share, int16, mi_sint2korr, mi_int2store, 2, to);
break;
case HA_KEYTYPE_USHORT_INT:
RT_PAGE_MBR_KORR(share, uint16, mi_uint2korr, mi_int2store, 2, to);
break;
case HA_KEYTYPE_INT24:
RT_PAGE_MBR_KORR(share, int32, mi_sint3korr, mi_int3store, 3, to);
break;
case HA_KEYTYPE_UINT24:
RT_PAGE_MBR_KORR(share, uint32, mi_uint3korr, mi_int3store, 3, to);
break;
case HA_KEYTYPE_LONG_INT:
RT_PAGE_MBR_KORR(share, int32, mi_sint4korr, mi_int4store, 4, to);
break;
case HA_KEYTYPE_ULONG_INT:
RT_PAGE_MBR_KORR(share, uint32, mi_uint4korr, mi_int4store, 4, to);
break;
#ifdef HAVE_LONG_LONG
case HA_KEYTYPE_LONGLONG:
RT_PAGE_MBR_KORR(share, longlong, mi_sint8korr, mi_int8store, 8, to);
break;
case HA_KEYTYPE_ULONGLONG:
RT_PAGE_MBR_KORR(share, ulonglong, mi_uint8korr, mi_int8store, 8, to);
break;
#endif
case HA_KEYTYPE_FLOAT:
RT_PAGE_MBR_GET(share, float, mi_float4get, mi_float4store, 4, to);
break;
case HA_KEYTYPE_DOUBLE:
RT_PAGE_MBR_GET(share, double, mi_float8get, mi_float8store, 8, to);
break;
case HA_KEYTYPE_END:
return 0;
default:
return 1;
}
}
return 0;
}
#endif /*HAVE_RTREE_KEYS*/
|