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, Inc.
4
* Copyright (C) 2008-2009 Sun Microsystems
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
30
#include "drizzled/server_includes.h"
31
31
#include "drizzled/sql_select.h" /* include join.h */
32
32
#include "drizzled/field/blob.h"
33
#include "drizzled/drizzled.h"
34
#include "drizzled/internal/my_sys.h"
36
34
#include <algorithm>
38
36
using namespace std;
43
static uint32_t used_blob_length(CacheField **ptr);
45
static uint32_t used_blob_length(CacheField **ptr)
38
static uint32_t used_blob_length(CACHE_FIELD **ptr);
40
static uint32_t used_blob_length(CACHE_FIELD **ptr)
47
42
uint32_t length,blob_length;
48
43
for (length=0 ; *ptr ; ptr++)
62
57
******************************************************************************/
63
58
int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
60
register unsigned int i;
65
61
unsigned int length, blobs;
67
CacheField *copy,**blob_ptr;
63
CACHE_FIELD *copy,**blob_ptr;
69
65
JoinTable *join_tab;
71
67
cache= &tables[table_count].cache;
72
68
cache->fields=blobs=0;
75
for (unsigned int i= 0; i < table_count ; i++, join_tab++)
71
for (i=0 ; i < table_count ; i++,join_tab++)
77
73
if (!join_tab->used_fieldlength) /* Not calced yet */
78
74
calc_used_field_length(session, join_tab);
83
79
if (join_tab->rowid_keep_flags & JoinTable::KEEP_ROWID)
86
join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
82
join_tab->used_fieldlength += join_tab->table->file->ref_length;
89
if (!(cache->field=(CacheField*)
90
memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
85
if (!(cache->field=(CACHE_FIELD*)
86
sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
88
sizeof(CACHE_FIELD*))))
92
size= cache->end - cache->buff;
93
global_join_buffer.sub(size);
94
free((unsigned char*) cache->buff);
90
free((unsigned char*) cache->buff); /* purecov: inspected */
91
cache->buff=0; /* purecov: inspected */
92
return(1); /* purecov: inspected */
99
blob_ptr=cache->blob_ptr=(CacheField**)
95
blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
100
96
(cache->field+cache->fields+table_count*2);
103
for (unsigned int i= 0 ; i < table_count ; i++)
99
for (i=0 ; i < table_count ; i++)
105
101
uint32_t null_fields=0, used_fields;
106
102
Field **f_ptr,*field;
107
for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
103
for (f_ptr= tables[i].table->field,used_fields= tables[i].used_fields; used_fields; f_ptr++)
110
106
if (field->isReadSet())
146
142
/* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
147
143
if (tables[i].rowid_keep_flags & JoinTable::KEEP_ROWID)
149
copy->str= tables[i].table->cursor->ref;
150
copy->length= tables[i].table->cursor->ref_length;
145
copy->str= tables[i].table->file->ref;
146
copy->length= tables[i].table->file->ref_length;
152
148
copy->blob_field=0;
153
149
copy->get_rowid= NULL;
166
162
cache->blobs= blobs;
167
163
*blob_ptr= NULL; /* End sequentel */
168
164
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
165
if (!(cache->buff= (unsigned char*) malloc(size)))
166
return 1; /* Don't use cache */ /* purecov: inspected */
176
167
cache->end= cache->buff+size;
177
cache->reset_cache_write();
168
reset_cache_write(cache);
182
bool JoinCache::store_record_in_cache()
172
bool store_record_in_cache(JOIN_CACHE *cache)
184
JoinCache *cache= this;
185
unsigned char *local_pos;
186
CacheField *copy,*end_field;
176
CACHE_FIELD *copy,*end_field;
187
177
bool last_record;
189
local_pos= cache->pos;
190
180
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;
182
length= cache->length;
184
length+= used_blob_length(cache->blob_ptr);
185
if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
186
cache->ptr_record= cache->records;
208
188
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*);
197
copy->blob_field->get_image(pos, copy->length+sizeof(char*), copy->blob_field->charset());
198
pos+= copy->length+sizeof(char*);
222
copy->blob_field->get_image(local_pos, copy->length, // blob length
202
copy->blob_field->get_image(pos, copy->length, // blob length
223
203
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;
204
memcpy(pos+copy->length,copy->str,copy->blob_length); // Blob data
205
pos+= copy->length+copy->blob_length;
230
210
// SemiJoinDuplicateElimination: Get the rowid into table->ref:
231
211
if (copy->get_rowid)
232
copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
212
copy->get_rowid->file->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;
216
unsigned char *str,*end;
217
for (str= copy->str,end= str+copy->length; end > str && end[-1] == ' '; end--)
219
length= (uint32_t) (end-str);
220
memcpy(pos+2, str, length);
221
int2store(pos, length);
246
memcpy(local_pos, copy->str, copy->length);
247
local_pos+= copy->length;
226
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;
232
return last_record || (size_t) (cache->end - pos) < cache->length;
235
void reset_cache_read(JOIN_CACHE *cache)
238
cache->pos= cache->buff;
241
void reset_cache_write(JOIN_CACHE *cache)
243
reset_cache_read(cache);
245
cache->ptr_record= UINT32_MAX;
269
249
@} (end of group Query_Optimizer)
272
} /* namespace drizzled */