1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008-2009 Sun Microsystems
4
* Copyright (C) 2008-2009 Sun Microsystems, Inc.
6
6
* This program is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
41
static uint32_t used_blob_length(CACHE_FIELD **ptr);
43
static uint32_t used_blob_length(CacheField **ptr);
43
static uint32_t used_blob_length(CACHE_FIELD **ptr)
45
static uint32_t used_blob_length(CacheField **ptr)
45
47
uint32_t length,blob_length;
46
48
for (length=0 ; *ptr ; ptr++)
60
62
******************************************************************************/
61
63
int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
63
register unsigned int i;
64
65
unsigned int length, blobs;
66
CACHE_FIELD *copy,**blob_ptr;
67
CacheField *copy,**blob_ptr;
68
69
JoinTable *join_tab;
70
71
cache= &tables[table_count].cache;
71
72
cache->fields=blobs=0;
74
for (i=0 ; i < table_count ; i++,join_tab++)
75
for (unsigned int i= 0; i < table_count ; i++, join_tab++)
76
77
if (!join_tab->used_fieldlength) /* Not calced yet */
77
78
calc_used_field_length(session, join_tab);
85
86
join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
88
if (!(cache->field=(CACHE_FIELD*)
89
memory::sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
91
sizeof(CACHE_FIELD*))))
89
if (!(cache->field=(CacheField*)
90
memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
92
size= cache->end - cache->buff;
93
global_join_buffer.sub(size);
93
94
free((unsigned char*) cache->buff);
98
blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
99
blob_ptr=cache->blob_ptr=(CacheField**)
99
100
(cache->field+cache->fields+table_count*2);
102
for (i=0 ; i < table_count ; i++)
103
for (unsigned int i= 0 ; i < table_count ; i++)
104
105
uint32_t null_fields=0, used_fields;
105
106
Field **f_ptr,*field;
106
for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
107
for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
109
110
if (field->isReadSet())
165
166
cache->blobs= blobs;
166
167
*blob_ptr= NULL; /* End sequentel */
167
168
size= max((size_t) session->variables.join_buff_size, (size_t)cache->length);
169
if (not global_join_buffer.add(size))
171
my_error(ER_OUT_OF_GLOBAL_JOINMEMORY, MYF(ME_ERROR+ME_WAITTANG));
168
174
if (!(cache->buff= (unsigned char*) malloc(size)))
170
176
cache->end= cache->buff+size;
171
reset_cache_write(cache);
177
cache->reset_cache_write();
175
bool store_record_in_cache(JOIN_CACHE *cache)
182
bool JoinCache::store_record_in_cache()
179
CACHE_FIELD *copy,*end_field;
184
JoinCache *cache= this;
185
unsigned char *local_pos;
186
CacheField *copy,*end_field;
180
187
bool last_record;
189
local_pos= cache->pos;
183
190
end_field= cache->field+cache->fields;
185
length= cache->length;
187
length+= used_blob_length(cache->blob_ptr);
188
if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
189
cache->ptr_record= cache->records;
193
uint32_t local_length;
195
local_length= cache->length;
198
local_length+= used_blob_length(cache->blob_ptr);
201
if ((last_record= (local_length + cache->length > (size_t) (cache->end - local_pos))))
203
cache->ptr_record= cache->records;
191
208
There is room in cache. Put record there
200
copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
201
pos+= copy->length+sizeof(char*);
217
copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
218
local_pos+= copy->length+sizeof(char*);
205
copy->blob_field->get_image(pos, copy->length, // blob length
222
copy->blob_field->get_image(local_pos, copy->length, // blob length
206
223
copy->blob_field->charset());
207
memcpy(pos+copy->length,copy->str,copy->blob_length); // Blob data
208
pos+= copy->length+copy->blob_length;
224
memcpy(local_pos + copy->length,copy->str,copy->blob_length); // Blob data
225
local_pos+= copy->length+copy->blob_length;
213
230
// SemiJoinDuplicateElimination: Get the rowid into table->ref:
214
231
if (copy->get_rowid)
215
copy->get_rowid->cursor->position(copy->get_rowid->record[0]);
232
copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
219
unsigned char *str,*end;
220
for (str= copy->str,end= str+copy->length; end > str && end[-1] == ' '; end--)
222
length= (uint32_t) (end-str);
223
memcpy(pos+2, str, length);
224
int2store(pos, length);
236
unsigned char *str, *local_end;
237
for (str= copy->str,local_end= str+copy->length; local_end > str && local_end[-1] == ' '; local_end--) {}
239
uint32_t local_length= (uint32_t) (local_end - str);
240
memcpy(local_pos+2, str, local_length);
241
int2store(local_pos, local_length);
242
local_pos+= local_length+2;
229
memcpy(pos,copy->str,copy->length);
246
memcpy(local_pos, copy->str, copy->length);
247
local_pos+= copy->length;
235
return last_record || (size_t) (cache->end - pos) < cache->length;
238
void reset_cache_read(JOIN_CACHE *cache)
241
cache->pos= cache->buff;
244
void reset_cache_write(JOIN_CACHE *cache)
246
reset_cache_read(cache);
248
cache->ptr_record= UINT32_MAX;
251
cache->pos= local_pos;
252
return last_record || (size_t) (cache->end - local_pos) < cache->length;
255
void JoinCache::reset_cache_read()
261
void JoinCache::reset_cache_write()
265
ptr_record= UINT32_MAX;