-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmi_search.c
1923 lines (1719 loc) · 58.3 KB
/
mi_search.c
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
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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 St, Fifth Floor, Boston, MA 02110-1301 USA */
/* key handling functions */
#include "fulltext.h"
#include "m_ctype.h"
static my_bool _mi_get_prev_key(MI_INFO *info, MI_KEYDEF *keyinfo, uchar *page,
uchar *key, uchar *keypos,
uint *return_key_length);
/* Check index */
int _mi_check_index(MI_INFO *info, int inx)
{
if (inx == -1) /* Use last index */
inx=info->lastinx;
if (inx < 0)
{
my_errno= HA_ERR_WRONG_INDEX;
return -1;
}
if (!mi_is_key_active(info->s->state.key_map, inx))
{
my_errno= info->s->state.state.records ? HA_ERR_WRONG_INDEX :
HA_ERR_END_OF_FILE;
return -1;
}
if (info->lastinx != inx) /* Index changed */
{
info->lastinx = inx;
info->page_changed=1;
info->update= ((info->update & (HA_STATE_CHANGED | HA_STATE_ROW_CHANGED)) |
HA_STATE_NEXT_FOUND | HA_STATE_PREV_FOUND);
}
if (info->opt_flag & WRITE_CACHE_USED && flush_io_cache(&info->rec_cache))
return(-1);
return(inx);
} /* mi_check_index */
/*
** Search after row by a key
** Position to row is stored in info->lastpos
** Return: -1 if not found
** 1 if one should continue search on higher level
*/
int _mi_search(register MI_INFO *info, register MI_KEYDEF *keyinfo,
uchar *key, uint key_len, uint nextflag, register my_off_t pos)
{
my_bool last_key;
int error,flag;
uint nod_flag;
uchar *keypos,*maxpos;
uchar lastkey[MI_MAX_KEY_BUFF],*buff;
DBUG_ENTER("_mi_search");
DBUG_PRINT("enter",("pos: %lu nextflag: %u lastpos: %lu",
(ulong) pos, nextflag, (ulong) info->lastpos));
DBUG_EXECUTE("key",_mi_print_key(DBUG_FILE,keyinfo->seg,key,key_len););
if (pos == HA_OFFSET_ERROR)
{
my_errno=HA_ERR_KEY_NOT_FOUND; /* Didn't find key */
info->lastpos= HA_OFFSET_ERROR;
if (!(nextflag & (SEARCH_SMALLER | SEARCH_BIGGER | SEARCH_LAST)))
DBUG_RETURN(-1); /* Not found ; return error */
DBUG_RETURN(1); /* Search at upper levels */
}
if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,
MY_TEST(!(nextflag & SEARCH_SAVE_BUFF)))))
goto err;
DBUG_DUMP("page", buff, mi_getint(buff));
flag=(*keyinfo->bin_search)(info,keyinfo,buff,key,key_len,nextflag,
&keypos,lastkey, &last_key);
if (flag == MI_FOUND_WRONG_KEY)
DBUG_RETURN(-1);
nod_flag=mi_test_if_nod(buff);
maxpos=buff+mi_getint(buff)-1;
if (flag)
{
if ((error=_mi_search(info,keyinfo,key,key_len,nextflag,
_mi_kpos(nod_flag,keypos))) <= 0)
DBUG_RETURN(error);
if (flag >0)
{
if (nextflag & (SEARCH_SMALLER | SEARCH_LAST) &&
keypos == buff+2+nod_flag)
DBUG_RETURN(1); /* Bigger than key */
}
else if (nextflag & SEARCH_BIGGER && keypos >= maxpos)
DBUG_RETURN(1); /* Smaller than key */
}
else
{
if ((nextflag & SEARCH_FIND) && nod_flag &&
((keyinfo->flag & (HA_NOSAME | HA_NULL_PART)) != HA_NOSAME ||
key_len != USE_WHOLE_KEY))
{
if ((error=_mi_search(info,keyinfo,key,key_len,SEARCH_FIND,
_mi_kpos(nod_flag,keypos))) >= 0 ||
my_errno != HA_ERR_KEY_NOT_FOUND)
DBUG_RETURN(error);
info->last_keypage= HA_OFFSET_ERROR; /* Buffer not in mem */
}
}
if (pos != info->last_keypage)
{
uchar *old_buff=buff;
if (!(buff=_mi_fetch_keypage(info,keyinfo,pos,DFLT_INIT_HITS,info->buff,
MY_TEST(!(nextflag & SEARCH_SAVE_BUFF)))))
goto err;
keypos=buff+(keypos-old_buff);
maxpos=buff+(maxpos-old_buff);
}
if ((nextflag & (SEARCH_SMALLER | SEARCH_LAST)) && flag != 0)
{
uint not_used[2];
if (_mi_get_prev_key(info,keyinfo, buff, info->lastkey, keypos,
&info->lastkey_length))
goto err;
if (!(nextflag & SEARCH_SMALLER) &&
ha_key_cmp(keyinfo->seg, info->lastkey, key, key_len, SEARCH_FIND,
not_used))
{
my_errno=HA_ERR_KEY_NOT_FOUND; /* Didn't find key */
goto err;
}
}
else
{
info->lastkey_length=(*keyinfo->get_key)(keyinfo,nod_flag,&keypos,lastkey);
if (!info->lastkey_length)
goto err;
memcpy(info->lastkey,lastkey,info->lastkey_length);
}
info->lastpos=_mi_dpos(info,0,info->lastkey+info->lastkey_length);
/* Save position for a possible read next / previous */
info->int_keypos=info->buff+ (keypos-buff);
info->int_maxpos=info->buff+ (maxpos-buff);
info->int_nod_flag=nod_flag;
info->int_keytree_version=keyinfo->version;
info->last_search_keypage=info->last_keypage;
info->page_changed=0;
info->buff_used= (info->buff != buff); /* If we have to reread buff */
DBUG_PRINT("exit",("found key at %lu",(ulong) info->lastpos));
DBUG_RETURN(0);
err:
DBUG_PRINT("exit",("Error: %d",my_errno));
info->lastpos= HA_OFFSET_ERROR;
info->page_changed=1;
DBUG_RETURN (-1);
} /* _mi_search */
/* Search after key in page-block */
/* If packed key puts smaller or identical key in buff */
/* ret_pos point to where find or bigger key starts */
/* ARGSUSED */
int _mi_bin_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
uchar *key, uint key_len, uint comp_flag, uchar **ret_pos,
uchar *buff __attribute__((unused)), my_bool *last_key)
{
reg4 int start,mid,end,save_end;
int flag;
uint totlength,nod_flag,not_used[2];
DBUG_ENTER("_mi_bin_search");
LINT_INIT(flag);
totlength=keyinfo->keylength+(nod_flag=mi_test_if_nod(page));
start=0; mid=1;
save_end=end=(int) ((mi_getint(page)-2-nod_flag)/totlength-1);
DBUG_PRINT("test",("mi_getint: %d end: %d",mi_getint(page),end));
page+=2+nod_flag;
while (start != end)
{
mid= (start+end)/2;
if ((flag=ha_key_cmp(keyinfo->seg,page+(uint) mid*totlength,key,key_len,
comp_flag, not_used))
>= 0)
end=mid;
else
start=mid+1;
}
if (mid != start)
flag=ha_key_cmp(keyinfo->seg,page+(uint) start*totlength,key,key_len,
comp_flag, not_used);
if (flag < 0)
start++; /* point at next, bigger key */
*ret_pos=page+(uint) start*totlength;
*last_key= end == save_end;
DBUG_PRINT("exit",("flag: %d keypos: %d",flag,start));
DBUG_RETURN(flag);
} /* _mi_bin_search */
/*
Locate a packed key in a key page.
SYNOPSIS
_mi_seq_search()
info Open table information.
keyinfo Key definition information.
page Key page (beginning).
key Search key.
key_len Length to use from search key or USE_WHOLE_KEY
comp_flag Search flags like SEARCH_SAME etc.
ret_pos RETURN Position in key page behind this key.
buff RETURN Copy of previous or identical unpacked key.
last_key RETURN If key is last in page.
DESCRIPTION
Used instead of _mi_bin_search() when key is packed.
Puts smaller or identical key in buff.
Key is searched sequentially.
RETURN
> 0 Key in 'buff' is smaller than search key.
0 Key in 'buff' is identical to search key.
< 0 Not found.
*/
int _mi_seq_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
uchar *key, uint key_len, uint comp_flag, uchar **ret_pos,
uchar *buff, my_bool *last_key)
{
int UNINIT_VAR(flag);
uint nod_flag,UNINIT_VAR(length),not_used[2];
uchar t_buff[MI_MAX_KEY_BUFF],*end;
DBUG_ENTER("_mi_seq_search");
end= page+mi_getint(page);
nod_flag=mi_test_if_nod(page);
page+=2+nod_flag;
*ret_pos=page;
t_buff[0]=0; /* Avoid bugs */
while (page < end)
{
length=(*keyinfo->get_key)(keyinfo,nod_flag,&page,t_buff);
if (length == 0 || page > end)
{
mi_print_error(info->s, HA_ERR_CRASHED);
my_errno=HA_ERR_CRASHED;
DBUG_PRINT("error",
("Found wrong key: length: %u page: 0x%lx end: 0x%lx",
length, (long) page, (long) end));
DBUG_RETURN(MI_FOUND_WRONG_KEY);
}
if ((flag=ha_key_cmp(keyinfo->seg,t_buff,key,key_len,comp_flag,
not_used)) >= 0)
break;
#ifdef EXTRA_DEBUG
DBUG_PRINT("loop",("page: 0x%lx key: '%s' flag: %d", (long) page, t_buff,
flag));
#endif
memcpy(buff,t_buff,length);
*ret_pos=page;
}
if (flag == 0)
memcpy(buff,t_buff,length); /* Result is first key */
*last_key= page == end;
DBUG_PRINT("exit",("flag: %d ret_pos: 0x%lx", flag, (long) *ret_pos));
DBUG_RETURN(flag);
} /* _mi_seq_search */
int _mi_prefix_search(MI_INFO *info, register MI_KEYDEF *keyinfo, uchar *page,
uchar *key, uint key_len, uint nextflag, uchar **ret_pos,
uchar *buff, my_bool *last_key)
{
/*
my_flag is raw comparison result to be changed according to
SEARCH_NO_FIND,SEARCH_LAST and HA_REVERSE_SORT flags.
flag is the value returned by ha_key_cmp and as treated as final
*/
int flag=0, my_flag=-1;
uint nod_flag, UNINIT_VAR(length), len, matched, cmplen, kseg_len;
uint UNINIT_VAR(prefix_len), suffix_len;
int key_len_skip, UNINIT_VAR(seg_len_pack), key_len_left;
uchar *end, *kseg, *vseg;
uchar *sort_order=keyinfo->seg->charset->sort_order;
uchar tt_buff[MI_MAX_KEY_BUFF+2], *t_buff=tt_buff+2;
uchar *UNINIT_VAR(saved_from), *UNINIT_VAR(saved_to);
uchar *UNINIT_VAR(saved_vseg);
uint saved_length=0, saved_prefix_len=0;
uint length_pack;
DBUG_ENTER("_mi_prefix_search");
t_buff[0]=0; /* Avoid bugs */
end= page+mi_getint(page);
nod_flag=mi_test_if_nod(page);
page+=2+nod_flag;
*ret_pos=page;
kseg=key;
get_key_pack_length(kseg_len,length_pack,kseg);
key_len_skip=length_pack+kseg_len;
key_len_left=(int) key_len- (int) key_len_skip;
/* If key_len is 0, then lenght_pack is 1, then key_len_left is -1. */
cmplen=(key_len_left>=0) ? kseg_len : key_len-length_pack;
DBUG_PRINT("info",("key: '%.*s'",kseg_len,kseg));
/*
Keys are compressed the following way:
If the max length of first key segment <= 127 bytes the prefix is
1 byte else it's 2 byte
(prefix) length The high bit is set if this is a prefix for the prev key.
[suffix length] Packed length of suffix if the previous was a prefix.
(suffix) data Key data bytes (past the common prefix or whole segment).
[next-key-seg] Next key segments (([packed length], data), ...)
pointer Reference to the data file (last_keyseg->length).
*/
matched=0; /* how many char's from prefix were alredy matched */
len=0; /* length of previous key unpacked */
while (page < end)
{
uint packed= *page & 128;
vseg=page;
if (keyinfo->seg->length >= 127)
{
suffix_len=mi_uint2korr(vseg) & 32767;
vseg+=2;
}
else
suffix_len= *vseg++ & 127;
if (packed)
{
if (suffix_len == 0)
{
/* == 0x80 or 0x8000, same key, prefix length == old key length. */
prefix_len=len;
}
else
{
/* > 0x80 or 0x8000, this is prefix lgt, packed suffix lgt follows. */
prefix_len=suffix_len;
get_key_length(suffix_len,vseg);
}
}
else
{
/* Not packed. No prefix used from last key. */
prefix_len=0;
}
len=prefix_len+suffix_len;
seg_len_pack=get_pack_length(len);
t_buff=tt_buff+3-seg_len_pack;
store_key_length(t_buff,len);
if (prefix_len > saved_prefix_len)
memcpy(t_buff+seg_len_pack+saved_prefix_len,saved_vseg,
prefix_len-saved_prefix_len);
saved_vseg=vseg;
saved_prefix_len=prefix_len;
DBUG_PRINT("loop",("page: '%.*s%.*s'",prefix_len,t_buff+seg_len_pack,
suffix_len,vseg));
{
uchar *from=vseg+suffix_len;
HA_KEYSEG *keyseg;
uint l;
for (keyseg=keyinfo->seg+1 ; keyseg->type ; keyseg++ )
{
if (keyseg->flag & HA_NULL_PART)
{
if (!(*from++))
continue;
}
if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK))
{
get_key_length(l,from);
}
else
l=keyseg->length;
from+=l;
}
from+=keyseg->length;
page=from+nod_flag;
length= (uint) (from - vseg);
}
if (page > end)
{
mi_print_error(info->s, HA_ERR_CRASHED);
my_errno=HA_ERR_CRASHED;
DBUG_PRINT("error",
("Found wrong key: length: %u page: 0x%lx end: %lx",
length, (long) page, (long) end));
DBUG_RETURN(MI_FOUND_WRONG_KEY);
}
if (matched >= prefix_len)
{
/* We have to compare. But we can still skip part of the key */
uint left;
uchar *k=kseg+prefix_len;
/*
If prefix_len > cmplen then we are in the end-space comparison
phase. Do not try to acces the key any more ==> left= 0.
*/
left= ((len <= cmplen) ? suffix_len :
((prefix_len < cmplen) ? cmplen - prefix_len : 0));
matched=prefix_len+left;
if (sort_order)
{
for (my_flag=0;left;left--)
if ((my_flag= (int) sort_order[*vseg++] - (int) sort_order[*k++]))
break;
}
else
{
for (my_flag=0;left;left--)
if ((my_flag= (int) *vseg++ - (int) *k++))
break;
}
if (my_flag>0) /* mismatch */
break;
if (my_flag==0) /* match */
{
/*
** len cmplen seg_left_len more_segs
** < matched=len; continue search
** > = prefix ? found : (matched=len; continue search)
** > < - ok, found
** = < - ok, found
** = = - ok, found
** = = + next seg
*/
if (len < cmplen)
{
if ((keyinfo->seg->type != HA_KEYTYPE_TEXT &&
keyinfo->seg->type != HA_KEYTYPE_VARTEXT1 &&
keyinfo->seg->type != HA_KEYTYPE_VARTEXT2))
my_flag= -1;
else
{
/* We have to compare k and vseg as if they were space extended */
uchar *k_end= k+ (cmplen - len);
for ( ; k < k_end && *k == ' '; k++) ;
if (k == k_end)
goto cmp_rest; /* should never happen */
if (*k < (uchar) ' ')
{
my_flag= 1; /* Compared string is smaller */
break;
}
my_flag= -1; /* Continue searching */
}
}
else if (len > cmplen)
{
uchar *vseg_end;
if ((nextflag & SEARCH_PREFIX) && key_len_left == 0)
goto fix_flag;
/* We have to compare k and vseg as if they were space extended */
for (vseg_end= vseg + (len-cmplen) ;
vseg < vseg_end && *vseg == (uchar) ' ';
vseg++, matched++) ;
DBUG_ASSERT(vseg < vseg_end);
if (*vseg > (uchar) ' ')
{
my_flag= 1; /* Compared string is smaller */
break;
}
my_flag= -1; /* Continue searching */
}
else
{
cmp_rest:
if (key_len_left>0)
{
uint not_used[2];
if ((flag = ha_key_cmp(keyinfo->seg+1,vseg,
k, key_len_left, nextflag, not_used)) >= 0)
break;
}
else
{
/*
at this line flag==-1 if the following lines were already
visited and 0 otherwise, i.e. flag <=0 here always !!!
*/
fix_flag:
DBUG_ASSERT(flag <= 0);
if (nextflag & (SEARCH_NO_FIND | SEARCH_LAST))
flag=(nextflag & (SEARCH_BIGGER | SEARCH_LAST)) ? -1 : 1;
if (flag>=0)
break;
}
}
}
matched-=left;
}
/* else (matched < prefix_len) ---> do nothing. */
memcpy(buff,t_buff,saved_length=seg_len_pack+prefix_len);
saved_to=buff+saved_length;
saved_from=saved_vseg;
saved_length=length;
*ret_pos=page;
}
if (my_flag)
flag=(keyinfo->seg->flag & HA_REVERSE_SORT) ? -my_flag : my_flag;
if (flag == 0)
{
memcpy(buff,t_buff,saved_length=seg_len_pack+prefix_len);
saved_to=buff+saved_length;
saved_from=saved_vseg;
saved_length=length;
}
if (saved_length)
memcpy(saved_to,saved_from,saved_length);
*last_key= page == end;
DBUG_PRINT("exit",("flag: %d ret_pos: 0x%lx", flag, (long) *ret_pos));
DBUG_RETURN(flag);
} /* _mi_prefix_search */
/* Get pos to a key_block */
my_off_t _mi_kpos(uint nod_flag, uchar *after_key)
{
after_key-=nod_flag;
switch (nod_flag) {
#if SIZEOF_OFF_T > 4
case 7:
return mi_uint7korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
case 6:
return mi_uint6korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
case 5:
return mi_uint5korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH;
#else
case 7:
after_key++;
case 6:
after_key++;
case 5:
after_key++;
#endif
case 4:
return ((my_off_t) mi_uint4korr(after_key))*MI_MIN_KEY_BLOCK_LENGTH;
case 3:
return ((my_off_t) mi_uint3korr(after_key))*MI_MIN_KEY_BLOCK_LENGTH;
case 2:
return (my_off_t) (mi_uint2korr(after_key)*MI_MIN_KEY_BLOCK_LENGTH);
case 1:
return (uint) (*after_key)*MI_MIN_KEY_BLOCK_LENGTH;
case 0: /* At leaf page */
default: /* Impossible */
return(HA_OFFSET_ERROR);
}
} /* _kpos */
/* Save pos to a key_block */
void _mi_kpointer(register MI_INFO *info, register uchar *buff, my_off_t pos)
{
pos/=MI_MIN_KEY_BLOCK_LENGTH;
switch (info->s->base.key_reflength) {
#if SIZEOF_OFF_T > 4
case 7: mi_int7store(buff,pos); break;
case 6: mi_int6store(buff,pos); break;
case 5: mi_int5store(buff,pos); break;
#else
case 7: *buff++=0;
/* fall trough */
case 6: *buff++=0;
/* fall trough */
case 5: *buff++=0;
/* fall trough */
#endif
case 4: mi_int4store(buff,pos); break;
case 3: mi_int3store(buff,pos); break;
case 2: mi_int2store(buff,(uint) pos); break;
case 1: buff[0]= (uchar) pos; break;
default: abort(); /* impossible */
}
} /* _mi_kpointer */
/* Calc pos to a data-record from a key */
my_off_t _mi_dpos(MI_INFO *info, uint nod_flag, uchar *after_key)
{
my_off_t pos;
after_key-=(nod_flag + info->s->rec_reflength);
switch (info->s->rec_reflength) {
#if SIZEOF_OFF_T > 4
case 8: pos= (my_off_t) mi_uint8korr(after_key); break;
case 7: pos= (my_off_t) mi_uint7korr(after_key); break;
case 6: pos= (my_off_t) mi_uint6korr(after_key); break;
case 5: pos= (my_off_t) mi_uint5korr(after_key); break;
#else
case 8: pos= (my_off_t) mi_uint4korr(after_key+4); break;
case 7: pos= (my_off_t) mi_uint4korr(after_key+3); break;
case 6: pos= (my_off_t) mi_uint4korr(after_key+2); break;
case 5: pos= (my_off_t) mi_uint4korr(after_key+1); break;
#endif
case 4: pos= (my_off_t) mi_uint4korr(after_key); break;
case 3: pos= (my_off_t) mi_uint3korr(after_key); break;
case 2: pos= (my_off_t) mi_uint2korr(after_key); break;
default:
pos=0L; /* Shut compiler up */
}
return (info->s->options &
(HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ? pos :
pos*info->s->base.pack_reclength;
}
/* Calc position from a record pointer ( in delete link chain ) */
my_off_t _mi_rec_pos(MYISAM_SHARE *s, uchar *ptr)
{
my_off_t pos;
switch (s->rec_reflength) {
#if SIZEOF_OFF_T > 4
case 8:
pos= (my_off_t) mi_uint8korr(ptr);
if (pos == HA_OFFSET_ERROR)
return HA_OFFSET_ERROR; /* end of list */
break;
case 7:
pos= (my_off_t) mi_uint7korr(ptr);
if (pos == (((my_off_t) 1) << 56) -1)
return HA_OFFSET_ERROR; /* end of list */
break;
case 6:
pos= (my_off_t) mi_uint6korr(ptr);
if (pos == (((my_off_t) 1) << 48) -1)
return HA_OFFSET_ERROR; /* end of list */
break;
case 5:
pos= (my_off_t) mi_uint5korr(ptr);
if (pos == (((my_off_t) 1) << 40) -1)
return HA_OFFSET_ERROR; /* end of list */
break;
#else
case 8:
case 7:
case 6:
case 5:
ptr+= (s->rec_reflength-4);
/* fall through */
#endif
case 4:
pos= (my_off_t) mi_uint4korr(ptr);
if (pos == (my_off_t) (uint32) ~0L)
return HA_OFFSET_ERROR;
break;
case 3:
pos= (my_off_t) mi_uint3korr(ptr);
if (pos == (my_off_t) (1 << 24) -1)
return HA_OFFSET_ERROR;
break;
case 2:
pos= (my_off_t) mi_uint2korr(ptr);
if (pos == (my_off_t) (1 << 16) -1)
return HA_OFFSET_ERROR;
break;
default: abort(); /* Impossible */
}
return ((s->options &
(HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) ? pos :
pos*s->base.pack_reclength);
}
/* save position to record */
void _mi_dpointer(MI_INFO *info, uchar *buff, my_off_t pos)
{
if (!(info->s->options &
(HA_OPTION_PACK_RECORD | HA_OPTION_COMPRESS_RECORD)) &&
pos != HA_OFFSET_ERROR)
pos/=info->s->base.pack_reclength;
switch (info->s->rec_reflength) {
#if SIZEOF_OFF_T > 4
case 8: mi_int8store(buff,pos); break;
case 7: mi_int7store(buff,pos); break;
case 6: mi_int6store(buff,pos); break;
case 5: mi_int5store(buff,pos); break;
#else
case 8: *buff++=0;
/* fall trough */
case 7: *buff++=0;
/* fall trough */
case 6: *buff++=0;
/* fall trough */
case 5: *buff++=0;
/* fall trough */
#endif
case 4: mi_int4store(buff,pos); break;
case 3: mi_int3store(buff,pos); break;
case 2: mi_int2store(buff,(uint) pos); break;
default: abort(); /* Impossible */
}
} /* _mi_dpointer */
/* Get key from key-block */
/* page points at previous key; its advanced to point at next key */
/* key should contain previous key */
/* Returns length of found key + pointers */
/* nod_flag is a flag if we are on nod */
/* same as _mi_get_key but used with fixed length keys */
uint _mi_get_static_key(register MI_KEYDEF *keyinfo, uint nod_flag,
register uchar **page, register uchar *key)
{
memcpy((uchar*) key,(uchar*) *page,
(size_t) (keyinfo->keylength+nod_flag));
*page+=keyinfo->keylength+nod_flag;
return(keyinfo->keylength);
} /* _mi_get_static_key */
/*
get key witch is packed against previous key or key with a NULL column.
SYNOPSIS
_mi_get_pack_key()
keyinfo key definition information.
nod_flag If nod: Length of node pointer, else zero.
page_pos RETURN position in key page behind this key.
key IN/OUT in: prev key, out: unpacked key.
RETURN
key_length + length of data pointer
*/
uint _mi_get_pack_key(register MI_KEYDEF *keyinfo, uint nod_flag,
register uchar **page_pos, register uchar *key)
{
reg1 HA_KEYSEG *keyseg;
uchar *start_key,*page=*page_pos;
uint length;
start_key=key;
for (keyseg=keyinfo->seg ; keyseg->type ;keyseg++)
{
if (keyseg->flag & HA_PACK_KEY)
{
/* key with length, packed to previous key */
uchar *start=key;
uint packed= *page & 128,tot_length,rest_length;
if (keyseg->length >= 127)
{
length=mi_uint2korr(page) & 32767;
page+=2;
}
else
length= *page++ & 127;
if (packed)
{
if (length > (uint) keyseg->length)
{
mi_print_error(keyinfo->share, HA_ERR_CRASHED);
my_errno=HA_ERR_CRASHED;
return 0; /* Error */
}
if (length == 0) /* Same key */
{
if (keyseg->flag & HA_NULL_PART)
*key++=1; /* Can't be NULL */
get_key_length(length,key);
key+= length; /* Same diff_key as prev */
if (length > keyseg->length)
{
DBUG_PRINT("error",
("Found too long null packed key: %u of %u at 0x%lx",
length, keyseg->length, (long) *page_pos));
DBUG_DUMP("key", *page_pos, 16);
mi_print_error(keyinfo->share, HA_ERR_CRASHED);
my_errno=HA_ERR_CRASHED;
return 0;
}
continue;
}
if (keyseg->flag & HA_NULL_PART)
{
key++; /* Skip null marker*/
start++;
}
get_key_length(rest_length,page);
tot_length=rest_length+length;
/* If the stored length has changed, we must move the key */
if (tot_length >= 255 && *start != 255)
{
/* length prefix changed from a length of one to a length of 3 */
bmove_upp(key+length+3, key+length+1, length);
*key=255;
mi_int2store(key+1,tot_length);
key+=3+length;
}
else if (tot_length < 255 && *start == 255)
{
bmove(key+1,key+3,length);
*key=tot_length;
key+=1+length;
}
else
{
store_key_length_inc(key,tot_length);
key+=length;
}
memcpy(key,page,rest_length);
page+=rest_length;
key+=rest_length;
continue;
}
else
{
if (keyseg->flag & HA_NULL_PART)
{
if (!length--) /* Null part */
{
*key++=0;
continue;
}
*key++=1; /* Not null */
}
}
if (length > (uint) keyseg->length)
{
DBUG_PRINT("error",("Found too long packed key: %u of %u at 0x%lx",
length, keyseg->length, (long) *page_pos));
DBUG_DUMP("key", *page_pos, 16);
mi_print_error(keyinfo->share, HA_ERR_CRASHED);
my_errno=HA_ERR_CRASHED;
return 0; /* Error */
}
store_key_length_inc(key,length);
}
else
{
if (keyseg->flag & HA_NULL_PART)
{
if (!(*key++ = *page++))
continue;
}
if (keyseg->flag &
(HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK))
{
uchar *tmp=page;
get_key_length(length,tmp);
length+=(uint) (tmp-page);
}
else
length=keyseg->length;
}
memcpy((uchar*) key,(uchar*) page,(size_t) length);
key+=length;
page+=length;
}
length=keyseg->length+nod_flag;
bmove((uchar*) key,(uchar*) page,length);
*page_pos= page+length;
return ((uint) (key-start_key)+keyseg->length);
} /* _mi_get_pack_key */
/* key that is packed relatively to previous */
uint _mi_get_binary_pack_key(register MI_KEYDEF *keyinfo, uint nod_flag,
register uchar **page_pos, register uchar *key)
{
reg1 HA_KEYSEG *keyseg;
uchar *start_key,*page,*page_end,*from,*from_end;
uint length,tmp;
DBUG_ENTER("_mi_get_binary_pack_key");
page= *page_pos;
page_end=page+MI_MAX_KEY_BUFF+1;
start_key=key;
/*
Keys are compressed the following way:
prefix length Packed length of prefix common with prev key (1 or 3 bytes)
for each key segment:
[is null] Null indicator if can be null (1 byte, zero means null)
[length] Packed length if varlength (1 or 3 bytes)
key segment 'length' bytes of key segment value
pointer Reference to the data file (last_keyseg->length).
get_key_length() is a macro. It gets the prefix length from 'page'
and puts it into 'length'. It increments 'page' by 1 or 3, depending
on the packed length of the prefix length.
*/
get_key_length(length,page);
if (length)
{
if (length > keyinfo->maxlength)
{
DBUG_PRINT("error",
("Found too long binary packed key: %u of %u at 0x%lx",
length, keyinfo->maxlength, (long) *page_pos));
DBUG_DUMP("key", *page_pos, 16);
goto crashed; /* Wrong key */
}
/* Key is packed against prev key, take prefix from prev key. */
from= key;
from_end= key + length;
}
else
{
/* Key is not packed against prev key, take all from page buffer. */
from= page;
from_end= page_end;
}
/*
The trouble is that key can be split in two parts:
The first part (prefix) is in from .. from_end - 1.
The second part starts at page.
The split can be at every byte position. So we need to check for
the end of the first part before using every byte.
*/
for (keyseg=keyinfo->seg ; keyseg->type ;keyseg++)
{
if (keyseg->flag & HA_NULL_PART)
{
/* If prefix is used up, switch to rest. */
if (from == from_end) { from=page; from_end=page_end; }
if (!(*key++ = *from++))
continue; /* Null part */
}
if (keyseg->flag & (HA_VAR_LENGTH_PART | HA_BLOB_PART | HA_SPACE_PACK))
{
/* If prefix is used up, switch to rest. */
if (from == from_end) { from=page; from_end=page_end; }
/* Get length of dynamic length key part */
if ((length= (*key++ = *from++)) == 255)
{
/* If prefix is used up, switch to rest. */
if (from == from_end) { from=page; from_end=page_end; }
length= (uint) ((*key++ = *from++)) << 8;
/* If prefix is used up, switch to rest. */
if (from == from_end) { from=page; from_end=page_end; }
length+= (uint) ((*key++ = *from++));
}
if (length > keyseg->length)
goto crashed;
}
else
length=keyseg->length;
if ((tmp=(uint) (from_end-from)) <= length)
{
key+=tmp; /* Use old key */
length-=tmp;