1
/* Copyright (C) 2002-2006 MySQL AB & Ramil Kalimullin
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include "myisamdef.h"
18
#ifdef HAVE_RTREE_KEYS
24
#define REINSERT_BUFFER_INC 10
26
/*#define PICK_BY_PERIMETER*/
28
typedef struct st_page_level
34
typedef struct st_page_list
43
Find next key in r-tree according to search_flag recursively
46
Used in rtree_find_first() and rtree_find_next()
54
static int rtree_find_req(MI_INFO *info, MI_KEYDEF *keyinfo, uint search_flag,
55
uint nod_cmp_flag, my_off_t page, int level)
63
uint *saved_key = (uint*) (info->rtree_recursion_state) + level;
65
if (!(page_buf = (uchar*)my_alloca((uint)keyinfo->block_length)))
67
my_errno = HA_ERR_OUT_OF_MEM;
70
if (!_mi_fetch_keypage(info, keyinfo, page, DFLT_INIT_HITS, page_buf, 0))
72
nod_flag = mi_test_if_nod(page_buf);
74
k_len = keyinfo->keylength - info->s->base.rec_reflength;
76
if(info->rtree_recursion_depth >= level)
78
k = page_buf + *saved_key;
82
k = rt_PAGE_FIRST_KEY(page_buf, nod_flag);
84
last = rt_PAGE_END(page_buf);
86
for (; k < last; k = rt_PAGE_NEXT_KEY(k, k_len, nod_flag))
90
/* this is an internal node in the tree */
91
if (!(res = rtree_key_cmp(keyinfo->seg, info->first_mbr_key, k,
92
info->last_rkey_length, nod_cmp_flag)))
94
switch ((res = rtree_find_req(info, keyinfo, search_flag, nod_cmp_flag,
95
_mi_kpos(nod_flag, k), level + 1)))
97
case 0: /* found - exit from recursion */
98
*saved_key = k - page_buf;
100
case 1: /* not found - continue searching */
101
info->rtree_recursion_depth = level;
112
if (!rtree_key_cmp(keyinfo->seg, info->first_mbr_key, k,
113
info->last_rkey_length, search_flag))
115
uchar *after_key = rt_PAGE_NEXT_KEY(k, k_len, nod_flag);
116
info->lastpos = _mi_dpos(info, 0, after_key);
117
info->lastkey_length = k_len + info->s->base.rec_reflength;
118
memcpy(info->lastkey, k, info->lastkey_length);
119
info->rtree_recursion_depth = level;
120
*saved_key = last - page_buf;
122
if (after_key < last)
124
info->int_keypos = info->buff;
125
info->int_maxpos = info->buff + (last - after_key);
126
memcpy(info->buff, after_key, last - after_key);
139
info->lastpos = HA_OFFSET_ERROR;
140
my_errno = HA_ERR_KEY_NOT_FOUND;
144
my_afree((uchar*)page_buf);
148
my_afree((uchar*)page_buf);
149
info->lastpos = HA_OFFSET_ERROR;
155
Find first key in r-tree according to search_flag condition
159
info Handler to MyISAM file
160
uint keynr Key number to use
161
key Key to search for
162
key_length Length of 'key'
163
search_flag Bitmap of flags how to do the search
171
int rtree_find_first(MI_INFO *info, uint keynr, uchar *key, uint key_length,
176
MI_KEYDEF *keyinfo = info->s->keyinfo + keynr;
178
if ((root = info->s->state.key_root[keynr]) == HA_OFFSET_ERROR)
180
my_errno= HA_ERR_END_OF_FILE;
185
Save searched key, include data pointer.
186
The data pointer is required if the search_flag contains MBR_DATA.
187
(minimum bounding rectangle)
189
memcpy(info->first_mbr_key, key, keyinfo->keylength);
190
info->last_rkey_length = key_length;
192
info->rtree_recursion_depth = -1;
195
nod_cmp_flag = ((search_flag & (MBR_EQUAL | MBR_WITHIN)) ?
196
MBR_WITHIN : MBR_INTERSECT);
197
return rtree_find_req(info, keyinfo, search_flag, nod_cmp_flag, root, 0);
202
Find next key in r-tree according to search_flag condition
206
info Handler to MyISAM file
207
uint keynr Key number to use
208
search_flag Bitmap of flags how to do the search
216
int rtree_find_next(MI_INFO *info, uint keynr, uint search_flag)
220
MI_KEYDEF *keyinfo = info->s->keyinfo + keynr;
222
if (info->update & HA_STATE_DELETED)
223
return rtree_find_first(info, keynr, info->lastkey, info->lastkey_length,
226
if (!info->buff_used)
228
uchar *key= info->int_keypos;
230
while (key < info->int_maxpos)
232
if (!rtree_key_cmp(keyinfo->seg, info->first_mbr_key, key,
233
info->last_rkey_length, search_flag))
235
uchar *after_key = key + keyinfo->keylength;
237
info->lastpos= _mi_dpos(info, 0, after_key);
238
memcpy(info->lastkey, key, info->lastkey_length);
240
if (after_key < info->int_maxpos)
241
info->int_keypos= after_key;
246
key+= keyinfo->keylength;
249
if ((root = info->s->state.key_root[keynr]) == HA_OFFSET_ERROR)
251
my_errno= HA_ERR_END_OF_FILE;
255
nod_cmp_flag = ((search_flag & (MBR_EQUAL | MBR_WITHIN)) ?
256
MBR_WITHIN : MBR_INTERSECT);
257
return rtree_find_req(info, keyinfo, search_flag, nod_cmp_flag, root, 0);
262
Get next key in r-tree recursively
265
Used in rtree_get_first() and rtree_get_next()
273
static int rtree_get_req(MI_INFO *info, MI_KEYDEF *keyinfo, uint key_length,
274
my_off_t page, int level)
282
uint *saved_key = (uint*) (info->rtree_recursion_state) + level;
284
if (!(page_buf = (uchar*)my_alloca((uint)keyinfo->block_length)))
286
if (!_mi_fetch_keypage(info, keyinfo, page, DFLT_INIT_HITS, page_buf, 0))
288
nod_flag = mi_test_if_nod(page_buf);
290
k_len = keyinfo->keylength - info->s->base.rec_reflength;
292
if(info->rtree_recursion_depth >= level)
294
k = page_buf + *saved_key;
297
/* Only leaf pages contain data references. */
298
/* Need to check next key with data reference. */
299
k = rt_PAGE_NEXT_KEY(k, k_len, nod_flag);
304
k = rt_PAGE_FIRST_KEY(page_buf, nod_flag);
306
last = rt_PAGE_END(page_buf);
308
for (; k < last; k = rt_PAGE_NEXT_KEY(k, k_len, nod_flag))
312
/* this is an internal node in the tree */
313
switch ((res = rtree_get_req(info, keyinfo, key_length,
314
_mi_kpos(nod_flag, k), level + 1)))
316
case 0: /* found - exit from recursion */
317
*saved_key = k - page_buf;
319
case 1: /* not found - continue searching */
320
info->rtree_recursion_depth = level;
330
uchar *after_key = rt_PAGE_NEXT_KEY(k, k_len, nod_flag);
331
info->lastpos = _mi_dpos(info, 0, after_key);
332
info->lastkey_length = k_len + info->s->base.rec_reflength;
333
memcpy(info->lastkey, k, info->lastkey_length);
335
info->rtree_recursion_depth = level;
336
*saved_key = k - page_buf;
338
if (after_key < last)
340
info->int_keypos = (uchar*)saved_key;
341
memcpy(info->buff, page_buf, keyinfo->block_length);
342
info->int_maxpos = rt_PAGE_END(info->buff);
354
info->lastpos = HA_OFFSET_ERROR;
355
my_errno = HA_ERR_KEY_NOT_FOUND;
359
my_afree((uchar*)page_buf);
363
my_afree((uchar*)page_buf);
364
info->lastpos = HA_OFFSET_ERROR;
370
Get first key in r-tree
378
int rtree_get_first(MI_INFO *info, uint keynr, uint key_length)
381
MI_KEYDEF *keyinfo = info->s->keyinfo + keynr;
383
if ((root = info->s->state.key_root[keynr]) == HA_OFFSET_ERROR)
385
my_errno= HA_ERR_END_OF_FILE;
389
info->rtree_recursion_depth = -1;
392
return rtree_get_req(info, &keyinfo[keynr], key_length, root, 0);
397
Get next key in r-tree
405
int rtree_get_next(MI_INFO *info, uint keynr, uint key_length)
408
MI_KEYDEF *keyinfo = info->s->keyinfo + keynr;
410
if (!info->buff_used)
412
uint k_len = keyinfo->keylength - info->s->base.rec_reflength;
413
/* rt_PAGE_NEXT_KEY(info->int_keypos) */
414
uchar *key = info->buff + *(int*)info->int_keypos + k_len +
415
info->s->base.rec_reflength;
416
/* rt_PAGE_NEXT_KEY(key) */
417
uchar *after_key = key + k_len + info->s->base.rec_reflength;
419
info->lastpos = _mi_dpos(info, 0, after_key);
420
info->lastkey_length = k_len + info->s->base.rec_reflength;
421
memcpy(info->lastkey, key, k_len + info->s->base.rec_reflength);
423
*(int*)info->int_keypos = key - info->buff;
424
if (after_key >= info->int_maxpos)
433
if ((root = info->s->state.key_root[keynr]) == HA_OFFSET_ERROR)
435
my_errno= HA_ERR_END_OF_FILE;
439
return rtree_get_req(info, &keyinfo[keynr], key_length, root, 0);
445
Choose non-leaf better key for insertion
448
#ifdef PICK_BY_PERIMETER
449
static uchar *rtree_pick_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
450
uint key_length, uchar *page_buf, uint nod_flag)
453
double best_incr = DBL_MAX;
455
double best_perimeter;
457
uchar *k = rt_PAGE_FIRST_KEY(page_buf, nod_flag);
458
uchar *last = rt_PAGE_END(page_buf);
460
LINT_INIT(best_perimeter);
463
for (; k < last; k = rt_PAGE_NEXT_KEY(k, key_length, nod_flag))
465
if ((increase = rtree_perimeter_increase(keyinfo->seg, k, key, key_length,
468
if ((increase < best_incr)||
469
(increase == best_incr && perimeter < best_perimeter))
472
best_perimeter= perimeter;
473
best_incr = increase;
479
#endif /*PICK_BY_PERIMETER*/
482
static uchar *rtree_pick_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
483
uint key_length, uchar *page_buf, uint nod_flag)
489
uchar *best_key= NULL;
490
uchar *k = rt_PAGE_FIRST_KEY(page_buf, nod_flag);
491
uchar *last = rt_PAGE_END(page_buf);
493
LINT_INIT(best_area);
495
LINT_INIT(best_incr);
497
for (; k < last; k = rt_PAGE_NEXT_KEY(k, key_length, nod_flag))
499
/* The following is safe as -1.0 is an exact number */
500
if ((increase = rtree_area_increase(keyinfo->seg, k, key, key_length,
503
/* The following should be safe, even if we compare doubles */
504
if (!best_key || increase < best_incr ||
505
((increase == best_incr) && (area < best_area)))
509
best_incr = increase;
515
#endif /*PICK_BY_AREA*/
518
Go down and insert key into tree
522
0 Child was not split
526
static int rtree_insert_req(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
527
uint key_length, my_off_t page, my_off_t *new_page,
528
int ins_level, int level)
534
DBUG_ENTER("rtree_insert_req");
536
if (!(page_buf = (uchar*)my_alloca((uint)keyinfo->block_length +
539
my_errno = HA_ERR_OUT_OF_MEM;
540
DBUG_RETURN(-1); /* purecov: inspected */
542
if (!_mi_fetch_keypage(info, keyinfo, page, DFLT_INIT_HITS, page_buf, 0))
544
nod_flag = mi_test_if_nod(page_buf);
545
DBUG_PRINT("rtree", ("page: %lu level: %d ins_level: %d nod_flag: %u",
546
(ulong) page, level, ins_level, nod_flag));
548
if ((ins_level == -1 && nod_flag) || /* key: go down to leaf */
549
(ins_level > -1 && ins_level > level)) /* branch: go down to ins_level */
551
if ((k = rtree_pick_key(info, keyinfo, key, key_length, page_buf,
554
switch ((res = rtree_insert_req(info, keyinfo, key, key_length,
555
_mi_kpos(nod_flag, k), new_page, ins_level, level + 1)))
557
case 0: /* child was not split */
559
rtree_combine_rect(keyinfo->seg, k, key, k, key_length);
560
if (_mi_write_keypage(info, keyinfo, page, DFLT_INIT_HITS, page_buf))
564
case 1: /* child was split */
566
uchar *new_key = page_buf + keyinfo->block_length + nod_flag;
567
/* set proper MBR for key */
568
if (rtree_set_key_mbr(info, keyinfo, k, key_length,
569
_mi_kpos(nod_flag, k)))
571
/* add new key for new page */
572
_mi_kpointer(info, new_key - nod_flag, *new_page);
573
if (rtree_set_key_mbr(info, keyinfo, new_key, key_length, *new_page))
575
res = rtree_add_key(info, keyinfo, new_key, key_length,
577
if (_mi_write_keypage(info, keyinfo, page, DFLT_INIT_HITS, page_buf))
590
res = rtree_add_key(info, keyinfo, key, key_length, page_buf, new_page);
591
if (_mi_write_keypage(info, keyinfo, page, DFLT_INIT_HITS, page_buf))
597
my_afree((uchar*)page_buf);
601
my_afree((uchar*)page_buf);
602
DBUG_RETURN(-1); /* purecov: inspected */
607
Insert key into the tree
615
static int rtree_insert_level(MI_INFO *info, uint keynr, uchar *key,
616
uint key_length, int ins_level)
619
MI_KEYDEF *keyinfo = info->s->keyinfo + keynr;
622
DBUG_ENTER("rtree_insert_level");
624
if ((old_root = info->s->state.key_root[keynr]) == HA_OFFSET_ERROR)
626
if ((old_root = _mi_new(info, keyinfo, DFLT_INIT_HITS)) == HA_OFFSET_ERROR)
629
mi_putint(info->buff, 2, 0);
630
res = rtree_add_key(info, keyinfo, key, key_length, info->buff, NULL);
631
if (_mi_write_keypage(info, keyinfo, old_root, DFLT_INIT_HITS, info->buff))
633
info->s->state.key_root[keynr] = old_root;
637
switch ((res = rtree_insert_req(info, keyinfo, key, key_length,
638
old_root, &new_page, ins_level, 0)))
640
case 0: /* root was not split */
644
case 1: /* root was split, grow a new root */
649
uint nod_flag = info->s->base.key_reflength;
651
DBUG_PRINT("rtree", ("root was split, grow a new root"));
652
if (!(new_root_buf = (uchar*)my_alloca((uint)keyinfo->block_length +
655
my_errno = HA_ERR_OUT_OF_MEM;
656
DBUG_RETURN(-1); /* purecov: inspected */
659
mi_putint(new_root_buf, 2, nod_flag);
660
if ((new_root = _mi_new(info, keyinfo, DFLT_INIT_HITS)) ==
664
new_key = new_root_buf + keyinfo->block_length + nod_flag;
666
_mi_kpointer(info, new_key - nod_flag, old_root);
667
if (rtree_set_key_mbr(info, keyinfo, new_key, key_length, old_root))
669
if (rtree_add_key(info, keyinfo, new_key, key_length, new_root_buf, NULL)
672
_mi_kpointer(info, new_key - nod_flag, new_page);
673
if (rtree_set_key_mbr(info, keyinfo, new_key, key_length, new_page))
675
if (rtree_add_key(info, keyinfo, new_key, key_length, new_root_buf, NULL)
678
if (_mi_write_keypage(info, keyinfo, new_root,
679
DFLT_INIT_HITS, new_root_buf))
681
info->s->state.key_root[keynr] = new_root;
682
DBUG_PRINT("rtree", ("new root page: %lu level: %d nod_flag: %u",
683
(ulong) new_root, 0, mi_test_if_nod(new_root_buf)));
685
my_afree((uchar*)new_root_buf);
688
my_afree((uchar*)new_root_buf);
689
DBUG_RETURN(-1); /* purecov: inspected */
702
Insert key into the tree - interface function
709
int rtree_insert(MI_INFO *info, uint keynr, uchar *key, uint key_length)
711
DBUG_ENTER("rtree_insert");
712
DBUG_RETURN((!key_length ||
713
(rtree_insert_level(info, keynr, key, key_length, -1) == -1)) ?
719
Fill reinsert page buffer
726
static int rtree_fill_reinsert_list(stPageList *ReinsertList, my_off_t page,
729
DBUG_ENTER("rtree_fill_reinsert_list");
730
DBUG_PRINT("rtree", ("page: %lu level: %d", (ulong) page, level));
731
if (ReinsertList->n_pages == ReinsertList->m_pages)
733
ReinsertList->m_pages += REINSERT_BUFFER_INC;
734
if (!(ReinsertList->pages = (stPageLevel*)my_realloc((uchar*)ReinsertList->pages,
735
ReinsertList->m_pages * sizeof(stPageLevel), MYF(MY_ALLOW_ZERO_PTR))))
738
/* save page to ReinsertList */
739
ReinsertList->pages[ReinsertList->n_pages].offs = page;
740
ReinsertList->pages[ReinsertList->n_pages].level = level;
741
ReinsertList->n_pages++;
745
DBUG_RETURN(-1); /* purecov: inspected */
750
Go down and delete key from the tree
759
static int rtree_delete_req(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *key,
760
uint key_length, my_off_t page, uint *page_size,
761
stPageList *ReinsertList, int level)
769
DBUG_ENTER("rtree_delete_req");
771
if (!(page_buf = (uchar*)my_alloca((uint)keyinfo->block_length)))
773
my_errno = HA_ERR_OUT_OF_MEM;
774
DBUG_RETURN(-1); /* purecov: inspected */
776
if (!_mi_fetch_keypage(info, keyinfo, page, DFLT_INIT_HITS, page_buf, 0))
778
nod_flag = mi_test_if_nod(page_buf);
779
DBUG_PRINT("rtree", ("page: %lu level: %d nod_flag: %u",
780
(ulong) page, level, nod_flag));
782
k = rt_PAGE_FIRST_KEY(page_buf, nod_flag);
783
last = rt_PAGE_END(page_buf);
785
for (i = 0; k < last; k = rt_PAGE_NEXT_KEY(k, key_length, nod_flag), ++i)
790
if (!rtree_key_cmp(keyinfo->seg, key, k, key_length, MBR_WITHIN))
792
switch ((res = rtree_delete_req(info, keyinfo, key, key_length,
793
_mi_kpos(nod_flag, k), page_size, ReinsertList, level + 1)))
795
case 0: /* deleted */
797
/* test page filling */
798
if (*page_size + key_length >= rt_PAGE_MIN_SIZE(keyinfo->block_length))
801
/* Calculate a new key value (MBR) for the shrinked block. */
802
if (rtree_set_key_mbr(info, keyinfo, k, key_length,
803
_mi_kpos(nod_flag, k)))
805
if (_mi_write_keypage(info, keyinfo, page,
806
DFLT_INIT_HITS, page_buf))
812
Too small: delete key & add it descendant to reinsert list.
813
Store position and level of the block so that it can be
814
accessed later for inserting the remaining keys.
816
DBUG_PRINT("rtree", ("too small. move block to reinsert list"));
817
if (rtree_fill_reinsert_list(ReinsertList, _mi_kpos(nod_flag, k),
821
Delete the key that references the block. This makes the
822
block disappear from the index. Hence we need to insert
823
its remaining keys later. Note: if the block is a branch
824
block, we do not only remove this block, but the whole
825
subtree. So we need to re-insert its keys on the same
826
level later to reintegrate the subtrees.
828
rtree_delete_key(info, page_buf, k, key_length, nod_flag);
829
if (_mi_write_keypage(info, keyinfo, page,
830
DFLT_INIT_HITS, page_buf))
832
*page_size = mi_getint(page_buf);
837
case 1: /* not found - continue searching */
841
case 2: /* vacuous case: last key in the leaf */
843
rtree_delete_key(info, page_buf, k, key_length, nod_flag);
844
if (_mi_write_keypage(info, keyinfo, page,
845
DFLT_INIT_HITS, page_buf))
847
*page_size = mi_getint(page_buf);
862
if (!rtree_key_cmp(keyinfo->seg, key, k, key_length, MBR_EQUAL | MBR_DATA))
864
rtree_delete_key(info, page_buf, k, key_length, nod_flag);
865
*page_size = mi_getint(page_buf);
868
/* last key in the leaf */
870
if (_mi_dispose(info, keyinfo, page, DFLT_INIT_HITS))
876
if (_mi_write_keypage(info, keyinfo, page, DFLT_INIT_HITS, page_buf))
886
my_afree((uchar*)page_buf);
890
my_afree((uchar*)page_buf);
891
DBUG_RETURN(-1); /* purecov: inspected */
896
Delete key - interface function
903
int rtree_delete(MI_INFO *info, uint keynr, uchar *key, uint key_length)
906
stPageList ReinsertList;
908
MI_KEYDEF *keyinfo = info->s->keyinfo + keynr;
909
DBUG_ENTER("rtree_delete");
911
if ((old_root = info->s->state.key_root[keynr]) == HA_OFFSET_ERROR)
913
my_errno= HA_ERR_END_OF_FILE;
914
DBUG_RETURN(-1); /* purecov: inspected */
916
DBUG_PRINT("rtree", ("starting deletion at root page: %lu",
919
ReinsertList.pages = NULL;
920
ReinsertList.n_pages = 0;
921
ReinsertList.m_pages = 0;
923
switch (rtree_delete_req(info, keyinfo, key, key_length, old_root,
924
&page_size, &ReinsertList, 0))
928
info->s->state.key_root[keynr] = HA_OFFSET_ERROR;
931
case 0: /* deleted */
935
for (i = 0; i < ReinsertList.n_pages; ++i)
941
if (!(page_buf = (uchar*)my_alloca((uint)keyinfo->block_length)))
943
my_errno = HA_ERR_OUT_OF_MEM;
946
if (!_mi_fetch_keypage(info, keyinfo, ReinsertList.pages[i].offs,
947
DFLT_INIT_HITS, page_buf, 0))
949
nod_flag = mi_test_if_nod(page_buf);
950
DBUG_PRINT("rtree", ("reinserting keys from "
951
"page: %lu level: %d nod_flag: %u",
952
(ulong) ReinsertList.pages[i].offs,
953
ReinsertList.pages[i].level, nod_flag));
955
k = rt_PAGE_FIRST_KEY(page_buf, nod_flag);
956
last = rt_PAGE_END(page_buf);
957
for (; k < last; k = rt_PAGE_NEXT_KEY(k, key_length, nod_flag))
960
if ((res= rtree_insert_level(info, keynr, k, key_length,
961
ReinsertList.pages[i].level)) == -1)
963
my_afree((uchar*)page_buf);
969
DBUG_PRINT("rtree", ("root has been split, adjust levels"));
970
for (j= i; j < ReinsertList.n_pages; j++)
972
ReinsertList.pages[j].level++;
973
DBUG_PRINT("rtree", ("keys from page: %lu now level: %d",
974
(ulong) ReinsertList.pages[i].offs,
975
ReinsertList.pages[i].level));
979
my_afree((uchar*)page_buf);
980
if (_mi_dispose(info, keyinfo, ReinsertList.pages[i].offs,
984
if (ReinsertList.pages)
985
my_free((uchar*) ReinsertList.pages, MYF(0));
987
/* check for redundant root (not leaf, 1 child) and eliminate */
988
if ((old_root = info->s->state.key_root[keynr]) == HA_OFFSET_ERROR)
990
if (!_mi_fetch_keypage(info, keyinfo, old_root, DFLT_INIT_HITS,
993
nod_flag = mi_test_if_nod(info->buff);
994
page_size = mi_getint(info->buff);
995
if (nod_flag && (page_size == 2 + key_length + nod_flag))
997
my_off_t new_root = _mi_kpos(nod_flag,
998
rt_PAGE_FIRST_KEY(info->buff, nod_flag));
999
if (_mi_dispose(info, keyinfo, old_root, DFLT_INIT_HITS))
1001
info->s->state.key_root[keynr] = new_root;
1003
info->update= HA_STATE_DELETED;
1007
DBUG_RETURN(-1); /* purecov: inspected */
1009
case 1: /* not found */
1011
my_errno = HA_ERR_KEY_NOT_FOUND;
1012
DBUG_RETURN(-1); /* purecov: inspected */
1015
case -1: /* error */
1017
DBUG_RETURN(-1); /* purecov: inspected */
1024
Estimate number of suitable keys in the tree
1030
ha_rows rtree_estimate(MI_INFO *info, uint keynr, uchar *key,
1031
uint key_length, uint flag)
1033
MI_KEYDEF *keyinfo = info->s->keyinfo + keynr;
1044
if (flag & MBR_DISJOINT)
1045
return info->state->records;
1047
if ((root = info->s->state.key_root[keynr]) == HA_OFFSET_ERROR)
1048
return HA_POS_ERROR;
1049
if (!(page_buf = (uchar*)my_alloca((uint)keyinfo->block_length)))
1050
return HA_POS_ERROR;
1051
if (!_mi_fetch_keypage(info, keyinfo, root, DFLT_INIT_HITS, page_buf, 0))
1053
nod_flag = mi_test_if_nod(page_buf);
1055
k_len = keyinfo->keylength - info->s->base.rec_reflength;
1057
k = rt_PAGE_FIRST_KEY(page_buf, nod_flag);
1058
last = rt_PAGE_END(page_buf);
1060
for (; k < last; k = rt_PAGE_NEXT_KEY(k, k_len, nod_flag), ++i)
1064
double k_area = rtree_rect_volume(keyinfo->seg, k, key_length);
1066
/* The following should be safe, even if we compare doubles */
1069
if (flag & (MBR_CONTAIN | MBR_INTERSECT))
1073
else if (flag & (MBR_WITHIN | MBR_EQUAL))
1075
if (!rtree_key_cmp(keyinfo->seg, key, k, key_length, MBR_WITHIN))
1083
if (flag & (MBR_CONTAIN | MBR_INTERSECT))
1085
area += rtree_overlapping_area(keyinfo->seg, key, k, key_length) /
1088
else if (flag & (MBR_WITHIN | MBR_EQUAL))
1090
if (!rtree_key_cmp(keyinfo->seg, key, k, key_length, MBR_WITHIN))
1091
area += rtree_rect_volume(keyinfo->seg, key, key_length) /
1100
if (!rtree_key_cmp(keyinfo->seg, key, k, key_length, flag))
1107
res = (ha_rows) (area / i * info->state->records);
1112
my_afree((uchar*)page_buf);
1116
my_afree((uchar*)page_buf);
1117
return HA_POS_ERROR;
1120
#endif /*HAVE_RTREE_KEYS*/