List:Internals« Previous MessageNext Message »
From:KPATKOCTb Date:June 30 2011 12:06am
Subject:[beginner question] select a row by primary key
View as plain text  
Just started to work with mysql internals, and I'm trying to access a row by primary key. 
The row is in fact found, as i can see in the debugger, but when i'm trying to access it
with val_int or val_str I recieve a SIGSEGV from, ASSERT_COLUMN_MARKED_FOR_READ.
I don't think this matters, but i have mysqld version 5.1.56-debug-log for pc-linux-gnu on
i686 (Source distribution).

That's the query, the effect of which i want to achieve:
mysql> select * from where id=2793;
| 2793 | 56f9beb3bc4578b3bca40ede0408e2a3 |

That's is my code:

THD *thd = _current_thd();
TABLE_LIST tables;
tables.init_one_table("test","data", TL_READ);
simple_open_n_lock_tables(thd, &tables);
bool refresh = true;
TABLE *table = tables.table;
table->file->ha_index_init(0, 1);
KEY *index = table->key_info;

int key_len= index->key_part[0].store_length;
uchar* key_buf= (uchar*) thd->alloc(index->key_length);
bzero(key_buf, index->key_length);
int null_offset= table->field[0]->null_bit;
int4store(key_buf + null_offset, 2793);

table->file->index_read_last_map(table->record[0], key_buf, 1);
//table->file->index_read_map(table->record[0], key_buf, 1, HA_READ_KEY_EXACT);
also tried that 

std::cerr << table->field[0]->val_int() << std::endl; //line 175 which

char rwpstr_buf[1024];
::String rwpstr(rwpstr_buf, sizeof(rwpstr_buf),&my_charset_bin);
Field *const fld = table->field[1];
fld->val_str(&rwpstr, &rwpstr); //if i comment out line 175, i'll get SIGSEGV
on this line 
std::cerr << rwpstr_buf << std::endl;

The debugger:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0xb5128b70 (LWP 11766)]
0x08197f54 in bitmap_is_set (this=0x8639c78) at ../include/my_bitmap.h:141
141       return _bitmap_is_set(map,bit);
(gdb) bt
#0  0x08197f54 in bitmap_is_set (this=0x8607c80) at ../include/my_bitmap.h:141
#1  Field_long::val_int (this=0x8607c80) at
#2  0xb51b649f in xFind (args=...) at xmysql.hpp:175
(gdb) frame 2
(gdb) p *((int*)key_buf)
$6 = 2793
(gdb) p table->field[1]->ptr
$10 = (uchar *) 0x8607f54 " "
(gdb) p table->field[1]->ptr[1]
$11 = 0 '\000'
(gdb) p table->field[1]->ptr[2]
$12 = 53 '5'
(gdb) p table->field[1]->ptr[3]
$13 = 54 '6'
(gdb) p table->field[1]->ptr[4]
$14 = 102 'f'
(gdb) p table->field[1]->ptr[5]
$15 = 57 '9'

So, as you can see, 56f9 is the first characters of second columnt of the row I searched
for. So it's really found.

And that's, lines 3721-3724:
longlong Field_long::val_int(void)
  int32 j;

[beginner question] select a row by primary keyKPATKOCTb2 Jul