43
static uint32_t used_blob_length(CacheField **ptr);
41
static uint32_t used_blob_length(CACHE_FIELD **ptr);
45
static uint32_t used_blob_length(CacheField **ptr)
43
static uint32_t used_blob_length(CACHE_FIELD **ptr)
47
45
uint32_t length,blob_length;
48
46
for (length=0 ; *ptr ; ptr++)
62
60
******************************************************************************/
63
61
int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
63
register unsigned int i;
65
64
unsigned int length, blobs;
67
CacheField *copy,**blob_ptr;
66
CACHE_FIELD *copy,**blob_ptr;
69
68
JoinTable *join_tab;
71
70
cache= &tables[table_count].cache;
72
71
cache->fields=blobs=0;
75
for (unsigned int i= 0; i < table_count ; i++, join_tab++)
74
for (i=0 ; i < table_count ; i++,join_tab++)
77
76
if (!join_tab->used_fieldlength) /* Not calced yet */
78
77
calc_used_field_length(session, join_tab);
86
85
join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
89
if (!(cache->field=(CacheField*)
90
memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
88
if (!(cache->field=(CACHE_FIELD*)
89
memory::sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
91
sizeof(CACHE_FIELD*))))
92
size= cache->end - cache->buff;
93
global_join_buffer.sub(size);
94
93
free((unsigned char*) cache->buff);
99
blob_ptr=cache->blob_ptr=(CacheField**)
98
blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
100
99
(cache->field+cache->fields+table_count*2);
103
for (unsigned int i= 0 ; i < table_count ; i++)
102
for (i=0 ; i < table_count ; i++)
105
104
uint32_t null_fields=0, used_fields;
106
105
Field **f_ptr,*field;
107
for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
106
for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
110
109
if (field->isReadSet())
166
165
cache->blobs= blobs;
167
166
*blob_ptr= NULL; /* End sequentel */
168
167
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));
174
168
if (!(cache->buff= (unsigned char*) malloc(size)))
176
170
cache->end= cache->buff+size;
177
cache->reset_cache_write();
171
reset_cache_write(cache);
182
bool JoinCache::store_record_in_cache()
175
bool store_record_in_cache(JOIN_CACHE *cache)
184
JoinCache *cache= this;
185
unsigned char *local_pos;
186
CacheField *copy,*end_field;
179
CACHE_FIELD *copy,*end_field;
187
180
bool last_record;
189
local_pos= cache->pos;
190
183
end_field= cache->field+cache->fields;
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;
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;
208
191
There is room in cache. Put record there
217
copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
218
local_pos+= copy->length+sizeof(char*);
200
copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
201
pos+= copy->length+sizeof(char*);
222
copy->blob_field->get_image(local_pos, copy->length, // blob length
205
copy->blob_field->get_image(pos, copy->length, // blob length
223
206
copy->blob_field->charset());
224
memcpy(local_pos + copy->length,copy->str,copy->blob_length); // Blob data
225
local_pos+= copy->length+copy->blob_length;
207
memcpy(pos+copy->length,copy->str,copy->blob_length); // Blob data
208
pos+= copy->length+copy->blob_length;
230
213
// SemiJoinDuplicateElimination: Get the rowid into table->ref:
231
214
if (copy->get_rowid)
232
copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
215
copy->get_rowid->cursor->position(copy->get_rowid->record[0]);
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;
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);
246
memcpy(local_pos, copy->str, copy->length);
247
local_pos+= copy->length;
229
memcpy(pos,copy->str,copy->length);
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;
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;