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)* sizeof(CACHE_FIELD*))))
92
size= cache->end - cache->buff;
93
global_join_buffer.sub(size);
94
91
free((unsigned char*) cache->buff);
99
blob_ptr=cache->blob_ptr=(CacheField**)
96
blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
100
97
(cache->field+cache->fields+table_count*2);
103
for (unsigned int i= 0 ; i < table_count ; i++)
100
for (i=0 ; i < table_count ; i++)
105
102
uint32_t null_fields=0, used_fields;
106
103
Field **f_ptr,*field;
107
for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
104
for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
110
107
if (field->isReadSet())
166
163
cache->blobs= blobs;
167
164
*blob_ptr= NULL; /* End sequentel */
168
165
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
166
if (!(cache->buff= (unsigned char*) malloc(size)))
176
168
cache->end= cache->buff+size;
177
cache->reset_cache_write();
169
reset_cache_write(cache);
182
bool JoinCache::store_record_in_cache()
173
bool store_record_in_cache(JOIN_CACHE *cache)
184
JoinCache *cache= this;
185
unsigned char *local_pos;
186
CacheField *copy,*end_field;
177
CACHE_FIELD *copy,*end_field;
187
178
bool last_record;
189
local_pos= cache->pos;
190
181
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;
183
length= cache->length;
185
length+= used_blob_length(cache->blob_ptr);
186
if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
187
cache->ptr_record= cache->records;
208
189
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*);
198
copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
199
pos+= copy->length+sizeof(char*);
222
copy->blob_field->get_image(local_pos, copy->length, // blob length
203
copy->blob_field->get_image(pos, copy->length, // blob length
223
204
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;
205
memcpy(pos+copy->length,copy->str,copy->blob_length); // Blob data
206
pos+= copy->length+copy->blob_length;
230
211
// SemiJoinDuplicateElimination: Get the rowid into table->ref:
231
212
if (copy->get_rowid)
232
copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
213
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;
217
unsigned char *str,*end;
218
for (str= copy->str,end= str+copy->length; end > str && end[-1] == ' '; end--)
220
length= (uint32_t) (end-str);
221
memcpy(pos+2, str, length);
222
int2store(pos, length);
246
memcpy(local_pos, copy->str, copy->length);
247
local_pos+= copy->length;
227
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;
233
return last_record || (size_t) (cache->end - pos) < cache->length;
236
void reset_cache_read(JOIN_CACHE *cache)
239
cache->pos= cache->buff;
242
void reset_cache_write(JOIN_CACHE *cache)
244
reset_cache_read(cache);
246
cache->ptr_record= UINT32_MAX;