17 #include "my_global.h"
19 #ifdef HAVE_QUERY_CACHE
21 #include "emb_qcache.h"
22 #include "embedded_priv.h"
23 #include "sql_class.h"
25 void Querycache_stream::store_uchar(uchar c)
27 if (data_end == cur_data)
35 void Querycache_stream::store_short(ushort s)
40 if (data_end - cur_data > 1)
42 int2store(cur_data, s);
46 if (data_end == cur_data)
49 int2store(cur_data, s);
53 *cur_data= ((uchar *)(&s))[0];
55 *(cur_data++)= ((uchar *)(&s))[1];
58 void Querycache_stream::store_int(uint
i)
63 size_t rest_len= data_end - cur_data;
66 int4store(cur_data, i);
73 int4store(cur_data, i);
79 memcpy(cur_data, buf, rest_len);
81 memcpy(cur_data, buf+rest_len, 4-rest_len);
82 cur_data+= 4-rest_len;
85 void Querycache_stream::store_ll(ulonglong ll)
90 size_t rest_len= data_end - cur_data;
93 int8store(cur_data, ll);
100 int8store(cur_data, ll);
104 memcpy(cur_data, &ll, rest_len);
105 use_next_block(TRUE);
106 memcpy(cur_data, ((uchar*)&ll)+rest_len, 8-rest_len);
107 cur_data+= 8-rest_len;
110 void Querycache_stream::store_str_only(
const char *str, uint str_len)
113 stored_size+= str_len;
117 size_t rest_len= data_end - cur_data;
118 if (rest_len >= str_len)
120 memcpy(cur_data, str, str_len);
124 memcpy(cur_data, str, rest_len);
125 use_next_block(TRUE);
131 void Querycache_stream::store_str(
const char *str, uint str_len)
134 store_str_only(str, str_len);
137 void Querycache_stream::store_safe_str(
const char *str, uint str_len)
141 store_int(str_len+1);
142 store_str_only(str, str_len);
148 uchar Querycache_stream::load_uchar()
150 if (cur_data == data_end)
151 use_next_block(FALSE);
152 return *(cur_data++);
155 ushort Querycache_stream::load_short()
158 if (data_end-cur_data > 1)
160 result= uint2korr(cur_data);
164 if (data_end == cur_data)
166 use_next_block(FALSE);
167 result= uint2korr(cur_data);
171 ((uchar*)&result)[0]= *cur_data;
172 use_next_block(FALSE);
173 ((uchar*)&result)[1]= *(cur_data++);
177 uint Querycache_stream::load_int()
180 size_t rest_len= data_end - cur_data;
183 result= uint4korr(cur_data);
189 use_next_block(FALSE);
190 result= uint4korr(cur_data);
195 memcpy(buf, cur_data, rest_len);
196 use_next_block(FALSE);
197 memcpy(buf+rest_len, cur_data, 4-rest_len);
198 cur_data+= 4-rest_len;
199 result= uint4korr(buf_p);
203 ulonglong Querycache_stream::load_ll()
206 size_t rest_len= data_end - cur_data;
209 result= uint8korr(cur_data);
215 use_next_block(FALSE);
216 result= uint8korr(cur_data);
220 memcpy(&result, cur_data, rest_len);
221 use_next_block(FALSE);
222 memcpy(((uchar*)&result)+rest_len, cur_data, 8-rest_len);
223 cur_data+= 8-rest_len;
227 void Querycache_stream::load_str_only(
char *buffer, uint str_len)
231 size_t rest_len= data_end - cur_data;
232 if (rest_len >= str_len)
234 memcpy(buffer, cur_data, str_len);
239 memcpy(buffer, cur_data, rest_len);
240 use_next_block(FALSE);
247 char *Querycache_stream::load_str(
MEM_ROOT *alloc, uint *str_len)
250 *str_len= load_int();
251 if (!(result= (
char*) alloc_root(alloc, *str_len + 1)))
253 load_str_only(result, *str_len);
257 int Querycache_stream::load_safe_str(
MEM_ROOT *alloc,
char **str, uint *str_len)
259 if (!(*str_len= load_int()))
265 if (!(*str= (
char*) alloc_root(alloc, *str_len + 1)))
267 load_str_only(*str, *str_len);
271 int Querycache_stream::load_column(
MEM_ROOT *alloc,
char** column)
274 if (!(len = load_int()))
280 if (!(*column= (
char *)alloc_root(alloc, len +
sizeof(uint) + 1)))
282 *((uint*)*column)= len;
283 (*column)+=
sizeof(uint);
284 load_str_only(*column, len);
288 uint emb_count_querycache_size(THD *thd)
297 while (data->embedded_info->next)
298 data= data->embedded_info->next;
299 field= data->embedded_info->fields_list;
300 field_end= field + data->fields;
304 *data->embedded_info->prev_ptr= NULL;
308 result+= (uint) (4+8 + 42*data->fields);
310 for(; field < field_end; field++)
312 result+= field->name_length + field->table_length +
313 field->org_name_length + field->org_table_length + field->db_length +
314 field->catalog_length;
316 result+= field->def_length;
319 if (thd->protocol == &thd->protocol_binary)
321 result+= (uint) (4*n_rows);
322 for (; cur_row; cur_row=cur_row->next)
323 result+= cur_row->length;
327 result+= (uint) (4*n_rows*data->fields);
328 for (; cur_row; cur_row=cur_row->next)
330 MYSQL_ROW col= cur_row->data;
331 MYSQL_ROW col_end= col + data->fields;
332 for (; col < col_end; col++)
334 result+= *(uint *)((*col) -
sizeof(uint));
348 DBUG_ENTER(
"emb_store_querycache_result");
350 while (data->embedded_info->next)
351 data= data->embedded_info->next;
352 field= data->embedded_info->fields_list;
353 field_end= field + data->fields;
358 *data->embedded_info->prev_ptr= NULL;
362 dst->store_int((uint)data->fields);
363 dst->store_ll((ulonglong)n_rows);
365 for(; field < field_end; field++)
367 dst->store_int((uint)field->length);
368 dst->store_int((uint)field->max_length);
369 dst->store_uchar((uchar)field->type);
370 dst->store_short((ushort)field->flags);
371 dst->store_short((ushort)field->charsetnr);
372 dst->store_uchar((uchar)field->decimals);
373 dst->store_str(field->name, field->name_length);
374 dst->store_str(field->table, field->table_length);
375 dst->store_str(field->org_name, field->org_name_length);
376 dst->store_str(field->org_table, field->org_table_length);
377 dst->store_str(field->db, field->db_length);
378 dst->store_str(field->catalog, field->catalog_length);
379 dst->store_safe_str(field->def, field->def_length);
382 if (thd->protocol == &thd->protocol_binary)
384 for (; cur_row; cur_row=cur_row->next)
385 dst->store_str((
char *) cur_row->data, cur_row->length);
389 for (; cur_row; cur_row=cur_row->next)
391 MYSQL_ROW col= cur_row->data;
392 MYSQL_ROW col_end= col + data->fields;
393 for (; col < col_end; col++)
395 uint len= *col ? *(uint *)((*col) -
sizeof(uint)) : 0;
396 dst->store_safe_str(*col, len);
400 DBUG_ASSERT(emb_count_querycache_size(thd) == dst->stored_size);
414 DBUG_ENTER(
"emb_load_querycache_result");
418 init_alloc_root(&data->alloc, 8192,0);
419 f_alloc= &data->alloc;
421 data->fields= src->load_int();
422 rows= src->load_ll();
425 alloc_root(f_alloc,data->fields*
sizeof(
MYSQL_FIELD))))
427 data->embedded_info->fields_list= field;
428 for(field_end= field+data->fields; field < field_end; field++)
430 field->length= src->load_int();
431 field->max_length= (
unsigned int)src->load_int();
432 field->type= (
enum enum_field_types)src->load_uchar();
433 field->flags= (
unsigned int)src->load_short();
434 field->charsetnr= (
unsigned int)src->load_short();
435 field->decimals= src->load_uchar();
437 if (!(field->name= src->load_str(f_alloc, &field->name_length)) ||
438 !(field->table= src->load_str(f_alloc,&field->table_length)) ||
439 !(field->org_name= src->load_str(f_alloc, &field->org_name_length)) ||
440 !(field->org_table= src->load_str(f_alloc, &field->org_table_length))||
441 !(field->db= src->load_str(f_alloc, &field->db_length)) ||
442 !(field->catalog= src->load_str(f_alloc, &field->catalog_length)) ||
443 src->load_safe_str(f_alloc, &field->def, &field->def_length))
450 if (thd->protocol == &thd->protocol_binary)
458 for (prev_row= &row->next; row < end_row; prev_row= &row->next, row++)
461 row->data= (MYSQL_ROW) src->load_str(&data->alloc, &length);
469 rows*(data->fields+1)*
sizeof(
char*)));
471 columns= (MYSQL_ROW)end_row;
475 for (prev_row= &row->next; row < end_row; prev_row= &row->next, row++)
479 MYSQL_ROW col_end= columns + data->fields;
480 for (; columns < col_end; columns++)
481 src->load_column(&data->alloc, columns);
487 data->embedded_info->prev_ptr= prev_row;
490 thd->get_stmt_da()->current_statement_warn_count());