Commit c1f3be5b authored by unknown's avatar unknown

Heap table code cleanup

parent e494b724
......@@ -82,11 +82,11 @@ extern void hp_make_key(HP_KEYDEF *keydef,byte *key,const byte *rec);
extern uint hp_rb_make_key(HP_KEYDEF *keydef, byte *key,
const byte *rec, byte *recpos);
extern uint hp_rb_key_length(HP_KEYDEF *keydef, const byte *key);
extern uint hp_rb_null_key_length(HP_KEYDEF *keydef, const byte *key);
extern my_bool hp_if_null_in_key(HP_KEYDEF *keyinfo, const byte *record);
extern int hp_close(register HP_INFO *info);
extern void hp_clear(HP_SHARE *info);
extern uint hp_rb_pack_key(HP_INFO *info, uint inx, uchar *key,
const uchar *old, uint k_length);
extern uint hp_rb_pack_key(HP_KEYDEF *keydef, uchar *key, const uchar *old);
#ifdef THREAD
extern pthread_mutex_t THR_LOCK_heap;
#else
......
......@@ -26,16 +26,17 @@ ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, const byte *start_key,
enum ha_rkey_function end_search_flag)
{
ha_rows start_pos, end_pos;
TREE *rb_tree = &info->s->keydef[inx].rb_tree;
HP_KEYDEF *keyinfo= info->s->keydef + inx;
TREE *rb_tree = &keyinfo->rb_tree;
heap_rb_param custom_arg;
info->lastinx = inx;
custom_arg.keyseg = info->s->keydef[inx].seg;
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(info, inx, info->recbuf, start_key, start_key_len);
hp_rb_pack_key(keyinfo, info->recbuf, start_key);
start_pos= tree_record_pos(rb_tree, info->recbuf, start_search_flag,
&custom_arg);
}
......@@ -47,7 +48,7 @@ ha_rows hp_rb_records_in_range(HP_INFO *info, int inx, const byte *start_key,
custom_arg.key_length = end_key_len;
if (end_key)
{
hp_rb_pack_key(info, inx, info->recbuf, end_key, end_key_len);
hp_rb_pack_key(keyinfo, info->recbuf, end_key);
end_pos= tree_record_pos(rb_tree, info->recbuf, end_search_flag,
&custom_arg);
}
......@@ -473,12 +474,10 @@ uint hp_rb_make_key(HP_KEYDEF *keydef, byte *key,
return key - start_key;
}
uint hp_rb_pack_key(HP_INFO *info, uint inx, uchar *key, const uchar *old,
uint k_length)
uint hp_rb_pack_key(HP_KEYDEF *keydef, uchar *key, const uchar *old)
{
HA_KEYSEG *seg, *endseg;
uchar *start_key= key;
HP_KEYDEF *keydef= info->s->keydef + inx;
for (seg= keydef->seg, endseg= seg + keydef->keysegs; seg < endseg;
old+= seg->length, seg++)
......@@ -494,28 +493,24 @@ uint hp_rb_pack_key(HP_INFO *info, uint inx, uchar *key, const uchar *old,
return key - start_key;
}
uint hp_rb_key_length(HP_KEYDEF *keydef, const byte *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;
if (keydef->flag & HA_NULL_PART_KEY)
{
for (seg= keydef->seg, endseg= seg + keydef->keysegs; seg < endseg; seg++)
{
if (seg->null_bit)
{
if (!*key++)
if (seg->null_bit && !*key++)
continue;
}
key += seg->length;
key+= seg->length;
}
return key - start_key;
}
else
{
return keydef->length;
}
}
/*
......
......@@ -48,30 +48,37 @@ HP_INFO *heap_open(const char *name, int mode, uint keys, HP_KEYDEF *keydef,
pthread_mutex_lock(&THR_LOCK_heap);
if (!(share = hp_find_named_heap(name)))
{
HP_KEYDEF *keyinfo;
DBUG_PRINT("info",("Initializing new table"));
for (i=key_segs=max_length=0 ; i < keys ; i++)
for (i=key_segs=max_length=0, keyinfo= keydef; i < keys; i++, keyinfo++)
{
key_segs+= keydef[i].keysegs;
if (keydef[i].algorithm == HA_KEY_ALG_BTREE)
key_segs++;
bzero((char*) &keydef[i].block,sizeof(keydef[i].block));
bzero((char*) &keydef[i].rb_tree ,sizeof(keydef[i].rb_tree));
for (j=length=0 ; j < keydef[i].keysegs; j++)
bzero((char*) &keyinfo->block,sizeof(keyinfo->block));
bzero((char*) &keyinfo->rb_tree ,sizeof(keyinfo->rb_tree));
for (j=length=0 ; j < keyinfo->keysegs; j++)
{
length+=keydef[i].seg[j].length;
if (keydef[i].seg[j].null_bit)
length+=keyinfo->seg[j].length;
if (keyinfo->seg[j].null_bit)
{
if (!(keydef[i].flag & HA_NULL_ARE_EQUAL))
keydef[i].flag |= HA_NULL_PART_KEY;
if (keydef[i].algorithm == HA_KEY_ALG_BTREE)
keydef[i].rb_tree.size_of_element++;
if (!(keyinfo->flag & HA_NULL_ARE_EQUAL))
keyinfo->flag |= HA_NULL_PART_KEY;
if (keyinfo->algorithm == HA_KEY_ALG_BTREE)
keyinfo->rb_tree.size_of_element++;
}
}
keydef[i].length= length;
length+= keydef[i].rb_tree.size_of_element +
((keydef[i].algorithm == HA_KEY_ALG_BTREE) ? sizeof(byte*) : 0);
keyinfo->length= length;
length+= keyinfo->rb_tree.size_of_element +
((keyinfo->algorithm == HA_KEY_ALG_BTREE) ? sizeof(byte*) : 0);
if (length > max_length)
max_length= length;
key_segs+= keyinfo->keysegs;
if (keyinfo->algorithm == HA_KEY_ALG_BTREE)
{
key_segs++; /* additional HA_KEYTYPE_END segment */
if (keyinfo->flag & HA_NULL_PART_KEY)
keyinfo->get_key_length = hp_rb_null_key_length;
else
keyinfo->get_key_length = hp_rb_key_length;
}
}
if (!(share= (HP_SHARE*) my_malloc((uint) sizeof(HP_SHARE)+
keys*sizeof(HP_KEYDEF)+
......@@ -86,9 +93,8 @@ HP_INFO *heap_open(const char *name, int mode, uint keys, HP_KEYDEF *keydef,
init_block(&share->block, reclength + 1, min_records, max_records);
/* Fix keys */
memcpy(share->keydef, keydef, (size_t) (sizeof(keydef[0]) * keys));
for (i= 0; i < keys; i++)
for (i= 0, keyinfo= share->keydef; i < keys; i++, keyinfo++)
{
HP_KEYDEF *keyinfo= share->keydef + i;
uint nsegs= keydef[i].keysegs;
if (keydef[i].algorithm == HA_KEY_ALG_BTREE)
......
......@@ -31,7 +31,8 @@ int heap_rfirst(HP_INFO *info, byte *record, int inx)
if ((pos = tree_search_edge(&keyinfo->rb_tree, info->parents,
&info->last_pos, offsetof(TREE_ELEMENT, left))))
{
memcpy(&pos, pos + hp_rb_key_length(keyinfo, pos), sizeof(byte*));
memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos),
sizeof(byte*));
info->current_ptr = pos;
memcpy(record, pos, (size_t)share->reclength);
info->update = HA_STATE_AKTIV;
......
......@@ -20,56 +20,56 @@ int heap_rkey(HP_INFO *info, byte *record, int inx, const byte *key,
uint key_len, enum ha_rkey_function find_flag)
{
byte *pos;
HP_SHARE *share=info->s;
HP_KEYDEF *keyinfo = share->keydef+inx;
HP_SHARE *share= info->s;
HP_KEYDEF *keyinfo= share->keydef + inx;
DBUG_ENTER("heap_rkey");
DBUG_PRINT("enter",("base: %lx inx: %d",info,inx));
if ((uint) inx >= share->keys)
{
DBUG_RETURN(my_errno=HA_ERR_WRONG_INDEX);
DBUG_RETURN(my_errno= HA_ERR_WRONG_INDEX);
}
info->lastinx=inx;
info->current_record = (ulong) ~0L; /* For heap_rrnd() */
info->lastinx= inx;
info->current_record= (ulong) ~0L; /* For heap_rrnd() */
if (keyinfo->algorithm == HA_KEY_ALG_BTREE)
{
heap_rb_param custom_arg;
hp_rb_pack_key(info, inx, info->recbuf, key, key_len);
hp_rb_pack_key(keyinfo, info->recbuf, key);
custom_arg.keyseg = info->s->keydef[inx].seg;
custom_arg.key_length = key_len;
custom_arg.search_flag = SEARCH_FIND | SEARCH_SAME;
custom_arg.keyseg= info->s->keydef[inx].seg;
custom_arg.key_length= key_len;
custom_arg.search_flag= SEARCH_FIND | SEARCH_SAME;
/* for next rkey() after deletion */
if (find_flag == HA_READ_AFTER_KEY)
info->last_find_flag = HA_READ_KEY_OR_NEXT;
info->last_find_flag= HA_READ_KEY_OR_NEXT;
else if (find_flag == HA_READ_BEFORE_KEY)
info->last_find_flag = HA_READ_KEY_OR_PREV;
info->last_find_flag= HA_READ_KEY_OR_PREV;
else
info->last_find_flag = find_flag;
info->lastkey_len = key_len;
if (!(pos = tree_search_key(&keyinfo->rb_tree, info->recbuf, info->parents,
info->last_find_flag= find_flag;
info->lastkey_len= key_len;
if (!(pos= tree_search_key(&keyinfo->rb_tree, info->recbuf, info->parents,
&info->last_pos, find_flag, &custom_arg)))
{
info->update = 0;
DBUG_RETURN(my_errno = HA_ERR_KEY_NOT_FOUND);
info->update= 0;
DBUG_RETURN(my_errno= HA_ERR_KEY_NOT_FOUND);
}
memcpy(&pos, pos + hp_rb_key_length(keyinfo, pos), sizeof(byte*));
info->current_ptr = pos;
memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos), sizeof(byte*));
info->current_ptr= pos;
}
else
{
if (!(pos=hp_search(info,share->keydef+inx,key,0)))
if (!(pos= hp_search(info, share->keydef + inx, key, 0)))
{
info->update=0;
info->update= 0;
DBUG_RETURN(my_errno);
}
if (!(keyinfo->flag & HA_NOSAME))
memcpy(info->lastkey,key,(size_t) keyinfo->length);
memcpy(info->lastkey, key, (size_t) keyinfo->length);
}
memcpy(record,pos,(size_t) share->reclength);
info->update=HA_STATE_AKTIV;
memcpy(record, pos, (size_t) share->reclength);
info->update= HA_STATE_AKTIV;
DBUG_RETURN(0);
}
......
......@@ -32,7 +32,8 @@ int heap_rlast(HP_INFO *info, byte *record, int inx)
if ((pos = tree_search_edge(&keyinfo->rb_tree, info->parents,
&info->last_pos, offsetof(TREE_ELEMENT, right))))
{
memcpy(&pos, pos + hp_rb_key_length(keyinfo, pos), sizeof(byte*));
memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos),
sizeof(byte*));
info->current_ptr = pos;
memcpy(record, pos, (size_t)share->reclength);
info->update = HA_STATE_AKTIV;
......
......@@ -47,7 +47,8 @@ int heap_rnext(HP_INFO *info, byte *record)
}
if (pos)
{
memcpy(&pos, pos + hp_rb_key_length(keyinfo, pos), sizeof(byte*));
memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos),
sizeof(byte*));
info->current_ptr = pos;
}
else
......
......@@ -47,7 +47,8 @@ int heap_rprev(HP_INFO *info, byte *record)
}
if (pos)
{
memcpy(&pos, pos + hp_rb_key_length(keyinfo, pos), sizeof(byte*));
memcpy(&pos, pos + (*keyinfo->get_key_length)(keyinfo, pos),
sizeof(byte*));
info->current_ptr = pos;
}
else
......
......@@ -91,6 +91,7 @@ typedef struct st_hp_keydef /* Key definition with open */
const byte *record, byte *recpos);
int (*delete_key)(struct st_heap_info *info, struct st_hp_keydef *keyinfo,
const byte *record, byte *recpos, int flag);
uint (*get_key_length)(struct st_hp_keydef *keydef, const byte *key);
} HP_KEYDEF;
typedef struct st_heap_share
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment