summaryrefslogtreecommitdiff
path: root/heap/hp_hash.c
diff options
context:
space:
mode:
Diffstat (limited to 'heap/hp_hash.c')
-rw-r--r--heap/hp_hash.c241
1 files changed, 187 insertions, 54 deletions
diff --git a/heap/hp_hash.c b/heap/hp_hash.c
index e29e51d2b75..e28f4725caf 100644
--- a/heap/hp_hash.c
+++ b/heap/hp_hash.c
@@ -19,30 +19,73 @@
#include "heapdef.h"
#include <m_ctype.h>
+ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, const byte *start_key,
+ uint start_key_len,
+ enum ha_rkey_function start_search_flag,
+ const byte *end_key, uint end_key_len,
+ enum ha_rkey_function end_search_flag)
+{
+ ha_rows start_pos, end_pos;
+ HP_KEYDEF *keyinfo= info->s->keydef + inx;
+ TREE *rb_tree = &keyinfo->rb_tree;
+ heap_rb_param custom_arg;
+
+ info->lastinx = inx;
+ custom_arg.keyseg = keyinfo->seg;
+ custom_arg.search_flag = SEARCH_FIND | SEARCH_SAME;
+ custom_arg.key_length = start_key_len;
+ if (start_key)
+ {
+ hp_rb_pack_key(keyinfo, info->recbuf, start_key);
+ start_pos= tree_record_pos(rb_tree, info->recbuf, start_search_flag,
+ &custom_arg);
+ }
+ else
+ {
+ start_pos= 0;
+ }
+
+ custom_arg.key_length = end_key_len;
+ if (end_key)
+ {
+ hp_rb_pack_key(keyinfo, info->recbuf, end_key);
+ end_pos= tree_record_pos(rb_tree, info->recbuf, end_search_flag,
+ &custom_arg);
+ }
+ else
+ {
+ end_pos= rb_tree->elements_in_tree + (ha_rows)1;
+ }
+
+ if (start_pos == HA_POS_ERROR || end_pos == HA_POS_ERROR)
+ return HA_POS_ERROR;
+ return end_pos < start_pos ? (ha_rows) 0 :
+ (end_pos == start_pos ? (ha_rows) 1 : end_pos - start_pos);
+}
+
/* Search after a record based on a key */
/* Sets info->current_ptr to found record */
/* next_flag: Search=0, next=1, prev =2, same =3 */
-byte *_hp_search(HP_INFO *info, HP_KEYDEF *keyinfo, const byte *key,
+byte *hp_search(HP_INFO *info, HP_KEYDEF *keyinfo, const byte *key,
uint nextflag)
{
reg1 HASH_INFO *pos,*prev_ptr;
int flag;
uint old_nextflag;
HP_SHARE *share=info->s;
- DBUG_ENTER("_hp_search");
-
+ DBUG_ENTER("hp_search");
old_nextflag=nextflag;
flag=1;
prev_ptr=0;
if (share->records)
{
- pos=hp_find_hash(&keyinfo->block,_hp_mask(_hp_hashnr(keyinfo,key),
- share->blength,share->records));
+ pos=hp_find_hash(&keyinfo->block, hp_mask(hp_hashnr(keyinfo, key),
+ share->blength, share->records));
do
{
- if (!_hp_key_cmp(keyinfo,pos->ptr_to_rec,key))
+ if (!hp_key_cmp(keyinfo, pos->ptr_to_rec, key))
{
switch (nextflag) {
case 0: /* Search after key */
@@ -74,8 +117,8 @@ byte *_hp_search(HP_INFO *info, HP_KEYDEF *keyinfo, const byte *key,
{
flag=0; /* Reset flag */
if (hp_find_hash(&keyinfo->block,
- _hp_mask(_hp_rec_hashnr(keyinfo,pos->ptr_to_rec),
- share->blength,share->records)) != pos)
+ hp_mask(hp_rec_hashnr(keyinfo, pos->ptr_to_rec),
+ share->blength, share->records)) != pos)
break; /* Wrong link */
}
}
@@ -102,14 +145,14 @@ byte *_hp_search(HP_INFO *info, HP_KEYDEF *keyinfo, const byte *key,
since last read !
*/
-byte *_hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo, const byte *key,
+byte *hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo, const byte *key,
HASH_INFO *pos)
{
- DBUG_ENTER("_hp_search_next");
+ DBUG_ENTER("hp_search_next");
while ((pos= pos->next_key))
{
- if (!_hp_key_cmp(keyinfo,pos->ptr_to_rec,key))
+ if (! hp_key_cmp(keyinfo, pos->ptr_to_rec, key))
{
info->current_hash_ptr=pos;
DBUG_RETURN (info->current_ptr= pos->ptr_to_rec);
@@ -124,7 +167,7 @@ byte *_hp_search_next(HP_INFO *info, HP_KEYDEF *keyinfo, const byte *key,
/* Calculate pos according to keys */
-ulong _hp_mask(ulong hashnr, ulong buffmax, ulong maxlength)
+ulong hp_mask(ulong hashnr, ulong buffmax, ulong maxlength)
{
if ((hashnr & (buffmax-1)) < maxlength) return (hashnr & (buffmax-1));
return (hashnr & ((buffmax >> 1) -1));
@@ -133,7 +176,7 @@ ulong _hp_mask(ulong hashnr, ulong buffmax, ulong maxlength)
/* Change link from pos to new_link */
-void _hp_movelink(HASH_INFO *pos, HASH_INFO *next_link, HASH_INFO *newlink)
+void hp_movelink(HASH_INFO *pos, HASH_INFO *next_link, HASH_INFO *newlink)
{
HASH_INFO *old_link;
do
@@ -149,10 +192,11 @@ void _hp_movelink(HASH_INFO *pos, HASH_INFO *next_link, HASH_INFO *newlink)
/* Calc hashvalue for a key */
-ulong _hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
+ulong hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
{
- register ulong nr=1, nr2=4;
- HP_KEYSEG *seg,*endseg;
+ /*register*/
+ ulong nr=1, nr2=4;
+ HA_KEYSEG *seg,*endseg;
for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
{
@@ -170,12 +214,7 @@ ulong _hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
}
if (seg->type == HA_KEYTYPE_TEXT)
{
- for (; pos < (uchar*) key ; pos++)
- {
- nr^=(ulong) ((((uint) nr & 63)+nr2) *
- ((uint) my_sort_order[(uint) *pos])) + (nr << 8);
- nr2+=3;
- }
+ seg->charset->hash_sort(seg->charset,pos,((uchar*)key)-pos,&nr,&nr2);
}
else
{
@@ -191,10 +230,11 @@ ulong _hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
/* Calc hashvalue for a key in a record */
-ulong _hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
+ulong hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
{
- register ulong nr=1, nr2=4;
- HP_KEYSEG *seg,*endseg;
+ /*register*/
+ ulong nr=1, nr2=4;
+ HA_KEYSEG *seg,*endseg;
for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
{
@@ -209,12 +249,7 @@ ulong _hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
}
if (seg->type == HA_KEYTYPE_TEXT)
{
- for (; pos < end ; pos++)
- {
- nr^=(ulong) ((((uint) nr & 63)+nr2)*
- ((uint) my_sort_order[(uint) *pos]))+ (nr << 8);
- nr2+=3;
- }
+ seg->charset->hash_sort(seg->charset,pos,end-pos,&nr,&nr2);
}
else
{
@@ -245,10 +280,10 @@ ulong _hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
* far, and works well on both numbers and strings.
*/
-ulong _hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
+ulong hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
{
register ulong nr=0;
- HP_KEYSEG *seg,*endseg;
+ HA_KEYSEG *seg,*endseg;
for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
{
@@ -266,11 +301,7 @@ ulong _hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
}
if (seg->type == HA_KEYTYPE_TEXT)
{
- for (; pos < (uchar*) key ; pos++)
- {
- nr *=16777619;
- nr ^=((uint) my_sort_order[(uint) *pos]);
- }
+ seg->charset->hash_sort(seg->charset,pos,((uchar*)key)-pos,&nr,NULL);
}
else
{
@@ -286,10 +317,10 @@ ulong _hp_hashnr(register HP_KEYDEF *keydef, register const byte *key)
/* Calc hashvalue for a key in a record */
-ulong _hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
+ulong hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
{
register ulong nr=0;
- HP_KEYSEG *seg,*endseg;
+ HA_KEYSEG *seg,*endseg;
for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
{
@@ -304,11 +335,7 @@ ulong _hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
}
if (seg->type == HA_KEYTYPE_TEXT)
{
- for ( ; pos < end ; pos++)
- {
- nr *=16777619;
- nr ^=(uint) my_sort_order[(uint) *pos];
- }
+ seg->charset->hash_sort(seg->charset,pos,((uchar*)key)-pos,&nr,NULL);
}
else
{
@@ -327,9 +354,9 @@ ulong _hp_rec_hashnr(register HP_KEYDEF *keydef, register const byte *rec)
/* Compare keys for two records. Returns 0 if they are identical */
-int _hp_rec_key_cmp(HP_KEYDEF *keydef, const byte *rec1, const byte *rec2)
+int hp_rec_key_cmp(HP_KEYDEF *keydef, const byte *rec1, const byte *rec2)
{
- HP_KEYSEG *seg,*endseg;
+ HA_KEYSEG *seg,*endseg;
for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
{
@@ -343,7 +370,7 @@ int _hp_rec_key_cmp(HP_KEYDEF *keydef, const byte *rec1, const byte *rec2)
}
if (seg->type == HA_KEYTYPE_TEXT)
{
- if (my_sortcmp(rec1+seg->start,rec2+seg->start,seg->length))
+ if (my_strnncoll(seg->charset,rec1+seg->start,seg->length,rec2+seg->start,seg->length))
return 1;
}
else
@@ -357,9 +384,9 @@ int _hp_rec_key_cmp(HP_KEYDEF *keydef, const byte *rec1, const byte *rec2)
/* Compare a key in a record to a whole key */
-int _hp_key_cmp(HP_KEYDEF *keydef, const byte *rec, const byte *key)
+int hp_key_cmp(HP_KEYDEF *keydef, const byte *rec, const byte *key)
{
- HP_KEYSEG *seg,*endseg;
+ HA_KEYSEG *seg,*endseg;
for (seg=keydef->seg,endseg=seg+keydef->keysegs ;
seg < endseg ;
@@ -375,7 +402,7 @@ int _hp_key_cmp(HP_KEYDEF *keydef, const byte *rec, const byte *key)
}
if (seg->type == HA_KEYTYPE_TEXT)
{
- if (my_sortcmp(rec+seg->start,key,seg->length))
+ if (my_strnncoll(seg->charset,rec+seg->start,seg->length,key,seg->length))
return 1;
}
else
@@ -390,9 +417,9 @@ int _hp_key_cmp(HP_KEYDEF *keydef, const byte *rec, const byte *key)
/* Copy a key from a record to a keybuffer */
-void _hp_make_key(HP_KEYDEF *keydef, byte *key, const byte *rec)
+void hp_make_key(HP_KEYDEF *keydef, byte *key, const byte *rec)
{
- HP_KEYSEG *seg,*endseg;
+ HA_KEYSEG *seg,*endseg;
for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
{
@@ -403,7 +430,65 @@ void _hp_make_key(HP_KEYDEF *keydef, byte *key, const byte *rec)
}
}
+uint hp_rb_make_key(HP_KEYDEF *keydef, byte *key,
+ const byte *rec, byte *recpos)
+{
+ byte *start_key= key;
+ HA_KEYSEG *seg, *endseg;
+
+ for (seg= keydef->seg, endseg= seg + keydef->keysegs; seg < endseg; seg++)
+ {
+ if (seg->null_bit)
+ {
+ if (!(*key++= 1 - test(rec[seg->null_pos] & seg->null_bit)))
+ continue;
+ }
+ memcpy(key, rec + seg->start, (size_t) seg->length);
+ key+= seg->length;
+ }
+ memcpy(key, &recpos, sizeof(byte*));
+ return key - start_key;
+}
+
+uint hp_rb_pack_key(HP_KEYDEF *keydef, uchar *key, const uchar *old)
+{
+ HA_KEYSEG *seg, *endseg;
+ uchar *start_key= key;
+
+ for (seg= keydef->seg, endseg= seg + keydef->keysegs; seg < endseg;
+ old+= seg->length, seg++)
+ {
+ if (seg->null_bit)
+ {
+ if (!(*key++= (char) 1 - *old++))
+ continue;
+ }
+ memcpy((byte*) key, old, seg->length);
+ key+= seg->length;
+ }
+ return key - start_key;
+}
+
+uint hp_rb_key_length(HP_KEYDEF *keydef,
+ const byte *key __attribute__((unused)))
+{
+ return keydef->length;
+}
+uint hp_rb_null_key_length(HP_KEYDEF *keydef, const byte *key)
+{
+ const byte *start_key= key;
+ HA_KEYSEG *seg, *endseg;
+
+ for (seg= keydef->seg, endseg= seg + keydef->keysegs; seg < endseg; seg++)
+ {
+ if (seg->null_bit && !*key++)
+ continue;
+ key+= seg->length;
+ }
+ return key - start_key;
+}
+
/*
Test if any of the key parts are NULL.
Return:
@@ -413,7 +498,7 @@ void _hp_make_key(HP_KEYDEF *keydef, byte *key, const byte *rec)
my_bool hp_if_null_in_key(HP_KEYDEF *keydef, const byte *record)
{
- HP_KEYSEG *seg,*endseg;
+ HA_KEYSEG *seg,*endseg;
for (seg=keydef->seg,endseg=seg+keydef->keysegs ; seg < endseg ; seg++)
{
if (seg->null_bit && (record[seg->null_pos] & seg->null_bit))
@@ -421,3 +506,51 @@ my_bool hp_if_null_in_key(HP_KEYDEF *keydef, const byte *record)
}
return 0;
}
+
+void heap_update_auto_increment(HP_INFO *info, const byte *record)
+{
+ ulonglong value;
+ HA_KEYSEG *keyseg= info->s->keydef[info->s->auto_key - 1].seg;
+ const uchar *key= (uchar*) record + keyseg->start;
+
+ switch (info->s->auto_key_type) {
+ case HA_KEYTYPE_INT8:
+ case HA_KEYTYPE_BINARY:
+ value= (ulonglong) *(uchar*) key;
+ break;
+ case HA_KEYTYPE_SHORT_INT:
+ case HA_KEYTYPE_USHORT_INT:
+ value= (ulonglong) uint2korr(key);
+ break;
+ case HA_KEYTYPE_LONG_INT:
+ case HA_KEYTYPE_ULONG_INT:
+ value= (ulonglong) uint4korr(key);
+ break;
+ case HA_KEYTYPE_INT24:
+ case HA_KEYTYPE_UINT24:
+ value= (ulonglong) uint3korr(key);
+ break;
+ case HA_KEYTYPE_FLOAT: /* This shouldn't be used */
+ {
+ float f_1;
+ float4get(f_1, key);
+ value= (ulonglong) f_1;
+ break;
+ }
+ case HA_KEYTYPE_DOUBLE: /* This shouldn't be used */
+ {
+ double f_1;
+ float8get(f_1, key);
+ value= (ulonglong) f_1;
+ break;
+ }
+ case HA_KEYTYPE_LONGLONG:
+ case HA_KEYTYPE_ULONGLONG:
+ value= uint8korr(key);
+ break;
+ default:
+ value= 0; /* Error */
+ break;
+ }
+ set_if_bigger(info->s->auto_increment, value);
+}