Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00030 #include <config.h>
00031
00032 #include <drizzled/sql_select.h>
00033 #include <drizzled/field/blob.h>
00034 #include <drizzled/drizzled.h>
00035 #include <drizzled/internal/my_sys.h>
00036 #include <drizzled/table.h>
00037 #include <drizzled/session.h>
00038 #include <drizzled/system_variables.h>
00039
00040 #include <algorithm>
00041
00042 using namespace std;
00043
00044 namespace drizzled {
00045
00046 static uint32_t used_blob_length(CacheField **ptr)
00047 {
00048 uint32_t length,blob_length;
00049 for (length=0 ; *ptr ; ptr++)
00050 {
00051 (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
00052 length+=blob_length;
00053 (*ptr)->str= (*ptr)->blob_field->get_ptr();
00054 }
00055 return length;
00056 }
00057
00058
00059
00060
00061
00062
00063
00064 int join_init_cache(Session *session, JoinTable *tables, uint32_t table_count)
00065 {
00066 unsigned int length, blobs;
00067 size_t size;
00068 CacheField *copy,**blob_ptr;
00069 JoinCache *cache;
00070 JoinTable *join_tab;
00071
00072 cache= &tables[table_count].cache;
00073 cache->fields=blobs=0;
00074
00075 join_tab= tables;
00076 for (unsigned int i= 0; i < table_count ; i++, join_tab++)
00077 {
00078 if (!join_tab->used_fieldlength)
00079 calc_used_field_length(session, join_tab);
00080 cache->fields+=join_tab->used_fields;
00081 blobs+=join_tab->used_blobs;
00082
00083
00084 if (join_tab->rowid_keep_flags & JoinTable::KEEP_ROWID)
00085 {
00086 cache->fields++;
00087 join_tab->used_fieldlength += join_tab->table->cursor->ref_length;
00088 }
00089 }
00090 if (!(cache->field=(CacheField*)
00091 memory::sql_alloc(sizeof(CacheField)*(cache->fields+table_count*2)+(blobs+1)* sizeof(CacheField*))))
00092 {
00093 size= cache->end - cache->buff;
00094 global_join_buffer.sub(size);
00095 free((unsigned char*) cache->buff);
00096 cache->buff=0;
00097 return 1;
00098 }
00099 copy=cache->field;
00100 blob_ptr=cache->blob_ptr=(CacheField**)
00101 (cache->field+cache->fields+table_count*2);
00102
00103 length=0;
00104 for (unsigned int i= 0 ; i < table_count ; i++)
00105 {
00106 uint32_t null_fields=0, used_fields;
00107 Field **f_ptr,*field;
00108 for (f_ptr= tables[i].table->getFields(), used_fields= tables[i].used_fields; used_fields; f_ptr++)
00109 {
00110 field= *f_ptr;
00111 if (field->isReadSet())
00112 {
00113 used_fields--;
00114 length+=field->fill_cache_field(copy);
00115 if (copy->blob_field)
00116 (*blob_ptr++)=copy;
00117 if (field->maybe_null())
00118 null_fields++;
00119 copy->get_rowid= NULL;
00120 copy++;
00121 }
00122 }
00123
00124 if (null_fields && tables[i].table->getNullFields())
00125 {
00126 copy->str= tables[i].table->null_flags;
00127 copy->length= tables[i].table->getShare()->null_bytes;
00128 copy->strip=0;
00129 copy->blob_field=0;
00130 copy->get_rowid= NULL;
00131 length+=copy->length;
00132 copy++;
00133 cache->fields++;
00134 }
00135
00136 if (tables[i].table->maybe_null)
00137 {
00138 copy->str= (unsigned char*) &tables[i].table->null_row;
00139 copy->length=sizeof(tables[i].table->null_row);
00140 copy->strip=0;
00141 copy->blob_field=0;
00142 copy->get_rowid= NULL;
00143 length+=copy->length;
00144 copy++;
00145 cache->fields++;
00146 }
00147
00148 if (tables[i].rowid_keep_flags & JoinTable::KEEP_ROWID)
00149 {
00150 copy->str= tables[i].table->cursor->ref;
00151 copy->length= tables[i].table->cursor->ref_length;
00152 copy->strip=0;
00153 copy->blob_field=0;
00154 copy->get_rowid= NULL;
00155 if (tables[i].rowid_keep_flags & JoinTable::CALL_POSITION)
00156 {
00157
00158 copy->get_rowid= tables[i].table;
00159
00160 tables[i].rowid_keep_flags&= ~((int)JoinTable::CALL_POSITION);
00161 }
00162 copy++;
00163 }
00164 }
00165
00166 cache->length= length+blobs*sizeof(char*);
00167 cache->blobs= blobs;
00168 *blob_ptr= NULL;
00169 size= max((size_t) session->variables.join_buff_size, (size_t)cache->length);
00170 if (not global_join_buffer.add(size))
00171 {
00172 my_error(ER_OUT_OF_GLOBAL_JOINMEMORY, MYF(ME_ERROR+ME_WAITTANG));
00173 return 1;
00174 }
00175 cache->buff= (unsigned char*) malloc(size);
00176 cache->end= cache->buff+size;
00177 cache->reset_cache_write();
00178
00179 return 0;
00180 }
00181
00182 bool JoinCache::store_record_in_cache()
00183 {
00184 JoinCache *cache= this;
00185 unsigned char *local_pos;
00186 CacheField *copy,*end_field;
00187 bool last_record;
00188
00189 local_pos= cache->pos;
00190 end_field= cache->field+cache->fields;
00191
00192 {
00193 uint32_t local_length;
00194
00195 local_length= cache->length;
00196 if (cache->blobs)
00197 {
00198 local_length+= used_blob_length(cache->blob_ptr);
00199 }
00200
00201 if ((last_record= (local_length + cache->length > (size_t) (cache->end - local_pos))))
00202 {
00203 cache->ptr_record= cache->records;
00204 }
00205 }
00206
00207
00208
00209
00210 cache->records++;
00211 for (copy= cache->field; copy < end_field; copy++)
00212 {
00213 if (copy->blob_field)
00214 {
00215 if (last_record)
00216 {
00217 copy->blob_field->get_image(local_pos, copy->length+sizeof(char*), copy->blob_field->charset());
00218 local_pos+= copy->length+sizeof(char*);
00219 }
00220 else
00221 {
00222 copy->blob_field->get_image(local_pos, copy->length,
00223 copy->blob_field->charset());
00224 memcpy(local_pos + copy->length,copy->str,copy->blob_length);
00225 local_pos+= copy->length+copy->blob_length;
00226 }
00227 }
00228 else
00229 {
00230
00231 if (copy->get_rowid)
00232 copy->get_rowid->cursor->position(copy->get_rowid->getInsertRecord());
00233
00234 if (copy->strip)
00235 {
00236 unsigned char *str, *local_end;
00237 for (str= copy->str,local_end= str+copy->length; local_end > str && local_end[-1] == ' '; local_end--) {}
00238
00239 uint32_t local_length= (uint32_t) (local_end - str);
00240 memcpy(local_pos+2, str, local_length);
00241 int2store(local_pos, local_length);
00242 local_pos+= local_length+2;
00243 }
00244 else
00245 {
00246 memcpy(local_pos, copy->str, copy->length);
00247 local_pos+= copy->length;
00248 }
00249 }
00250 }
00251 cache->pos= local_pos;
00252 return last_record || (size_t) (cache->end - local_pos) < cache->length;
00253 }
00254
00255 void JoinCache::reset_cache_read()
00256 {
00257 record_nr= 0;
00258 pos= buff;
00259 }
00260
00261 void JoinCache::reset_cache_write()
00262 {
00263 reset_cache_read();
00264 records= 0;
00265 ptr_record= UINT32_MAX;
00266 }
00267
00272 }